Saturday 22 September 2012

Pics have Timers

PICs come with hardware counters know as timers. I have just been playing with its timer features using the PicKit3 demoboard. The diagram below is taken from the PIC tutorial I am following.


There are some interesting things to note:

  • The timer has a prescaler
  • TMR0IF is the output and it needs to be cleared when set.
  • There is a complex arrangement for reading TMR0H
The timer has a prescaler
The prescaler allows us to divide the timers clock. In the example below their are two line of code (one commented out) That set the value of the prescaler.

 // Init Timer
    INTCONbits.TMR0IF = 0;          // clear roll-over interrupt flag
    //T0CON = 0b00001000;             // no prescale - increments every instruction clock
    T0CON = 0b00000001;             // prescale 1:4 - four times the delay.
    TMR0H = 0;                      // clear timer - always write upper byte first
    TMR0L = 0;
    T0CONbits.TMR0ON = 1;           // start timer

 TMR0IF is the output and it needs to be cleared when set.
We need to clear the output of the timer before we start it. The counter can be started using T0CONbits.

There is a complex arrangement for reading TMR0H
The high byte of the timer is no directly accessible. It can only be accessed in conjunction with the lower bytes. To read the timer always read TMR0L first then TMR0H. To write to the timer always write to TMR0H first then followed by TMR0L.

Reference
MicroChip PICkit3 Debug Express, PIC18F45k20 - MPLAB C Lessons. 

No comments:

Post a Comment