Esempio n. 1
0
// *** triggered by middle button ***
// This function tests the motors by first ramping motor1 speed from zero
// to full speed "forward", to full speed "reverse", and finally back to zero.
// It then does the same for motor2 before repeating all over again.
// While motor1 is running, the red user LED is on, otherwise it is off.
// While the currently active motor is moving "forward", the green user LED
// is on, otherwise it is off.  The LCD gives you feedback as to which motor
// is currently moving in which direction (F = "forward", R = "reverse", and
// - = inactive).
unsigned char motorTest()
{
	unsigned char button;
	int speed;
	unsigned char motor = 0;

	clear();			// clear the LCD, go to the start of the first LCD line
	print("motor2");	// print to the first line of the LCD
	lcd_goto_xy(0, 1);	// go to the start of the second LCD line
	print("motor1");	// print to the second line of the LCD

	while (1)
	{
		red_led(!motor);	// turn red LED on when m1 is active, off for m2
		lcd_goto_xy(7, !motor);	// go to end of LCD line for active motor
		print("F");				// print "F" for "forward"
		lcd_goto_xy(7, motor);	// go to end of LCD line for inactive motor
		print("-");				// print "-" for "inactive"
		green_led(1);		// turn green LED on when motor is moving "forward"
		for (speed = 0; speed < 255; speed++)
		{
			button = motorUpdate(motor, speed);	// ramp up motor speed
			if (button != 0)					//  from 0 to 255
				return button;
		}

		for (speed = 255; speed > -255; speed--)
		{
			if (speed == -1)	// motor starts moving in "reverse"
			{
				green_led(0);	// green LED off when motor going "reverse"
				lcd_goto_xy(7, !motor);	// go to end of active motor's LCD line
				print("R");		// print "R" for "reverse"
			}

			button = motorUpdate(motor, speed);	// ramp down motor speed
			if (button != 0)					//  from 255 to -255
				return button;
		}

		for (speed = -255; speed <= 0; speed++)
		{
			button = motorUpdate(motor, speed);	// ramp up motor speed
			if (button != 0)					//  from -255 to 0
				return button;
		}

		motor = !motor;		// alternate between m1 and m2
	}
}
Esempio n. 2
0
void motorInit(void) {
    // Configure GPIO Pins for PWM Output
    SYSCTL_RCGCPWM_R |= SYSCTL_RCGCPWM_R0;  // activate clock for PWM Mod 0
    SYSCTL_RCGCPWM_R |= SYSCTL_RCGCPWM_R1;  // activate clock for PWM Mod 1

    SYSCTL_RCGCGPIO_R |= SYSCTL_RCGCGPIO_R0;    // activate clock for Port A
    SYSCTL_RCGCGPIO_R |= SYSCTL_RCGCGPIO_R1;    // activate clock for Port B
    SYSCTL_RCGCGPIO_R |= SYSCTL_RCGCGPIO_R3;    // activate clock for Port D
    SYSCTL_RCGCGPIO_R |= SYSCTL_RCGCGPIO_R4;    // activate clock for Port E
    SYSCTL_RCGCGPIO_R |= SYSCTL_RCGCGPIO_R5;    // activate clock for Port F

    while((SYSCTL_PRGPIO_R&SYSCTL_PRGPIO_R0) == 0) {}   // wait for clock
    while((SYSCTL_PRGPIO_R&SYSCTL_PRGPIO_R1) == 0) {}   // wait for clock
    while((SYSCTL_PRGPIO_R&SYSCTL_PRGPIO_R3) == 0) {}   // wait for clock
    while((SYSCTL_PRGPIO_R&SYSCTL_PRGPIO_R4) == 0) {}   // wait for clock
    while((SYSCTL_PRGPIO_R&SYSCTL_PRGPIO_R5) == 0) {}   // wait for clock

    GPIO_PORTA_AFSEL_R |= 0xC0;         // enable alt funct on PA6-7
    GPIO_PORTA_PCTL_R &= ~0xFF000000;   // configure PA6-7 as PWMmod1
    GPIO_PORTA_PCTL_R |= 0x55000000;
    GPIO_PORTA_AMSEL_R &= ~0xC0;        // disable analog function on PA6-7
    GPIO_PORTA_DEN_R |= 0xC0;           // enable digital I/O on PA6-7

    GPIO_PORTB_AFSEL_R |= 0xF0;         // enable alt funct on PB4-7
    GPIO_PORTB_PCTL_R &= ~0xFFFF0000;   // configure PB4-7 as PWMmod0
    GPIO_PORTB_PCTL_R |= 0x44440000;
    GPIO_PORTB_AMSEL_R &= ~0xF0;        // disable analog function on PB4-7
    GPIO_PORTB_DEN_R |= 0xF0;           // enable digital I/O on PB4-7

    GPIO_PORTD_AFSEL_R |= 0x03;         // enable alt funct on PD0-1
    GPIO_PORTD_PCTL_R &= ~0x000000FF;   // configure PD0-1 as PWMmod0
    GPIO_PORTD_PCTL_R |= 0x00000044;
    GPIO_PORTD_AMSEL_R &= ~0x03;        // disable analog function on PD0-1
    GPIO_PORTD_DEN_R |= 0x03;           // enable digital I/O on PD0-1

    GPIO_PORTE_AFSEL_R |= 0x30;         // enable alt funct on PE4-5
    GPIO_PORTE_PCTL_R &= ~0x00FF0000;   // configure PE4-5 as PWMmod0
    GPIO_PORTE_PCTL_R |= 0x00440000;
    GPIO_PORTE_AMSEL_R &= ~0x30;        // disable analog function on PE4-5
    GPIO_PORTE_DEN_R |= 0x30;           // enable digital I/O on PE4-5

    GPIO_PORTF_AFSEL_R |= 0x0C;         // enable alt funct on PF2-3
    GPIO_PORTF_PCTL_R &= ~0x0000FF00;   // configure PF2-3 as PWMmod1
    GPIO_PORTF_PCTL_R |= 0x00005500;
    GPIO_PORTF_AMSEL_R &= ~0x0C;        // disable analog function on PF2-3
    GPIO_PORTF_DEN_R |= 0x0C;           // enable digital I/O on PF2-3
   
    // Set PWM clock to desired fraction of system clock
    SYSCTL_RCC_R |= SYSCTL_RCC_USEPWMDIV;
    SYSCTL_RCC_R &= ~SYSCTL_RCC_PWMDIV_M;
    SYSCTL_RCC_R += SYSCTL_RCC_PWMDIV_2;

    // Configure PWM timing
    PWM0_0_CTL_R = 0;   // set countdown mode for mod 0 block 0
    PWM0_1_CTL_R = 0;   // set countdown mode for mod 0 block 1
    PWM0_2_CTL_R = 0;   // set countdown mode for mod 0 block 2
    PWM0_3_CTL_R = 0;   // set countdown mode for mod 0 block 3
    PWM1_1_CTL_R = 0;   // set countdown mode for mod 1 block 1
    PWM1_3_CTL_R = 0;   // set countdown mode for mod 1 block 3
    PWM0_0_GENA_R = (PWM_0_GENA_ACTCMPAD_ONE|PWM_0_GENA_ACTLOAD_ZERO);
    PWM0_0_GENB_R = (PWM_0_GENB_ACTCMPBD_ONE|PWM_0_GENB_ACTLOAD_ZERO);
    PWM0_1_GENA_R = (PWM_1_GENA_ACTCMPAD_ONE|PWM_1_GENA_ACTLOAD_ZERO);
    PWM0_1_GENB_R = (PWM_1_GENB_ACTCMPBD_ONE|PWM_1_GENB_ACTLOAD_ZERO);
    PWM0_2_GENA_R = (PWM_2_GENA_ACTCMPAD_ONE|PWM_2_GENA_ACTLOAD_ZERO);
    PWM0_2_GENB_R = (PWM_2_GENB_ACTCMPBD_ONE|PWM_2_GENB_ACTLOAD_ZERO);
    PWM0_3_GENA_R = (PWM_3_GENA_ACTCMPAD_ONE|PWM_3_GENA_ACTLOAD_ZERO);
    PWM0_3_GENB_R = (PWM_3_GENB_ACTCMPBD_ONE|PWM_3_GENB_ACTLOAD_ZERO);
    PWM1_1_GENA_R = (PWM_1_GENA_ACTCMPAD_ONE|PWM_1_GENA_ACTLOAD_ZERO);
    PWM1_1_GENB_R = (PWM_1_GENB_ACTCMPBD_ONE|PWM_1_GENB_ACTLOAD_ZERO);
    PWM1_3_GENA_R = (PWM_3_GENA_ACTCMPAD_ONE|PWM_3_GENA_ACTLOAD_ZERO);
    PWM1_3_GENB_R = (PWM_3_GENB_ACTCMPBD_ONE|PWM_3_GENB_ACTLOAD_ZERO);
                                    // define signal triggers
    PWM0_0_LOAD_R = MOT_PERIOD;    // set counter reset values (period)
    PWM0_1_LOAD_R = MOT_PERIOD;
    PWM0_2_LOAD_R = MOT_PERIOD;
    PWM0_3_LOAD_R = MOT_PERIOD;
    PWM1_1_LOAD_R = MOT_PERIOD;
    PWM1_3_LOAD_R = MOT_PERIOD;

    motorUpdate(speedInit); // init comparator values (duty)

    PWM0_0_CTL_R |= PWM_0_CTL_ENABLE;   // start Mod 0 Blk 0
    PWM0_1_CTL_R |= PWM_1_CTL_ENABLE;   // start Mod 0 Blk 1
    PWM0_2_CTL_R |= PWM_2_CTL_ENABLE;   // start Mod 0 Blk 2
    PWM0_3_CTL_R |= PWM_3_CTL_ENABLE;   // start Mod 0 Blk 3
    PWM1_1_CTL_R |= PWM_1_CTL_ENABLE;   // start Mod 1 Blk 1
    PWM1_3_CTL_R |= PWM_3_CTL_ENABLE;   // start Mod 1 Blk 3

    PWM0_ENABLE_R |= (PWM_ENABLE_PWM0EN|PWM_ENABLE_PWM1EN|PWM_ENABLE_PWM2EN|   \
        PWM_ENABLE_PWM3EN|PWM_ENABLE_PWM4EN|PWM_ENABLE_PWM5EN|PWM_ENABLE_PWM6EN\
        |PWM_ENABLE_PWM7EN);    // enable PWM Module 0
    PWM1_ENABLE_R |= (PWM_ENABLE_PWM2EN|PWM_ENABLE_PWM3EN|PWM_ENABLE_PWM6EN    \
        |PWM_ENABLE_PWM7EN);    // enable PWM Module 1
}