Group21_Project/main.c

94 lines
3.2 KiB
C

#include <stdint.h>
#include <stdbool.h>
#include "tm4c123gh6pm.h"
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define MAX_FIELD_SIZE 100
volatile char nmea_sentence[MAX_FIELD_SIZE];
volatile uint8_t nmea_index = 0;
volatile bool nmea_ready = false;
void GPIO_PORT_F_init(void)
{
SYSCTL_RCGC2_R |= 0x00000020; // ENABLE CLOCK TO GPIOF
GPIO_PORTF_LOCK_R = 0x4C4F434B; // UNLOCK COMMIT REGISTER
GPIO_PORTF_CR_R = 0x1F; // MAKE PORTF0 CONFIGURABLE
GPIO_PORTF_DEN_R = 0x1F; // SET PORTF DIGITAL ENABLE
GPIO_PORTF_DIR_R = 0x0E; // SET PF0, PF4 as input and PF1, PF2 and PF3 as output
GPIO_PORTF_PUR_R = 0x11; // PORTF PF0 and PF4 IS PULLED UP
}
void GPIO_PORT_B_init(void)
{
SYSCTL_RCGCGPIO_R |= 0x02; // ENABLE CLOCK FOR GPIOB
SYSCTL_RCGCUART_R |= 0x02; // ENABLE CLOCK FOR UART1
GPIO_PORTB_DEN_R |= 0x03; // DIGITAL ENABLE FOR PB0 AND PB1
GPIO_PORTB_AFSEL_R |= 0x03; // ENABLE ALTERNATE FUNCTION ON PB0,PB1
GPIO_PORTB_PCTL_R = (GPIO_PORTB_PCTL_R & 0xFFFFFF00) | 0x00000011; // Set PB0, PB1 for UART
UART1_CTL_R &= ~0x01; // DISABLE UART1 DURING SETUP
UART1_IBRD_R = 104; // Set integer part of baud rate (for 9600 baud at 16 MHz clock)
UART1_FBRD_R = 11; // Set fractional part of baud rate
UART1_LCRH_R = 0x60; // 8-bit, No parity, 1 stop bit
UART1_CC_R = 0x00; // Use system clock
UART1_CTL_R |= 0x301; // Enable UART1, RX, and TX0
UART1_IM_R |= 0x10;
NVIC_EN0_R |= (1 << 6);
}
// Control LEDs based on speed
void control_leds_based_on_speed(float speed_kmh) {
// Clear all LED bits (assuming LEDs are connected to bits 1, 2, and 3)
GPIO_PORTF_DATA_R &= 0x00;
// Determine which 5 km/h range the speed falls into
if (speed_kmh >= 0.0 && speed_kmh < 10.0) {
// Range 0-10 km/h
GPIO_PORTF_DATA_R |= 0x08; // Red LED
} else if (speed_kmh >= 10.0 && speed_kmh < 20.0) {
// Range 10-20 km/h
GPIO_PORTF_DATA_R |= 0x0C; // Blue LED
} else if (speed_kmh >= 20.0 && speed_kmh < 30.0) {
// Range 20-30 km/h
GPIO_PORTF_DATA_R |= 0x04; // Green LED
} else if (speed_kmh >= 30.0 && speed_kmh < 40.0) {
// Range 30-40 km/h
GPIO_PORTF_DATA_R |= 0x06; // Yellow LED (Red + Green)
} else if (speed_kmh >= 40.0 && speed_kmh < 50.0) {
// Range 40-50 km/h
GPIO_PORTF_DATA_R |= 0x02; // Blue LED
} else {
// Above 50 km/h
GPIO_PORTF_DATA_R |= 0x0E; // Maximum LED (Red + Blue + Green)
}
}
void UART1_Handler(void)
{
char data = UART1_DR_R & 0xFF;
if (data == '\n' || data == '\r')
{
nmea_sentence[nmea_index] = '\0';
if (strncmp((const char *)nmea_sentence, "$GPRMC", 6) == 0)
{
nmea_ready = true;
}
nmea_index = 0;
}
else if (nmea_index < MAX_FIELD_SIZE - 1)
{
nmea_sentence[nmea_index++] = data;
}
}