Tutorial 1-PIC Timer Programming In C

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.
  1. 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.t0con
  2. 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
  3. Steps to Program Timer0
    1. 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
    2. Set values for TMR0L and TMR0H according to your delay
    3. Timer on
    4. Keep monitoring TMR0IF bit. TMR0IF bit becomes one when Timer0 reaches value FFFFH
    5. When TMR0IF bit becomes 1 Stop Timer
    6. Clear TMR0IF
  4. In the following Program Led is blinking and you have two different delays which can be choose with a switch 

Twitter Delicious Facebook Digg Stumbleupon Favorites More

Design by Engr.Arif Naseem | Bloggerized by Naseem - | Affiliate Network Reviews