counter repeat
dherde
Posts: 13
in Propeller 1
I see in all of the code samples someone setting up ctra for a purpose, putting values in the frqa, phsa, etc registers, maybe set an output, then put in a repeat statement after all this which apparently is necessary for accumulation. (see demo code below)
How much of this is actually necessary?
Is it necessary to reload everything in order for the counter to advance?
It would seem that once the registers are initialized the counter should start running in whatever mode it is programmed in due to the CLK pulse.
Why do we need to reload the counter control register and all that?
What is the minimum overhead we need for the counter advance (in addition to the CLK pulse)?
Thanks..
David
''Demonstration of NCO counter mode (%00100)
CON
_clkmode = xtal1 + pll16x
_xinfreq = 5_000_000
led_a = 17
PUB NCO_PWM_single_ended_mode
' mode PLL Divider BPIN APIN
ctra := (%00100 << 26) + (%000 << 23) + (0 << 9) + led_a 'Establish mode and APIN (BPIN is ignored)
frqa := 48 'Set FRQA so PHS[31] toggles
dira[led_a] := 1 'Set APIN to output
repeat 'infinite loop, so counter continues to run
How much of this is actually necessary?
Is it necessary to reload everything in order for the counter to advance?
It would seem that once the registers are initialized the counter should start running in whatever mode it is programmed in due to the CLK pulse.
Why do we need to reload the counter control register and all that?
What is the minimum overhead we need for the counter advance (in addition to the CLK pulse)?
Thanks..
David
''Demonstration of NCO counter mode (%00100)
CON
_clkmode = xtal1 + pll16x
_xinfreq = 5_000_000
led_a = 17
PUB NCO_PWM_single_ended_mode
' mode PLL Divider BPIN APIN
ctra := (%00100 << 26) + (%000 << 23) + (0 << 9) + led_a 'Establish mode and APIN (BPIN is ignored)
frqa := 48 'Set FRQA so PHS[31] toggles
dira[led_a] := 1 'Set APIN to output
repeat 'infinite loop, so counter continues to run
Comments
-Phil
1) The Propeller boot's up from reset.
2) It loads the code.
3) It runs the top level object.method. In this case NCO_PWM_single_ended_mode
4) If that method ever exits the COG running it stops. Counters and all. Your program is dead. The Prop is asleep.
So, by putting a repeat at the end you ensure the thing runs forever.
Edit: Not that the "repeat" at the end is not reloading anything. It's just running around in a loop doing nothing.
Also, when posting code to the forum do remember to use the code tags so that it is displayed correctly.
So for proper timing, do I need to forgo other code on the cog?
(I guess I need to figure out code tags..)
Nope, the cog can continue to run real code. The repeat is there just to keep the cog alive.
-Phil
That is, if you actually don't want to run any code in that cog, which you can do without interfering with the counters. Each cog's two counters have their own hardware that runs independently of their cog. But it's generally a waste to have a cog do nothing and only be running so its counters can run.