As we know frequency = Number of full circle per second. So for making frequency meter we have to count number of positive pulses for external signal which we have to measure. As I mention earlier we have to configure T1CON (Timer1 Control Register) properly as per our requirement. For reading external clock (or pulse) we normally set the prescaler 1:1 ratio. It means we do not delay the sampling of the external pulse, but treat the external clock as it is to count number of pulses for specified time duration.
So if we run Timer 1 as a counter mode, there are two pins we can use to apply the external clock pulse RC0/T1OSO and RC1/T1OSI. Selection of one of them is controlled by the T1OSCEN bit.
Setting the bit selects RC1/T1OSO and clearing it does for RC0/T1OSI. In our project as we use counter mode is synchronous, we clear the T1SYNC bit. For TMR1CS bit, we set it for external clock counting. Finally, we set the TMR1ON bit to start the Timer1 module. Counting of the rising edge of the external clock pulse would increase the TMR1 registers by one for every external clock. And TMR1 register made with two 8 bit register TMR1L and TMR1H.When the content of TMR1 crosses from FFFFh to 0000h, the Timer1 interrupt bit TMR1IF would be set, if interrupt is enabled. Usually, when we count number of pulses within a period, we disable the interrupt, and after the lapse of the time, we stop the timer and read the content of TMR1 register.
To make our project “Digital frequency meter by PIC microcontroller using timer 1” Then what will be our bit pattern of TICON register? For counting external clock pulses entered to the pin 15 RC0/T1OSO, bit pattern of T1CON register would be 00000010. When we start the counting, we set the TMR1ON, bit0 of the T1CON and that time bit pattern of T1CON register will be 00000011.
In bellow you see the circuit diagram for simulation in Proteus for our project “Digital frequency meter by PIC microcontroller using timer 1 “.
this is a video to explain you more about this project:
In bellow you will find the complete c code written in mikro c pro for pic.
// LCD module connections
sbit LCD_RS at RB4_bit;
sbit LCD_EN at RB5_bit;
sbit LCD_D7 at RB0_bit;
sbit LCD_D6 at RB1_bit;
sbit LCD_D5 at RB2_bit;
sbit LCD_D4 at RB3_bit;sbit LCD_RS_Direction at TRISB4_bit;
sbit LCD_EN_Direction at TRISB5_bit;
sbit LCD_D7_Direction at TRISB0_bit;
sbit LCD_D6_Direction at TRISB1_bit;
sbit LCD_D5_Direction at TRISB2_bit;
sbit LCD_D4_Direction at TRISB3_bit;
// End LCD module connections
void main() {
int c=0,i=0,z=0;
char txt[5],txt1[5],*res;;
lcd_init();
lcd_cmd(_LCD_CLEAR);
lcd_cmd(_LCD_CURSOR_OFF);
TRISC=1;
TRISB=0;
TRISD=0;
PORTD= 0;
TMR1L=0;
TMR1H=0;
lcd_out(1,1," WORKING..");
delay_ms(100);
lcd_out(1,1," WORKING….");
delay_ms(100);
lcd_out(1,1," WORKING……");
T1CON= 0B00000011;delay_ms(1000);
T1CON= 0B00000010;
c= TMR1L;
i= TMR1H * 256;
z= c+i;
IntToStr(z,txt);
lcd_cmd(_LCD_CLEAR);
delay_ms(100);
lcd_out(1,1," FREQUENCY IS");
delay_ms(100);lcd_out(2,0,txt);
lcd_out(2,7," Hz");
}
1 commentaires:
Why do you multiply by 256 TMR1H?
Enregistrer un commentaire