5.4 Add the Application Logic
main.c file of the project
and perform the following steps: -
Include the following files in the
main.cfile:#include <util/delay.h>
-
Add the following definitions, which specify the value to be loaded into the timer’s CCMP register to generate the compare interrupt.
// Value to load into the TCB0 CCMP register for the 1s timer interrupt; CCMP = (F_CPU / Prescaler) * Desired_Time #define TIMER_PERIOD_1S (19530U) // Value to load into the TCB0 CCMP register for the 200 ms timer interrupt; CCMP = (F_CPU / Prescaler) * Desired_Time #define TIMER_PERIOD_200MS (3906U) // Value to clear the TCB0 counter #define TIMER_COUNTER_CLEAR (0x00)
-
Add the following global variables, to track the state of the LED blink interval and the switch press.
static bool blinkIntervalState = false; static volatile bool isSwitchPressed = false;
-
Add the callback functions for the timer compare and switch press-detection interrupts.
void DetectSwitchPress(void) { // Debounce delay _delay_ms(20); if (!(SW0_GetValue())) { isSwitchPressed = true; } } void ToggleLED(void) { LED0_Toggle(); }
-
Add the following functions, which are used to alternate the LED blinking interval.
The
TCB0_Reset()function clears the timer counter and Interrupt registers so that the timer starts from a known state when alternating the timer compare values.The
VaryLEDBlinkTime()function varies the LED blink interval by writing the corresponding value to the CCMP register of TCB0 for each switch press, depending on the previous LED blink interval state, and enables the timer interrupt.static void TCB0_Reset(void) { TCB0_ClearCaptInterruptFlag(); TCB0_Write(TIMER_COUNTER_CLEAR); TCB0_DisableCaptInterrupt(); } static void VaryLEDBlinkTime(void) { LED0_SetLow(); // Toggles the state of the LED blinking speed blinkIntervalState = !blinkIntervalState; // Disables the TCB0 interrupt to prevent ISR execution during the CCMP update and resets the timer counter TCB0_Reset(); // Updates the timer compare value based on the current blink interval state if (blinkIntervalState == false) { TCB0_CaptureCompareWrite(TIMER_PERIOD_1S); printf("LED blink interval changed to 2s\r\n"); } else { TCB0_CaptureCompareWrite(TIMER_PERIOD_200MS); printf("LED blink interval changed to 400 ms\r\n"); } TCB0_EnableCaptInterrupt(); }
-
Add the main function to initialize the peripherals, set up callback functions, and enable global interrupts. The function continuously monitors the switch input and alternates the blinking interval whenever the switch is pressed.
int main(void) { SYSTEM_Initialize(); // Sets the interrupt handler for the timer interrupt. LED0 toggles when the timer interrupt occurs. TCB0_CaptureCallbackRegister(ToggleLED); // Sets the interrupt handler for the switch press SW0_SetInterruptHandler(DetectSwitchPress); printf("***************PRINTING LED BLINK INTERVAL TIME***************\r\n"); printf("LED blink interval is 2s \r\n"); while (1) { if (isSwitchPressed) { VaryLEDBlinkTime(); isSwitchPressed = false; } } return 0; }
