175 lines
5.2 KiB
C
175 lines
5.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);
|
|
void GPIO_PORT_B_init(void);
|
|
void UART1_WRITE(char data);
|
|
void control_leds_based_on_speed(float speed_kmh);
|
|
void process_nmea_sentence(const char *nmea_sentence);
|
|
|
|
|
|
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);
|
|
}
|
|
|
|
void control_leds_based_on_speed(float speed_kmh) // Control LEDs based on speed
|
|
{
|
|
|
|
GPIO_PORTF_DATA_R &= 0x00; // Clear all LED bits (assuming LEDs are connected to bits 1, 2, and 3)
|
|
|
|
|
|
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 nmea_sentence_process(const char *nmea_sentence)
|
|
{
|
|
uint8_t i = 0;
|
|
uint8_t field_number = 0;
|
|
float speed_kmh = 0.0;
|
|
uint8_t field_index = 0;
|
|
|
|
if (nmea_sentence[0] != '$') return; // Invalid sentence, exit
|
|
|
|
if (strncmp(nmea_sentence + 1, "GPRMC", 5) == 0)
|
|
{
|
|
for (i = 6; nmea_sentence[i] != '\0'; i++)
|
|
{
|
|
if (nmea_sentence[i] == ',')
|
|
{
|
|
if (field_number == 7) // Speed in knots (field 7)
|
|
{ speed_kmh = atof(nmea_sentence + i - field_index);
|
|
control_leds_based_on_speed(speed_kmh);
|
|
}
|
|
field_number++;
|
|
field_index = 0;
|
|
}
|
|
else if (nmea_sentence[i] == '*')
|
|
{
|
|
break;
|
|
}
|
|
else
|
|
{
|
|
field_index++;
|
|
}
|
|
}
|
|
}
|
|
else if (strncmp(nmea_sentence + 1, "GPVTG", 5) == 0) // Process GPVTG sentence
|
|
{
|
|
for (i = 7; nmea_sentence[i] != '\0'; i++)
|
|
{
|
|
if (nmea_sentence[i] == ',')
|
|
{
|
|
if (field_number == 6)
|
|
{
|
|
speed_kmh = atof(nmea_sentence + i - field_index);
|
|
control_leds_based_on_speed(speed_kmh);
|
|
}
|
|
field_number++;
|
|
field_index = 0;
|
|
}
|
|
else if (nmea_sentence[i] == '*')
|
|
{
|
|
break;
|
|
}
|
|
else {
|
|
field_index++;
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
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;
|
|
}
|
|
}
|
|
|
|
int main(void)
|
|
{
|
|
GPIO_PORT_F_init();
|
|
GPIO_PORT_B_init();
|
|
while (1)
|
|
{
|
|
if (nmea_ready)
|
|
{
|
|
nmea_ready = false;
|
|
process_nmea_sentence((const char *)nmea_sentence);
|
|
}
|
|
}
|
|
}
|