Time plays an important role in our life. It is normal for a human being to reserve time for future plans. We only need our brain and a clock to decide what the current time is and how much time has passed. Similarly, a microcontroller can also calculate time. It serves the purpose of the brain as in the case of human being and the clock is provided by an oscillator. For us wasting one second may not be important but for microcontroller one microsecond is very crucial.
Timers with NRF24LE1
Time plays an important role in our life. It is normal for a human being to reserve time for future plans. We only need our brain and a clock to decide what the current time is and how much time has passed. Similarly, a microcontroller can also calculate time. It serves the purpose of the brain as in the case of human being and the clock is provided by an oscillator. For us wasting one second may not be important but for microcontroller one microsecond is very crucial.
In this article you will find answers to questions like – How can we use NRF to control the elapsed time how can we generate delay for a specific period and much more.
Most microcontrollers have built-in features known as Timers. These timers are used to control time. They can also be used as a counter to count pulses. The NRF24LE1 has three timers Timer0, Timer1 and Timer2. We will only discuss Timer0 in this article.
Fig. 1: Timer prototype based on NRF24LE module
The timer uses the clock frequency to update the time. It uses registers to store the time value. For example, if we use a 16 MHz clock that has a time period of 62.5 nS (nano seconds), then the timer register increments its value by 1 every 62.5 mS. This also means that the smallest measurement we can make is 62.5 mS.
Fig. 2: Image of the timer based on the NRF24LE module
We have already discussed that the timer uses registers to store the time value. In the NRF24LE1 we can choose between 8-bit register, 13-bit register and 16-bit register. Registration selection affects the amount of time a timer can store. For example, an 8-bit register can store values up to 255. If we use the frequency of 16 MHz with a time period of 62.5 mS, the maximum amount of time this register can store is (62.5 * 255 ) = 15.9 uS (microseconds).
Now, what if we change the frequency? Let's try.
Let's say my new frequency is 16 MHz/8 = 2 MHz, which means the time period is now 0.5 uS. Now, if we use an 8-bit register to store the time value, the maximum amount of time this register can store is (0.5 * 255) = 127.5 uS (microseconds). What we observed is that, if we decrease the frequency, the maximum time that the register can store increases. The divider we use to lower the frequency is known as a prescaler. We can change this prescaler to change the frequency. In the NRF module, the prescaler is built-in and set at 12. This means that the frequency now becomes 16/12 = 1.33 MHz.
We have to select Timer0 to function as a timer or counter. For this we have the TMOD (Timer Mode) register. The second bit of this register is used to select between timer and counter. If this bit is 0, the timer is selected, and if it is 1, the counter is selected. We have to write this part according to our need.
For Timer0, we have three modes:
1. Mode0 – this mode is used to select 13-bit register
2. Mode1 – This mode is for 16-bit register.
3. Mode2 – is for 8-bit auto reload.
4. Mode3 – this mode is used to select an 8-bit register as timer/counter and another 8-bit register as timer.
The first bit and the zero bit of the TMOD register are used to select between different modes. The different combinations of these bits refer to different modes. They are:
00 – Mode0
01 – Mode1
10 – Mode2
11 – Mode3
The registers that store the time value for timer0 are TL0 and TH0. These two registers are 8 bits each. They can be combined to be used as 16-bit register or 13-bit register. TH0 contains the highest byte and TL0 contains the lowest byte of data. Furthermore, if these registers become full, it is known as overflow. We can check whether an overflow has occurred by checking the overflow flag in the TCON (Timer Control) register. Bit number five represents the overflow flag. If the flag is 1, an overflow has occurred. We can write a service routine that is called when an overflow occurs.
Overflow flags are automatically cleared when the service routine is called.
To start the timer we have to set bit number four of the TCON. If this bit is 0, the timer will stop. We can also access this bit through the TR (Timer Run) variable. If TR = 1, the timer starts. If TR = 0, the timer stops.
There are more timer features such as overflow, hardware control, gate control and pulse counter. We will discuss these features in our upcoming articles.
We wrote code to understand timers. We are using timer0 in mode1. We created a delay of……
Project source code
###
//Program to
#include "reg24le1.h" // I/O header file for NRF24LE1
###
Circuit diagrams
Circuit Diagram-NRF24LE-Module-Timer |
Project video