lab5 done
This commit is contained in:
parent
48fd2e6aed
commit
91e85280d5
|
@ -13,5 +13,5 @@ void BumpInit(void) {
|
||||||
|
|
||||||
uint8_t BumpRead(void) {
|
uint8_t BumpRead(void) {
|
||||||
// write this as part of Lab 2
|
// write this as part of Lab 2
|
||||||
return P4->IN & BUMP_PINS;
|
return P4->IN;
|
||||||
}
|
}
|
|
@ -53,14 +53,8 @@ void MotorInit(void) {
|
||||||
P3->SEL1 &= ~(LEFT_SLEEP_PIN | RIGHT_SLEEP_PIN);
|
P3->SEL1 &= ~(LEFT_SLEEP_PIN | RIGHT_SLEEP_PIN);
|
||||||
P3->DIR |= (LEFT_SLEEP_PIN | RIGHT_SLEEP_PIN); // Output direction
|
P3->DIR |= (LEFT_SLEEP_PIN | RIGHT_SLEEP_PIN); // Output direction
|
||||||
|
|
||||||
// P2.6 (Right EN), P2.7 (Left EN)
|
|
||||||
P2->SEL0 &= ~(LEFT_PWM_PIN | RIGHT_PWM_PIN); // GPIO function
|
|
||||||
P2->SEL1 &= ~(LEFT_PWM_PIN | RIGHT_PWM_PIN);
|
|
||||||
P2->DIR |= (LEFT_PWM_PIN | RIGHT_PWM_PIN); // Output direction
|
|
||||||
|
|
||||||
// 2. Initial state: disable motors
|
// 2. Initial state: disable motors
|
||||||
P3->OUT &= ~(LEFT_SLEEP_PIN | RIGHT_SLEEP_PIN); // SLEEP pins low - DRV8838 disabled
|
P3->OUT &= ~(LEFT_SLEEP_PIN | RIGHT_SLEEP_PIN); // SLEEP pins low - DRV8838 disabled
|
||||||
P2->OUT &= ~(LEFT_PWM_PIN | RIGHT_PWM_PIN); // EN pins low
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Stop the motors, power down the drivers, and
|
// Stop the motors, power down the drivers, and
|
||||||
|
@ -78,7 +72,7 @@ void MotorStop(void) {
|
||||||
void MotorForward(uint16_t left_duty, uint16_t right_duty) {
|
void MotorForward(uint16_t left_duty, uint16_t right_duty) {
|
||||||
P5->OUT &= ~(LEFT_DIR_PIN | RIGHT_DIR_PIN); // set direction - PH low
|
P5->OUT &= ~(LEFT_DIR_PIN | RIGHT_DIR_PIN); // set direction - PH low
|
||||||
P3->OUT |= (LEFT_SLEEP_PIN | RIGHT_SLEEP_PIN); // wake up motors
|
P3->OUT |= (LEFT_SLEEP_PIN | RIGHT_SLEEP_PIN); // wake up motors
|
||||||
P2->OUT |= (LEFT_PWM_PIN | RIGHT_PWM_PIN);
|
// P2->OUT |= (LEFT_PWM_PIN | RIGHT_PWM_PIN);
|
||||||
|
|
||||||
SetPWMDutyRightMotor(right_duty);
|
SetPWMDutyRightMotor(right_duty);
|
||||||
SetPWMDutyLeftMotor(left_duty);
|
SetPWMDutyLeftMotor(left_duty);
|
||||||
|
@ -89,7 +83,7 @@ void MotorForward(uint16_t left_duty, uint16_t right_duty) {
|
||||||
void MotorBackward(uint16_t left_duty, uint16_t right_duty) {
|
void MotorBackward(uint16_t left_duty, uint16_t right_duty) {
|
||||||
P5->OUT |= (LEFT_DIR_PIN | RIGHT_DIR_PIN); // set direction - PH high
|
P5->OUT |= (LEFT_DIR_PIN | RIGHT_DIR_PIN); // set direction - PH high
|
||||||
P3->OUT |= (LEFT_SLEEP_PIN | RIGHT_SLEEP_PIN); // wake up motors
|
P3->OUT |= (LEFT_SLEEP_PIN | RIGHT_SLEEP_PIN); // wake up motors
|
||||||
P2->OUT |= (LEFT_PWM_PIN | RIGHT_PWM_PIN);
|
// P2->OUT |= (LEFT_PWM_PIN | RIGHT_PWM_PIN);
|
||||||
|
|
||||||
SetPWMDutyRightMotor(right_duty);
|
SetPWMDutyRightMotor(right_duty);
|
||||||
SetPWMDutyLeftMotor(left_duty);
|
SetPWMDutyLeftMotor(left_duty);
|
||||||
|
@ -99,9 +93,9 @@ void MotorBackward(uint16_t left_duty, uint16_t right_duty) {
|
||||||
// Left wheel backward, right wheel forward with the given duty cycles.
|
// Left wheel backward, right wheel forward with the given duty cycles.
|
||||||
// Duty cycle is 0 to 14,998.
|
// Duty cycle is 0 to 14,998.
|
||||||
void MotorLeft(uint16_t left_duty, uint16_t right_duty) {
|
void MotorLeft(uint16_t left_duty, uint16_t right_duty) {
|
||||||
P5->OUT &= ~(LEFT_DIR_PIN); // set direction
|
P5->OUT &= ~(RIGHT_DIR_PIN); // set direction forward
|
||||||
P3->OUT |= (LEFT_SLEEP_PIN); // wake up motor
|
P5->OUT |= LEFT_DIR_PIN; // set direction backward
|
||||||
P2->OUT |= (LEFT_PWM_PIN | RIGHT_PWM_PIN);
|
P3->OUT |= (LEFT_SLEEP_PIN | RIGHT_SLEEP_PIN); // wake up motors
|
||||||
|
|
||||||
SetPWMDutyRightMotor(right_duty);
|
SetPWMDutyRightMotor(right_duty);
|
||||||
SetPWMDutyLeftMotor(left_duty);
|
SetPWMDutyLeftMotor(left_duty);
|
||||||
|
@ -110,9 +104,9 @@ void MotorLeft(uint16_t left_duty, uint16_t right_duty) {
|
||||||
// Left wheel forward, right wheel backward with the given duty cycles.
|
// Left wheel forward, right wheel backward with the given duty cycles.
|
||||||
// Duty cycle is 0 to 14,998.
|
// Duty cycle is 0 to 14,998.
|
||||||
void MotorRight(uint16_t left_duty, uint16_t right_duty) {
|
void MotorRight(uint16_t left_duty, uint16_t right_duty) {
|
||||||
P5->OUT &= ~(RIGHT_DIR_PIN); // set direction - PH high
|
P5->OUT &= ~(LEFT_DIR_PIN); // set direction forward
|
||||||
P3->OUT |= (RIGHT_SLEEP_PIN); // wake up motors
|
P5->OUT |= RIGHT_DIR_PIN; // set direction backward
|
||||||
P2->OUT |= (LEFT_PWM_PIN | RIGHT_PWM_PIN);
|
P3->OUT |= (LEFT_SLEEP_PIN | RIGHT_SLEEP_PIN); // wake up motors
|
||||||
|
|
||||||
SetPWMDutyRightMotor(right_duty);
|
SetPWMDutyRightMotor(right_duty);
|
||||||
SetPWMDutyLeftMotor(left_duty);
|
SetPWMDutyLeftMotor(left_duty);
|
||||||
|
|
|
@ -90,7 +90,7 @@ void MotorForward(uint16_t duty, uint32_t times_10ms) {
|
||||||
SysTickWait1us(10000 - duty);
|
SysTickWait1us(10000 - duty);
|
||||||
|
|
||||||
uint8_t bump = BumpRead();
|
uint8_t bump = BumpRead();
|
||||||
if (!(bump & BUMP_PINS)) break;
|
if ((~bump & BUMP_PINS)) break;
|
||||||
}
|
}
|
||||||
|
|
||||||
MotorStop();
|
MotorStop();
|
||||||
|
@ -112,7 +112,7 @@ void MotorBackward(uint16_t duty, uint32_t times_10ms) {
|
||||||
SysTickWait1us(10000 - duty);
|
SysTickWait1us(10000 - duty);
|
||||||
|
|
||||||
uint8_t bump = BumpRead();
|
uint8_t bump = BumpRead();
|
||||||
if (!(bump & BUMP_PINS)) break;
|
if ((~bump & BUMP_PINS)) break;
|
||||||
}
|
}
|
||||||
|
|
||||||
MotorStop();
|
MotorStop();
|
||||||
|
@ -135,7 +135,7 @@ void MotorLeft(uint16_t duty, uint32_t times_10ms) {
|
||||||
SysTickWait1us(10000 - duty);
|
SysTickWait1us(10000 - duty);
|
||||||
|
|
||||||
uint8_t bump = BumpRead();
|
uint8_t bump = BumpRead();
|
||||||
if (!(bump & BUMP_PINS)) break;
|
if ((~bump & BUMP_PINS)) break;
|
||||||
}
|
}
|
||||||
|
|
||||||
MotorStop();
|
MotorStop();
|
||||||
|
@ -158,7 +158,7 @@ void MotorRight(uint16_t duty, uint32_t times_10ms) {
|
||||||
SysTickWait1us(10000 - duty);
|
SysTickWait1us(10000 - duty);
|
||||||
|
|
||||||
uint8_t bump = BumpRead();
|
uint8_t bump = BumpRead();
|
||||||
if (!(bump & BUMP_PINS)) break;
|
if ((~bump & BUMP_PINS)) break;
|
||||||
}
|
}
|
||||||
|
|
||||||
MotorStop();
|
MotorStop();
|
||||||
|
|
|
@ -12,12 +12,16 @@ void PWMInitP2_4_P2_5(uint16_t period) {
|
||||||
P2->DIR |= 0x30;
|
P2->DIR |= 0x30;
|
||||||
P2->SEL0 |= 0x30; // Timer0A functions
|
P2->SEL0 |= 0x30; // Timer0A functions
|
||||||
P2->SEL1 &= ~0x30;
|
P2->SEL1 &= ~0x30;
|
||||||
|
|
||||||
TIMER_A0->CCR[0] = period;
|
TIMER_A0->CCR[0] = period;
|
||||||
TIMER_A0->EX0 = 0; // Divide by 1
|
TIMER_A0->EX0 = 0; // Divide by 1
|
||||||
|
|
||||||
TIMER_A0->CCTL[1] = 0x40; // Toggle/reset
|
TIMER_A0->CCTL[1] = 0x40; // Toggle/reset
|
||||||
TIMER_A0->CCTL[2] = 0x40;
|
TIMER_A0->CCTL[2] = 0x40;
|
||||||
|
|
||||||
TIMER_A0->CCR[1] = 0; // Initial duty cycle to 0
|
TIMER_A0->CCR[1] = 0; // Initial duty cycle to 0
|
||||||
TIMER_A0->CCR[2] = 0;
|
TIMER_A0->CCR[2] = 0;
|
||||||
|
|
||||||
TIMER_A0->CTL = 0x2F0; // SMCLK = 12MHz, divide by 8, up-down mode
|
TIMER_A0->CTL = 0x2F0; // SMCLK = 12MHz, divide by 8, up-down mode
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -9,25 +9,24 @@ void (*timer_a1_task)(void); // user function
|
||||||
void TimerA1Init(void (*task)(void), uint16_t period) {
|
void TimerA1Init(void (*task)(void), uint16_t period) {
|
||||||
timer_a1_task = task;
|
timer_a1_task = task;
|
||||||
|
|
||||||
// bits9-8=10, clock source to SMCLK
|
// Stop timer, SMCLK, /4 divider
|
||||||
// bits7-6=10, input clock divider
|
|
||||||
// bits5-4=00, stop mode
|
|
||||||
// bit2=0, set this bit to clear
|
|
||||||
// bit1=0, no interrupt on
|
|
||||||
TIMER_A1->CTL = 0x0280;
|
TIMER_A1->CTL = 0x0280;
|
||||||
|
|
||||||
// bits15-14=00, no capture mode
|
// Configure CCR0
|
||||||
// bit8=0, compare mode
|
TIMER_A1->CCTL[0] = 0x0010; // Compare mode, enable interrupt
|
||||||
// bit4=1, enable capture/compare interrupt on CCIFG
|
|
||||||
// bit2=0, output this value in output mode 0
|
|
||||||
TIMER_A1->CCTL[0] &= ~0x0001;
|
|
||||||
|
|
||||||
TIMER_A1->CCR[0] = (period - 1);
|
TIMER_A1->CCR[0] = (period - 1);
|
||||||
TIMER_A1->EX0 = 0x0005;
|
|
||||||
|
// Clock divider (EX0 register)
|
||||||
|
TIMER_A1->EX0 = 0x0005; // /6 divider (additional to the /4 above)
|
||||||
|
|
||||||
|
// NVIC configuration
|
||||||
NVIC->IP[3] = (NVIC->IP[3]&0xFFFFFF00)|0x00000040; // priority 2
|
NVIC->IP[3] = (NVIC->IP[3]&0xFFFFFF00)|0x00000040; // priority 2
|
||||||
NVIC->ISER[0] = 0x00001000; // enable interrupt 12 in NVIC
|
NVIC->ISER[0] = 0x00001000; // enable interrupt 12 in NVIC
|
||||||
TIMER_A1->CTL |= 0x0014; // reset and start Timer A in up mode
|
|
||||||
TIMER_A1->CTL |= 0x0020; // Enable Timer A1 interrupts (TAIE)
|
// Enable timer interrupts and start in up mode
|
||||||
|
TIMER_A1->CTL |= 0x0014 | 0x0020; // reset, up mode, enable interrupts
|
||||||
|
|
||||||
|
NVIC_EnableIRQ(TA1_0_IRQn);
|
||||||
}
|
}
|
||||||
|
|
||||||
void TA1_0_IRQHandler(void) {
|
void TA1_0_IRQHandler(void) {
|
||||||
|
|
21
lab5/main.c
21
lab5/main.c
|
@ -52,7 +52,7 @@ int main_f(void) {
|
||||||
while (true) {}
|
while (true) {}
|
||||||
}
|
}
|
||||||
|
|
||||||
int main(void) {
|
int main_s(void) {
|
||||||
ClockInit48MHz();
|
ClockInit48MHz();
|
||||||
SwitchInit();
|
SwitchInit();
|
||||||
PWMInitMotor(7502); // your function
|
PWMInitMotor(7502); // your function
|
||||||
|
@ -69,15 +69,24 @@ int main(void) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void checkBump() {
|
||||||
|
uint8_t bump = BumpRead();
|
||||||
|
if ((~bump & BUMP_PINS))
|
||||||
|
MotorStop();
|
||||||
|
}
|
||||||
|
|
||||||
// Write a program that uses PWM to move the robot
|
// Write a program that uses PWM to move the robot
|
||||||
// but uses TimerA1 to periodically check the bump switches,
|
// but uses TimerA1 to periodically check the bump switches,
|
||||||
// stopping the robot on collision
|
// stopping the robot on collision
|
||||||
int main_final(void) {
|
int main(void) {
|
||||||
ClockInit48MHz();
|
ClockInit48MHz();
|
||||||
// write code here
|
BumpInit();
|
||||||
|
PWMInitMotor(7502);
|
||||||
|
MotorInit();
|
||||||
|
MotorForward(1500, 1500);
|
||||||
|
TimerA1Init(checkBump, 15000);
|
||||||
CPU_cpsie(); // Enable interrupts
|
CPU_cpsie(); // Enable interrupts
|
||||||
while (true) {
|
|
||||||
// write code here
|
while (true);
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue