lab5 done

This commit is contained in:
Ondrej Hladuvka 2025-05-13 18:32:44 +03:00
parent 48fd2e6aed
commit 91e85280d5
6 changed files with 47 additions and 41 deletions

View File

@ -13,5 +13,5 @@ void BumpInit(void) {
uint8_t BumpRead(void) {
// write this as part of Lab 2
return P4->IN & BUMP_PINS;
return P4->IN;
}

View File

@ -52,15 +52,9 @@ void MotorInit(void) {
P3->SEL0 &= ~(LEFT_SLEEP_PIN | RIGHT_SLEEP_PIN); // GPIO function
P3->SEL1 &= ~(LEFT_SLEEP_PIN | RIGHT_SLEEP_PIN);
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
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
@ -78,7 +72,7 @@ void MotorStop(void) {
void MotorForward(uint16_t left_duty, uint16_t right_duty) {
P5->OUT &= ~(LEFT_DIR_PIN | RIGHT_DIR_PIN); // set direction - PH low
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);
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) {
P5->OUT |= (LEFT_DIR_PIN | RIGHT_DIR_PIN); // set direction - PH high
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);
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.
// Duty cycle is 0 to 14,998.
void MotorLeft(uint16_t left_duty, uint16_t right_duty) {
P5->OUT &= ~(LEFT_DIR_PIN); // set direction
P3->OUT |= (LEFT_SLEEP_PIN); // wake up motor
P2->OUT |= (LEFT_PWM_PIN | RIGHT_PWM_PIN);
P5->OUT &= ~(RIGHT_DIR_PIN); // set direction forward
P5->OUT |= LEFT_DIR_PIN; // set direction backward
P3->OUT |= (LEFT_SLEEP_PIN | RIGHT_SLEEP_PIN); // wake up motors
SetPWMDutyRightMotor(right_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.
// Duty cycle is 0 to 14,998.
void MotorRight(uint16_t left_duty, uint16_t right_duty) {
P5->OUT &= ~(RIGHT_DIR_PIN); // set direction - PH high
P3->OUT |= (RIGHT_SLEEP_PIN); // wake up motors
P2->OUT |= (LEFT_PWM_PIN | RIGHT_PWM_PIN);
P5->OUT &= ~(LEFT_DIR_PIN); // set direction forward
P5->OUT |= RIGHT_DIR_PIN; // set direction backward
P3->OUT |= (LEFT_SLEEP_PIN | RIGHT_SLEEP_PIN); // wake up motors
SetPWMDutyRightMotor(right_duty);
SetPWMDutyLeftMotor(left_duty);

View File

@ -90,7 +90,7 @@ void MotorForward(uint16_t duty, uint32_t times_10ms) {
SysTickWait1us(10000 - duty);
uint8_t bump = BumpRead();
if (!(bump & BUMP_PINS)) break;
if ((~bump & BUMP_PINS)) break;
}
MotorStop();
@ -112,7 +112,7 @@ void MotorBackward(uint16_t duty, uint32_t times_10ms) {
SysTickWait1us(10000 - duty);
uint8_t bump = BumpRead();
if (!(bump & BUMP_PINS)) break;
if ((~bump & BUMP_PINS)) break;
}
MotorStop();
@ -135,7 +135,7 @@ void MotorLeft(uint16_t duty, uint32_t times_10ms) {
SysTickWait1us(10000 - duty);
uint8_t bump = BumpRead();
if (!(bump & BUMP_PINS)) break;
if ((~bump & BUMP_PINS)) break;
}
MotorStop();
@ -156,9 +156,9 @@ void MotorRight(uint16_t duty, uint32_t times_10ms) {
P2->OUT &= ~(RIGHT_PWM_PIN); // Disable motors
SysTickWait1us(10000 - duty);
uint8_t bump = BumpRead();
if (!(bump & BUMP_PINS)) break;
if ((~bump & BUMP_PINS)) break;
}
MotorStop();

View File

@ -12,12 +12,16 @@ void PWMInitP2_4_P2_5(uint16_t period) {
P2->DIR |= 0x30;
P2->SEL0 |= 0x30; // Timer0A functions
P2->SEL1 &= ~0x30;
TIMER_A0->CCR[0] = period;
TIMER_A0->EX0 = 0; // Divide by 1
TIMER_A0->CCTL[1] = 0x40; // Toggle/reset
TIMER_A0->CCTL[2] = 0x40;
TIMER_A0->CCR[1] = 0; // Initial duty cycle to 0
TIMER_A0->CCR[2] = 0;
TIMER_A0->CTL = 0x2F0; // SMCLK = 12MHz, divide by 8, up-down mode
}

View File

@ -9,28 +9,27 @@ void (*timer_a1_task)(void); // user function
void TimerA1Init(void (*task)(void), uint16_t period) {
timer_a1_task = task;
// bits9-8=10, clock source to SMCLK
// bits7-6=10, input clock divider
// bits5-4=00, stop mode
// bit2=0, set this bit to clear
// bit1=0, no interrupt on
// Stop timer, SMCLK, /4 divider
TIMER_A1->CTL = 0x0280;
// bits15-14=00, no capture mode
// bit8=0, compare mode
// bit4=1, enable capture/compare interrupt on CCIFG
// bit2=0, output this value in output mode 0
TIMER_A1->CCTL[0] &= ~0x0001;
// Configure CCR0
TIMER_A1->CCTL[0] = 0x0010; // Compare mode, enable interrupt
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->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) {
TIMER_A1->CCTL[0] &= ~0x0001;
timer_a1_task();
}

View File

@ -52,7 +52,7 @@ int main_f(void) {
while (true) {}
}
int main(void) {
int main_s(void) {
ClockInit48MHz();
SwitchInit();
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
// but uses TimerA1 to periodically check the bump switches,
// stopping the robot on collision
int main_final(void) {
int main(void) {
ClockInit48MHz();
// write code here
BumpInit();
PWMInitMotor(7502);
MotorInit();
MotorForward(1500, 1500);
TimerA1Init(checkBump, 15000);
CPU_cpsie(); // Enable interrupts
while (true) {
// write code here
}
while (true);
}