HELP PLEASE ...Assembly code for counter not working
Crescendo
Posts: 4
Hi,
We need to count the vibrations from a vibration sensor on an input pin on our Propeller board. We want to write the code in Assembly language (we already wrote it in Spin) so that one cog can keeping counting while we use the count number as a variable for another program in Spin language. We hope to decrease our lag time by doing this. However, we are having trouble with our Assembly code. We tried to write it based on our Spin code and this is what we have:
DAT
org 0
:loop movi ctra, #000 'bs2.COUNT_Start(Pin) --> reference to Spin code
movi ctra, Hash
mov frqa, #1
mov phsa, #0
rdlong Delay, #0 'bs2.Pause(Duration)
shr Delay, #2
mov Time, cnt
add Time, Delay
waitcnt Time, Delay
mov Vibes, phsa 'bs2.Count_Read
movi ctra, #000 'bs2.Count_ Stop
jmp #:loop
Vibes long
Pin long |<4
Hash long (010 << 26 ) | (1 << 23) | (0 << 9) | (4)
Delay res 1
Time res 1
The Spin code that this code is based on is below where "bs2" is from the object exchange http://obex.parallax.com/objects/30/ :
bs2.Start
bs2.COUNT_Start(4)
bs2.pause(100)
vibes := bs2.Count_Read
bs2.Count_Stop
Any help with our Assembly code would be greatly appreciated!!
Thank you.
We need to count the vibrations from a vibration sensor on an input pin on our Propeller board. We want to write the code in Assembly language (we already wrote it in Spin) so that one cog can keeping counting while we use the count number as a variable for another program in Spin language. We hope to decrease our lag time by doing this. However, we are having trouble with our Assembly code. We tried to write it based on our Spin code and this is what we have:
DAT
org 0
:loop movi ctra, #000 'bs2.COUNT_Start(Pin) --> reference to Spin code
movi ctra, Hash
mov frqa, #1
mov phsa, #0
rdlong Delay, #0 'bs2.Pause(Duration)
shr Delay, #2
mov Time, cnt
add Time, Delay
waitcnt Time, Delay
mov Vibes, phsa 'bs2.Count_Read
movi ctra, #000 'bs2.Count_ Stop
jmp #:loop
Vibes long
Pin long |<4
Hash long (010 << 26 ) | (1 << 23) | (0 << 9) | (4)
Delay res 1
Time res 1
The Spin code that this code is based on is below where "bs2" is from the object exchange http://obex.parallax.com/objects/30/ :
bs2.Start
bs2.COUNT_Start(4)
bs2.pause(100)
vibes := bs2.Count_Read
bs2.Count_Stop
Any help with our Assembly code would be greatly appreciated!!
Thank you.
Comments
[Edit] Here's how:
Create a variable and a stack for the counter cog:
Then launch the method that handles the counting. Note that I mark these methods as private as an indicator that they're call-and-return type methods. You'll see that inside the code it runs in a loop and can never escape, hence you cannot call it normally, you must launch it with cognew
Here's the counter method. Remember, this is launched into its own cog so you cannot read its phsa register directly.
After launching your hub variable will be updated with the counts on the selected pin at the rate you specify. For very high frequency signals you may still have to convert to PASM; this code is closer to what you want than using the BS2 object.
[Edit 2] Here's how to do the same thing in PASM.
You launch the PASM code like this:
Note that this counts on a specific order of variables and the delay timing is passed in the vibes variable to the PASM code. Once the delay is read by the PASM code this variable is cleared in prep for counts on the pin.
It looks like you're trying to use PLL single-ended mode with divide by 64 (counter mode 0_00010_001) with an output on pin 4. If that's the case you would either use a regular mov instruction: mov ctra, Hash or piece it together as in: movi ctra,#%0_00010_001 and movs ctra,#%00100. That gets the clock ticking, but you need to enable an output pin: mov dira,#%1000. I don't really see how that's useful for counting though. Seems to me counter modes 0_01010_000 (posedge detector) or 0_01110_000 (negedge detector) would be better suited.
You're loading a delay from memory location 0, which contains the clock frequency (typically 80_000_000) which shifted right twice is a 250ms delay.
And lastly a nitpicky detail, it's better to load your delay into the register and add the current cnt, than to load the cnt and add the delay. mov Time,Delay then add Time,cnt. This makes sure you the most recent value of cnt.
... actually it looks like you're starting the counter mov phsa,#0 quite a few cycles before you get the delay established. Ideally, you'd get the delay read in and prepared first, and then zero phsa and wait out the delay.