Header Ads

Timer0 module use as a counter in PIC Microcontroller

In this tutorial we will learn how Timer0 module use as a counter. Timer module is most powerful feature of microcntroller. Here I am use PIC16F877A Micrcontroller and Lm16x2 Lcd display. The Timer0 module of PIC16F877A microcontroller use as timer or counter mode. You can watch the video or read the written tutorial below.





TIMER0 MODULE

The Timer0 module timer/counter has the following feature:

  • 8 bit timer/counter
  • Readable and writeable
  • 8 bit software programmable prescaler
  • Internal or external clock select
  • Edge select for external clock.
  • Interrupt on overflow from FFh to 00h


TMR0 register is 8 bit register it contain external or internal clock source bit.

TMR0 register

The TMR0 Register can measure ( 0 to 255 ) pulse. 






Circuit Diagram of Pulse Counter Circuit




Mikro C code:

// lcd module connections
sbit LCD_RS at RB0_bit;
sbit LCD_EN at RB1_bit;
sbit LCD_D4 at RB2_bit;
sbit LCD_D5 at RB3_bit;
sbit LCD_D6 at RB4_bit;
sbit LCD_D7 at RB5_bit;

sbit LCD_RS_Direction at TRISB0_bit;
sbit LCD_EN_Direction at TRISB1_bit;
sbit LCD_D4_Direction at TRISB2_bit;
sbit LCD_D5_Direction at TRISB3_bit;
sbit LCD_D6_Direction at TRISB4_bit;
sbit LCD_D7_Direction at TRISB5_bit;
//end lcd module connections
int pulse=0;
char text[5];
void main() {
     TRISD.F0=1;
     PORTD.F0 = 0;
     Lcd_Init();
     Lcd_Cmd(_LCD_CLEAR);
     Lcd_Cmd(_LCD_CURSOR_OFF);
     Lcd_Out(1,2,"TIMER0 Module");
     Lcd_Out(2,2, "As a Counter");
     delay_ms(2000);
     Lcd_Cmd(_LCD_CLEAR);

     OPTION_REG.T0CS = 1;      // Timer0 module as use counter
     OPTION_REG.T0SE = 1;        // Increment high to low
     TMR0 = 0;
     Lcd_Out(1,1,"Pulse");

     while(1){
              pulse = TMR0;
              if(RD0_bit==1){                                        // clear the TMR0 register
                             while(RD0_bit==1);
                             TMR0 = 0;
                             }

              IntToStr(pulse,text);
              Lcd_Out(2,1,text);
              }

}




Click the download button for hex code:
download





No comments

Theme images by 5ugarless. Powered by Blogger.