Shop OBEX P1 Docs P2 Docs Learn Events
Need help converting code to output Byte — Parallax Forums

Need help converting code to output Byte

eagletalontimeagletalontim Posts: 1,399
edited 2009-02-28 17:56 in General Discussion
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.

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

  • David BaylissDavid Bayliss Posts: 58
    edited 2009-02-28 17:36
    Peter has posted some math libraries; I haven't used them myself but Peter's work is usually excellent - so they may well do what you need.

    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 ...
  • eagletalontimeagletalontim Posts: 1,399
    edited 2009-02-28 17:38
    It would definitely have to be RPM since most other functions use RPM as a variable in the math. The more accurate, the better [noparse]:)[/noparse]
  • Peter VerkaikPeter Verkaik Posts: 3,956
    edited 2009-02-28 17:56
    My libraries are for 16bit math only and this is 32bit math.
    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
Sign In or Register to comment.