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

how to count the no. of pulses (or high-to-low transitions)

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







how to count the no. of pulses (or high-to-low transitions)
PostPosted: Tue May 08, 2007 1:17 pm     Reply with quote

hello!!!
i am using an optocoupler for getting pulses from the wheel of a toy vehicle for knowin the speed of the toy car. the output of the optocoupler is a pulse waveform whose frequency is approx. 10khz ... most importantly i need to count the no. of high-to-low transitions which are occuring in one second and use the logged value for further calculations.
the time for which the waveform remains low is jus 2 us as compared to 100 us for staying high.
how can i count the no. of pulses.
thnks in advance
Ttelmah
Guest







PostPosted: Tue May 08, 2007 3:04 pm     Reply with quote

Two methods.
1) Feed the signal into an interrupt input, and increment a counter in the interrupt handler. Interrupts are _latched_events, triggered by the selected edge, so even a short pulse will be seen, provided the 'repeat' rate is below the time needed for the handler.
2) Feed the signal into a CCP input on the PIC. This can be programmed as a hardware counter, and again will capture even tiny pulses (typical count rates are up with the maximum clock rates of the chips, so often in excess of 40MHz!). All you need d, is zero the counter, wait one second, and read the counter. Provided the count doesn't exceed 65535, nothing else is needed.

Best Wishes
treitmey



Joined: 23 Jan 2004
Posts: 1094
Location: Appleton,WI USA

View user's profile Send private message Visit poster's website

PostPosted: Tue May 08, 2007 4:54 pm     Reply with quote

The tricky part might be the 1 sec timming.

Depending on how accurate you need, I would put a
32.768KHz crystal from c0 to c1
see page 21 Y3 of picdem 2 plus users guide.
http://ww1.microchip.com/downloads/en/DeviceDoc/PICDEM%202Plus%20Old%20Version%2051275b.pdf
This should get you a good 1sec timer.
rahulbose
Guest







thanks a lot
PostPosted: Tue May 08, 2007 9:53 pm     Reply with quote

thanks a lot guys ...
actually i'm not plannin to use the interrupt method because the pulses will keep on comin from the sensor continuously nd its not a triggerin event....

2nd method is wat i am follwin but for some reason i haven't got d output ...

pls tel me if i am correct in thinkin dis ... m not very good at using the PIC but tat is d only microcontroller i've worked with ....
Ttelmah
Guest







PostPosted: Wed May 09, 2007 1:56 am     Reply with quote

Your 'reasoning' about not using an interrupt, seems invalid.
Counting with an interrupt, is exactly the sort of thing it is designed to do. Since the standard interrupts trigger on the 'edge' of the pulse (on the PIC, they are 'edge sensitive', not 'level sensitive'), the handler can be truly easy.
The interrupt approach is easier, provided the pulses are not too close together (your description suggests they are not), and if you want a large counter.
However as another poster has said, it is the 'one second' part that is harder!. It really though depends on the accuracies involved, and what else is going on at the same time. Using the hardware CCP, since it is donein hardware (and therefore does not interfere with 'software timings), you can for instance, simply set the counter to zero, delay_ms(1000), and then read the counter, to get a value!.

Best Wishes
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