From 9af22be64d8e0b121927af0d7232fed3219a3697 Mon Sep 17 00:00:00 2001 From: Ondrej Hladuvka Date: Wed, 30 Apr 2025 15:41:24 +0300 Subject: [PATCH] lab3 --- common/reflectance.c | 88 ++++++++++++++++++++++++++++++++++++++++---- lab2/main.c | 18 ++++++++- lab3/main.c | 31 +++++++++------- 3 files changed, 115 insertions(+), 22 deletions(-) diff --git a/common/reflectance.c b/common/reflectance.c index a2a4cda..b522deb 100644 --- a/common/reflectance.c +++ b/common/reflectance.c @@ -5,33 +5,105 @@ #include "inc/msp432p401r.h" void ReflectanceInit(void) { - // write this as part of Lab 3 + P5->SEL0 &= ~0x08; + P5->SEL1 &= ~0x08; // configure P5.3 as GPIO + P5->DIR |= 0x08; // make P5.3 output + P5->OUT &= ~0x08; // set output low + + P9->SEL0 &= ~0x04; + P9->SEL1 &= ~0x04; // configure P9.2 as GPIO + P9->DIR |= 0x04; // make P9.2 output + P9->OUT &= ~0x04; // set output low + + P7->SEL0 &= ~0x255; + P7->SEL1 &= ~0x255; // configure P7.0-7 as GPIO + P7->DIR &= ~0x255; // make them input + // P7->REN |= 0x255; //? enable pull resistors } +// 0 -> while, 1 -> black uint8_t ReflectanceRead(uint32_t time) { - // write this as part of Lab 3 - return 0; + uint8_t input; + + // 1) Set P5.3 and P9.2 high (turn on IR LED) + P5->OUT |= 0x08; + P9->OUT |= 0x04; + + // 2) Make P7.7 – P7.0 outputs, and set them high (charging 8 capacitors) + P7->DIR |= 0x255; + P7->OUT |= 0x255; + + // 3) Wait 10 us, Delay1us(10); + Delay1us(10); + + // 4) Make P7.7 – P7.0 input + P7->DIR &= ~0x255; + + // 5) Wait time us, Delay1us(time); + Delay1us(time); + + // 6) Read P7.7 – P7.0 inputs (converts voltages into binary) + input = P7->IN; + + // 7) Set P5.3 and P9.2 low (turn off IR LED, saving power) + P5->OUT &= ~0x08; + P9->OUT &= ~0x04; + + //P7->DIR &= ~0x255; + //P7->OUT &= ~0x255; + + // 8) Return 8-bit binary measured in step 6 + return input; } int32_t ReflectancePosition(uint8_t sensor_data) { - // write this as part of Lab 3 - return 0; + // Data falls into four categories: + // 1) (off the line or on white surface) + // 2) , e.g., 00000111 (off to the left) + // 3) , e.g., 00110000 (over the line) + // 4) < some 1’s, some 0’s>, e.g., 11110000 (off to the right) + + int32_t W[] = {334, 238, 142, 48, -48, -142, -238, -334}; + // if (!sensor_data) return 0; // off the line or on white surface + + int32_t i, up, down, b; + for (up = down = i = 0; i < 8; ++i) { + b = (sensor_data & (1 << i)) != 0; + up += b * W[i]; + down += b; + } + + return up / down; } void ReflectanceStart(void) { - // write this as part of Lab 3 // turn on 4 even IR LEDs // turn on 4 odd IR LEDs + P5->OUT |= 0x08; + P9->OUT |= 0x04; + // make P7.7-P7.0 out + P7->DIR |= 0x255; + // prime for measurement + P7->OUT |= 0x255; + // wait 10 us + Delay1us(10); + // make P7.7-P7.0 in + P7->DIR &= ~0x255; } uint8_t ReflectanceEnd(void) { - // write this as part of Lab 3 + uint8_t input; // convert P7.0 input to digital + input = P7->IN; + // turn off 4 even IR LEDs // turn off 4 odd IR LEDs - return 0; // replace this line + P5->OUT &= ~0x08; + P9->OUT &= ~0x04; + + return input; } diff --git a/lab2/main.c b/lab2/main.c index a2e3e99..1a781e5 100644 --- a/lab2/main.c +++ b/lab2/main.c @@ -2,6 +2,23 @@ #include #include "../common/bump.h" +/* +#define BUMP_PINS 0b11101101 + +void BumpInit(void) { + // Port 4 pins 0, 2, 3, 5, 6, 7 + // and enables internal resistors + P4->SEL0 &= ~BUMP_PINS; + P4->SEL1 &= ~BUMP_PINS; // 1) configure P4.0 P4.2 P4.3 P4.5 P4.6 P4.7 as gpio + P4->DIR &= ~BUMP_PINS; // 2) configure as input + P4->REN |= BUMP_PINS; // 3) enable pull resisors +} + +uint8_t BumpRead(void) { + // write this as part of Lab 2 + return P4->IN & BUMP_PINS; +} +*/ #include "../common/delay.h" #include "../common/clock.h" @@ -52,7 +69,6 @@ void Port2Init(void) { int main(void) { ClockInit48MHz(); BumpInit(); - Port2Init(); while (true) { diff --git a/lab3/main.c b/lab3/main.c index fc4d649..e373902 100644 --- a/lab3/main.c +++ b/lab3/main.c @@ -9,17 +9,18 @@ #include "inc/msp432p401r.h" -#define REDLED (*((volatile uint8_t *)(0x42098040))) - #define DUMP_SIZE 256 uint8_t bump_status; uint8_t bump_dump[DUMP_SIZE]; +uint8_t refl_dump[DUMP_SIZE]; -void DebugDump(uint8_t bump) { +void DebugDump(uint8_t bump, uint8_t refl) { static int idx = 0; idx = (idx + 1) % DUMP_SIZE; + bump_dump[idx] = bump; + refl_dump[idx] = refl; } // **************SysTick_Init********************* @@ -38,23 +39,27 @@ void SysTickInit(uint32_t period, uint32_t priority) { SysTick->CTRL = 0x00000007; // 4) enable SysTick with core clock and interrupts } +volatile uint8_t reflectance = 0x255; +int tick_count = 0; + void SysTick_Handler(void) { // every 1ms - // REDLED = 1; - // uint8_t bump = BumpRead(); - // REDLED = 0; - volatile int i = 0; - ++i; + ++tick_count; + + if (1 == (tick_count % 10)) { + ReflectanceStart(); // every 10ms + } + + if (2 == (tick_count % 10)) { + DebugDump( BumpRead(), ReflectanceEnd() ); + } } int main(void) { ClockInit48MHz(); BumpInit(); ReflectanceInit(); - SysTickInit(480000, 1); // 1ms SysTickInit + SysTickInit(480000, 1); // 1ms SysTickInit CPU_cpsie(); // Enable interrupts - while (true) { - REDLED = 1; - REDLED = 0; - } + while (true); }