CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

Frequency meter calculation error

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
jojos



Joined: 30 Apr 2007
Posts: 64

View user's profile Send private message

Frequency meter calculation error
PostPosted: Thu Jan 24, 2008 6:26 am     Reply with quote

Hello.I am working on a frequency meter project.Processor 18F2420 8 Mhz internal oscillator As signal i am using a 50 Hz square wave (5Vp-p) that comes from a frequency generator.
On the oscilloscope i can measure 50 Hz to the T0CKI pin.

I use Timer0 for external edge counter (high to low) T0CON=0b00111000; . Inside interrupt of Timer1 i calculate the frequency.
Timer 1 interrupt occurs every 40 msec.
The problem that i have is i get a 51 to 52 Hz indication and not 50 Hz as i should.Also as i raise the frequency the error gets bigger

I used two methods to calculate frequency.I had the same error in both of them.
Here is the first (simple) method that i calculate frequency.Every 1 sec i stop Timer0 ,read the T0 counter value that equals the frequency i need :

Code:


#INT_TIMER1

void service1(void)
{
   char butt=0;
   int16 temp=0,calc_pulse=0;


   disable_interrupts(INT_TIMER1);
   //disable_interrupts(INT_TIMER2);
   
   
   
   sec_timer++;   

   

   if(sec_timer==25)            //25 *40 msec = 1sec
{
   sec_timer=0;

   TMR0ON=OFF;             //Stop T0 during calculation
   pulses=make16(TMR0H,TMR0L);

   
   sec_pulses=pulses;
   SET_TIMER0(0);            //Restart Timer0 and set to zero its counter.
   TMR0ON=ON;

}   


   Bin2Bcd(sec_pulses);

   


enable_interrupts(INT_TIMER1);
//enable_interrupts(INT_TIMER2);
TMR1IF=0;
TMR1H=0xD8;            //40 msec set interrupt
TMR1L=0xEF;
//TMR1H=0x9E;
//TMR1L=0x57;

   } 




Here is the second method that i calculate frequency.Now i dont sytop - restart Timer 0 i let it in count all the time and every time i calculate freq i save the current value of Timer 0 (pulses variable) as
previous pulses ( pr_pulses variable) :


Code:


#INT_TIMER1

void service1(void)
{
   char butt=0;
   int16 temp=0,calc_pulse=0;


   disable_interrupts(INT_TIMER1);
   
   
   
   sec_timer++;   



   if(sec_timer==25)
{
   sec_timer=0;
   
   pulses=make16(TMR0H,TMR0L);

      
   sechigh_pulses=0;
   sec_pulses=0;
      if(pulses>pr_secpulses)
      sec_pulses=pulses-pr_secpulses;   
      
      if(pulses<pr_secpulses)
      {
         sec_pulses=0xFFFF-pr_secpulses;
         sec_pulses=pulses+sec_pulses;
         sec_pulses++;
      }   
   
   
      pr_secpulses=pulses;
      if(sec_pulses>999)
      {
      sechigh_pulses=sec_pulses/1000;
      sec_pulses=sec_pulses%1000;
      
      }

   
}   


   Bin2Bcd(sec_pulses);

   


enable_interrupts(INT_TIMER1);
//enable_interrupts(INT_TIMER2);
TMR1IF=0;
TMR1H=0xD8;
TMR1L=0xEF;
//TMR1H=0x9E;
//TMR1L=0x57;

   }




P.S. I changed from internal 8 Mhz to an external 8 Mhz and i get 50 -51 Hz indication instead of the previous 51- 52 Hz , but still there is an accurasy problem cause this 1 Hz error in 50 Hz it gets 3-4 Hz error on 200 Hz input frequency and so on

Thank you for your time.
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Thu Jan 24, 2008 11:46 am     Reply with quote

Use the forum search page to search for:
Quote:
tachometer

There are many posts with sample code and a discussion of how to use
the CCP module in Capture mode to find the frequency of a signal.
http://www.ccsinfo.com/forum/search.php
RLScott



Joined: 10 Jul 2007
Posts: 465

View user's profile Send private message

Re: Frequency meter calculation error
PostPosted: Thu Jan 24, 2008 1:34 pm     Reply with quote

jojos wrote:
...there is an accurasy problem cause this 1 Hz error in 50 Hz it gets 3-4 Hz error on 200 Hz input frequency and so on


This would tend to indicate that your 50 Hz and 200 Hz are not accurate. How did you check them? An o-scope timebase is not good enough for the small errors you are talking about. The other thing to check is the accuracy of the 40 msec. Timer 1 interrupts. You could toggle an I/O bit every time you enter the Timer 1 ISR and then see if you really are getting exactly 25 interrupts per second.

Robert Scott
Real-Time Specialties
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group