From 89113917dcad88159717b05521c3f38aca9bbeee Mon Sep 17 00:00:00 2001 From: Ondrej Hladuvka Date: Mon, 5 May 2025 12:43:09 +0300 Subject: [PATCH] lab3 finished --- common/reflectance.c | 71 ++++++++++++++++++++++++-------------------- lab3/main.c | 26 ++++++++-------- 2 files changed, 52 insertions(+), 45 deletions(-) diff --git a/common/reflectance.c b/common/reflectance.c index b522deb..7cded31 100644 --- a/common/reflectance.c +++ b/common/reflectance.c @@ -4,21 +4,26 @@ #include "inc/msp432p401r.h" +#define BIT_2 (uint8_t)0x4 +#define BIT_3 (uint8_t)0x8 + +#define ALL_BITS (uint8_t)0xff +#define NONE_BITS (uint8_t)0x00 + void ReflectanceInit(void) { - P5->SEL0 &= ~0x08; - P5->SEL1 &= ~0x08; // configure P5.3 as GPIO - P5->DIR |= 0x08; // make P5.3 output - P5->OUT &= ~0x08; // set output low + P5->SEL0 &= ~BIT_3; // EVEN LEDs + P5->SEL1 &= ~BIT_3; // configure P5.3 as GPIO + P5->DIR |= BIT_3; // make P5.3 output + P5->OUT &= ~BIT_3; // 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 + P9->SEL0 &= ~BIT_2; // ODD LEDs + P9->SEL1 &= ~BIT_2; // configure P9.2 as GPIO + P9->DIR |= BIT_2; // make P9.2 output + P9->OUT &= ~BIT_2; // 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 + P7->SEL0 &= NONE_BITS; + P7->SEL1 &= NONE_BITS; // configure P7.0-7 as GPIO + P7->DIR &= NONE_BITS; // make them input } // 0 -> while, 1 -> black @@ -26,18 +31,18 @@ uint8_t ReflectanceRead(uint32_t time) { uint8_t input; // 1) Set P5.3 and P9.2 high (turn on IR LED) - P5->OUT |= 0x08; - P9->OUT |= 0x04; + P5->OUT |= BIT_3; + P9->OUT |= BIT_2; // 2) Make P7.7 – P7.0 outputs, and set them high (charging 8 capacitors) - P7->DIR |= 0x255; - P7->OUT |= 0x255; + P7->DIR = ALL_BITS; + P7->OUT = ALL_BITS; // 3) Wait 10 us, Delay1us(10); Delay1us(10); // 4) Make P7.7 – P7.0 input - P7->DIR &= ~0x255; + P7->DIR = NONE_BITS; // 5) Wait time us, Delay1us(time); Delay1us(time); @@ -46,26 +51,26 @@ uint8_t ReflectanceRead(uint32_t time) { input = P7->IN; // 7) Set P5.3 and P9.2 low (turn off IR LED, saving power) - P5->OUT &= ~0x08; - P9->OUT &= ~0x04; + P5->OUT &= ~BIT_3; + P9->OUT &= ~BIT_2; - //P7->DIR &= ~0x255; - //P7->OUT &= ~0x255; + P7->DIR = NONE_BITS; + P7->OUT = NONE_BITS; // 8) Return 8-bit binary measured in step 6 return input; } int32_t ReflectancePosition(uint8_t sensor_data) { + const int32_t W[] = {334, 238, 142, 48, -48, -142, -238, -334}; + // Data falls into four categories: // 1) (off the line or on white surface) + if (!sensor_data) return 0; // 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 - + // 4) , e.g., 11110000 (off to the right) int32_t i, up, down, b; for (up = down = i = 0; i < 8; ++i) { b = (sensor_data & (1 << i)) != 0; @@ -79,20 +84,20 @@ int32_t ReflectancePosition(uint8_t sensor_data) { void ReflectanceStart(void) { // turn on 4 even IR LEDs // turn on 4 odd IR LEDs - P5->OUT |= 0x08; - P9->OUT |= 0x04; + P5->OUT |= BIT_3; + P9->OUT |= BIT_2; // make P7.7-P7.0 out - P7->DIR |= 0x255; + P7->DIR = ALL_BITS; // prime for measurement - P7->OUT |= 0x255; + P7->OUT = ALL_BITS; // wait 10 us Delay1us(10); // make P7.7-P7.0 in - P7->DIR &= ~0x255; + P7->DIR = NONE_BITS; } uint8_t ReflectanceEnd(void) { @@ -102,8 +107,8 @@ uint8_t ReflectanceEnd(void) { // turn off 4 even IR LEDs // turn off 4 odd IR LEDs - P5->OUT &= ~0x08; - P9->OUT &= ~0x04; + P5->OUT &= ~BIT_3; + P9->OUT &= ~BIT_2; return input; } diff --git a/lab3/main.c b/lab3/main.c index e373902..0afc4a6 100644 --- a/lab3/main.c +++ b/lab3/main.c @@ -1,21 +1,21 @@ #include #include -#include "bump.h" -#include "delay.h" -#include "clock.h" -#include "reflectance.h" -#include "cpu.h" +#include "../common/bump.h" +#include "../common/delay.h" +#include "../common/clock.h" +#include "../common/reflectance.h" +#include "../common/cpu.h" -#include "inc/msp432p401r.h" +#include "../inc/msp432p401r.h" -#define DUMP_SIZE 256 +#define DUMP_SIZE 16 uint8_t bump_status; uint8_t bump_dump[DUMP_SIZE]; -uint8_t refl_dump[DUMP_SIZE]; +int32_t refl_dump[DUMP_SIZE]; -void DebugDump(uint8_t bump, uint8_t refl) { +void DebugDump(uint8_t bump, int32_t refl) { static int idx = 0; idx = (idx + 1) % DUMP_SIZE; @@ -39,18 +39,20 @@ void SysTickInit(uint32_t period, uint32_t priority) { SysTick->CTRL = 0x00000007; // 4) enable SysTick with core clock and interrupts } -volatile uint8_t reflectance = 0x255; +volatile uint8_t reflectance = 0xff; int tick_count = 0; void SysTick_Handler(void) { // every 1ms ++tick_count; if (1 == (tick_count % 10)) { - ReflectanceStart(); // every 10ms + //ReflectanceStart(); // every 10ms } if (2 == (tick_count % 10)) { - DebugDump( BumpRead(), ReflectanceEnd() ); + uint8_t r = ReflectanceRead(1000); + DebugDump( BumpRead(), ReflectancePosition(r) ); + return; } }