PIC16F877A TIMERS


Programming Timers in PIC16F:


  • The PIC16 has 3 timers depends on the family members.They are arranged as Timers 0,1,2.
  • They can be used either as timer to generate a time delay or counters to count events happening outside of the microcontroller. 


TIMER0 :




Timer0 is an 8-bit Timer/Counter module with the following features:

  1.  8-bit prescaler (shared with WDT). 
  2.  Selectable internal or external clock source.
  3.  Interrupt on overflow (255→0).
  4.  Source edge selection (positive or negative going edge). To configure the Timer0 module the OPTION_REG Special Function Register (SFR) is used. 


Simplified block diagram of timer 0 :


Figure 1 : Simplified block diagram of Timer 0

OPERATION :

           In this section we will step through the various blocks of the Timer0 module and configure each using the OPTION_REG. 

Selecting the Timer0 Source:

T0CS: Timer0 Clock Source Select bit 
  • 1 = Signal present on T0CKI (Timer0 Clock Input) pin used as Timer0 source
  •  0 = Internal instruction cycle clock used as source
  •  Internal instruction cycle = (Microcontroller oscillator Frequency)/4

Figure 2 :Option_register

The Timer0 source is selected using the Timer0 Clock Source Select bit. Setting or clearing this bit will select one of the channels on the multiplexer

Figure 3:TIMER0 CLOCK SOURCE SELECT MULTIPLEXER



SOURCE EDGE SELECTION (EXTERNAL SOURCE ONLY)

 T0SE: Timer0 Source Edge Select bit 
  •  1 = TMR0 register increments on high-to-low transition on T0CKI pin 
  • 0 = TMR0 register increments on low-to-high transition on T0CKI pin 
  • This bit is XOR’d with the logic level on the T0CKI pin.


Figure 4 :OPTION_REG


EFFECT OF TIMER0 SOURCE EDGE SELECT BIT :

Figure 5 : EFFECT OF TIMER0 SOURCE EDGE SELECT BIT





PRESCALER ASSIGNMENT AND CONFIGURATION

PSA: Prescaler Assignment bit

  • 1 = Prescaler is assigned to the Watchdog Timer module
  • 0 = Prescaler is assigned to Timer0 module
  • The prescaler will determine how many source edges will increment the TMR0 register value by 1. 
  • The Timer0 prescaler on the mid-range microcontrollers can be configured to increment the value in TMR0 from a 1:1 ratio to selected source edges or up to a ratio of 1:256 

PRESCALER ASSIGNMENT AND PRESCALER RATE SELECT BITS




PS2:PS0: Prescaler Rate Select bits :


Figure 6 :  PRESCALER RATE SELECTION






TIMER0 register and programming:

// PIC16F877A Configuration Bit Settings

// 'C' source line config statements

#include <xc.h>

// #pragma config statements should precede project file includes.
// Use project enums instead of #define for ON and OFF.

// CONFIG
#pragma config FOSC = HS        // Oscillator Selection bits (HS oscillator)
#pragma config WDTE = OFF       // Watchdog Timer Enable bit (WDT disabled)
#pragma config PWRTE = OFF      // Power-up Timer Enable bit (PWRT disabled)
#pragma config BOREN = OFF      // Brown-out Reset Enable bit (BOR disabled)
#pragma config LVP = OFF        // Low-Voltage (Single-Supply) In-Circuit Serial Programming Enable bit (RB3 is digital I/O, HV on MCLR must be used for programming)
#pragma config CPD = OFF        // Data EEPROM Memory Code Protection bit (Data EEPROM code protection off)
#pragma config WRT = OFF        // Flash Program Memory Write Enable bits (Write protection off; all program memory may be written to by EECON control)
#pragma config CP = OFF         // Flash Program Memory Code Protection bit (Code protection off)


#ifndef _XTAL_FREQ
#define _XTAL_FREQ 20000000
#endif

#define Ouput PORTBbits.RB7

void InitTimer0();


//Main function
void main(void)
{
    TRISBbits.TRISB7=0;  // Make RB7 direction register as output
    InitTimer0();
    while(1)
    {
     
       if (T0IF)
        {
        Ouput^=1; // toggle output
        T0IF = 0;//Clear the T0IF flag so that the next overflow can be detected
        }

    }
 
}



void InitTimer0()
{
    TMR0=0;  //Clear the TMR0 register
    /*
      Configure Timer0 as follows:
        - Use the internal instruction clock as the source to the module
        - Assign the Prescaler to the WatchdogTimer so that TMR0 increments at a 1:1 ratio with the internal instruction clock
     */
    OPTION_REG= 0B00001000;    
}



Figure 7 :Main function




Figure 8: Timer 0 Function

Output:


Figure 9 : Proteus ouput

Comments

Popular posts from this blog

555 Timer Astable mode, Lt spice simulation, and real-time testing

Exploring USB Device Detection with Arduino and USB Host Shield

PIC12F675 LED Blinking Code