fbpx

Timer using PIC

Description

The timer is used as a time delay generator.

  • PIC basically has three timer modules.
    1.TIMER-0
    2.TIMER-1
    3.TIMER-2.

Timer-0

  • Timer-0 module has an inbuilt 8-bit timer/counter.
  • It can be easily readable/writable.
  • Built in 8-bit software programmable pre-scalar functions.
  • Easily select internal/external clock pulses.
  • Interrupt with overflow from the value FFh to 00h.
  • Edge selection for external clock pulse.
  • Timer mode is selected by clearing bit T0CS.
  • In Timer mode, the Timer0 module will increment every instruction cycle.
  • If the TMR0 register is written, the increment is inhibited for the following two instruction cycles.

Prescaler

  • There is only one prescaler available which is mutually exclusively shared between the Timer0 module and the Watchdog Timer.
  • OPTION_REG REGISTER.

Option_Reg Register

  • Bit 7 RBPU.
  • Bit 6 INTEDG.
  • Bit 5 T0CS: TMR0 Clock Source Select bit
    1 = Transition on T0CKI pin
    0 = Internal instruction cycle clock (CLKO).
  • Bit 4 T0SE: TMR0 Source Edge Select bit
    1 = Increment on high-to-low transition on T0CKI pin
    0 = Increment on low-to-high transition on T0CKI pin
  • Bit 3 PSA: Prescaler Assignment bit
    1 = Prescaler is assigned to the WDT
    0 = Prescaler is assigned to the Timer0 module
  • Bit 2-0 PS2:PS0: Prescaler Rate Select bits.

Timer1

  • Timer 1 module is a 16 bit timer/counter unit.
  • It consists of two 8 bit (8+8)registers (TMR1H, TMR1L) which read and write easily.
  • TMR1 register is a pair of TMR1H and TMR1L and also its value increment its value from 0000h to FFFFh and rolls over to 0000h.
  • Timer 1 module basically operates in two different modes.
    1.Timer  mode
    2.Counter mode.
  • The operating mode of timer 1 module is selected by using the clock select bit (TMR1CS), in timer mode.
  • Timer 1 pin can be enabled/disabled easily by setting/clearing the control bit (TMR1ON).

T1CON: Timer1 Control Register

  • Bit 7-6 Unimplemented: Read as 0.
  • Bit 5-4 T1CKPS1:T1CKPS0: Timer1 Input Clock Prescale Select bits
    11 = 1:8 prescale value
    10 = 1:4 prescale value
    01 = 1:2 prescale value
    00 = 1:1 prescale value.
  • Bit 2 T1SYNC: Timer1 External Clock Input Synchronization Control bit When TMR1CS = 1:
    1 = Do not synchronize external clock input
    0 = Synchronize external clock input
    When TMR1CS = 0:
    This bit is ignored. Timer1 uses the internal clock when TMR1CS = 0.
  • Bit 1 TMR1CS: Timer1 Clock Source Select bit
    1 = External clock from pin RC0/T1OSO/T1CKI (on the rising edge)
    0 = Internal clock (FOSC/4).
  • Bit 0 TMR1ON: Timer1 On bit
    1 = Enables Timer1 0 = Stops Timer1.

Timer 2

  • Timer 2 is an 8-bit timer with a Prescaler and a postscaler.
  • It can be used as the PWM(pulse width modulation) time base for the PWM mode of the CCP modules.
  • The TMR2 register is readable and writable and is cleared on any device Reset.
  • The Timer2 module has an 8-bit period register, PR2.
  • Timer2 increments from 00h until it match PR2 and then resets to 00h on the next increment cycle.

T2CON: Timer2 Control Register

  • Bit 7 Unimplemented: Read as 0
  • Bit 6-3 TOUTPS3:TOUTPS0: Timer2 Output Postscale Select bits
    0000 = 1:1 postscale.
    0001 = 1:2 postscale.
    0010 = 1:3 postscale.
    1111 = 1:16 postscale.
  • Bit 2 TMR2ON: Timer2 On bit
    1 = Timer2 is on 0 = Timer2 is off
  • Bit 1-0 T2CKPS1:T2CKPS0: Timer2 Clock Prescale Select bits
    00 = Prescaler is 1
    01 = Prescaler is 4
    1x = Prescaler is 16.

Code

 void InitTimer0(void)
 {
 // Make Prescalar 1:2
 OPTION_REG &= 0xC0;
 T0IE = 1;
 GIE = 1;
 }
 void main(void)
 {
 TRISB0 = 0;
 RB0 = 0;
 InitTimer0();
 while(1)
 {
 }
 }
 void interrupt ISR(void)
 {
 if(T0IF) //If Timer0 Interrupt
 {
 RB0 = ~RB0;
 T0IF = 0;
 }
 } 

Leave a Reply

Your email address will not be published. Required fields are marked *