Shop OBEX P1 Docs P2 Docs Learn Events
How to Duty Cycle More Than 1 Pin with Counter? — Parallax Forums

How to Duty Cycle More Than 1 Pin with Counter?

one-zeroone-zero Posts: 5
edited 2013-03-22 09:38 in Propeller 1
I'm new to the forum and relatively new to the Propeller. I'm stumped on a piece of code. I have a group of 8 LEDs connected to 8 conescutive Propeller I/O pins. I want to flash the 8 LEDs in various patterns. One of the desired patterns is to fade all of them in and out all at once. I was going to use one of the counters to increase/decrease the duty cycle as shown in the LedDutySweep.spin code that comes with the PE Kit:
CON
scale = 16_777_216 ' 2³²÷ 256

PUB TestDuty | pin, duty, mode
'Configure counter module.
ctra[30..26] := %00110 ' Set ctra to DUTY mode
ctra[5..0] := 4 ' Set ctra's APIN
frqa := duty * scale ' Set frqa register
'Use counter to take LED from off to gradually brighter, repeating at 2 Hz.
dira[4]~~ ' Set P5 to output
repeat ' Repeat indefinitely
repeat duty from 0 to 255 ' Sweep duty from 0 to 255
frqa := duty * scale ' Update frqa register
waitcnt(clkfreq/128 + cnt) ' Delay for 1/128th s


The problem I'm having is that in bits 5:0 of ctra, you can only specify one pin for the counter to control. How do a get the counter to fade the entire group of LEDs all at once? The only solution I could think of is to use 8 counters spanning 4 COGs with each one controlling a single pin. I have done that and it works but it chews up a bunch of COGs that I need for other tasks.

Any ideas?

Comments

  • Bobb FwedBobb Fwed Posts: 1,119
    edited 2013-03-22 09:04
    You can control a second LED using CTRB/FRQB
    But there are only 2 counters per cog. If you want to do more, you have to move on to a dedicated cog programmed to do so. I have a couple objects that do this.

    http://obex.parallax.com/objects/422/ - control up to 8 pins
    http://obex.parallax.com/objects/506/ - control up to 31 pins

    To change the duty in these objects "on-the-fly", change the value sent in the parameter, then call the start method again.
    I have some dedicated on-the-fly object that I don't have in the OBEX that can change the duty (and frequency) of the pin without restarting the cog (though at visually pleasing frequencies ~60-120Hz, it won't be an issue to use the above objects).


    Also, for future reference, please use the [ code][ /code] tags when posting code, because SPIN is format-dependent in it's functionality, it is difficult (if not impossible) to understand what is happening in your code without it.
  • one-zeroone-zero Posts: 5
    edited 2013-03-22 09:38
    Thanks for the pointer to the objects. I'm looking them over now.

    I used the system monospaced font to try and format the code correctly but obviously that didn't work. That's good to know about the
    
    tags.  I will use them in the future.                        
Sign In or Register to comment.