|
|
View previous topic :: View next topic |
Author |
Message |
lam Guest
|
... falling edge counter? |
Posted: Sat Jan 16, 2010 4:39 pm |
|
|
I would like make a bike speedometer... and I suppose I need a falling or rising edge detection. (and somehow to mix it with timer to count the speed of the bike)...
Now I'm doing this by checking every clock cycle a state of the PIN.
However its not an optimal solution... I think.
How to use interrupt ? How to program PIC to make some action every time the falling or rising edge detected?
When interrupt is used the executed code is stopped for a time of the interrupt action ?
[CPU running some program] ---interrupt detected---[interrupt action]--when end -->[CPU continue to run some program] |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Sat Jan 16, 2010 5:47 pm |
|
|
Read these threads on measuring frequency with the PIC by using
the CCP module in Capture mode, using CCP interrupts.
http://www.ccsinfo.com/forum/viewtopic.php?t=33153
http://www.ccsinfo.com/forum/viewtopic.php?t=29963
With this method, you can create a tachometer to measure the number
of wheel revolutions per second. Then multiply the revs/second by the
circumference of the wheel, and you have the distance traveled per
second. Do some more math to get the speed in mph or kph. |
|
|
Ttelmah Guest
|
|
Posted: Sun Jan 17, 2010 3:46 am |
|
|
However, you need to slightly 'rethink'.
You don't need a 'edge counter', you need an 'interval timer'.
The pulses from a single sensor on a bike wheel, are at relatively low frequencies. On a normal bike, expect pulse rates below 8Hz, and when going at low speeds (6/7mph), this falls to about 1Hz. Instead of trying to count how many edges occur in a time, you need to time the interval between the edges.
The examples from PCMprogrammer use this approach, so are a great start.
The easiest way to do the maths, is to store the wheel diameter (say in mm), then if the interval between edges, is measured in uSec (typical if using a 4MHz processor clock), use:
(36000000000*diameter/1609344)/(interval in uSec)
You calculate the constant parts (36000000000*diameter/1609344), _once_ when the system is setup. The arithmetic is all done using int32.
If (for instance), the diameter is 2150mm, and the interval is 1/4 second (250000), you get
36000000000/1609344 = 22369 - use this as a constant - see below.
22369*2150 = 48093350 (this is small enough to easily fit in an int32) - store this when the diameter is input.
Then when the pulse arrives, and your count is 250000, you get:
48093350/250000 = 192
This says '19.2' mph (print using %0.1LW)
The nice thing is that you only get one calculation, an int32 division, when the pulse is received. Even at 4MHz, this only takes about 1mSec.
To 'prove' how accurate this is, even using the rounded '22369' constant, with an interval of 250000uSec on a wheel at 2150mm diameter, the bike is doing:
3600 * 2150 * 4/1000 m/hour = 30960
At 1609.344m/km you get 19.237mph
So the result is 'spot on'.
If you change the constant to 35999, you get the speed in km/hr, making it a 'doddle' to switch.
I also have to point out though, just how cheap simple speedos like this are now. You can get ones half the size of a matchbox, that market for about $5. By the time you have even found a display, you are going to be past this. In fact they are a useful source of a cheap device that can often be used for other things - I used one as a rev counter on a drill, by just putting in a wheel diameter calculated to give the required result....
Best Wishes |
|
|
Guest
|
|
Posted: Sun Jan 17, 2010 4:09 am |
|
|
I need to do something like this:
CPU counts pulses from two sources (on INT0 and INT1 pins)
is it possible to make a++ (from INT0) and b++(from pulse on INT1) when pulses on INT0 and INT1 are in the same moment? |
|
|
|
|
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
|