Guys, It's URGENT..Need your help on Timer.

Thread Starter


Joined Nov 15, 2013
Hi, Friends In my last post I had talked about timer.
Actually i am working on pic 18F4520 microcontroller on easy pic5 development board & coding in MikroC Pro for pic. I need 1 sec delay and for that I have used TMR0 & INTCON register using internal clock of freq=8MHz.
I don't know how to get TMROH & TMROL value to get 1sec delay.
Can u guide me to find the value.


Joined Feb 11, 2008
In MikroC there are delays included in the compiler libraries.

Just write this code;


to make a 1 second delay.


Joined Jun 26, 2012
The built in delay is fine but you can't get anything else done while delaying.. If you want to use timer 0 (or any timer) here are the steps.

Figure out the PIC's Tcyc time = 4/Fosc. At 8MHz that's 500nsec per Tcyc. That is the input to the timer chain. You will set the timer to enough Tcycs to make 1 sec.

Next figure out how many Tcyc counts are needed for 1sec. 1sec/500ns =2,000,000. So it takes 2E6 Tcycs to count 1 sec.

2E6 won't fit into a 16 bit counter so you'll need the prescaler. You have several choices. Trying prescaler = 32 gives a timer count of 62500 (2E6/32). That DOES fit into 16 bits so.. set the prescaler to 32, TMR0H= F4h, TMR0L=24h and there you go. (62500 = F424h)

500ns * 32 * 62500 = 1sec

This kind of calculation is necessary for ANY timer so it might be useful to make up an Excel sheet that you can enter prescaler values, Fosc etc. and the rest and have it calculate timer settings.

To set it up, stop the timer and clear TMR0IE. If you have not done so, configure the prescaler, 16 bit timer, internal clock source etc. (it only has to be done once.) Load the timer registers. Clear TMR0IF. Enable TMR0IE (if using interrupts). Start the timer (TMR0ON=1). TMR0IF will be set 1 second later.

At this point you have to reload it for 1 second if you want periodic TMR0IFs (or interrupts). If this is the case, consider using TIMER2 / PR2 which does the reload automatically. The settings will be different but are calculated in the same way. Note that it will take some time to do the setups so if you want it very accurate, you'll have to adjust the timer register setting to account for that i.e. if it takes 20Tcyc to set up the timer, set it to 62500-20/PrescalerValue to compensate.

Have fun.
Last edited:


Joined Apr 28, 2012
Delays are wrong for larger programs.

you need to program a scheduler, first you have to figure out what is the smallest timing step you need for the program, then you generate an interrupt for the fastest task.

For the slower tasks, you count the timer cycles.

If you really want a 1 sec delay, you can still program it with this approach but it is bad to have the display go off for a second or keys not responding.

For really short delays it is sometimes easier to wait in a loop, let say, until a few timer cycles have elapsed. But normally, you set a flag, branch back into the scheduler, and then test all the flags.

If the programme time has elapsed, you reset the flag, and do some action.

It is the only way you can manage larger programs.

It is not a multitasking OS but it has some elements of it, not full tasks are maintaine but often this is not neccessary and you can do with a simple scheduler.

The main interrupt frequency often is some KHz.