Code for simulating a 36-2 hall effect sensor
turbosupra
Posts: 1,088
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.