STM32F Timers – Simple Up/Down Counter Demo – Blinky LED
Keywords: Embedded systems, ARM, STM32F4, Timers, Counter, LED
Code Link: Source Code Github Keil- Bare-Metal
Code Link: Source Code Github Keil- HAL Libraries
In this tutorial we will go one step further into programming STM32 timers to blink an LED each time the timer count a preprogrammed number of counts. This tutorial is based on previous introductory tutorial of STM32 Timers. So if you haven’t gone through previous tutorial, it is highly recommended to go through the tutorial with the link given bellow.
In this tutorial the board used is STM32f4-Discovery(STM32F407VG MCU). The STM32F407VG has total 14 timers (including General Purpose timers, Basic Timers, and Advanced Timers). In this tutorial, we will program Time Base of Timer-2 (General-Purpose Timer) to count certain counts (resulting in total time elapse of 500msec). Upon completing the counting, an interrupt is generated. In Timer-2 ISR, GPIOD-15 (PD#15) is toggled. On STM32F4-Discovery, GPIOD-15 is connected to Onboard Blue LED so ultimately the LED blinks every second (toggle half a second).
Let’s jump into actual programming:
1. Enable Timer-2 Clock:
As can be seen from STM32F407 Datasheet, Timer-2 is connect to Advanced Peripheral Bus-1 (APB-1) i.e. Timer-2 is clocked from APB-1 bus which is subsequently by default clocked from HSI internal 16Mhz Oscillator unless reconfigured. Other options available to feed time base of Timers is explained in introductory Timers tutorial above.

By default, timers are clocked from internal Oscillator (16-Mhz). To enable bus clock to Timer-2 from APB1, bit-0 in RCC_APB1ENR Register needs to be set, Figure-2.

/* Enable clock to Timer-2 on AHB-1 Bus */ __setbit(RCC->APB1ENR, 0U);
2. Clock Prescale:
We will divide clock by a factor of 1000 before it is feed to timer base counter. Thus the actual frequency that the timer base counter will be receiving is 16Mhz/1000 = 16Khz. The input clock is divided by writing appropriate value to TIM2_PSC Register, Figure-3.

/* Divide the timer-2 input frequency (16Mhz) by a factor of 1000 (16,000,000/1,000 = 16,000 = 16Khz) */ TIM2->PSC = 1000;
3. Timer Counts:
Next thing is to configure Timer counts. As we need 500msec delay, we need timer-2 to count X number of cycles that can result into total time of 500msec. Timer-2 is receiving input frequency of 16kHz / 16000Hz after Prescaler division value of 1000. Thus:
1-Cycle ———-> 1/f seconds (Time Period)
How Many Cycles (X) —————-> 0.5sec (500msec)
Rearranging and after cross multiplication:
X = 0.5sec x f ; f = 16000
=> X = 8000
So we want the timer-2 to count 8000 counts. This value needs to be programmed into timer-2 Auto-Reload Register i.e. TIM2_ARR, Figure-4.

/* We want the time count to be 500msec (half a second). As the input frequency is 16khz so the total counts required for 500msec delay: total counts = 500msec * f = (.5 sec) * 16,000 = 8,000 = 0x1F40 */ TIM2->ARR = 8000;
4. Up/Down Counter:
Next thing we need to do is to configure timer-2 to either count UP/DOWN. If bit-4 of TIM2_CR1 register is clear, the timer will count up otherwise it will count down, Figure-5.

/* SET UP_COUNTER TO 0 IF DOWN COUNTER IS REQUIRED */ #define UP_COUNTER 1 #if (UP_COUNTER) /* Up-Counter mode*/ __clearbit(TIM2->CR1, 4U); #else /* Down Counter*/ __clearbit(TIM2->CR1, 4U); #endif
5. Enable Interrupt:
To get notification of timer overflow/underflow, we will enable timer-2 interrupt on timer-2 side via TIM2_DIER (Figure-6) and on NVIC side.

/* Enable timer-2 Update interrupt to generate interrupt on counter overflow/underflow */ __setbit(TIM2->DIER, 0U); /* Timer-2 interrupt is received on IRQ-6 on NVIC so enable IRQ-6 on NVIC for interrupt detection */ NVIC_EnableIRQ(TIM2_IRQn);
6. Interrupt ISR:
Next step is to implement Timer-2 ISR. From STM32F407 vector table/ STM32F4xx Startup file the ISR for Timer-2 is TIM2_IRQHandler
.
The ISR implementation should include GPIOD.15 (BLUE LED) Toggle as per OUR application requirements. The ISR should also clear timer interrupt otherwise it will keep calling the timer-2 ISR thus starving the main application.
/********************************************************* Timer-2 Interrupt Handler **********************************************************/ #ifdef __cplusplus extern "C" { #endif void TIM2_IRQHandler (void) { /* clear timer interrupt */ __clearbit(TIM2->SR, 0U); /* Toggle BLUE-LED PD#15 on timer interrupt*/ __togglebit(GPIOD->ODR, 15); } #ifdef __cplusplus } #endif
7. Kick Timer-2:
The final step is to kick the timer for starting counter. This can be done by setting bit-0 in TIM2_CR1 register, Figure-6.
/* Start Timer-2 */ __setbit(TIM2->CR1, 0U);
8. LED Configuration:
LED Configuration tutorial is covered in the GPIO-Output Tutorial. The link is given bellow.
For complete source code refer to the Github links given at the start of this tutorial.
Video demonstration:
[1] – Reference manual-RM0090
If you notice the pictures in Chapter 9 you can clearly see her room in Ankawa even though her face is blurred. Her name is Fabiana Rassam. Idalia Wilfred Ephraim
Great post. I am experiencing a few of these issues as well.. Rose Arin Argyres
It is really a nice and useful piece of information. I’m glad that
you simply shared this helpful info with us. Please stay
us informed like this. Thank you for sharing.
Keep this going please, great job!
Fantastic beat ! I wish to apprentice while you amend
your web site, how could i subscribe for a blog web site?
The account helped me a acceptable deal. I had been tiny bit acquainted of this
your broadcast offered bright clear concept
Excellent beat ! I would like to apprentice while you
amend your web site, how could i subscribe for a blog
web site? The account aided me a acceptable deal. I had been a little
bit acquainted of this your broadcast provided bright clear idea
Appreciating the hard work you put into your site and detailed
information you offer. It’s awesome to come across a blog every once in a while that isn’t the same
unwanted rehashed information. Excellent read!
I’ve saved your site and I’m including your RSS feeds to my Google account.
This is my first time pay a quick visit at here and i am actually happy to read everthing
at alone place.
I’m really enjoying the theme/design of your web site.
Do you ever run into any internet browser compatibility problems?
A number of my blog audience have complained about my site not working correctly in Explorer
but looks great in Opera. Do you have any tips to help fix this issue?
continuously i used to read smaller articles which as well clear their motive, and that
is also happening with this paragraph which I am reading here.
Pretty! This has been a really wonderful article. Thanks for providing this info.
quest bars http://tinyurl.com/49u8p8w7 quest bars
Hey There. I found your blog using msn. This is a really well written article.
I will be sure to bookmark it and return to read more of your
useful information. Thanks for the post. I’ll definitely return. asmr https://app.gumroad.com/asmr2021/p/best-asmr-online asmr
I was wondering if you ever considered changing the structure of your website?
Its very well written; I love what youve got to say.
But maybe you could a little more in the way of content
so people could connect with it better. Youve got an awful lot of text
for only having one or two pictures. Maybe you could space it out better?
cheap flights http://1704milesapart.tumblr.com/ cheap
flights
It’s not my first time to visit this web page, i am
visiting this website dailly and take fastidious information from
here all the time. scoliosis surgery https://0401mm.tumblr.com/ scoliosis surgery
whoah this blog is magnificent i really like studying your articles.
Stay up the good work! You already know, lots of people are hunting
round for this information, you could help them greatly.
ps4 games https://bit.ly/3z5HwTp ps4
Howdy! This post could not be written any better!
Going through this post reminds me of my previous roommate!
He always kept talking about this. I will forward this post to him.
Pretty sure he will have a good read. Many thanks for sharing!
quest bars https://www.iherb.com/search?kw=quest%20bars quest bars
each time i used to read smaller articles that also clear their motive, and that is
also happening with this article which I am reading
here. scoliosis surgery https://coub.com/stories/962966-scoliosis-surgery scoliosis surgery
Thanks for any other informative blog. Where else could I am getting that kind of information written in such an ideal method? I’ve a mission that I’m simply now running on, and I have been at the glance out for such info.
Simply desire to say your article is as surprising.
The clearness for your post is just cool and i could
suppose you’re an expert on this subject. Fine along with your permission allow me to snatch your RSS feed to keep updated with drawing close post.
Thanks 1,000,000 and please carry on the enjoyable work.
https://parttimejobshiredin30minutes.wildapricot.org/ part time jobs hired in 30 minutes
My coder is trying to convince me to move to .net
from PHP. I have always disliked the idea because of the costs.
But he’s tryiong none the less. I’ve been using Movable-type on several websites for about a year and am worried about switching to another platform.
I have heard great things about blogengine.net.
Is there a way I can import all my wordpress content into it?
Any kind of help would be greatly appreciated!
Im obliged for the blog.Really thank you!
Awesome post.