Exemplo n.º 1
0
/* Funktionen initierar PWM-signalen */
void pwm_setup(void)
{
	pmc_enable_periph_clk(ID_PWM);
	pwm_channel_disable(PWM, PWM_CHANNEL);
	
	pwm_clock_t clock_setting = {
		.ul_clka = PWM_FREQUENCY * PWM_PERIOD,
		.ul_clkb = 0,
		.ul_mck = SYS_CLOCK
	};
	
	pwm_init(PWM, &clock_setting);
	
	pwm_channel_instance.alignment = PWM_ALIGN_LEFT;
	pwm_channel_instance.polarity = PWM_LOW;
	pwm_channel_instance.ul_prescaler = PWM_CMR_CPRE_CLKA;
	pwm_channel_instance.ul_period = PWM_PERIOD;
	pwm_channel_instance.ul_duty = PWM_INIT_DUTY_CYCLE;
	pwm_channel_instance.channel = PWM_CHANNEL;
	
	pwm_channel_init(PWM, &pwm_channel_instance);
	pio_set_peripheral(PWM_PIO, PWM_PERIPHERAL, PWM_PIN);
	pwm_channel_enable(PWM, PWM_CHANNEL);
}


/* Funktionen uppdaterar PWM-signalens duty-cycle */
void update_pwm(int ul_duty)
{
	// Kontrollerar så att angiven duty-cycle befinner sig inom rätt område (0-1000).
	if(ul_duty < PWM_MIN_DUTY_CYCLE)
	{
		pwm_channel_update_duty(PWM, &pwm_channel_instance, PWM_MIN_DUTY_CYCLE);
	}
	else if(ul_duty > PWM_MAX_DUTY_CYCLE)
	{
		pwm_channel_update_duty(PWM, &pwm_channel_instance, PWM_MAX_DUTY_CYCLE);
	}
	else
	{
		pwm_channel_update_duty(PWM, &pwm_channel_instance, ul_duty);
	}
}


/* Funktionen initierar motor-shielden */
void motor_shield_setup(void)
{
	ioport_set_pin_dir(PIO_PD8_IDX, IOPORT_DIR_OUTPUT);
	ioport_set_pin_level(PIO_PD8_IDX, IOPORT_PIN_LEVEL_HIGH);
	
	ioport_set_pin_dir(PIO_PC21_IDX, IOPORT_DIR_OUTPUT);
	ioport_set_pin_level(PIO_PC21_IDX, IOPORT_PIN_LEVEL_LOW);
}
Exemplo n.º 2
0
void PWM_Handler(void)
#endif
{
	static uint32_t ul_count = 0;  /* PWM counter value */
	static uint32_t ul_duty = INIT_DUTY_VALUE;  /* PWM duty cycle rate */
	static uint8_t fade_in = 1;  /* LED fade in flag */

#if (SAMV70 || SAMV71 || SAME70 || SAMS70)
	uint32_t events = pwm_channel_get_interrupt_status(PWM0);
#else
	uint32_t events = pwm_channel_get_interrupt_status(PWM);
#endif

	/* Interrupt on PIN_PWM_LED0_CHANNEL */
	if ((events & (1 << PIN_PWM_LED0_CHANNEL)) ==
	(1 << PIN_PWM_LED0_CHANNEL)) {
		ul_count++;

		/* Fade in/out */
		if (ul_count == (PWM_FREQUENCY / (PERIOD_VALUE - INIT_DUTY_VALUE))) {
			/* Fade in */
			if (fade_in) {
				ul_duty++;
				if (ul_duty == PERIOD_VALUE) {
					fade_in = 0;
				}
				} else {
				/* Fade out */
				ul_duty--;
				if (ul_duty == INIT_DUTY_VALUE) {
					fade_in = 1;
				}
			}

			/* Set new duty cycle */
			ul_count = 0;
			g_pwm_channel_led.channel = PIN_PWM_LED0_CHANNEL;
#if (SAMV70 || SAMV71 || SAME70 || SAMS70)
			pwm_channel_update_duty(PWM0, &g_pwm_channel_led, ul_duty);
#else
			pwm_channel_update_duty(PWM, &g_pwm_channel_led, ul_duty);
#endif
			g_pwm_channel_led.channel = PIN_PWM_LED1_CHANNEL;
#if (SAMV70 || SAMV71 || SAME70 || SAMS70)
			pwm_channel_update_duty(PWM0, &g_pwm_channel_led, ul_duty);
#else
			pwm_channel_update_duty(PWM, &g_pwm_channel_led, ul_duty);
#endif
		}
	}
}
/**
 * \brief Interrupt handler for the PWM controller.
 */
void PWM0_Handler(void)
{
	static uint32_t ul_count = 0;  /* PWM counter value */
	static uint32_t ul_duty = INIT_DUTY_VALUE;  /* PWM duty cycle rate */
	static uint8_t fade_in = 1;  /* LED fade in flag */

	uint32_t events = pwm_channel_get_interrupt_status(PWM0);

	/* Interrupt on PIN_PWM_LED0_CHANNEL */
	if ((events & (1 << PIN_PWM_LED0_CHANNEL)) ==
	(1 << PIN_PWM_LED0_CHANNEL)) {
		ul_count++;

		/* Fade in/out */
		if (ul_count == (PWM_FREQUENCY / (PERIOD_VALUE - INIT_DUTY_VALUE))) {
			/* Fade in */
			if (fade_in) {
				ul_duty++;
				if (ul_duty == PERIOD_VALUE) {
					fade_in = 0;
					}
				} else {
				/* Fade out */
				ul_duty--;
				if (ul_duty == INIT_DUTY_VALUE) {
					fade_in = 1;
				}
			}

			/* Set new duty cycle */
			ul_count = 0;
			g_pwm_channel_led.channel = PIN_PWM_LED0_CHANNEL;
//jsi 16feb16			pwm_channel_update_duty(PWM0, &g_pwm_channel_led, ul_duty);
					pwm_channel_update_duty(PWM0, &g_pwm_channel_led, (PERIOD_VALUE/2)); //jsi 16feb16 just fixed for now
//jsi 15feb16			g_pwm_channel_led.channel = PIN_PWM_LED1_CHANNEL;
//jsi 15feb16			pwm_channel_update_duty(PWM0, &g_pwm_channel_led, ul_duty);
		}
	}
}
Exemplo n.º 4
0
void Hall_Phase(void)
{
	static uint8_t hall_code = 0;
	static uint32_t ul_duty1, ul_duty2, ul_duty3, high1, high2, low1;
	
	hall_1 = ioport_get_pin_level(PIN_HALL_1);
	hall_2 = ioport_get_pin_level(PIN_HALL_2);
	hall_3 = ioport_get_pin_level(PIN_HALL_3);

	hall_code = (hall_3<<2) | (hall_2<<1) | (hall_1);
	
	if (sel_rot) hall_code = ~hall_code;

	switch (hall_code & 0b00000111){
	
	case 5 : //phase 1
		phase=1;
		ul_duty1 = ul_duty;
		ul_duty2 = 0;
		ul_duty3 = 0;
		high1 = PIN_PWM_EN1_GPIO;
		high2 = PIN_PWM_EN2_GPIO;
		low1 = PIN_PWM_EN3_GPIO;
		break; 
	case 1 : //phase 2
		phase=2;
		ul_duty1 = ul_duty;
		ul_duty2 = 0;
		ul_duty3 = 0;
		high1 = PIN_PWM_EN1_GPIO;
		high2 = PIN_PWM_EN3_GPIO;
		low1 = PIN_PWM_EN2_GPIO;
		break;
	case 3 : //phase 3
		phase=3;
		ul_duty1 = 0;
		ul_duty2 = ul_duty;
		ul_duty3 = 0;
		high1 = PIN_PWM_EN2_GPIO;
		high2 = PIN_PWM_EN3_GPIO;
		low1 = PIN_PWM_EN1_GPIO;
		break;
	case 2 : //phase 4
		phase=4;
		ul_duty1 = 0;
		ul_duty2 = ul_duty;
		ul_duty3 = 0;
		high1 = PIN_PWM_EN1_GPIO;
		high2 = PIN_PWM_EN2_GPIO;
		low1 = PIN_PWM_EN3_GPIO;
		break;
	case 6 : //phase 5
		phase=5;
		ul_duty1 = 0;
		ul_duty2 = 0;
		ul_duty3 = ul_duty;
		high1 = PIN_PWM_EN1_GPIO;
		high2 = PIN_PWM_EN3_GPIO;
		low1 = PIN_PWM_EN2_GPIO;
		break;
	case 4 : //phase 6
		phase=6;
		ul_duty1 = 0;
		ul_duty2 = 0;
		ul_duty3 = ul_duty;
		high1 = PIN_PWM_EN2_GPIO;
		high2 = PIN_PWM_EN3_GPIO;
		low1 = PIN_PWM_EN1_GPIO;
		break; 
	}
	
	g_pwm_channel.channel = PIN_PWM_IN1_CHANNEL;
	pwm_channel_update_duty(PWM, &g_pwm_channel, ul_duty1);
	g_pwm_channel.channel = PIN_PWM_IN2_CHANNEL;
	pwm_channel_update_duty(PWM, &g_pwm_channel, ul_duty2);
	g_pwm_channel.channel = PIN_PWM_IN3_CHANNEL;
	pwm_channel_update_duty(PWM, &g_pwm_channel, ul_duty3);
	gpio_set_pin_high(high1);
	gpio_set_pin_high(high2);
	gpio_set_pin_low(low1);
	
}
Exemplo n.º 5
0
void RightWheelPWM(uint32_t dutyCycle){
pwm_channel_update_duty(PWM, &PWMPin38, dutyCycle);
}
Exemplo n.º 6
0
void LeftWheelPWM(uint32_t dutyCycle)
{
pwm_channel_update_duty(PWM, &PWMPin40, dutyCycle);
}