Hi there,
Steps to program Timer0 (16-bit mode)
1. Configure the T0CON register indicating which mode (8-bit or 16- bit) to be
used and the selected prescaler option.
2. Load register TMR0H followed by register TMR0L with initial count values.
3. Start the timer with the instruction “T0CONbits.TMR0ON = 1”
4. Keep monitoring the timer flag (T0CONbits.TMR0IF) to see if it is raised. Get
out of the loop when TMR0IF becomes high.
5. Stop the timer with the instruction “T0CONbits.TMR0ON = 0”.
6. Clear the TMR0IF flag for the next round (T0CONbits.TMR0IF = 0).
7. Go back to Step 2 to load TMR0H and TMR0L again.
Values for TMR0H and TMR0L
1. Divide the desired time delay by 0.4 μs.
2. Perform 65,536 — n, where n is the decimal value we got in Step 1.
3. Convert the result of Step 2 to hex, where yyxx is the initial hex value to be loaded into the timer’s registers.
4. Set TMR0H = yy and TMR0L = xx.
Steps to program Timer0 (8-bit mode)
1. Load the T0CON value register indicating 8-bit mode is selected.
2. Load the TMR0L registers with the initial count value.
3. Start the timer.
4. Keep monitoring the TMR0IF to see if it is raised. Get out of the loop when
TMR0IF becomes HIGH.
5. Clear the TMR0IF flag for the next round.
6. Start the timer.
7. Go back to Step 2 to load TMR0L again.
Notice that when we choose the 8-bit option, only the TMR0L register is used and the TMR0H has a zero value during the count up.
HTML:
void T0Delay(void);
void main(void)
{
TRISB=0; //PORTB output port
while (1) //repeat forever
{
PORTB=0x55; //toggle all bits of Port B
T0Delay(); //delay size unknown
PORTB 0xAA; //toggle all bits of Port B
T0Delay();
}
}
void T0Delay()
{
T0CON=0x08; //Timer0, 16-bit mode, no prescaler
TMR0H=0x35; //load TH0
TMR0L=0x00; //load TL0
T0CONbits.TMR0ON = 1; //turn on T0
while (INTCONbits.TMR0IF == 0); //wait for TF0 to roll over
T0CONbits TMR0ON = 0; //turn off T0
INTCONbits.TMR0IF = 0; //clear TF0
}
Best regards,
- - - Updated - - -
Try these also
HTML:
Write a C18 program to toggle only the PORTB.4 bit continuously every 50 ms.
Use Timer0, 16-bit mode, the 1:4 prescaler to create the delay. Assume XTAL = 10MHz.
FFFFh - 85EEH = 7A11H
= 31249 + 1 = 31250
Timer delay =
31250 x 4 x 0.4 ts = 50 ms
void T0Delay(void);
#define mybit PORTBbits.RB4
void main(void)
{
TRISBbits.TRISB4 = 0;
while repeat forever
{
mybit ^ = 1; //toggle using ex-or (^)
T0Delay();
}
}
void T0Delay()
{
T0CON=0x01; //Timer0, 16-bit mode, 1:4 prescaler
TMR0H=0x85; //load TH0
TMR0L=0xEE; //load TL0
T0CONbits.TMR0ON = 1; //turn on T0
while (INTCONbits.TMR0IF == 0); //wait for TF0 to roll over
T0CONbits TMR0ON = 0; //turn off T0
INTCONbits.TMR0IF = 0; //clear TF0
}