Friday, 17 February 2017

Timer1, Timer2 & Timer3 Pic18f 2420/2520/4420/4520

Timer1, Timer2 & Timer3 Pic18f 2420/2520/4420/4520




Pic 18f2520 microcontroller has 4 different Timer modules inside. Those are Timer0, Timer1, Timer2 & Timer3. You have studied Timer0 module working procedure in my previous post. Now we will study the existing PIC timer modules Timer1, Timer2 & Timer3.

We can use these timers for various purposes. Many of times we need a specific time in various types of processes.  We can use these timers as a counter also to check that what is happening inside the processor and/or outside field.

Timer-1
Timer-2
Timer-3
Bit Mode
16 bit
8 bit
16 bit
Clock Source
internal/external
internal
internal/external
Interrupt
Overflow interrupt
Interrupt on TMR2 to PR2 match
Overflow interrupt
Readable/writable
Yes
Yes
Yes
Prescaling
Yes
Yes
Yes


We would know every timer separately, because all of the timers have different registers and can be use for different peruses.





Timer-1
Rounded Rectangle: Timer-1


The Timer1 timer/counter module incorporates these features:

  • ·        Software selectable operation as a 16-bit timer or counter
  • ·        Readable and writable 8-bit registers (TMR1H and TMR1L)
  • ·        Selectable clock source (internal or external) with device clock or Timer1 oscillator internal options
  • ·        Interrupt-on-overflow
  • ·        Reset on CCP Special Event Trigger
  • ·        Device clock status flag (T1RUN)


Timer1 can also be used to provide Real-Time Clock (RTC) functionality to applications with only a minimal addition of external components and code overhead. Timer1 has a control register T1CON and two buffer registers TMR1H (high byte) & TMR1L (low byte).



Timer1 external oscillator

We have seen that timer1 has two types of clock sources internal and external. Internal clock source is system clock and external clock driven by external oscillator connect to PORTC0/T1OSO and PORTC1/T1OSI pins.



Timer1 LP oscillator circuit




T1CON: TIMER1 CONTROL REGISTER

Bit7   :- 16-Bit Read/Write Mode Enable bit

·        Timer buffer registers can be read as 8 bit or 16 bit modes. The high byte of Timer1 is not directly readable or writable in this mode. All reads and writes must take place through the Timer1 High Byte Buffer register. Writes to TMR1H do not clear the Timer1 prescaler.

T1CONbits.RD16 = 1; // 16 bit mode.
T1CONbits.RD16 = 0; // 8 bit mode.


Bit6   :- Timer1 clock source selection

·        Selection of timer1 clock source internal or external.

T1CONbits.T1RUN = 1;       //Clock source from Timer1 oscillator.
T1CONbits.T1RUN = 0;       //Clock source from system oscillator.


Bit5-4   :- Prescaler ratio selection
  • ·        There are only 4 modes to select your needed prescaler. We are selecting 1:8 prescale.


           T1CONbits.T1CKPS1 = 1;   // 1:8 prescale
           T1CONbits.T1CKPS0 = 1;  


Bit3   :- Timer1 Oscillator Enable bit


  • ·        Bit for timer1 external oscillator enabling.


         T1CONbits.T1OSCEN = 1;  // Timer1 external oscillator enable.
         T1CONbits.T1OSCEN = 0;  // Timer1 external oscillator disable.


Bit2   :- Timer1 external oscillator clock Synchronization
  • ·        This bit will use when we selected external clock otherwise not need to set this bit.


         T1CONbits.T1SYNC = 1;     // Do not synchronize external clock input
         T1CONbits.T1SYNC = 0;     // Synchronize external clock input


Bit1   :- Clock source selection
  • ·        You can select your desire input type by this bit internal or external.


        T1CONbits.TMR1CS = 1;    // External clock source
        T1CONbits.TMR1CS = 0;    // Inte
rnal clock source


Bit0   :- Timer1 on off bit

        T1CONbits.TMR1ON = 1;    // Timer1 on.
        T1CONbits.TMR1ON = 0;    // Timer1 off.





Timer-1 Block diagram




Codes for generate 10ms delay

void delay_10ms(void);       // 10ms delay time function.
void config_timer1(void);     // Configuration timer1

void delay_10ms()
{
            TMR1H = 0x3C;                              // Timer higher register value.
            TMR1L = 0XB0;                             // Timer higher register value.
            T1CONbits.TMR1ON = 1;             // Turn on timer.
            while(PIR1bits.TMR1IF  ==  0);    // Wait until interrupt generate.
            T1CONbits.TMR1ON = 0;             // Turn off timer.  
            PIR1bits.TMR1IF = 0;                     // Clear interrupt flag.
}


void config_timer1(void)      // Timer1 configuration
{
          //Configure timer1 bits.
          T1CONbits.T1CKPS1 = 0;      // 1:1 prescale
          T1CONbits.T1CKPS0 = 0;     
          T1CONbits.T1OSCEN = 0;     // Timer1 external oscillator disable.
          T1CONbits.TMR1CS  = 0;      // Timer1 Internal clock (FOSC/4)
          T1CONbits.TMR1ON  = 0;     // disable timer
}





Timer-2
Rounded Rectangle: Timer-1 



The Timer2 module timer incorporates the following features:

  • ·        8-Bit Timer and Period registers (TMR2 and PR2, respectively)
  • ·        Readable and writable (both registers)
  • ·        Software programmable prescaler (1:1, 1:4 and 1:16)
  • ·        Software programmable postscaler (1:1 through 1:16)
  • ·        Interrupt on TMR2 to PR2 match
  • ·        Optional use as the shift clock for the MSSP module



Timer2 is a 8bit timer/counter. Basically timer is incorporated with CCP module to generate the pulse on CCP1 and CCP2 pins. In normal operation, TMR2 is incremented from 00h on each clock (FOSC/4). The value of TMR2 is compared to that of the Period register, PR2, on each clock cycle. When the two values match, the comparator generates a match signal as the timer output. This signal also resets the value of TMR2 to 00h on the next cycle and drives the output counter/postscaler.



T2CON: TIMER2 CONTROL REGISTER

   Bit7   :- Not in use

Bit6-2   :- Timer2 Postscaler ratio selection

          T2CONbits.T2OUTPS3 = 0;          // 1:1 Postscaler select
          T2CONbits.T2OUTPS2 = 0;
          T2CONbits.T2OUTPS1 = 0;
          T2CONbits.T2OUTPS0 = 0;

If you are not using postscaler ratio, you would not need to define these bits.

Bit2   :- Timer2 On/Off

          T2CONbits.TMR2ON = 1;    // Turn on timer2
          T2CONbits.TMR2ON = 0;    // Turn off timer2

Bit1-0   :- Timer2 Prescler selection

          T2CONbits.T2CKPS1 =0;    // Select Prescaler 1:4
          T2CONbits.T2CKPS0 =1;   




Timer-2 Block diagram




Codes for generate 100us delay time

void delay_100us(void);     // 100us delay time function.
void config_timer2(void);   // Configuration timer2

void delay_100us()
{
            T2CONbits.TMR2ON = 1;           // Turn on timer.
            while(PIR1bits.TMR2IF == 0);     // Wait until interrupt generate.
            T2CONbits.TMR2ON = 0;           // Turn off timer.  
            PIR1bits.TMR2IF = 0;                  // Clear interrupt flag.
}


void config_timer2(void)       // Timer2 configuration
{
          // Timer2 Registers:
// Prescaler=1:1; TMR2 PostScaler=1:2; PR2=250 - Freq = 10,000.00Hz - Period = 100.00 µs
          T2CONbits.T2OUTPS3 = 0;
          T2CONbits.T2OUTPS2 = 0;
          T2CONbits.T2OUTPS1 = 0;
          T2CONbits.T2OUTPS0 = 1;
          T2CONbits.TMR2ON  = 0;   // Timer2 off
          T2CONbits.T2CKPS1 = 0;   // Prescaler Rate Select bits
          T2CONbits.T2CKPS0 = 0;
          PR2 = 250;                            // PR2 (Timer2 Match value)
}






Timer-3
Rounded Rectangle: Timer-1




The Timer3 module timer/counter incorporates these features:

·        Software selectable operation as a 16-bit timer or counter
·        Readable and writable 8-bit registers (TMR3H and TMR3L)
·        Selectable clock source (internal or external) with device clock or Timer1 oscillator internal options
·        Interrupt-on-overflow
·        Module Reset on CCP Special Event Trigger


T3CON: TIMER3 CONTROL REGISTER


Bit7   :- 16-Bit Read/Write Mode Enable bit

  • ·        Timer buffer registers can be read as 8 bit or 16 bit modes. The high byte of Timer3 is not directly readable or writable in this mode. All reads and writes must take place through the Timer3 High Byte Buffer register. Writes to TMR3H do not clear the Timer3 prescaler.


         T3CONbits.RD16 = 1; // 16 bit mode.
         T3CONbits.RD16 = 0; // 8 bit mode.


Bit6 & 3   :- Timer1 & Timer3 use for CCP module

  • ·        Timer1 and/or Timer3 can be integrated with CCP module.


         T3CONbits.T3CCP2 = 1;     // Set Timer3 for CCP modules
         T3CONbits.T3CCP1 = 1;     // Set Timer3 clock source for CCP2
                                                // Set Timer1 clock source for CCP1

                      
         T3CONbits.T3CCP2 = 0;     // Set Timer1 for CCP modules
         T3CONbits.T3CCP1 = 0;    


Bit5-4   :- Timer3 prescaler ratio selection

         T3CONbits.T3CKPS1 = 1;   // Set prescaler 1:4
         T3CONbits.T3CKPS0 = 0;  

Bit2   :- Timer3 external clock synchronies

  • ·        We can use Timer1 internal oscillator as timer3 clock source. this also configures Timer3 to increment on every rising edge of the oscillator source.


        T3CONbits.T3SYNC = 0;     // Timer3 external clock synchronization.
        T3CONbits.T3SYNC = 1;     // Do not synchronization.


Bit1   :- Timer3 clock selection bit

        T3CONbits.TMR3CS = 1;    // Clock source from Timer1 oscillator.
        T3CONbits.TMR3CS = 0;    // Internal clock source.


Bit0   :- Timer3 on/off bit

        T3CONbits.TMR3ON = 1;    // Enable timer.
        T3CONbits.TMR3ON = 0;    // Stop timer.




Timer-3 Block diagram




Codes for generate 100ms delay

void delay_100ms(void);   // 100ms delay time function.
void config_timer3(void);   // Configuration timer2


void delay_100ms()
{
          TMR3H = 0x0B;
          TMR3L = 0xDC;
          T3CONbits.TMR3ON = 1;      // Turn on timer.
          while(PIR2bits.TMR3IF==0); // Wait until interrupt generate.
          T3CONbits.TMR3ON = 0;      // Turn off timer.
          PIR2bits.TMR3IF = 0;             // Clear interrupt flag.
}


void config_timer3(void)       // Timer2 configuration
{
          // Timer3 Registers:
// Prescaler=1:8; TMR3 Preset=3036; Freq=10.00Hz; Period=100ms

          T3CONbits.T3CKPS1 = 1;    // Prescaler Rate Select bits
          T3CONbits.T3CKPS0 = 1;
          T3CONbits.TMR3CS  = 0;   // Internal clock (FOSC/4)
          T3CONbits.TMR3ON  = 0;   // Disable timer  

}






No comments:

Post a Comment