Shop OBEX P1 Docs P2 Docs Learn Events
Code for simulating a 36-2 hall effect sensor — Parallax Forums

Code for simulating a 36-2 hall effect sensor

turbosupraturbosupra Posts: 1,088
edited 2010-10-20 10:50 in Propeller 1
I know this code could be much more efficient, but I need a baseline and was wondering if this is solid? I've tried it and I'm having problems reading rpms over 50 with my code and am trying to figure out where the issue is. I have a screen cap of what my oscope reads, the frequency doesn't seem correct either? The gap is approximately 10 teeth wide instead of 2, based on the oscope output?

Thanks for reading, and thoughts?

Pub 36Minus2Simulator

  DesiredRPM := 100
  toothcount := 36 * DesiredRPM
  dira[c_CrankSensorOutputSimPin] := 1

  repeat
  
    outa[c_CrankSensorOutputSimPin] := 1                ' output high
    Waitcnt((clkfreq/(toothcount * 2)) + cnt)           ' wait half the tooth count or 5 degrees of rotation
    outa[c_CrankSensorOutputSimPin] := 0                ' output low
    Waitcnt((clkfreq/(toothcount * 2)) + cnt)           ' wait half the tooth count or 5 degrees of rotation
    outa[c_CrankSensorOutputSimPin] := 1                ' output high
    Waitcnt((clkfreq/(toothcount * 2)) + cnt)           ' wait half the tooth count or 5 degrees of rotation
    outa[c_CrankSensorOutputSimPin] := 0                ' output low
    Waitcnt((clkfreq/(toothcount * 2)) + cnt)           ' wait half the tooth count or 5 degrees of rotation
    outa[c_CrankSensorOutputSimPin] := 1                ' output high
    Waitcnt((clkfreq/(toothcount * 2)) + cnt)           ' wait half the tooth count or 5 degrees of rotation
    outa[c_CrankSensorOutputSimPin] := 0                ' output low
    Waitcnt((clkfreq/(toothcount * 2)) + cnt)           ' wait half the tooth count or 5 degrees of rotation 
    outa[c_CrankSensorOutputSimPin] := 1                ' output high
    Waitcnt((clkfreq/(toothcount * 2)) + cnt)           ' wait half the tooth count or 5 degrees of rotation
    outa[c_CrankSensorOutputSimPin] := 0                ' output low
    Waitcnt((clkfreq/(toothcount * 2)) + cnt)           ' wait half the tooth count or 5 degrees of rotation
    outa[c_CrankSensorOutputSimPin] := 1                ' output high
    Waitcnt((clkfreq/(toothcount * 2)) + cnt)           ' wait half the tooth count or 5 degrees of rotation
    outa[c_CrankSensorOutputSimPin] := 0                ' output low
    Waitcnt((clkfreq/(toothcount * 2)) + cnt)           ' wait half the tooth count or 5 degrees of rotation
    outa[c_CrankSensorOutputSimPin] := 1                ' output high
    Waitcnt((clkfreq/(toothcount * 2)) + cnt)           ' wait half the tooth count or 5 degrees of rotation
    outa[c_CrankSensorOutputSimPin] := 0                ' output low
    Waitcnt((clkfreq/(toothcount * 2)) + cnt)           ' wait half the tooth count or 5 degrees of rotation
    outa[c_CrankSensorOutputSimPin] := 1                ' output high
    Waitcnt((clkfreq/(toothcount * 2)) + cnt)           ' wait half the tooth count or 5 degrees of rotation
    outa[c_CrankSensorOutputSimPin] := 0                ' output low
    Waitcnt((clkfreq/(toothcount * 2)) + cnt)           ' wait half the tooth count or 5 degrees of rotation
    outa[c_CrankSensorOutputSimPin] := 1                ' output high
    Waitcnt((clkfreq/(toothcount * 2)) + cnt)           ' wait half the tooth count or 5 degrees of rotation
    outa[c_CrankSensorOutputSimPin] := 0                ' output low
    Waitcnt((clkfreq/(toothcount * 2)) + cnt)           ' wait half the tooth count or 5 degrees of rotation
    outa[c_CrankSensorOutputSimPin] := 1                ' output high
    Waitcnt((clkfreq/(toothcount * 2)) + cnt)           ' wait half the tooth count or 5 degrees of rotation
    outa[c_CrankSensorOutputSimPin] := 0                ' output low
    Waitcnt((clkfreq/(toothcount * 2)) + cnt)           ' wait half the tooth count or 5 degrees of rotation
    outa[c_CrankSensorOutputSimPin] := 1                ' output high
    Waitcnt((clkfreq/(toothcount * 2)) + cnt)           ' wait half the tooth count or 5 degrees of rotation
    outa[c_CrankSensorOutputSimPin] := 0                ' output low
    Waitcnt((clkfreq/(toothcount * 2)) + cnt)           ' wait half the tooth count or 5 degrees of rotation
    outa[c_CrankSensorOutputSimPin] := 1                ' output high
    Waitcnt((clkfreq/(toothcount * 2)) + cnt)           ' wait half the tooth count or 5 degrees of rotation
    outa[c_CrankSensorOutputSimPin] := 0                ' output low
    Waitcnt((clkfreq/(toothcount * 2)) + cnt)           ' wait half the tooth count or 5 degrees of rotation
    outa[c_CrankSensorOutputSimPin] := 1                ' output high
    Waitcnt((clkfreq/(toothcount * 2)) + cnt)           ' wait half the tooth count or 5 degrees of rotation
    outa[c_CrankSensorOutputSimPin] := 0                ' output low
    Waitcnt((clkfreq/(toothcount * 2)) + cnt)           ' wait half the tooth count or 5 degrees of rotation
    outa[c_CrankSensorOutputSimPin] := 1                ' output high
    Waitcnt((clkfreq/(toothcount * 2)) + cnt)           ' wait half the tooth count or 5 degrees of rotation
    outa[c_CrankSensorOutputSimPin] := 0                ' output low
    Waitcnt((clkfreq/(toothcount * 2)) + cnt)           ' wait half the tooth count or 5 degrees of rotation
    outa[c_CrankSensorOutputSimPin] := 1                ' output high
    Waitcnt((clkfreq/(toothcount * 2)) + cnt)           ' wait half the tooth count or 5 degrees of rotation
    outa[c_CrankSensorOutputSimPin] := 0                ' output low
    Waitcnt((clkfreq/(toothcount * 2)) + cnt)           ' wait half the tooth count or 5 degrees of rotation
    outa[c_CrankSensorOutputSimPin] := 1                ' output high
    Waitcnt((clkfreq/(toothcount * 2)) + cnt)           ' wait half the tooth count or 5 degrees of rotation
    outa[c_CrankSensorOutputSimPin] := 0                ' output low
    Waitcnt((clkfreq/(toothcount * 2)) + cnt)           ' wait half the tooth count or 5 degrees of rotation
    outa[c_CrankSensorOutputSimPin] := 1                ' output high
    Waitcnt((clkfreq/(toothcount * 2)) + cnt)           ' wait half the tooth count or 5 degrees of rotation
    outa[c_CrankSensorOutputSimPin] := 0                ' output low
    Waitcnt((clkfreq/(toothcount * 2)) + cnt)           ' wait half the tooth count or 5 degrees of rotation
    outa[c_CrankSensorOutputSimPin] := 1                ' output high
    Waitcnt((clkfreq/(toothcount * 2)) + cnt)           ' wait half the tooth count or 5 degrees of rotation
    outa[c_CrankSensorOutputSimPin] := 0                ' output low
    Waitcnt((clkfreq/(toothcount * 2)) + cnt)           ' wait half the tooth count or 5 degrees of rotation
    outa[c_CrankSensorOutputSimPin] := 1                ' output high
    Waitcnt((clkfreq/(toothcount * 2)) + cnt)           ' wait half the tooth count or 5 degrees of rotation
    outa[c_CrankSensorOutputSimPin] := 0                ' output low
    Waitcnt((clkfreq/(toothcount * 2)) + cnt)           ' wait half the tooth count or 5 degrees of rotation
    outa[c_CrankSensorOutputSimPin] := 1                ' output high
    Waitcnt((clkfreq/(toothcount * 2)) + cnt)           ' wait half the tooth count or 5 degrees of rotation
    outa[c_CrankSensorOutputSimPin] := 0                ' output low
    Waitcnt((clkfreq/(toothcount * 2)) + cnt)           ' wait half the tooth count or 5 degrees of rotation
    outa[c_CrankSensorOutputSimPin] := 1                ' output high
    Waitcnt((clkfreq/(toothcount * 2)) + cnt)           ' wait half the tooth count or 5 degrees of rotation
    outa[c_CrankSensorOutputSimPin] := 0                ' output low
    Waitcnt((clkfreq/(toothcount * 2)) + cnt)           ' wait half the tooth count or 5 degrees of rotation
    outa[c_CrankSensorOutputSimPin] := 1                ' output high
    Waitcnt((clkfreq/(toothcount * 2)) + cnt)           ' wait half the tooth count or 5 degrees of rotation
    outa[c_CrankSensorOutputSimPin] := 0                ' output low
    Waitcnt((clkfreq/(toothcount * 2)) + cnt)           ' wait half the tooth count or 5 degrees of rotation
    outa[c_CrankSensorOutputSimPin] := 1                ' output high
    Waitcnt((clkfreq/(toothcount * 2)) + cnt)           ' wait half the tooth count or 5 degrees of rotation
    outa[c_CrankSensorOutputSimPin] := 0                ' output low
    Waitcnt((clkfreq/(toothcount * 2)) + cnt)           ' wait half the tooth count or 5 degrees of rotation
    outa[c_CrankSensorOutputSimPin] := 1                ' output high
    Waitcnt((clkfreq/(toothcount * 2)) + cnt)           ' wait half the tooth count or 5 degrees of rotation
    outa[c_CrankSensorOutputSimPin] := 0                ' output low
    Waitcnt((clkfreq/(toothcount * 2)) + cnt)           ' wait half the tooth count or 5 degrees of rotation
    outa[c_CrankSensorOutputSimPin] := 1                ' output high
    Waitcnt((clkfreq/(toothcount * 2)) + cnt)           ' wait half the tooth count or 5 degrees of rotation
    outa[c_CrankSensorOutputSimPin] := 0                ' output low
    Waitcnt((clkfreq/(toothcount * 2)) + cnt)           ' wait half the tooth count or 5 degrees of rotation
    outa[c_CrankSensorOutputSimPin] := 1                ' output high
    Waitcnt((clkfreq/(toothcount * 2)) + cnt)           ' wait half the tooth count or 5 degrees of rotation
    outa[c_CrankSensorOutputSimPin] := 0                ' output low
    Waitcnt((clkfreq/(toothcount * 2)) + cnt)           ' wait half the tooth count or 5 degrees of rotation
    outa[c_CrankSensorOutputSimPin] := 1                ' output high
    Waitcnt((clkfreq/(toothcount * 2)) + cnt)           ' wait half the tooth count or 5 degrees of rotation
    outa[c_CrankSensorOutputSimPin] := 0                ' output low
    Waitcnt((clkfreq/(toothcount * 2)) + cnt)           ' wait half the tooth count or 5 degrees of rotation
    outa[c_CrankSensorOutputSimPin] := 1                ' output high
    Waitcnt((clkfreq/(toothcount * 2)) + cnt)           ' wait half the tooth count or 5 degrees of rotation
    outa[c_CrankSensorOutputSimPin] := 0                ' output low
    Waitcnt((clkfreq/(toothcount * 2)) + cnt)           ' wait half the tooth count or 5 degrees of rotation
    outa[c_CrankSensorOutputSimPin] := 1                ' output high
    Waitcnt((clkfreq/(toothcount * 2)) + cnt)           ' wait half the tooth count or 5 degrees of rotation
    outa[c_CrankSensorOutputSimPin] := 0                ' output low
    Waitcnt((clkfreq/(toothcount * 2)) + cnt)           ' wait half the tooth count or 5 degrees of rotation
    outa[c_CrankSensorOutputSimPin] := 1                ' output high
    Waitcnt((clkfreq/(toothcount * 2)) + cnt)           ' wait half the tooth count or 5 degrees of rotation
    outa[c_CrankSensorOutputSimPin] := 0                ' output low
    Waitcnt((clkfreq/(toothcount * 2)) + cnt)           ' wait half the tooth count or 5 degrees of rotation
    outa[c_CrankSensorOutputSimPin] := 1                ' output high
    Waitcnt((clkfreq/(toothcount * 2)) + cnt)           ' wait half the tooth count or 5 degrees of rotation
    outa[c_CrankSensorOutputSimPin] := 0                ' output low
    Waitcnt((clkfreq/(toothcount * 2)) + cnt)           ' wait half the tooth count or 5 degrees of rotation
    outa[c_CrankSensorOutputSimPin] := 1                ' output high
    Waitcnt((clkfreq/(toothcount * 2)) + cnt)           ' wait half the tooth count or 5 degrees of rotation
    outa[c_CrankSensorOutputSimPin] := 0                ' output low
    Waitcnt((clkfreq/(toothcount * 2)) + cnt)           ' wait half the tooth count or 5 degrees of rotation
    outa[c_CrankSensorOutputSimPin] := 1                ' output high
    Waitcnt((clkfreq/(toothcount * 2)) + cnt)           ' wait half the tooth count or 5 degrees of rotation
    outa[c_CrankSensorOutputSimPin] := 0                ' output low
    Waitcnt((clkfreq/(toothcount * 2)) + cnt)           ' wait half the tooth count or 5 degrees of rotation
    outa[c_CrankSensorOutputSimPin] := 1                ' output high
    Waitcnt((clkfreq/(toothcount * 2)) + cnt)           ' wait half the tooth count or 5 degrees of rotation
    outa[c_CrankSensorOutputSimPin] := 0                ' output low
    Waitcnt((clkfreq/(toothcount * 2)) + cnt)           ' wait half the tooth count or 5 degrees of rotation
    outa[c_CrankSensorOutputSimPin] := 1                ' output high
    Waitcnt((clkfreq/(toothcount * 2)) + cnt)           ' wait half the tooth count or 5 degrees of rotation
    outa[c_CrankSensorOutputSimPin] := 0                ' output low
    Waitcnt((clkfreq/(toothcount * 2)) + cnt)           ' wait half the tooth count or 5 degrees of rotation



    
    outa[c_CrankSensorOutputSimPin] := 1                ' output high
    Waitcnt((clkfreq/(toothcount * 2)) + cnt)           ' wait half the tooth count or 5 degrees of rotation
    outa[c_CrankSensorOutputSimPin] := 0                ' output low
    Waitcnt((clkfreq/(toothcount * 2)) + cnt)           ' wait half the tooth count or 5 degrees of rotation
    outa[c_CrankSensorOutputSimPin] := 1                ' output high
    Waitcnt((clkfreq/(toothcount * 2)) + cnt)           ' wait half the tooth count or 5 degrees of rotation
    outa[c_CrankSensorOutputSimPin] := 0                ' output low
    Waitcnt((clkfreq/(toothcount * 2)) + cnt)           ' wait half the tooth count or 5 degrees of rotation
    Debug.Str(string(" TDC time count is "))          ' for debug purposes
    Debug.Dec(cnt)
    Debug.Tx(13) 








attachment.php?attachmentid=74536&stc=1&d=1287461167
628 x 424 - 20K

Comments

  • turbosupraturbosupra Posts: 1,088
    edited 2010-10-20 10:50
    Through attrition, this output at 16.5hz signal on my oscope, which is almost 1000 cycles per minute (960) or 1000rpms.

    Whether this works as a tooth counter (36000 cycles per minute or more) remains untested, hopefully I will find out later today.


    PUB m_36Minus2Simulator
    
      'DesiredRPM := 1000
      toothcount := 1
    
      dira[c_CrankSensorOutputSimPin] := 1
    
      ' waitcnt tdcdivisor = (rpm/60) * 2 so that it can go high 1/2 the time and low 1/2 the time
      ' waitcnt toothcountdivisor = ((rpm/60) *2 ) * 36 teeth
    
      repeat
    
        outa[c_CrankSensorOutputSimPin] := 1                ' output high
        Waitcnt((clkfreq/33) + cnt)           ' wait 1/33rd of a second 
        'toothcount := toothcount + 18         ' wait half the rotation or 180 degrees of rotation
        outa[c_CrankSensorOutputSimPin] := 0                ' output low
        Waitcnt((clkfreq/33) + cnt)           ' wait a second 1/33rd of a second
        'toothcount := toothcount + 18         ' wait half the rotation or 180 degrees of rotation
        {If toothcount => 36
          toothcount := 1
          previous := current
          current := cnt
          Debug.Str(String(" TDC after 36 teeth "))
          Debug.Tx(13)
         }
    
    
Sign In or Register to comment.