hippy
Ex-Staff (retired)
This code demonstrates controlling the brightness of two LED's independently with one PWMOUT pin. It multiplexes the LED's, in this case a three-leg LED, tri-colour common cathode. PWM the cathode, raise one of the LED anodes high, repeat for next. Should also work for RGB.
This is for the 20M2, coded so I could plug the LED R-0V-G straight into the socket of an AXE091 alongside the C.1-C.2-C.3 pins with no resistors. Note that because it's PWM'ing the 0V, duty of 0 is full on, 255 is off.
This is for the 20M2, coded so I could plug the LED R-0V-G straight into the socket of an AXE091 alongside the C.1-C.2-C.3 pins with no resistors. Note that because it's PWM'ing the 0V, duty of 0 is full on, 255 is off.
Code:
#Picaxe 20M2
#No_Data
Symbol redLevel = w0
Symbol redLevel.msb = b1
Symbol redFadeUp = b11
Symbol grnLevel = w1
Symbol grnLevel.msb = b3
Symbol grnFadeUp = b13
SetFreq M16
Low C.1 ; LED Red - Anode
Low C.2 ; LED 0V - Cathode (PWM'd)
Low C.3 ; LED Grn - Anode
redLevel = $FFFF : redFadeUp = 0
grnLevel = $FFFF : grnFadeUp = 0
PwmOut C.2, 63, 0
Do
PwmDuty C.2, redLevel.msb : PulsOut C.1, 100
PwmDuty C.2, grnLevel.msb : PulsOut C.3, 100
If redFadeUp = 1 Then
redLevel = redLevel + $10 : If redLevel.msb = $FF Then : redFadeUp = 0 : End If
Else
redLevel = redLevel - $10 : If redLevel.msb = $00 Then : redFadeUp = 1 : End If
End If
If grnFadeUp = 1 Then
grnLevel = grnLevel + $18 : If grnLevel.msb = $FF Then : grnFadeUp = 0 : End If
Else
grnLevel = grnLevel - $18 : If grnLevel.msb = $00 Then : grnFadeUp = 1 : End If
End If
Loop