Code for simulating a 36-2 hall effect sensor
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?
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)


Comments
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) }