cancel
Showing results for 
Search instead for 
Did you mean: 

Hi, I need to blink on and off 3 led at different interval such as 4s,0.3s,0.7s,4.7 second using timer. Can you please help me how to implement this .

Chandana
Associate II
 
6 REPLIES 6

Homework?

Run the timer so that it throws an interrupt each 0.1s, in that interrupt count individually for the three LEDs and toggle them accordingly.

JW

Chandana
Associate II

Hi,

Thanks for the reply.

I am new to this controller .Could you please explain it briefly. Or can you please give me the pseudocode as i am not getting how to count individually for 3 LEDS.

This has nothing to do with STM32, you would do it in the same way in any mcu.

Define a global variable

int led1counter;

then in the 0.1s interrupt or any other function periodically called at 0.1s period

if (led1counter == 40) { // 4.0s

Led1On();

} else if (led1counter == 40 + 3) { // after further 0.3s

Led1Off();

led1counter = 0; // restart

} else {

led1counter++;

}

and do the same for the remaining LEDs, with a different variable.

JW

Chandana
Associate II

Hi,

Thanks for the reply.

I have tried the method but still the led is not on/off on the respective counter value i have generated an interrupt of 0.5 sec .

Here i am sharing my code please correct me if i am doing wrong.

> void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)

> [...]

>     timerValue = __HAL_TIM_GET_COUNTER(&htim3);

> [...]

This is nonsense, in the Update interrupt (which is what leads to HAL_TIM_PeriodElapsedCallback()), TIMx_CNT is always close to 0.

Do what I told you to do above.

JW

Paul1
Lead

Here is an implementation like what JW suggests.

1. Grab a periodic interrupt, either set up a 10Hz/100msec timer in MX, or piggyback on an existing.

I'm piggybacking on the 1KHz as I have things I need checked at 1msec.

//stm32f4xx_it.c (or equivalent depending on which MCU is used): we need to call our system timer callback in the SysTick_Handler() function (around line 186).
//The function should now look like this:
//This function handles System tick timer.
void SysTick_Handler(void)
{
  /* USER CODE BEGIN SysTick_IRQn 0 */
 
  /* USER CODE END SysTick_IRQn 0 */
  HAL_IncTick();
  /* USER CODE BEGIN SysTick_IRQn 1 */
 
  void vTmrCallbackMy1KHz(void); vTmrCallbackMy1KHz();
  /* USER CODE END SysTick_IRQn 1 */
}

2. Write your own vTmrCallbackMy1KHz() (or whatever you called it)

This is very simple implementation.

In a real project I might use callbacks for critical timing events (not blinking a LED so slowly).

I'd also use functions to check the bools, good coding style is to NOT make them global

You could also blink the LEDs directly in the function below using the LED's matching Toggle call, but I prefer a bool incase LED is also following another signal for whether On/Off/blinking, or an enum for On/BlinkFast/BlinkSlow/Off.

bool  b4000msec; 
bool  b300msec; 
bool  b700msec; 
bool  b4700msec; 
void vTmrCallbackMy1KHz(void)
{
    static uint32_t uCount4000msec; 
    static uint32_t uCount300msec; 
    static uint32_t uCount700msec; 
    static uint32_t uCount4700msec; 
    if (++uCount4000msec >=4000){uCount4000msec =0; b4000msec=true;}
    if (++uCount300msec>=300){uCount300msec=0; b300msec=true;}
    if (++uCount700msec>=700){uCount700msec=0; b700msec=true;}
    if (++uCount4700msec>=4700){uCount4700msec=0; b4700msec=true;}
}

Paul