Hello, I'm trying to figure out how to work correctly with timer 0 in the nRF24LE1 microcontroller.
1)The Cclk frequency in my case is 16 MHz
2)Given a fixed limit of 12, we get a 0.75 microseconds delay per 1 tick of the timer.
----------------------------------------------------------------------------------------------------
In the 16-bit timer mode, we have 65536 ticks maximum, which is 65536 * 0.75 = 49152 microseconds = 0.049152 seconds. To set up a fixed delay for me, I use the following calculation:
N = 65536 - (65536 * (X/0.049152))
N - TH0, TL0
X - expected delay
Example: I need a 0.0001 sec delay.
N = 65536 - (65536 * (0.0001 / 0.049152)) = 65402,66666(7) and this means that the exact number will not work. But I found a solution to this problem:
Let's take a look at the numbers close to this: (65536 - 65402) * 0.75 = 100.5 microseconds (65536 - 65403) * 0.75 = 99.75 microseconds
100.5 - 99.75 = 0.75 microseconds.
It follows from these considerations that in order to calculate the exact delay, we need to change each tick of the timer either by 1 more or by 1 less starting from 65403.
For example, we will switch the LED on the foot P0.1 with a delay of 1 second.
#include <stdio.h>
#include <reg24le1.h>
//10000 * 0.0001 = 1 sec
volatile uint32_t counter = 10000;
volatile uint8_t help_timer = 0;
void timer0_int() interrupt INTERRUPT_T0
{
counter--;
if(help_timer==0)
{
help_timer = 1;
//100.5 us
TL0 = 0x7A;
TH0 = 0xFF;
}
if(help_timer==1)
{
help_timer = 0;
//99.75 us
TL0 = 0x7B;
TH0 = 0xFF;
}
if(counter==0)
{
counter = 10000;
help_timer = 0;
P01=~P01;
// begin new time from 99.75 us
TL0 = 0x7B;
TH0 = 0xFF;
}
}
void main()
{
P0DIR = 0xFD;
// begin from 99.75 us
TL0 = 0x7B;
TH0 = 0xFF;
//timer 0 - overflow interrupt and all int enable
IEN0 = 0x82;
//timer 0 - 16 bit timer
TMOD = 0x1;
//run timer 0
TR0 = 1;
}
----------------------------------------------------------------------------------------------------
In the 8-bit timer mode, we have 256 ticks maximum, which is 256 * 0.75 = 192 microseconds = 0.000192 seconds. To set up a fixed delay for me, I use the following calculation:
N = 256 - (256 * (X/0.000192))
N - TH0, TL0
X - expected delay
Example: I need a 0.0001 sec delay.
N = 256 - (256 * (0.0001 / 0.000192)) = 122,66666(7) and this means that the exact number will not work. But I found a solution to this problem:
Let's take a look at the numbers close to this: (256 - 122) * 0.75 = 100.5 microseconds (256 - 123) * 0.75 = 99.75 microseconds
100.5 - 99.75 = 0.75 microseconds.
It follows from these considerations that in order to calculate the exact delay, we need to change each tick of the timer either by 1 more or by 1 less starting from 123.
For example, we will switch the LED on the foot P0.1 with a delay of 1 second.
#include <stdio.h>
#include <reg24le1.h>
//10000 * 0.0001 = 1 sec
volatile uint32_t counter = 10000;
volatile uint8_t help_timer = 0;
void timer0_int() interrupt INTERRUPT_T0
{
counter--;
if(help_timer==0)
{
help_timer = 1;
//100.5 us
TH0 = 0x7A;
}
if(help_timer==1)
{
help_timer = 0;
//99.75 us
TH0 = 0x7B;
}
if(counter==0)
{
counter = 10000;
help_timer = 0;
P01=~P01;
// begin new time from 99.75 us
TH0 = 0x7B;
}
}
void main()
{
P0DIR = 0xFD;
// begin from 99.75 us
TL0 = 0x7B;
TH0 = 0x7B;
//timer 0 - overflow interrupt and all int enable
IEN0 = 0x82;
//timer 0 - 8 bit timer auto-reload
TMOD = 0x2;
//run timer 0
TR0 = 1;
}
----------------------------------------------------------------------------------------------------
Is the order of register assignment correct? The documentation for nrf24le1 says that the TF0 flag is cleared automatically.