Monday, September 6, 2010

PIC16F887/877 programming in C Tutorial 3-2 (Timer 1)

Timer 1:
The Timer1 module is a 16-bit timer/counter consisting of two 8-bit registers (TMR1H and TMR1L) which are readable and writable. The TMR1 register pair (TMR1H:TMR1L) increments from 0000h to FFFFh and rolls over to 0000h.

Timer1 can be enabled/disabled by setting/clearing control bit, TMR1ON (T1CON<0>). The TMR1CS bit of the T1CON register is used to select the clock source. When TMR1CS = 0, the clock source is FOSC/4. When TMR1CS = 1, the clock source is supplied externally.

The TMR1 interrupt, if enabled, is generated on overflow which is latched in interrupt flag bit, TMR1IF (PIR1<0>). This interrupt can be enabled/disabled by setting/clearing TMR1 interrupt enable bit, TMR1IE (PIE1<0>). 

TMR1IF – TMR1 overflow Interrupt Flag bit.
This flag marks the end of ONE cycle count. The flag need to be reset in the software if you want to do another cycle count. We can read the value of the register TMR1 and write into. We can reset its value at any given moment (write) or we can check if there is a certain numeric value that we need (read).  

Timer1 has four prescaler options allowing 1, 2, 4 or 8 divisions of the clock input. The T1CKPS bits of the T1CON register control the prescale counter. The prescale counter is not directly readable or writable; however, the prescaler counter is cleared upon a write to TMR1H or TMR1L.

T1CON Register;
We perform all the necessary settings with T1CON register. As we can see, the size of the register is 8 bits. Let’s explore the relevant bits:
Where as T1CON of 887 also have bit 6 & 7;
bit 7               T1GINV: Timer1 Gate Invert bit(1)
                      1 = Timer1 gate is active-high (Timer1 counts when gate is high)
                      0 = Timer1 gate is active-low (Timer1 counts when gate is low)

bit 6             TMR1GE: Timer1 Gate Enable bit(2)
                     If TMR1ON = 0:
                     This bit is ignored

                     If TMR1ON = 1:
                     1 = Timer1 counting is controlled by the Timer1 Gate function
                     0 = Timer1 is always counting

Calculating Count, Fout, and Timer1 values

If using INTERNAL crystal as clock, the division is performed as follow:
TIMER1 formula

Fout– The output frequency after the division.
Tout – The Cycle Time after the division.
4 - The division of the original clock by 4, when using internal crystal as clock (and not external oscillator).
Count - A numeric value to be placed to obtain the desired output frequency - Fout.
(65536 - TMR1) - The number of times in the timer will count based on the register TMR1.

Now lets write the code to toggle the bits of portc after 1 sec;
 void main() {
    // using 4MHz ext xtal
     int count=0;

//////////////comment this block if you are using 877////////
     t1con=1;             //tmr1on bit is set
     tmr1h=0;           //begin with 0
        while(!tmr1if_bit);        //tmr1 flag bit
     if(count==16){               //for 1 sec delay
         portc=~portc;             //16*65535us=1sec


Timer 0 schematic can be used (tutorial 3 part 1). 

1 comment:

  1. how to do program in PIC16F887 for converting normal inverter into solar inverter