View previous topic :: View next topic |
Author |
Message |
srikanth bodduna
Joined: 04 Jan 2009 Posts: 9 Location: India
|
measure frequency of a device |
Posted: Mon Jan 05, 2009 8:16 am |
|
|
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
|
|
|
Guest
|
|
Posted: Mon Jan 05, 2009 10:03 pm |
|
|
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
|
|
Posted: Mon Jan 05, 2009 10:06 pm |
|
|
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
|
|
Posted: Mon Jan 05, 2009 10:09 pm |
|
|
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
|
|
Posted: Mon Jan 05, 2009 10:16 pm |
|
|
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
|
|
Posted: Mon Jan 05, 2009 10:56 pm |
|
|
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
|
|
Posted: Mon Jan 05, 2009 11:43 pm |
|
|
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
|
frequency with 16f873a... |
Posted: Fri Jan 09, 2009 4:28 am |
|
|
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
|
freq with pic16f873a using zer crossing |
Posted: Sat Jan 10, 2009 12:27 am |
|
|
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
|
frequecy in float (decimal) |
Posted: Wed Jan 21, 2009 10:58 pm |
|
|
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
|
|
Posted: Wed Jan 21, 2009 11:26 pm |
|
|
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. |
|
|
|