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

measuring pulse frequency using timer0

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



Joined: 05 Sep 2007
Posts: 252

View user's profile Send private message

measuring pulse frequency using timer0
PostPosted: Sat Aug 07, 2010 12:02 am     Reply with quote

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

View user's profile Send private message

PostPosted: Sun Aug 08, 2010 11:38 pm     Reply with quote

Guys any solution or hints
Geps



Joined: 05 Jul 2010
Posts: 129

View user's profile Send private message

PostPosted: Mon Aug 09, 2010 6:10 am     Reply with quote

You could use the RB interrupt and record the time differences.
bkamen



Joined: 07 Jan 2004
Posts: 1615
Location: Central Illinois, USA

View user's profile Send private message

PostPosted: Mon Aug 09, 2010 7:23 am     Reply with quote

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

View user's profile Send private message

PostPosted: Wed Aug 18, 2010 12:09 am     Reply with quote

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

View user's profile Send private message

PostPosted: Fri Sep 03, 2010 12:14 am     Reply with quote

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.
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