![](templates/subSilver/images/CCSLogo.jpg) |
![CCS C Software and Maintenance Offers](templates/subSilver/images/forumAd6.jpg) |
View previous topic :: View next topic |
Author |
Message |
rahulbose Guest
|
how to count the no. of pulses (or high-to-low transitions) |
Posted: Tue May 08, 2007 1:17 pm |
|
|
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 |
|
![](templates/subSilver/images/spacer.gif) |
Ttelmah Guest
|
|
Posted: Tue May 08, 2007 3:04 pm |
|
|
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 |
|
![](templates/subSilver/images/spacer.gif) |
treitmey
Joined: 23 Jan 2004 Posts: 1094 Location: Appleton,WI USA
|
|
![](templates/subSilver/images/spacer.gif) |
rahulbose Guest
|
thanks a lot |
Posted: Tue May 08, 2007 9:53 pm |
|
|
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 .... |
|
![](templates/subSilver/images/spacer.gif) |
Ttelmah Guest
|
|
Posted: Wed May 09, 2007 1:56 am |
|
|
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 |
|
![](templates/subSilver/images/spacer.gif) |
|
|
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
|