# 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).

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 .. (https://exploreembedded.com/wiki/5.8051_Timer_programming)

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

Now the Timer value for the required delay can be calculated as below.

Delay = TimerCount * tick

Count = (Delay/tick)

RegValue = TimerMax- Count RegValue = TimerMax-(Delay/tick) = TimerMax - (Delay/1.085us)

RegValue = TimerMax-((Delay/1.085) * 10^6)\$\$

``````#include "N76E003.h"

#include "Common.h"
#include "Delay.h"
#include "SFR_Macro.h"
#include "Function_define.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 ;
}
}
``````

(deleted)

To clarify what spycatcher said, the 8051 microcontrollers are not supported by any official arduino board, nor as far as I am aware any third party board package for arduino. You are unlikely to find someone who knows the 8051 family here. I am sure there are forums dedicated to 8051 programming on the internet; you want to find one and ask there.

The Nuvoton is only 8051 instruction set compatible so you cannot use standard 8051 tools for calculating timer values.

Read the datasheet, timer operation begins on page 99. The image on page 103 explains operation pictorially.

https://www.nuvoton.com/export/resource-files/DS_N76E003_EN_Rev1.08.pdf#page99

Not for nothing but your code doesn’t pass the sniff test. You’re constantly resetting the timer values.