2024-11-29 14:44:01 +05:30
|
|
|
#include <stdint.h>
|
|
|
|
#include <stdbool.h>
|
|
|
|
#include "tm4c123gh6pm.h"
|
2024-11-29 15:15:14 +05:30
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
2024-11-29 14:44:01 +05:30
|
|
|
|
2024-11-29 15:06:52 +05:30
|
|
|
#define MAX_FIELD_SIZE 100
|
|
|
|
volatile char nmea_sentence[MAX_FIELD_SIZE];
|
|
|
|
volatile uint8_t nmea_index = 0;
|
|
|
|
volatile bool nmea_ready = false;
|
|
|
|
|
2024-11-30 03:20:08 +05:30
|
|
|
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);
|
|
|
|
|
2024-11-29 15:15:14 +05:30
|
|
|
|
2024-11-29 14:44:01 +05:30
|
|
|
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
|
2024-11-29 14:49:18 +05:30
|
|
|
UART1_LCRH_R = 0x60; // 8-bit, No parity, 1 stop bit
|
2024-11-29 14:44:01 +05:30
|
|
|
UART1_CC_R = 0x00; // Use system clock
|
|
|
|
|
2024-11-29 14:52:52 +05:30
|
|
|
UART1_CTL_R |= 0x301; // Enable UART1, RX, and TX0
|
|
|
|
UART1_IM_R |= 0x10;
|
|
|
|
NVIC_EN0_R |= (1 << 6);
|
2024-11-29 14:44:01 +05:30
|
|
|
}
|
2024-11-29 14:54:00 +05:30
|
|
|
// 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)
|
|
|
|
}
|
|
|
|
}
|
2024-11-29 14:44:01 +05:30
|
|
|
|
2024-11-29 15:29:43 +05:30
|
|
|
|
|
|
|
|
|
|
|
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++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2024-11-30 03:08:33 +05:30
|
|
|
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++;
|
|
|
|
}
|
|
|
|
}
|
2024-11-29 15:29:43 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
|
2024-11-29 15:01:00 +05:30
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
2024-11-29 14:52:52 +05:30
|
|
|
|
2024-11-29 15:20:18 +05:30
|
|
|
int main(void)
|
|
|
|
{
|
|
|
|
GPIO_PORT_F_init();
|
|
|
|
GPIO_PORT_B_init();
|
|
|
|
while (1)
|
|
|
|
{
|
|
|
|
if (nmea_ready)
|
|
|
|
{
|
|
|
|
nmea_ready = false;
|
2024-11-30 03:13:06 +05:30
|
|
|
process_nmea_sentence((const char *)nmea_sentence);
|
2024-11-29 15:20:18 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|