Shop OBEX P1 Docs P2 Docs Learn Events
ctra and ctrb — Parallax Forums

ctra and ctrb

mikeamikea Posts: 283
edited 2015-02-17 18:00 in Propeller 1
Hi, I'm trying to scale 2 of the 3 pins of an rgb led with ctra, and ctrb. I have code that works from the pe kit for ctra in the first 255 to 0 loop which works great except pin 5 (ctrb is always on). The second loop for pin 5 using ctrb results in both pins 4 and 5 on, but no scaling. I was trying to scale them one at a time. I've read the section of the prop manual about counters, but not sure where I'm going wrong. It looks like for the third led I need to open a new cog for more counters. Thanks
CON
        _clkmode = xtal1 + pll16x                                               'Standard clock mode * crystal frequency = 80 MHz
        _xinfreq = 5_000_000

''LedDutySweep.spin
''Cycle P4 LED from off, gradually brighter, full brightness.
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

ctrb[30..26] := %00110
ctrb[14..9]:=5
frqb := duty * scale

Use counter to take LED from off to gradually brighter, repeating at 2 Hz.
dira[4..5]~~ ' Set P5,4 to output
repeat ' Repeat indefinitely
   repeat duty from 255 to 0 ' Sweep duty from 0 to 255
     frqa := duty * scale ' Update frqa register
     waitcnt(clkfreq/128 + cnt) ' Delay for 1/128th s 
     
  repeat duty from 255 to 0 
     frqb := duty * scale 
     waitcnt(clkfreq/128 + cnt)

Comments

  • JonnyMacJonnyMac Posts: 9,105
    edited 2015-02-17 15:42
    I haven't found direct control the counters (i.e., outside a control loop) useful for LED control. The reason is that you only have control of duty cycle, not frequency, and when the duty cycle is near 50% the frequency is so high that there is sometimes not enough time to properly light the LED. I would suggest you create a Spin cog that allows you to control the counters at a specific frequency. Launch this twice and you can control four LEDs.
  • ChrisGaddChrisGadd Posts: 310
    edited 2015-02-17 16:30
    You've got pin 5 assigned to counter b's b-pin rather than the a-pin. The B-pin output doesn't have any use in single-ended duty mode. Change ctrb[14..9]:=5 to ctrb[5..0]:=5, just like you set up counter A.

    Do you have the outputs connected directly to the LED? I tested this using a generic red LED and it worked, though the brightness seemed to change rather abruptly in a few places. Using a low-pass filter to create a digital-to-analog converter resulted in a much smoother transition, though the range was also reduced somewhat.
  • mikeamikea Posts: 283
    edited 2015-02-17 17:15
    I do have the outputs directly connected to leds through a resistor. Thanks for clearing that up with the b-pin, I changed [14..9] to [5..0] and it worked.
  • JonnyMacJonnyMac Posts: 9,105
    edited 2015-02-17 18:00
    It will be interesting to see how the LEDs respond -- they might work just fine. In practice I wasn't happy with direct control so I used a code loop to provide freqency AND duty-cycle control (it takes a cog but it's worth it).

    If you have a cog to spare, here's a very simple embedded RGB LED controller. Paste this into your program and then call start_rgb() with your pins.
    var
    
      long  rgbcog
      long  rgbstack[16]
      
      long  red
      long  green
      long  blue
    
    
    pri start_rgb(rpin, gpin, bpin)
    
      if (rgbcog)
        cogstop(rgbcog - 1)
    
      rgbcog := cognew(run_rgb(rpin, gpin, bpin), @rgbstack) + 1
    
    
    pri run_rgb(rpin, gpin, bpin) | racc, gacc, bacc
    
      dira[rpin] := 1
      dira[gpin] := 1
      dira[bpin] := 1
    
      repeat
        racc += red                                         ' update accumlator
        outa[rpin] := (racc > 255)                          ' move carry to output
        racc &= $FF                                         ' clear carry
    
        gacc += green
        outa[gpin] := (gacc > 255)   
        gacc &= $FF
    
        bacc += blue
        outa[bpin] := (bacc > 255)   
        bacc &= $FF
    
Sign In or Register to comment.