Possible Aliasing
in Propeller 1
Hello, I'm encountering weird sensor reading (pwm reading) problem and can't find the cause of this error. If anyone can share some thoughts, it would be very helpful. When I run the code below, it just works well. However, when I try to incorporate this with the main project, the pwm reading becomes irregular; say if the correct reading is 500, irregular numbers are 300-ish, 410-ish, 500 - they are quite discrete! The main project's (one of) cog calls pulse_in method to update this distance sensor reading. So far, no other codes are in the loop but "dist := pulse_in(pin)" . I first accused of a power source because a mpu is connected to the same 5V out. Yet, it's found okay. And of course, I checked if the "dist" variable is not aliased. So, my final accusation is that phsa register can be somehow corrupted? If that's not possible, what else could I accuse of? I didn't and couldn't attach the main project's code because not only it's lots of files to check and the specific cog calling this pulse_in() has one loop that calls the method and I don't think other cogs can affect this cog - stacks for the each cog are distinct!
CON
_clkmode = xtal1 + pll16x 'Standard clock mode * crystal frequency = 80 MHz
_xinfreq = 5_000_000
VAR
long stack[128], ping, gctra, counter, dist
OBJ
uart : "FullDuplexSerial.spin"
PUB main
uart.quickStart
cognew(report, @stack)
repeat
ping := pulse_in(8) ' ping in mm
pub pulse_in(pin) | mask, milimeter
mask := 1 << pin ' mask for pin
frqa := 1 ' set for high-resolution measurement
ctra := (%01000 << 26) | pin ' set ctra to POS detect on pin
waitpne(mask, mask, 0) ' wait for pin to be low
phsa := 0 ' clear accumulator
waitpeq(mask, mask, 0) ' wait for pin to go high
waitpne(mask, mask, 0) ' wait for pin to return low
milimeter := phsa / (clkfreq / 1_000_000) ' convert ticks to us
return milimeter
PUB report | temp
repeat
uart.clear
uart.dec(ping)
uart.newline
temp := ping
dist := temp'dist*70/100 + temp*30/100
uart.dec(dist/10)
uart.str(string("."))
uart.dec(dist//10)
uart.strln(string(" cm"))
waitcnt(cnt + clkfreq/10)

Comments
If the latter, you need to check for edges on both the beginning of the count and at the end. You're currently only checking for a low on the input to begin the count, which could occur any amount of time after a falling edge. I suspect the real issue is that your phsa := 0 and waitpeq(mask, mask, 0) need to be swapped.
-Phil
-Phil
http://www.maxbotix.com/Ultrasonic_Sensors/MB1003.htm
Still the odd thing is, the code only for reading pwm of this sensor works fine. Is it possible that full mpu9150 reading and 50Hz of calculation codes drops power flowing in for the sensor since my project code is quite full of all memory and cogs.
Jim