Working with Timers

Creating delays with Timers

Timers are usually  used for three kinds of tasks; creating delays, counting events and measuring the time between events.

For example:

  • Counting the number of people passing through  airport security
  • Measuring the time a sports car crosses the finish line
  • Keeping the office lights on exactly 8 hours  each day.
Counter vs. Timer

Anything a timing mechanism  depends on some sort of clock . This clock could be internal such as PLL, RC and XTAL .If you are not familiar with these terms please take a look at the lesson on cortex-m clocks.

This clock could also come from an external source such as feeding pulses to the CPU.

If the clock comes from an internal source it is known as a Timer and if it comes from an external source it is known as a Counter.

Cortex-M Timers

Cortex-microcontrollers from most vendors always come with more the one Timer in their Timer module . Some come with as many as 2o Timer blocks in their module . These Timers are mostly 16/32-bits Timers  and 32/64-bits Timers.For instance, the TM4C Tiva C line of Cortex- microcontrollers come with 12 Timer blocks. 6 of these are 16/32-bits Timers and the other 6 are 32/64-bits Timers.

Each Timer block has two Timers. A TimerA and a TimerB. These Timers can be used independently as 2 16-bits Timers or combined as one 32-bits Timer. We shall get into the details of that in another lesson. Also, we shall deal with the 32/64-bits Timers in another lesson. These Timers are usually known as Wide Timers.

Timer Modes

The Timers have 4 different modes of operation. 2 of these modes must be chosen at all times in order to set the timer. These modes are :

  • Periodic vs. One-shot mode

In periodic mode, the Timers continues counting after it reaches timeout. It               merely clears  the timeout flag of the Timer and restarts.

In one-shot mode, Timer stops counting after timeout.

  • Up-counter vs. Down-Counter.


    In the up-counter mode, the counter starts from zero and counts up to the value set in the counter Interval Load Value Register.
    In the down-counter mode, the counter counts down from the value set in the counter Interval Load Value Register to zero.
There is one mored mode selection we need to  make in order to set our Timer. Because this selection is made in another register, i am not counting it as part of the 4 above. We have to select whether we want to run a Timer in the block as an independent 16-bits Timer or whether we want to combine Timers A and B in the block and run them together as a 32-bits Timer. I will provide explanation as to how to perform the latter in subsequent lessons. Now lets take a look at the timer bit size.
Timer Bit Size and Maximum Delay Size

As mentioned, Timers depend on a clock. In the case of this lesson, that is going to be the clock at which the CPU is running. Assuming we running on a TM4C Tiva C Cortex-M4 which runs at 16MHz by default , lets see how that affects our Timer.

16MHz literally means           16 000 000 clock cycles per second.

To find the time taken for 1 clock cycle, which is also known as the clock period, all we have to do is

(1/16 000 000) = 62.5e-9

Note: e means “raised to the power of “

Therefore, it takes 62.5 nanoseconds to run a single clock cycle at 16MHz.

Now, if we have selected the 16-bit Timer mode,  the largest value that can provide  is

2e16  = 65,536

To find the maximum delay we can achieve , all we have to do is multiply the value above by our  clock period.

65536 * 62.5e-9   = 4.096 milliseconds.

If we select the 32-bit mode

2 e32 = 4294967296

4294967296 * 62.5e-9 = 268.435 seconds

Setting the Delay unit : microsecond, millisecond, second


16MHz literally means           16 000 000 clock cycles per second.

So if we want to create a delay interval (timer limit) of 1 second, all we have to do is load 16 000 000 into the Timer Interval Value Load Register

//Psuedo code
TIMER0_Limit  = 16000000 -1

Because the counter counts from zero in an up-counter mode
and to zero in a down-counter mode we, we have to subtract
1 from the 16000000.

Following the same calculation, if we want to create a millisecond delay Timer function, we will have to load the value  16 000 into the Timer Interval Value Load Register.

Because               1 millisecond = 0.001 seconds

So,                0.01 * 16000000 = 16000


For a microsecond delay, i will leave you to perform that calculation.Comment below if you found the answer or face any difficulty in calculating.

Steps to Program Cortex-M Timers for delays
  1. Enable clock to Timer block
  2. Disable Timer while changing configuration
  3. Select Timer mode, 16-bits vs. 32-bits
  4. Select Timer mode  one-shot vs. periodic and down vs. up
  5. Set counter limit
  6. Clear timeout flag
  7. Enable Timer
  8. Wait for timeout flag to be set
Phone: +447586761483
SW11 2DL London UK
Winstanley Rd,
Style switcher RESET
Body styles
Color settings
Link color
Menu color
User color
Background pattern
Background image