Hello everyone am trying to turn on the LED by using the timer and button interrupt function, my aim of the program is, if i press the button for 3secthen LED turn and also the LED will be turned off by pressing the button for 5 sec ,but for this program i am not getting the output please rectify the errors and i would appreciate your suggestions (efm32g210f128 is the controller)
#define LED_PORT TIM2_PORT /**< LED port *///gpio port D
#define LED_BIT TIM2_BIT /**< LED bit *///gpio pin7
#define BUTTON_PORT RX_PORT /**< Button port */
#define BUTTON_BIT RX_BIT /**< Button bit */
#define BUTTON_MASK RX_MASK /**< Button mask */
/*******************************************************************************
****************************** CONSTANTS ************************************
******************************************************************************/
/*******************************************************************************
************************* PRIVATE VARIABLES *******************************
******************************************************************************/
/** Button event flag */
static bool ButtonPressedEvent = false;
/*******************************************************************************
************************* PRIVATE FUNCTIONS *******************************
******************************************************************************/
/***************************************************************************//**
* @brief
* Button interrupt handler.
******************************************************************************/
static void ButtonInterrupt(uint32_t mask)
{
if(ButtonPressedEvent)
{
ButtonPressedEvent=true;
}
else
{
ButtonPressedEvent=false;
}
}
void timer0_Init() {
CMU_ClockEnable(cmuClock_TIMER0, true);
TIMER0->CTRL |= TIMER_CTRL_PRESC_DIV1024;
TIMER0->CTRL |= TIMER_CTRL_CLKSEL_PRESCHFPERCLK;
TIMER0->CTRL |= TIMER_CTRL_MODE_UP;
TIMER0->CTRL |= TIMER_CTRL_FALLA_START;
TIMER0->CTRL |= TIMER_CTRL_RISEA_STOP;
}
void timer0_Start() {
TIMER0->CMD = TIMER_CMD_START;
}
void timer0_Stop() {
TIMER0->CMD = TIMER_CMD_STOP;
}
/*******************************************************************************
************************** PUBLIC FUNCTIONS *******************************
******************************************************************************/
/***************************************************************************//**
* @brief
* User setup function.
******************************************************************************/
void TD_USER_Setup(void)
{
int type;
IRQn_Type irq_parity;
// Define the LED pin as an output in push-pull mode
GPIO_PinModeSet(LED_PORT, LED_BIT, gpioModePushPull, 0);
// Define the button pin as an input with an internal pull-up resistor
GPIO_PinModeSet(BUTTON_PORT, BUTTON_BIT, gpioModeInputPull, 1);
// Set up a user hook on button pin interrupt
type = (BUTTON_MASK & TD_GPIO_ODD_MASK) ?
TD_GPIO_USER_ODD : TD_GPIO_USER_EVEN;
TD_GPIO_SetCallback(type, ButtonInterrupt, BUTTON_MASK);
// Enable falling edge interrupts on button pin
GPIO_IntConfig(RX_PORT, RX_BIT, false, true, true);
// Enable Rising edge interrupts on button pin
GPIO_IntConfig(RX_PORT, RX_BIT, true, false, true);
// Clear and enable the corresponding interrupt in the CPU's Nested Vector
// Interrupt Controller
irq_parity =
(BUTTON_MASK & TD_GPIO_ODD_MASK) ? GPIO_ODD_IRQn : GPIO_EVEN_IRQn;
NVIC_ClearPendingIRQ(irq_parity);
NVIC_EnableIRQ(irq_parity);
}
/***************************************************************************//**
* @brief
* User loop function.
******************************************************************************/
void TD_USER_Loop(void)
{
CMU_ClockSelectSet(cmuClock_HF, cmuSelect_HFRCO); //Use HF clock
timer0_Init();
while(1)
// If RX module pin goes low
if (ButtonPressedEvent==true)
{
// Turn ON the Timer
timer0_Start();
}
else
{
ButtonPressedEvent=false;
// Turn OFF the Timer
timer0_Stop();
}
if (TIMER_COUNT < 3)
{
// Turn on the LED
GPIO_PinOutSet(LED_PORT, LED_BIT);
}
else if (TIMER_COUNT > 5)
{
// Turn OFF the LED
GPIO_PinOutClear(LED_PORT, LED_BIT);
}
}