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 support@ccsinfo.com

measure frequency of a device

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



Joined: 04 Jan 2009
Posts: 9
Location: India

View user's profile Send private message AIM Address

measure frequency of a device
PostPosted: Mon Jan 05, 2009 8:16 am     Reply with quote

I need to measure the frequency of a device using PIC16f877a. Can anybody please help me with the hardware connections and the software also.

Thanks in advance
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Mon Jan 05, 2009 11:09 am     Reply with quote

Read this 3-page thread:
http://www.ccsinfo.com/forum/viewtopic.php?t=29963
Guest








PostPosted: Mon Jan 05, 2009 10:03 pm     Reply with quote

I am using 20MHz with pic16f877a is there any code change for the below program bcoz I am unable to get the result. My device frequency will be betwen 47Hz to 50Hz. What should be the hardware connections...I am a little confused.
Code:
#include <16f877a.h>
#fuses HS,wdt,NOPROTECT,BROWNOUT
#use delay(clock=20000000,restart_wdt)

#include<freqlcd.c>

#byte    PORTA  = 0x05
#byte    PORTB  = 0x06
#byte    PORTC  = 0x07
#byte    PORTD  = 0x08
#byte    PORTE  = 0x09

int16 isr_ccp_delta;
int16 current_ccp_delta;
int16 frequency;

#int_ccp1
void ccp1_isr(void)
{
int16 current_ccp;
static int16 old_ccp = 0;
current_ccp = CCP_1;   
isr_ccp_delta = current_ccp - old_ccp;
old_ccp = current_ccp;
}


void main()
{
  set_tris_a(0xff);
  set_tris_b(0x00);
  set_tris_c(0x00);
  set_tris_d(0xff);
  set_tris_e(0x00);

  lcd_init();   
  delay_ms(10);
 
set_timer1(0);           
setup_timer_1(T1_INTERNAL | T1_DIV_BY_8); 
setup_ccp1(CCP_CAPTURE_RE);   
clear_interrupt(INT_CCP1);
enable_interrupts(INT_CCP1);
enable_interrupts(GLOBAL);


setup_wdt(WDT_2304MS);
 
while(TRUE)
{
 restart_wdt();
   disable_interrupts(GLOBAL);
   current_ccp_delta = isr_ccp_delta;
   enable_interrupts(GLOBAL);     
   frequency = (int16)(1000000L / current_ccp_delta);     
           lcd_gotoxy(1,2);
           printf(lcd_putc,"Frequency:%ldHz",frequency); 
  }
}
srikanth bodduna



Joined: 04 Jan 2009
Posts: 9
Location: India

View user's profile Send private message AIM Address

PostPosted: Mon Jan 05, 2009 10:06 pm     Reply with quote

i am using 20MHz with pic16f877a is there any code change for the below program bcoz i am unable to get the result. my device frequency will be betwen 47Hz to 50Hz.what should be the hardware connections...i am little confused.


#include <16f877a.h>
#fuses HS,wdt,NOPROTECT,BROWNOUT
#use delay(clock=20000000,restart_wdt)

#include<freqlcd.c>

#byte PORTA = 0x05
#byte PORTB = 0x06
#byte PORTC = 0x07
#byte PORTD = 0x08
#byte PORTE = 0x09

int16 isr_ccp_delta;
int16 current_ccp_delta;
int16 frequency;

#int_ccp1
void ccp1_isr(void)
{
int16 current_ccp;
static int16 old_ccp = 0;
current_ccp = CCP_1;
isr_ccp_delta = current_ccp - old_ccp;
old_ccp = current_ccp;
}


void main()
{
set_tris_a(0xff);
set_tris_b(0x00);
set_tris_c(0x00);
set_tris_d(0xff);
set_tris_e(0x00);

lcd_init();
delay_ms(10);

set_timer1(0);
setup_timer_1(T1_INTERNAL | T1_DIV_BY_8);
setup_ccp1(CCP_CAPTURE_RE);
clear_interrupt(INT_CCP1);
enable_interrupts(INT_CCP1);
enable_interrupts(GLOBAL);


setup_wdt(WDT_2304MS);

while(TRUE)
{
restart_wdt();
disable_interrupts(GLOBAL);
current_ccp_delta = isr_ccp_delta;
enable_interrupts(GLOBAL);
frequency = (int16)(1000000L / current_ccp_delta);
lcd_gotoxy(1,2);
printf(lcd_putc,"Frequency:%ldHz",frequency);
}
}
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Mon Jan 05, 2009 10:09 pm     Reply with quote

Let the compiler handle the TRIS. It will do it better than you do.
The CCP input is on Port C. You have set Port C to all outputs.
Delete all the set_tris_x() statements.
srikanth bodduna



Joined: 04 Jan 2009
Posts: 9
Location: India

View user's profile Send private message AIM Address

PostPosted: Mon Jan 05, 2009 10:16 pm     Reply with quote

Thx pcm programmer....

What should be the input signal for RC2 pin ..i am using ADC signal as we do for reading a voltage i.e variable DC voltage.is it ok or there is any hardware changes.
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Mon Jan 05, 2009 10:56 pm     Reply with quote

It should be a digital signal, and it should have voltage levels between
0v and at least +4v. This assumes that your PIC is running at +5v.
srikanth bodduna



Joined: 04 Jan 2009
Posts: 9
Location: India

View user's profile Send private message AIM Address

PostPosted: Mon Jan 05, 2009 11:43 pm     Reply with quote

i removed tris functions and fed 2.3v signal to RC2.i am getting
15hz,37hz,68hz,27hz..when ever i restart the device.but with the frequency meter it shows 49Hz.wats wrong.

#include <16f877a.h>
#fuses HS,wdt,NOPROTECT,BROWNOUT
#use delay(clock=20000000,restart_wdt)

#include<freqlcd.c>

#byte PORTA = 0x05
#byte PORTB = 0x06
#byte PORTC = 0x07
#byte PORTD = 0x08
#byte PORTE = 0x09

int16 isr_ccp_delta;
int16 current_ccp_delta;
int16 frequency;

#int_ccp1
void ccp1_isr(void)
{
int16 current_ccp;
static int16 old_ccp = 0;
current_ccp = CCP_1;
isr_ccp_delta = current_ccp - old_ccp;
old_ccp = current_ccp;
}


void main()
{
lcd_init();
delay_ms(10);

set_timer1(0);
setup_timer_1(T1_INTERNAL | T1_DIV_BY_8);
setup_ccp1(CCP_CAPTURE_RE);
clear_interrupt(INT_CCP1);
enable_interrupts(INT_CCP1);
enable_interrupts(GLOBAL);


setup_wdt(WDT_2304MS);

while(TRUE)
{
restart_wdt();
disable_interrupts(GLOBAL);
current_ccp_delta = isr_ccp_delta;
enable_interrupts(GLOBAL);
frequency = (int16)(1000000L / current_ccp_delta);
lcd_gotoxy(1,2);
printf(lcd_putc,"Frequency:%ldHz",frequency);
}
}

i should get frequency betwn 47 to 50Hz
srikanth bodduna



Joined: 04 Jan 2009
Posts: 9
Location: India

View user's profile Send private message AIM Address

frequency with 16f873a...
PostPosted: Fri Jan 09, 2009 4:28 am     Reply with quote

I have done this with pic16f877a and got the result (50Hz) correctly.
To reduce my project cost I have to do with PIC16f873a but I am getting
0Hz instead of 50Hz. Please can anybody help me.

This is my code for pic16f873a:
Code:
#include <16f873a.h>
#device adc=10
#fuses HS,wdt,NOPROTECT,BROWNOUT
#use delay(clock=20000000,restart_wdt)

#include<lcd873a.c>

#byte    PORTA  = 0x05
#byte    PORTB  = 0x06
#byte    PORTC  = 0x07

#bit t1_overflow=0x0f.0

   int cycles8, cycles;
   int32 freq;
   long freqc_high;
   long freqc_low;


void main()
{
  lcd_init();   
  delay_ms(10);
 
  servomax(); 
  setup_wdt(WDT_2304MS);
 
while(TRUE)
{
 restart_wdt();

      cycles8=0;
      cycles=0;
      freqc_high=0;
      t1_overflow=0;
      set_timer1(0);
      setup_timer_1(T1_EXTERNAL|T1_DIV_BY_8);

      while (cycles!=0xFF) {
       cycles8=0;       
       while (cycles8!=0xFF) {
         if (t1_overflow)     
            {
            t1_overflow=0;
            freqc_high++;
            }
         else                                       
            {
            delay_cycles(5);
            }
         delay_cycles(62);
         cycles8++;
  }
 delay_cycles(216);     
 cycles++;
}
      delay_cycles(211);   

      setup_timer_1(T1_DISABLED);   
      if (t1_overflow)           
          freqc_high++;
      freqc_low=get_timer1();     
      freq=make32(freqc_high,freqc_low);   
        lcd_gotoxy(1,2);
        printf(lcd_putc,"Frequency:%ldHz  ",freq);       
   }
}
srikanth bodduna



Joined: 04 Jan 2009
Posts: 9
Location: India

View user's profile Send private message AIM Address

freq with pic16f873a using zer crossing
PostPosted: Sat Jan 10, 2009 12:27 am     Reply with quote

i got the result with 4v signal using CCP .but i need to do the frequency mesurement with zero crossing using pic16f873a for this purpose i used the CCS example file EX_FREQC.C but i am not getting the desired freq (50Hz)...can anybody help me please.
srikanth bodduna



Joined: 04 Jan 2009
Posts: 9
Location: India

View user's profile Send private message AIM Address

frequecy in float (decimal)
PostPosted: Wed Jan 21, 2009 10:58 pm     Reply with quote

Dear all i am able to read the frequecy of a device perfectly using the ccp method but not able to read the same in float format ie 49.6,49.2,48.5,50.2....etc which is the exact reading of a device. I have changed the datatypes of isr_ccp_delta,current_ccp_delta and
frequency to float and found an error (only integers are allowed for this operation at: frequency = (int16)((1250000L + (int32)(current_ccp_delta >> 1)) / current_ccp_delta); ) can anybody please help me in doing so.

my code is
Code:


#include <16f873a.h>
#fuses HS,wdt,NOPROTECT,BROWNOUT
#use delay(clock=20000000,restart_wdt)

#include<lcd873a.c>

#byte    PORTA  = 0x05
#byte    PORTB  = 0x06
#byte    PORTC  = 0x07

int16 isr_ccp_delta;
int16 current_ccp_delta;
int16 frequency;

#int_ccp1
void ccp1_isr(void)
{
int16 current_ccp;
static int16 old_ccp = 0;
current_ccp = CCP_1;   
isr_ccp_delta = current_ccp - old_ccp;
old_ccp = current_ccp;
}


void main()
{
  lcd_init();   
  delay_ms(10);
 
set_timer1(0);           
setup_timer_1(T1_INTERNAL | T1_DIV_BY_4); 
setup_ccp1(CCP_CAPTURE_RE);   
clear_interrupt(INT_CCP1);
enable_interrupts(INT_CCP1);
enable_interrupts(GLOBAL);


  setup_wdt(WDT_2304MS);
 
while(TRUE)
{
 restart_wdt();
   disable_interrupts(GLOBAL);
   current_ccp_delta = isr_ccp_delta;
   enable_interrupts(GLOBAL);     
   frequency = (int16)((1250000L + (int32)(current_ccp_delta >> 1)) /  current_ccp_delta);
  }
}
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Wed Jan 21, 2009 11:26 pm     Reply with quote

Create a small test program in which you take that equation and try to
make it work with floating point math. Declare the variables used in
that equation and load them with some typical values. Don't use the
CCP code to load the values. You should load them with constants.
This is a test program. Add a printf statement so you can see the
results displayed in a terminal window. Experiment until you get it to work. Add some additional printf statements to display intermediate
results if you have trouble understanding the code.
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