As explained earlier, to get 2 second timer reload interval we need to prescale the timer by 2048 and
load its counter with 1952. This is what should be the setup for TIM2:
void TIM2_setup(void)
{
TIM2_DeInit();
TIM2_TimeBaseInit(TIM2_PRESCALER_2048, 1952);
TIM2_Cmd(ENABLE);
}
Our goal is to keep the LED on for 1 second and off for 1 second. It takes 1952 TIM2 counts for 2 second
interval and so one second passes when this count is 976. Thus, in the main loop we are checking the
value of TIM2’s counter. From 0 to 976 counts, the LED is on and from 977 to 1952 counts, the LED is
off. Note that the LED’s positive end is connected to VDD and so it will turn on only PD0 is low.
if(TIM2_GetCounter() > 976)
{
GPIO_WriteHigh(GPIOD, GPIO_PIN_0);
}
else
{
GPIO_WriteLow(GPIOD, GPIO_PIN_0);
}
Demo
Video link: https://youtu.be/ZstHDHAAHOM