Categories
Examples

PIC Timer1 External Clock Frequency Counter

We are using the PIC16f887 microcontroller’s internal Timer1 module in External Clock mode to count the number of clocks on the associated pin RC0. While using the timer1 module, we have two options for selecting the clock source, 1 is to use the internal clock or we can select the external clock source by setting the TIMER1_CS pin in T1CON(Timer1 Configuration) Register. The Clock Select (CS) bit is the 1st bit. The 0th bit of the T1CON register is for enabling and disabling the TIMER1 module.

We can also use the PORTC.F1 pin for counting the clock if we enable the T1OSCEN bit in the T1CON register. Which is the 3rd bit of the T1CON register. If we enable (write 1 to the bit) we will be able to use the PortC F1 bit for counting the external clock.

So overall Configuration for TIMER1 to use the external clock source is just by setting the 0th and 1st bit of the T1CON register. If we need to count the frequency, we need to enable this clock counting process for 1 second. For a rough estimate, we can use the built-in Delay_ms(1000); function in the MIKROC Pro for the PIC compiler. Once the 1-second delay is complete, we will disable the TIMER1 and read the TMR1H and TMR1L register. Combine the value into integer and display on LCD or seven-segment display (whichever you prefer) after converting the integer to string if we are displaying over the LCD.

Here is the complete code.




// LCD module connections
sbit LCD_RS at RB2_bit;
sbit LCD_EN at RB3_bit;
sbit LCD_D4 at RB4_bit;
sbit LCD_D5 at RB5_bit;
sbit LCD_D6 at RB6_bit;
sbit LCD_D7 at RB7_bit;

sbit LCD_RS_Direction at TRISB2_bit;
sbit LCD_EN_Direction at TRISB3_bit;
sbit LCD_D4_Direction at TRISB4_bit;
sbit LCD_D5_Direction at TRISB5_bit;
sbit LCD_D6_Direction at TRISB6_bit;
sbit LCD_D7_Direction at TRISB7_bit;



char txt3[] = "Freq: ";


void printFrequency(unsigned int val);


void main() {
unsigned int freq_low=0,freq_high=0,freq=0;
  ANSEL  = 0;                        // Configure AN pins as digital I/O
  ANSELH = 0;
  C1ON_bit = 0;                      // Disable comparators
  C2ON_bit = 0;
  TRISC.F0=1;

  Lcd_Init();                        // Initialize LCD

  Lcd_Cmd(_LCD_CLEAR);               // Clear display
  Lcd_Cmd(_LCD_CURSOR_OFF);          // Cursor off
  Lcd_Out(2,1,txt3);                 // Write text in first row


  for(;;){
    TMR1L=0;
    TMR1H=0;
    T1CON= 0B00000011;
    delay_ms(1000);
    T1CON= 0B00000010;
    freq_low= TMR1L;
    freq_high= TMR1H * 256;
    freq= c+i;
    printFrequency(freq);
  }//infinite Loop ends here


}//home ends here




void printFreqeuncy(unsigned int val){
 unsigned char d1,d2,d3,d4;
 d1 = ((val/1000)%10)+48;
 d2 = ((val/100)%10)+48;
 d3 = ((val/10)%10)+48;
 d4 = (val%10)+48;

 Lcd_Chr(2,10,d1);Lcd_Chr_CP(d2);Lcd_Chr_CP(d3);Lcd_Chr_CP(d4);
}Code language: JavaScript (javascript)

By Abdul Rehman

My name is Abdul Rehman and I love to do Reasearch in Embedded Systems, Artificial Intelligence, Computer Vision and Engineering related fields. With 10+ years of experience in Research and Development field in Embedded systems I touched lot of technologies including Web development, and Mobile Application development. Now with the help of Social Presence, I like to share my knowledge and to document everything I learned and still learning.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.