# How these timer values are derived?

I have a 8051 micro controller ,it has timer0,1,2,3 with different modes.

in timer0,mode0, i got a sample code (as shown below).

datasheet 8051

from that code

TH0_INIT=0xFC          //5.0ms@XTAL=12MHz, Period = (10.85/2) ms@XTAL=22.1184MHz
TL0_INIT=0x0F


both TH0,TL0 load with hex values..but could not derive these values from equation below . (website) Could you please tell me, how these hex values derive?

tick = (1/(Fosc/12)
tick = 12/Fosc$$For Fosc == 11.0592Mhz, the tick time will be tick = 12/11.0592M = 1.085069444us = 1.085us Delay = TimerCount * tick Count = (Delay/tick) RegValue = TimerMax- Count RegValue = TimerMax-(Delay/tick) = TimerMax - (Delay/1.085us) RegValue = TimerMax-((Delay/1.085) * 10^6)$$


source code for timer0,mode0

#include "N76E003.h"

#include "Common.h"
#include "Delay.h"
#include "Function_define.h"
#include "SFR_Macro.h"

#define TH0_INIT                                                               \
0xFC // 5.0ms@XTAL=12MHz, Period = (10.85/2) ms @XTAL = 22.1184MHz
#define TL0_INIT 0x0F

void Timer0_ISR(void) interrupt 1 // interrupt address is 0x000B
{
TH0 = TH0_INIT;
TL0 = TL0_INIT;
P12 = ~P12; // GPIO
toggle when interrupt
}

void main(void) {
TMOD = 0XFF;
Set_All_GPIO_Quasi_Mode;
TIMER0_MODE0_ENABLE;

clr_T0M;
clr_T1M;

TH0 = TH0_INIT;
TL0 = TL0_INIT;

//    set_ET0;                                    //enable Timer0 interrupt
// enable Timer1 interrupt
set_EA; // enable interrupts

set_TR0; // Timer0 run

while (1) {
TH0 = TH0_INIT;
TL0 = TL0_INIT;
set_TR0;
while (!TF0)
;
clr_TR0;
P12 = ~P12;
TF0 = 0;
}
}

• What is the delay you are looking for? What value of register are you getting? Mode 0 is only 13 bit mode which means counts of only upto 0x1FFF possible Commented Jan 25, 2020 at 12:57

For mode 0:

1. 8192 is the number of ticks available before the timer overflows (13 bit mode).
2. Assuming 12MHz clock, the timer will be clicking at 1 MHz.
3. One tick is equal to 1 us
4. Let us compute counters for 5 ms
5. Register value is 8192 - 0.005/1u.
6. So the counters should have value of 3192.
7. Timer higher byte = 0x0C
8. Timer lower byte = 0x78
• But...why ..TH0_INIT=0xFC TL0_INIT=0x0F?
– RAVI
Commented Jan 25, 2020 at 14:07
• @RAVI that nearly comes to about 1 ms.. since mode 0 only supports 13bit, there is no way the counter valid values can be higher than 0x2000. Does the code run? Can you check once and validate Commented Jan 25, 2020 at 15:17