View previous topic :: View next topic |
Author |
Message |
hayee
Joined: 05 Sep 2007 Posts: 252
|
measuring pulse frequency using timer0 |
Posted: Sat Aug 07, 2010 12:02 am |
|
|
Hi,
I want to measure pulses frequency using timer0. Pulses frequency is from 0 Hz to 2kHz. Can anyone tell me how to do that? Give me a sample program.
I am using CCS compiler version 4.093 and pic 18f252. |
|
|
hayee
Joined: 05 Sep 2007 Posts: 252
|
|
Posted: Sun Aug 08, 2010 11:38 pm |
|
|
Guys any solution or hints |
|
|
Geps
Joined: 05 Jul 2010 Posts: 129
|
|
Posted: Mon Aug 09, 2010 6:10 am |
|
|
You could use the RB interrupt and record the time differences. |
|
|
bkamen
Joined: 07 Jan 2004 Posts: 1615 Location: Central Illinois, USA
|
|
Posted: Mon Aug 09, 2010 7:23 am |
|
|
You have the CCP module which will be a much more hardware accurate method.
Using timer0 is prone to interrupt service handling latency...
Use the CCP module and you'll be better off. Read the datasheet as most PIC's have this built in.
-Ben _________________ Dazed and confused? I don't think so. Just "plain lost" will do. :D |
|
|
hayee
Joined: 05 Sep 2007 Posts: 252
|
|
Posted: Wed Aug 18, 2010 12:09 am |
|
|
What I am doing right now is that pulses is feed to the INT0/B0 pin, and I am counting the pulses using rising edge trigger. Now I also want to measure the pulses width or frequency.
If I use the CCP module as bkamen said then,
1st - I have to change my circuit.
2nd - Can I also count the pulses as i am doing right now ?
3rd - Is CCP module able to capture frequency of 1 --> 2kHz ? |
|
|
hayee
Joined: 05 Sep 2007 Posts: 252
|
|
Posted: Fri Sep 03, 2010 12:14 am |
|
|
I have written the following code to measure the frequency, but there is no accuracy. How I make more efficient code.
My theme for routine is this
>I have to measure the frequency of the signal from 2KHz to 10Hz.
>Frequency is coming at the int0 pin of 18f252 controller.
>In the code i select the value of prescaler so that the timer will not overflow until the ext interrupt (edge triggered) comes.
>when the ext int comes,the value of timer0 will be saved in a variable and then reset the timer.
>display the value on the PC using serial communtication.
Code: |
#include <18f252.h>
#fuses HS,NOWDT,NOLVP,PUT,NOBROWNOUT
#use delay(clock=20000000)
#use rs232(xmit=PIN_C6, rcv=PIN_C7, baud=9600)
long a;
#int_TIMER0
void timer0_interrupt(void)
{
set_timer0(0);//if the timer overflows it reset the timer0
clear_interrupt(int_TIMER0);
enable_interrupts(int_TIMER0);
output_toggle(PIN_C1);//if the timer overflows the led toggles.
}
#int_EXT
void EXT_isr(void)
{
a=get_timer0();//when ext interrupt comes,then the value of timer is stored in variable
set_timer0(0);//and then reset the timer,
printf(" %lu \r\n",a);//print that value on PC
output_toggle(PIN_C2);//toggle led2
}
void main()
{
setup_timer_0(RTCC_INTERNAL | RTCC_DIV_8);//timer is in 16-bit mode,prescaler is 8
set_timer0(0);//initially value of timer is 0
enable_interrupts(INT_TIMER0);//enable timer0
ext_int_edge(0,L_TO_H);//when interrupt goes low to high (rising edge)
enable_interrupts(INT_EXT);//enable int0
enable_interrupts(Global); //enable global interrupts
while(1)
{
}
} |
Any suggestion for improving the accuracy. |
|
|
|