Shop OBEX P1 Docs P2 Docs Learn Events
counter repeat — Parallax Forums

counter repeat

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

Comments

  • Without the repeat, the cog would terminate.The repeat just keeps it alive.

    -Phil
  • Heater.Heater. Posts: 21,230
    edited 2016-08-02 15:42
    That is a top level object you have presented. So what happens?

    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.

  • Aahhhh... So the repeat at the end of the code does not repeat the code above, it repeats the next section of code which in this case is nothing..
    So for proper timing, do I need to forgo other code on the cog?

    (I guess I need to figure out code tags..)
  • To insert code in your post, paste it between the code tags generated when you press the "C" button (in the list of icons above the text box as you're creating a post).
  • dherde wrote: »
    So for proper timing, do I need to forgo other code on the cog?

    Nope, the cog can continue to run real code. The repeat is there just to keep the cog alive.

  • In some cases you'll see a repeat loop that reloads one or both phsx registers, and has a waitcnt at the end. This is used for fixed-frequency, variable duty cycle PWM.
  • Also, to minimize power consumption, you could include a waitcnt(0) inside the repeat loop. This will stall execution (and power consumption) for every 53 seconds or so.

    -Phil
  • ElectrodudeElectrodude Posts: 1,657
    edited 2016-08-02 21:44
    Also, to minimize power consumption, you could include a waitcnt(0) inside the repeat loop. This will stall execution (and power consumption) for every 53 seconds or so.

    -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.
Sign In or Register to comment.