Continue to Site

Welcome to

Welcome to our site! is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Timer for mikrco pro compiler using pic 18f4550

Not open for further replies.


Member level 3
Oct 7, 2017
Reaction score
Trophy points
Activity points
This is the code for generating a delay of 1ms by using PIC18F4550 Timer0 Interrupt Service Routine.My question is
why these two lines
TMR0H    = 0xFF;     /* Load count for generating delay of 1ms */
TMR0L    = 0xB2;
have been used once inside the function void Interrupt()
and again inside the function void InitTimer0()?
I know these are value to be loaded to the timer. But What is the reason for writing in two places?
TMR0H    = 0xFF;     /* Load count for generating delay of 1ms */
TMR0L    = 0xB2;
This line would have been written only once in the function.
Here is y complete code
//**Generating a delay of 1ms by using PIC18F2550 Timer0 Interrupt Service Routine**//

#define Pulse LATB

void InitTimer0();

void main()
    OSCCON = 0b00000000; /* Configure oscillator frequency to 20MHz */
    TRISB = 0x00;        /* Set as output Port */
    Pulse = 0xff;        /* Send high on PortB */


/***************Interrupt Service Routine for Timer0******************/
void Interrupt()
 if (TMR0IF_bit)

    TMR0H    = 0xFF;     /* Load count for generating delay of 1ms */
    TMR0L    = 0xB2;
    TMR0IF_bit =0;    /* Make Timer0 Overflow Flag to '0' */

  //Enter your code here
    Pulse =~ Pulse;   /* Toggle PortB at of 500 Hz */


void InitTimer0()
 /* Enable 16-bit TMR0 register,pre-scaler=64,External clock=20MHz,timer OFF */

    T0CON = 0b00000101;   /*Configure TIMER0 control register*/

    TMR0H = 0xFF;         /* Load Count for generating delay of 1ms */
    TMR0L = 0xB2;

    GIE_bit =1;        /* Enable Global Interrupt */
    PEIE_bit =1;       /* Enable Peripheral Interrupt */
    TMR0IE_bit =1;     /* Enable Timer0 Overflow Interrupt */
    TMR0IF_bit = 0;    /* clear Timer0 interrupt flag bit */

    TMR0ON_bit = 1;    /* Turn ON Timer0 */

You need to initialise the timer and start it or you will never get an interrupt.
The interrupt handler resets the timer registers so that it will generate the next one.

So, once we are inside the isr this means timer 0 have overflowed and become zero so we gotta load it again with zero.


    Points: 2
    Helpful Answer Positive Rating

Some thoughts...

with "0xFFB2" .. this means you just use 78 ticks. So an 8 bit timer may be sufficient.

or use a lower value prescaler to get more accurate timing.

Both will have no influence on processing power.


One of the little tricks you may need when dealing with these timers that don't have limit registers is that the timer will keep on 'ticking' while the ISR is being called, especially if you have low pre-scalar values. If you need really precise timing, then you will find that you need to take these extra 'ticks' into account. (By the way they occur because the hardware needs to respond to the interrupt, the ISR preamble code needs to save registers etc before it starts to execute your code, your test to see which is the trigger interrupt, and then there are the times needed for the instructions that reload the TMRx registers themselves).
What you can do is to use code such as:
TMR0H = <your value>;
TMR0L = <your value> - TMR0L;
This also allows you to not be quite so strict about resetting the timer as the first instructions.
Just a suggestion...

Not open for further replies.

Part and Inventory Search

Welcome to