Help with counters needed
DRMorrison
Posts: 81
Hello,
I've done some programming with SPIN and am working on a project and need some help. I don't know much about how to use the counters
that are available. I understand that there are two, and that they can use two pins to detect or send a signal.
Of all of the things listed that it can do in the Prop. manual, I'm looking to count pulses. I've tried to decipher the counter mode in table, 2-7 on
page 98, but just can't seem to grasp it.
Here is what I would like to do, maybe someone can help with the code details.
The parts in red are the trouble areas.
PRI Alarm_Monitor
Repeat
waitpeq(|< AlarmActive, |< AlarmActive, 0)
set the pulse counter to 0
waitcnt(clkfreq*2 + cnt) ' wait ~ 2 seconds
if pulse counter > 0 ' pulses were counted, so change Mode
Mode := 1
If no pulses were counted, the loop simply repeats, waiting for the pin to go high. This runs in a separate cog by itself waiting for the AlarmActive pin to go high.
I've done some programming with SPIN and am working on a project and need some help. I don't know much about how to use the counters
that are available. I understand that there are two, and that they can use two pins to detect or send a signal.
Of all of the things listed that it can do in the Prop. manual, I'm looking to count pulses. I've tried to decipher the counter mode in table, 2-7 on
page 98, but just can't seem to grasp it.
Here is what I would like to do, maybe someone can help with the code details.
The parts in red are the trouble areas.
PRI Alarm_Monitor
Repeat
waitpeq(|< AlarmActive, |< AlarmActive, 0)
set the pulse counter to 0
waitcnt(clkfreq*2 + cnt) ' wait ~ 2 seconds
if pulse counter > 0 ' pulses were counted, so change Mode
Mode := 1
If no pulses were counted, the loop simply repeats, waiting for the pin to go high. This runs in a separate cog by itself waiting for the AlarmActive pin to go high.
Comments
The AlarmActive is one pin, and the pulses would be counted on another pin only if AlarmActive went high. No need to count them until then.
So the pulse_pin you show above would be that second pin? Do I need to set it as an input?
Also, does this code start counting as soon as the ctra := constant(%0_01010_000 << 23 | pulse_pin) is called?
Or can I wait until after the waitpeq has sensed a logic high?
Thank you, Daniel
Ynes[sic], this line enables the counter so it starts counting. But as frqa hasn't been set yet (default 0) it will only add 0. Once frqa is set to 1 (or any other non-null value) you will see change. So while the counter is armed, it doesn't really matter because you clear the accumulator (phsa) after you detect the high signal. Any (stray) accumulation until waitpeq is released will therefore be removed/ignored.
That is very clear.
I asked the second question because I was thinking about current draw, but not a deal breaker.
I've been looking through my books and can't find much on how to code the ctra :=
One instance shows ctra := %00100 << 26 as an example, and I can see how that moves everything over into the proper place.
But the constant and | pulse_pin you gave are not shown anywhere. How do they work?
Thank you, Daniel
Re: 23/26 shifts, yes the 5 mode bits start at location 26. That said, in assembler mode setup is usually done with a movi instruction which requires a value starting at 23. So just personal preference.
Thank you very much. I'm starting to get the hang of this stuff, and it's kinda fun too.
I referenced the manual and it is much clearer now.
Thanks again, Daniel
Thank you, Daniel