Using Timers of Microchip PIC18F Microcontrollers: Corrado Santoro
Using Timers of Microchip PIC18F Microcontrollers: Corrado Santoro
Microcontrollers
Corrado Santoro
L.A.P. 1 Course
The T0CON (Timer 0 Control) SFR includes all the bits which
control TIMER0 functioning.
Corrado Santoro Using Timers in PIC18F MCUs
TIMER0: Selecting clock source
int main(void)
{
TRISBbits.TRISB0 = 0; // output
for (;;) {
unsigned int t;
t = TMR0;
if (t >= 31250) { // equivalent of 500 ms
TMR0 = 0;
LATBbits.LATB0 = !LATBbits.LATB0;
}
}
}
TRISBbits.TRISB0 = 0; // output
TRISBbits.TRISB1 = 0; // output
for (;;) {
unsigned int t;
t = TMR0;
if (t >= 15625) { // equivalent of 250 ms
TMR0 = 0;
++c0; ++c1;
if (c0 == 2) { // flash led 0
LATBbits.LATB0 = !LATBbits.LATB0;
c0 = 0;
}
if (c1 == 3) { // flash led 1
LATBbits.LATB1 = !LATBbits.LATB1;
c1 = 0;
}
}
}
}
Corrado Santoro Using Timers in PIC18F MCUs
Timer Overflow
TRISBbits.TRISB0 = 0; // output
TRISBbits.TRISB1 = 0; // output
for (;;) {
if (INTCONbits.T0IF == 1) { // overflow!
TMR0 = 49911; // reload timer
INTCONbits.T0IF = 0; // clear overflow
++c0; ++c1;
if (c0 == 2) { // flash led 0
LATBbits.LATB0 = !LATBbits.LATB0;
c0 = 0;
}
if (c1 == 3) { // flash led 1
LATBbits.LATB1 = !LATBbits.LATB1;
c1 = 0;
}
}
}
}
Corrado Santoro Using Timers in PIC18F MCUs
Timer Overflow
TRISBbits.TRISB0 = 0; // output
TRISBbits.TRISB1 = 0; // output
for (;;) {
if (INTCONbits.T0IF == 1) { // overflow!
TMR0 = -15625; // reload timer
INTCONbits.T0IF = 0; // clear overflow
++c0; ++c1;
if (c0 == 2) { // flash led 0
LATBbits.LATB0 = !LATBbits.LATB0;
c0 = 0;
}
if (c1 == 3) { // flash led 1
LATBbits.LATB1 = !LATBbits.LATB1;
c1 = 0;
}
}
}
}
Corrado Santoro Using Timers in PIC18F MCUs
Comparing the techniques
Corrado Santoro
L.A.P. 1 Course