Need help converting code to output Byte
I have tried many different ways to read RPM and the version below seems to be the fastest and most accurate. The biggest problem I have is that it requires 4 WORD values and I only have enough room for 1 on the chip which I have to reuse over and over. The output is also a WORD value which will also need to be a Byte.
Post Edited (eagletalontim) : 2/28/2009 4:02:52 PM GMT
PULSIN Sensor, 0, pWidth0
PULSIN Sensor, 1, pWidth1
' Find total pulse time (high + low)
pWidth0 = pWidth0 + pWidth1
' Set dividend to 6,000,000 ($5B8D80) this many 10u/seconds in a minute
dividendMSW = $005B
dividendLSW = $8D80
rpm = 1
overflow = 0
DO
doneBit = rpm.15
rpm = rpm << 1
IF overflow = 1 THEN
rpm.0 = 1
dividendMSW = dividendMSW - pWidth0
ELSE
IF dividendMSW >= pWidth0 THEN
rpm.0 = 1
dividendMSW = dividendMSW - pWidth0
ENDIF
ENDIF
overflow = dividendMSW.15
dividendMSW = dividendMSW << 1
dividendMSW.0 = dividendLSW.15
dividendLSW = dividendLSW << 1
LOOP UNTIL doneBit = 1
rpm = rpm << 1
rpm.0 = overflow
IF dividendMSW >= pWidth0 THEN
rpm.0 = 1
ENDIF
Post Edited (eagletalontim) : 2/28/2009 4:02:52 PM GMT

Comments
One little 'out of the box' thought - are you SURE you need RPM rather than some other unit? You could divide by 4M, 8M and then average the result with very, very little code (and it would be lightning fast) - but it would not be a precise RPM ...
But to calculate RPM you don't need to count all pulses
during a minute. If you know the number of pulses in 0.1sec
you can calculate RPM from that.
0.1sec = 10000 * 10usec so you could work with 16bit numbers.
RPM = number_of_pulses_in_0.1sec * 600
In SXB 2.0 you can multiply two 16bits values
and the 32bit result is returned in __param1 to __param4.
regards peter