Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

frequency counter using pic16f877a

Status
Not open for further replies.
HI;
i want to count the pulses using pic16f877a. the pulses are being fed into pin RA4. i want to use timer interrupt for that so that other functions performed by the mico should not disturbed. the frequency is displayed on the LCD Screen.
kindly help me with Mikro C code.

thanking you in anticipation.
 
read on timer0, its pretty easy. the timer 0 won't need the interrupt unless you go over the 8 bits max, then you'll want the interrupt to increment a temp file, CCP does have 2 bytes jfyi. Also remember to set your TRISTA, I don't know Mikro C. so maybe you'll get more help w/ that.
 
please check my Mikro c code. it is not woking:

Code:
//Timer 0 will count and Timer 1 will measure 1 second time using interrupts ; mikro=16f877a
// OUTPUTS

#define led PORTB.F2 // simple blinking led to show the system is working

sbit LCD_RS at RD5_bit;
sbit LCD_EN at RD4_bit;
sbit LCD_D4 at RC4_bit;
sbit LCD_D5 at RC5_bit;
sbit LCD_D6 at RC6_bit;
sbit LCD_D7 at RC7_bit;

sbit LCD_RS_Direction at TRISD5_bit;
sbit LCD_EN_Direction at TRISD4_bit;
sbit LCD_D4_Direction at TRISC4_bit;
sbit LCD_D5_Direction at TRISC5_bit;
sbit LCD_D6_Direction at TRISC6_bit;
sbit LCD_D7_Direction at TRISC7_bit;
//=======End LCD Connections=============================================
unsigned int x=0,frq=0, cnt=0;

void interrupt()
{
     if(PIR1.TMR1IF)
     {
       T1CON.TMR1ON=0;  //stop
       TMR1L=0XEE;     // <------------- reload the timer
       TMR1H=0X85;


        if(cnt>=4)
        {
           led=~led;
	   frq=1;
           cnt=0;
           x=tmr0;
	   tmr0=0;
        }



        //frq=1;
       PIR1.TMR1IF=0;  // <------------- clear the timer interrupt flag
     }



}

void main()
{
        OPTION_REG=0xA0;  // for timer 0; NO presscaller; counter mode at T0CKI
	T1CON=0X60;  // 1:8 prescallar, timer off
	ADCON1=0x07;   // adc reg initialization  ; changes port a to digital I/O
	INTCON=0x80;       // globle interrupt enabled
	TMR1L=0xEE;
	TMR1H=0x85;
	T1CON.TMR1ON=1; // timer on
	PIR1.TMR1IF=0;
	TRISA.f4=1;// making ra4 as input


        x=0;
	frq=0;
  lcd_init();
  Lcd_Cmd(_LCD_CLEAR);                    // Clear display
  Lcd_Cmd(_LCD_CURSOR_OFF);
  Lcd_Cmd(_LCD_MOVE_CURSOR_RIGHT);      //*/

   Lcd_Out(1,1,"Frequency");
	 while(1)
	{
	  if(frq==1)
	  {
	    Lcd_Out(2,2,X);
	    Lcd_Out_CP("Hz");
	    frq=0;
	   }
	}

}
 
Last edited:
Code:
 T1CON.TMR1ON=0;  //stop

Ok to stop the timer....Where do you restart it?

oh yes thank you Ian i did it.... :)

now please see my code again i am stuck in a new issue now. acctually i wana measure the frequency of input power sine wave which is 50hz here in south of asia. i am counting the pulses using timer 0 at t0cki input, these pulses increment tmr0 register which is after every one second due to interrupt of timer 1 is made 0 to take counts for next pass ... it should count 50 to display but instead it counts 12... here is the code..

Code:
//Timer 0 will count and Timer 1 will measure 1 second time using interrupts ; mikro=16f877a
// OUTPUTS

#define led PORTB.F2

sbit LCD_RS at RD5_bit;
sbit LCD_EN at RD4_bit;
sbit LCD_D4 at RC4_bit;
sbit LCD_D5 at RC5_bit;
sbit LCD_D6 at RC6_bit;
sbit LCD_D7 at RC7_bit;

sbit LCD_RS_Direction at TRISD5_bit;
sbit LCD_EN_Direction at TRISD4_bit;
sbit LCD_D4_Direction at TRISC4_bit;
sbit LCD_D5_Direction at TRISC5_bit;
sbit LCD_D6_Direction at TRISC6_bit;
sbit LCD_D7_Direction at TRISC7_bit;
//=======End LCD Connections=============================================
unsigned int frq=0, cnt=0;
unsigned short G=0;
unsigned char op[12];

void interrupt()
{
     if(PIR1.TMR1IF)
     {
       T1CON.TMR1ON=0;  //stop
       TMR1L=0XEE;     // <------------- reload the timer
       TMR1H=0X85;
         cnt++;

        if(cnt>=4)
        {
           led=~led;
	   frq=1;
           cnt=0;
           G=tmr0;
	   tmr0=0;
        }



        //frq=1;
       PIR1.TMR1IF=0;  // <------------- clear the timer interrupt flag
       T1CON.TMR1ON=1;
     }



}

void main()
{
        OPTION_REG=0xA0;  // for timer 0; NO presscaller; counter mode at T0CKI
	T1CON=0X60;  // 1:8 prescallar, timer off
	ADCON1=0x07;  // adc reg initialization  ; changes port a to digital I/O
	INTCON=0xc0;       // globle & prephral interrupt enabled
	TMR1L=0xEE;
	TMR1H=0x85;
	PIR1.TMR1IF=0;
	PIE1.TMR1IE=1;
	T1CON.TMR1ON=1; // timer on

	TRISA.f4=1;// making ra4 as input
        TRISB.f2 = 0;  //o/p
        
        cnt=0;
        G=0;
	frq=0;
  lcd_init();
  Lcd_Cmd(_LCD_CLEAR);                    // Clear display
  Lcd_Cmd(_LCD_CURSOR_OFF);
  Lcd_Cmd(_LCD_MOVE_CURSOR_RIGHT);      //*/

   Lcd_Out(1,1,"Frequency");
	 while(1)
	{
	  if(frq==1)
	  { 
	   shortToStr(G,op);
           Lcd_Out(2,2,op);
	   Lcd_Out_CP("Hz");
	   frq=0;
	   }
	}

}
 
Whats the clock ( OSC ) speed?

I really can't see what you're trying to do... TMR1 seems to count to 124996 on a prescale of 1:8 ( 1 second ) then G = TMR0..

Are you using 4Mhz xtal? If so, the counting on the TOCKI pin is the problem.... Is the WDT set? If not the TMR0 module will count at 1:2 not 1:1.

OPTION_REG should be 0xA8... So the TMR0 will be on a 1:1
 
Whats the clock ( OSC ) speed?

I really can't see what you're trying to do... TMR1 seems to count to 124996 on a prescale of 1:8 ( 1 second ) then G = TMR0..

Are you using 4Mhz xtal? If so, the counting on the TOCKI pin is the problem.... Is the WDT set? If not the TMR0 module will count at 1:2 not 1:1.

OPTION_REG should be 0xA8... So the TMR0 will be on a 1:1

I am trying to count the frequency of the line(mains) power sin wave form. yes i have set timer one to generate 1s delay using interrupt and after 1s it will toggle the led and move the value of register tmr0 into variable G and and reset the tmr0 to make it ready to count the number of pulses again for 1s i.e; frequency.

yes i am using 4MHz crystal. what problem it offer to TOCKI pin???

no WDT is clear. i am not using it . i am using timer 0 as counter to count pulses at RA4 pin.
 
no WDT is clear. i am not using it . i am using timer 0 as counter to count pulses at RA4 pin.

You can still set the timer to 1:1 by setting the WDT bit in the option reg.. It doesn't enable the watchdog... That is done in the config bits....
 
Status
Not open for further replies.

New Articles From Microcontroller Tips

Back
Top