lab3 finished

This commit is contained in:
Ondrej Hladuvka 2025-05-05 12:43:09 +03:00
parent c4d769003c
commit 89113917dc
2 changed files with 52 additions and 45 deletions

View File

@ -4,21 +4,26 @@
#include "inc/msp432p401r.h" #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) { void ReflectanceInit(void) {
P5->SEL0 &= ~0x08; P5->SEL0 &= ~BIT_3; // EVEN LEDs
P5->SEL1 &= ~0x08; // configure P5.3 as GPIO P5->SEL1 &= ~BIT_3; // configure P5.3 as GPIO
P5->DIR |= 0x08; // make P5.3 output P5->DIR |= BIT_3; // make P5.3 output
P5->OUT &= ~0x08; // set output low P5->OUT &= ~BIT_3; // set output low
P9->SEL0 &= ~0x04; P9->SEL0 &= ~BIT_2; // ODD LEDs
P9->SEL1 &= ~0x04; // configure P9.2 as GPIO P9->SEL1 &= ~BIT_2; // configure P9.2 as GPIO
P9->DIR |= 0x04; // make P9.2 output P9->DIR |= BIT_2; // make P9.2 output
P9->OUT &= ~0x04; // set output low P9->OUT &= ~BIT_2; // set output low
P7->SEL0 &= ~0x255; P7->SEL0 &= NONE_BITS;
P7->SEL1 &= ~0x255; // configure P7.0-7 as GPIO P7->SEL1 &= NONE_BITS; // configure P7.0-7 as GPIO
P7->DIR &= ~0x255; // make them input P7->DIR &= NONE_BITS; // make them input
// P7->REN |= 0x255; //? enable pull resistors
} }
// 0 -> while, 1 -> black // 0 -> while, 1 -> black
@ -26,18 +31,18 @@ uint8_t ReflectanceRead(uint32_t time) {
uint8_t input; uint8_t input;
// 1) Set P5.3 and P9.2 high (turn on IR LED) // 1) Set P5.3 and P9.2 high (turn on IR LED)
P5->OUT |= 0x08; P5->OUT |= BIT_3;
P9->OUT |= 0x04; P9->OUT |= BIT_2;
// 2) Make P7.7 P7.0 outputs, and set them high (charging 8 capacitors) // 2) Make P7.7 P7.0 outputs, and set them high (charging 8 capacitors)
P7->DIR |= 0x255; P7->DIR = ALL_BITS;
P7->OUT |= 0x255; P7->OUT = ALL_BITS;
// 3) Wait 10 us, Delay1us(10); // 3) Wait 10 us, Delay1us(10);
Delay1us(10); Delay1us(10);
// 4) Make P7.7 P7.0 input // 4) Make P7.7 P7.0 input
P7->DIR &= ~0x255; P7->DIR = NONE_BITS;
// 5) Wait time us, Delay1us(time); // 5) Wait time us, Delay1us(time);
Delay1us(time); Delay1us(time);
@ -46,26 +51,26 @@ uint8_t ReflectanceRead(uint32_t time) {
input = P7->IN; input = P7->IN;
// 7) Set P5.3 and P9.2 low (turn off IR LED, saving power) // 7) Set P5.3 and P9.2 low (turn off IR LED, saving power)
P5->OUT &= ~0x08; P5->OUT &= ~BIT_3;
P9->OUT &= ~0x04; P9->OUT &= ~BIT_2;
//P7->DIR &= ~0x255; P7->DIR = NONE_BITS;
//P7->OUT &= ~0x255; P7->OUT = NONE_BITS;
// 8) Return 8-bit binary measured in step 6 // 8) Return 8-bit binary measured in step 6
return input; return input;
} }
int32_t ReflectancePosition(uint8_t sensor_data) { int32_t ReflectancePosition(uint8_t sensor_data) {
const int32_t W[] = {334, 238, 142, 48, -48, -142, -238, -334};
// Data falls into four categories: // Data falls into four categories:
// 1) <all 0s> (off the line or on white surface) // 1) <all 0s> (off the line or on white surface)
if (!sensor_data) return 0; // off the line or on white surface
// 2) <some 0s, some 1s>, e.g., 00000111 (off to the left) // 2) <some 0s, some 1s>, e.g., 00000111 (off to the left)
// 3) <some 0s, some 1s, some 0s>, e.g., 00110000 (over the line) // 3) <some 0s, some 1s, some 0s>, e.g., 00110000 (over the line)
// 4) <some 1s, some 0s>, e.g., 11110000 (off to the right) // 4) <some 1s, some 0s>, 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; int32_t i, up, down, b;
for (up = down = i = 0; i < 8; ++i) { for (up = down = i = 0; i < 8; ++i) {
b = (sensor_data & (1 << i)) != 0; b = (sensor_data & (1 << i)) != 0;
@ -79,20 +84,20 @@ int32_t ReflectancePosition(uint8_t sensor_data) {
void ReflectanceStart(void) { void ReflectanceStart(void) {
// turn on 4 even IR LEDs // turn on 4 even IR LEDs
// turn on 4 odd IR LEDs // turn on 4 odd IR LEDs
P5->OUT |= 0x08; P5->OUT |= BIT_3;
P9->OUT |= 0x04; P9->OUT |= BIT_2;
// make P7.7-P7.0 out // make P7.7-P7.0 out
P7->DIR |= 0x255; P7->DIR = ALL_BITS;
// prime for measurement // prime for measurement
P7->OUT |= 0x255; P7->OUT = ALL_BITS;
// wait 10 us // wait 10 us
Delay1us(10); Delay1us(10);
// make P7.7-P7.0 in // make P7.7-P7.0 in
P7->DIR &= ~0x255; P7->DIR = NONE_BITS;
} }
uint8_t ReflectanceEnd(void) { uint8_t ReflectanceEnd(void) {
@ -102,8 +107,8 @@ uint8_t ReflectanceEnd(void) {
// turn off 4 even IR LEDs // turn off 4 even IR LEDs
// turn off 4 odd IR LEDs // turn off 4 odd IR LEDs
P5->OUT &= ~0x08; P5->OUT &= ~BIT_3;
P9->OUT &= ~0x04; P9->OUT &= ~BIT_2;
return input; return input;
} }

View File

@ -1,21 +1,21 @@
#include <stdbool.h> #include <stdbool.h>
#include <stdint.h> #include <stdint.h>
#include "bump.h" #include "../common/bump.h"
#include "delay.h" #include "../common/delay.h"
#include "clock.h" #include "../common/clock.h"
#include "reflectance.h" #include "../common/reflectance.h"
#include "cpu.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_status;
uint8_t bump_dump[DUMP_SIZE]; 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; static int idx = 0;
idx = (idx + 1) % DUMP_SIZE; 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 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; int tick_count = 0;
void SysTick_Handler(void) { // every 1ms void SysTick_Handler(void) { // every 1ms
++tick_count; ++tick_count;
if (1 == (tick_count % 10)) { if (1 == (tick_count % 10)) {
ReflectanceStart(); // every 10ms //ReflectanceStart(); // every 10ms
} }
if (2 == (tick_count % 10)) { if (2 == (tick_count % 10)) {
DebugDump( BumpRead(), ReflectanceEnd() ); uint8_t r = ReflectanceRead(1000);
DebugDump( BumpRead(), ReflectancePosition(r) );
return;
} }
} }