Timer1 module use as a counter in PIC Microcontroller and Mikro c
In this tutorial we will learn how Timer1 module use as a counter. Timer module is most powerful feature of microcntroller. Here I am use PIC16F877A Micrcontroller and Lm16x2 Lcd display. The Timer1 module of PIC16F877A microcontroller use as timer or counter mode. You can watch the video or read the written tutorial below.
Timer1 Module
The Timer1 module is a 16-bit timer/counter consisting of two 8-bit register (TMR1H and TMR1L) , which are readable and writable. The TMR1 Register pair (TMR1H : TMR1L) increment from 0000h to FFFFh and rolls over to 0000h. The TMR1 Interrupt if enabled is generate on overflow. which is latched in interrupt flag bit TMRIF( PIE<0> ). This interrupt can be enable and disable by setting / clearing TMR1 interrupt enable bit TMR1IE(PIE<0>);
Timer1 can operate in one of two modes:
Timer1 Module
The Timer1 module is a 16-bit timer/counter consisting of two 8-bit register (TMR1H and TMR1L) , which are readable and writable. The TMR1 Register pair (TMR1H : TMR1L) increment from 0000h to FFFFh and rolls over to 0000h. The TMR1 Interrupt if enabled is generate on overflow. which is latched in interrupt flag bit TMRIF( PIE<0> ). This interrupt can be enable and disable by setting / clearing TMR1 interrupt enable bit TMR1IE(PIE<0>);
Timer1 can operate in one of two modes:
- As a timer
- As a counter
In this project I am use Timer1 module as a counter.
Timer1 module |
Circuit diagram:
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 mocule connections
#define SW PORTD.RD0
char text[15];
int pulse =0;
void main() {
TRISC.F1 = 1;
Lcd_Init();
Lcd_Cmd(_LCD_CLEAR);
Lcd_Cmd(_LCD_CURSOR_OFF);
Lcd_Out(1,1,"PUSE COUNTER");
Lcd_Out(2,1,"Use Timer1 module");
delay_ms(2000);
Lcd_Cmd(_LCD_CLEAR);
Lcd_Out(1,3,"DEVELOPED BY");
Lcd_Out(2,1,"MINT TECHNOLOGY");
delay_ms(2000);
Lcd_Cmd(_LCD_CLEAR);
Lcd_Out(1,1,"PULSE COUNTER");
T1CON.TMR1ON = 1;
T1CON.TMR1CS = 1; // External clock input on RC0 pin
TMR1L = 0;
TMR1H = 0;
while(1){
pulse = (TMR1H<<8)|(TMR1L); // Read the timer value
if(SW==1){
while(SW==1);
TMR1H = 0;
TMR1L = 0;
}
IntToStr(pulse,text);
Lcd_Out(2,1,text);
}
}
Click the download button for program file:
download |
No comments