Sanity check w.r.t counters
Hugh
Posts: 362
I have about 12 turns of wire wrapped around the lead of one spark-plug of my boat's outboard engine. This is connected to an IR LED (with a 3V3 zener protecting it and with the LEDs grounded to the engine). The light from this LED runs down some fibre-optic to a receiver that is connected to a pin of the Prop.
If with each pulse I increment the value of a variable and then display that value on an LCD display the count increases happily as the engine rotates.
It goes awry if I try to calculate the RPM based on the difference in the counter value between consecutive pulses. I'd be grateful for a sanity check on my code: I think that either I have a problem because of some misunderstanding of the counter or the result is too big for the variable, RPM (a long).
It must be obvious and simple, but it has me foxed.
Any observations gratefully received.
(Why this way? Fibre optic doesn't corrode, doesn't leak RF, doesn't get affected by RF and is easy to use)
If with each pulse I increment the value of a variable and then display that value on an LCD display the count increases happily as the engine rotates.
It goes awry if I try to calculate the RPM based on the difference in the counter value between consecutive pulses. I'd be grateful for a sanity check on my code: I think that either I have a problem because of some misunderstanding of the counter or the result is too big for the variable, RPM (a long).
It must be obvious and simple, but it has me foxed.
dira[PinRPM]~ repeat ' Repeat ad infintum... waitpeq(|<PinRPM, |< PinRPM, 0) ' Check the pin to which the sensor is connected. Wait until it is HIGH cntRPM := cnt ' Make a note of the counter value at this transition rpmDiff := cnt - cntRPMold ' Work out the number of clock cycles between this transition and the last cntRPMold := cntRPM ' The counter value at the last transition is updated to this transition waitpne(|< PinRPM, |< PinRPM, 0) ' Wait until the pin has changed state again. RPM := (60 * 80_000_000 )/ rpmDiff
Any observations gratefully received.
(Why this way? Fibre optic doesn't corrode, doesn't leak RF, doesn't get affected by RF and is easy to use)
Comments
Your RPM formula exceeds the maximum value of a long variable.
60 * 80_000_000 will equal 4_800_000_000. In Spin, the values for long range from -2_147_483_648 to +2_147_483_647.
Sandy
It is still possible to overrun.
So try this as its harder to overrun:
Duane J