So far we are able to provide delay by using for loop, Now in this tutorial we are going to learn how to program PIC Timer in C language. PIC Microcontroller(18f4550) has 4 Timers – Timer 0,Timer 1,Timer 2,Timer 3
Many of PIC 18 timers are 16 bit wide. PIC 18 has 8 bit Architecture so 16 bit timer is accessed as two separate registers of low byte(TMRxL) and high byte(TMRxH) where x=0,1,2,3. First of all we are going to talk about Timer0. Timer0 can be used as 8-bit or 16-bit timer.
- T0CON register is used to control Timer0. You don’t need to learn any of registers. These are in given in the Datasheet of PIC Microcontroller.
- PIC Microcontroller first divide the crystal frequency by 4 before further use for timers. Let’s say I am using 10MHZ crystal then PIC Microcontroller divide this frequency by 4 i.e. 10/4 = 2.5MHZ. So my one machine cycle is 1/2.5MHZ = .4 Micro sec
- Steps to Program Timer0
- Load T0CON register with selected prescaler As I told you PIC Microcontroller divides crystal frequency by 4 then prescale further divide this frequency more. let’s say I select prescale as 001 so I divide crystal frequency by 4 more 2.5/4 = 0.625
- Set values for TMR0L and TMR0H according to your delay
- Timer on
- Keep monitoring TMR0IF bit. TMR0IF bit becomes one when Timer0 reaches value FFFFH
- When TMR0IF bit becomes 1 Stop Timer
- Clear TMR0IF
- In the following Program Led is blinking and you have two different delays which can be choose with a switch
Incoming search terms:
- c program for using timer0 of pic18f452 (1)
- delay using timer1 in pic (1)
- pic c tutorial timer0 (1)
- pic led flash timer (1)
- pic16f delay using timer (1)
- pic18 timer c (1)
- tmr0if using in mplab ide using htc c (1)
- using PIC to receive frequencies - using C (1)
If I am not mistaken
Instead of “TMR0IF bit becomes one when Timer0 reaches value FFFFH”
Would be better to write “TMR0IF bit becomes 1 when Timer0 reaches value FFFFH.”