propeller PING code math explanation
AKruzel
Posts: 25
hello all,
this should be a very simple question however i've been pondering over it for way too long. I'm using the PING code with the propeller and all works well, however i really want to understand the math part where the number of ticks is converted to microseconds:
PUB ping(Pin):microseconds|cnt1, cnt2
dira[2..3]~~
outa[2] := 0
outa[3] := 1
outa[Pin]~
dira[Pin]~~
outa[Pin]~~
outa[Pin]~
dira[Pin]~
waitpne(0,|< Pin, 0)
cnt1 := cnt
waitpeq(0,|< Pin, 0)
cnt2 := cnt
microseconds := (||(cnt1-cnt2) / (clkfreq / 1_000_000)) >> 1
i understand everything except for why is the >> 1 added to the end? i tested the PING without it and saw that the measurement doubles so i can see the point of using it, but i want to make sure i have the correct reason. I'm thinking it's because the micro is working at 80MHz which is 80 million full cycles a second but the PING only outputs a high pulse which corresponds to the distance. do we use the >> 1 to divide by 2 since the ping ONLY outputs a high pulse? any simple explanation is greatly appreciated! thanks!
Adam
this should be a very simple question however i've been pondering over it for way too long. I'm using the PING code with the propeller and all works well, however i really want to understand the math part where the number of ticks is converted to microseconds:
PUB ping(Pin):microseconds|cnt1, cnt2
dira[2..3]~~
outa[2] := 0
outa[3] := 1
outa[Pin]~
dira[Pin]~~
outa[Pin]~~
outa[Pin]~
dira[Pin]~
waitpne(0,|< Pin, 0)
cnt1 := cnt
waitpeq(0,|< Pin, 0)
cnt2 := cnt
microseconds := (||(cnt1-cnt2) / (clkfreq / 1_000_000)) >> 1
i understand everything except for why is the >> 1 added to the end? i tested the PING without it and saw that the measurement doubles so i can see the point of using it, but i want to make sure i have the correct reason. I'm thinking it's because the micro is working at 80MHz which is 80 million full cycles a second but the PING only outputs a high pulse which corresponds to the distance. do we use the >> 1 to divide by 2 since the ping ONLY outputs a high pulse? any simple explanation is greatly appreciated! thanks!
Adam
Comments
As you've already figured out, dividing by (clkfreq / 1_000_000) converts the time to microseconds adjusting for the system clock frequency of the Propeller since the counts (cnt1 and cnt2) are in terms of system clock pulses.
Note that the -544 accounts for the time required by the Spin instruction to update the tix value. By doing this we remove ~7us error (@80MHz).