Add LED blink logic
This commit is contained in:
parent
15cb6ad7ad
commit
e1965417fd
54
main.c
54
main.c
|
@ -17,15 +17,53 @@ 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
|
||||
{ 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
|
||||
}
|
||||
|
||||
|
||||
#define STRELOAD *((volatile uint32_t *) 0xE000E014) // RELOAD VALUE REGISTER
|
||||
#define STCURRENT *((volatile uint32_t *) 0xE000E018) // CURRENT VALUE REGISTER
|
||||
#define STCTRL *((volatile uint32_t *) 0xE000E010) // CONTROL AND STATUS REGISTER
|
||||
#define COUNT_FLAG (1 << 16) // BIT 16 OF CSR AUTOMATICALLY SET TO 1
|
||||
#define ENABLE (1 << 0) // BIT 0 OF CSR TO ENABLE THE TIMER
|
||||
#define CLKINT (1 << 2) // BIT 2 OF CSR TO SPECIFY CPU CLOCK
|
||||
#define INTEN (1 << 1) // BIT 1 OF CSR TO ENABLE INTERRUPT
|
||||
#define CLOCK_HZ 16000000 // CLOCK FREQUENCY OF EK-TM4C123GXL
|
||||
#define SYSTICK_RELOAD_VALUE(us) ((CLOCK_HZ /1000000) * (us) - 1 ) // SYSTICK RELOAD VALUE IN MICROSECONDS BASED ON CLOCK FREQUENCY
|
||||
|
||||
#define RED_LED 0x02;
|
||||
#define BLUE_LED 0x04;
|
||||
#define GREEN_LED 0x08;
|
||||
volatile uint8_t LED_STATE = 0;
|
||||
|
||||
void systick_setting(void) // SYSTICK SETUP FUNCTION
|
||||
{
|
||||
STRELOAD = SYSTICK_RELOAD_VALUE(500000); // SYSTICK RELOAD VALUE 500 milliseconds
|
||||
STCURRENT = 0; //CLEARING STCURRENT REGISTER VALUE
|
||||
STCTRL |= (CLKINT | ENABLE | INTEN); // SET INTERNAL CLOCK, ENABLE THE TIMER AND INTERRUPT ENABLE
|
||||
}
|
||||
|
||||
void delay(int us) //DEFINING DELAY FUNCTION
|
||||
{
|
||||
STRELOAD = SYSTICK_RELOAD_VALUE(us) ; // RELOAD VALUE FOR REQUIRED DELAY IN MICROSECONDS
|
||||
STCURRENT = 0; //CLEARING STCURRENT REGISTER VALUE
|
||||
STCTRL |= (CLKINT | ENABLE | INTEN ); // SET INTERNAL CLOCK, ENABLE THE TIMER
|
||||
while ((STCTRL & COUNT_FLAG) == 0) //WAIT UNTIL FLAG IS SET
|
||||
{ // DO NOTHING
|
||||
}
|
||||
STCTRL &= 0x0; // STOP THE TIMER
|
||||
}
|
||||
|
||||
void SystickHandler(void) //SYSTICK INTERRUPT HANDLER
|
||||
{
|
||||
GPIO_PORTF_DATA_R ^= LED_STATE;
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
@ -55,10 +93,15 @@ void control_leds_based_on_speed(float speed_kmh) // Control LEDs based on spe
|
|||
|
||||
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; // Green LED
|
||||
LED_STATE = GREEN_LED;
|
||||
}
|
||||
if (speed_kmh >= 0.0 && speed_kmh < 10.0) // Range 0-10 km/h
|
||||
{
|
||||
GPIO_PORTF_DATA_R |= 0x08; // Green LED
|
||||
|
||||
}
|
||||
else if (speed_kmh >= 10.0 && speed_kmh < 20.0) // Range 10-20 km/h
|
||||
{
|
||||
|
@ -78,7 +121,7 @@ void control_leds_based_on_speed(float speed_kmh) // Control LEDs based on spe
|
|||
}
|
||||
else // Above 50 km/h
|
||||
{
|
||||
GPIO_PORTF_DATA_R |= 0x0E; // White LED (Red + Blue + Green)
|
||||
LED_STATE = RED_LED; // TOGGLING RED LED
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -168,6 +211,7 @@ int main(void)
|
|||
{
|
||||
GPIO_PORT_F_init();
|
||||
GPIO_PORT_B_init();
|
||||
systick_setting();
|
||||
while (1)
|
||||
{
|
||||
if (nmea_ready)
|
||||
|
|
|
@ -34,6 +34,7 @@ static void NmiSR(void);
|
|||
static void FaultISR(void);
|
||||
static void IntDefaultHandler(void);
|
||||
void UART1_Handler(void);
|
||||
void SystickHandler(void);
|
||||
//*****************************************************************************
|
||||
//
|
||||
// External declaration for the reset handler that is to be called when the
|
||||
|
@ -56,6 +57,7 @@ extern uint32_t __STACK_TOP;
|
|||
//*****************************************************************************
|
||||
// To be added by user
|
||||
void UART1_Handler(void);
|
||||
void SystickHandler(void);
|
||||
//*****************************************************************************
|
||||
//
|
||||
// The vector table. Note that the proper constructs must be placed on this to
|
||||
|
@ -82,7 +84,7 @@ void (* const g_pfnVectors[])(void) =
|
|||
IntDefaultHandler, // Debug monitor handler
|
||||
0, // Reserved
|
||||
IntDefaultHandler, // The PendSV handler
|
||||
IntDefaultHandler, // The SysTick handler
|
||||
SystickHandler, // The SysTick handler
|
||||
IntDefaultHandler, // GPIO Port A
|
||||
IntDefaultHandler, // GPIO Port B
|
||||
IntDefaultHandler, // GPIO Port C
|
||||
|
|
Loading…
Reference in New Issue