PDA

View Full Version : Variable PWM on 2 Channels using software.



Tissy
- 18th May 2005, 13:36
I basically have two working routines and am having serious difficulty on the third.

One is a variable PWM on a single coloured LED, which when you push a button, the LED gets brighter and then dimmer. Just like a standard house light dimmer switch.

The Second is a RGB sequence using PWM and three LEDs.

The third i would like to vary the brightness of TWO LEDs at the same time. Same as the first routine just with TWO LEDs.

Now as the PIC16F628A can only handle PWM on one channel at a time, i have tried modifying the RGB sequence to perform this task to no avail. I am thinking i need to do it along the lines of an Array, but to be honest am getting it round my neck.

I have posted the two routines below, along with my attempt which kind of works, but not properly. The LED Dims on one LED but not the other and you can't see the level of dim until you release the switch.

There is a lot more to this full code, but hopefully this will be enqough to explain what i am trying to achieve. Can some offer help please? I have been working on this for weeks and its driving me mad !!

[Fading Single LED]
VariableSolidColour:
PWM onled,bright,10
If FuncSwitch=0 Then
While FuncSwitch = 0
If Flag = 0 Then
If Bright <255 Then Bright = Bright + 1
If bright = 255 Then Gosub flash
EndIf
If Flag = 1 Then
If Bright >0 Then Bright = Bright - 1
EndIf
PWM onled,Bright,10
WEnd
Flag = Flag + 1
EndIf
Return
[/Fading Single LED]

[RGB Sequence]
RGBSequence:
rainbowval=0
While (rainbowval !=255) and (getout=0)
for RGBDelay = 1 to 10
gosub RGBRainbow
next RGBDelay
rainbowval=rainbowval+1
Wend
While rainbowval and (getout=0)
for RGBDelay = 1 to 10
gosub RGBRainbow
next RGBDelay
rainbowval=rainbowval-1
wend
Return


RGBRainbow:
rbg=0
While (rbg<=2) and (getout=0)
section = ((rainbowval + ((rbg * 2)*delta))/ delta) // 6 ' this gives
' what section
' it is in

Select case section
case 0
rbgarray[rbg] = (rainbowval // delta)*6 ' this is how far it
' has gone in its
' section
case 1
rbgarray[rbg] = 255
case 2
rbgarray[rbg] = 255
case 3
rbgarray[rbg] = 255 - ((rainbowval //delta)*6)
case 4
rbgarray[rbg] = 0
case 5
rbgarray[rbg] = 0
case 6 'this one is for pics bad math
rbgarray[rbg] = 0
end select
rbg=rbg+1
wend
Gosub ledpwm
Return


ledPWM:
y=0
While (y<=2) and (getout=0)
Select Case y
Case 0
PWM red, rbgarray[y], 1
Gosub ResetLEDs
Case 1
PWM green, rbgarray[y], 1
Gosub ResetLEDs
case 2
PWM blue, rbgarray[y], 1
Gosub ResetLEDs
End Select
y=y+1
wend
Return
[/RGB Sequence]

[My Attempt]
VariableDualColour:
' PWM onled,Brightx,1
' PWM secondled,brightx,1
Gosub pwmdual
If FuncSwitch=0 Then
While FuncSwitch = 0
If Flag = 0 Then
If Bright <255 Then Bright = Bright + 1
If bright = 255 Then Gosub flash
EndIf
If Flag = 1 Then
If Bright >0 Then Bright = Bright - 1
EndIf
gosub pwmdual
' PWM onled,Brightx,1
' PWM secondled,brightx,1
WEnd
Flag = Flag + 1
EndIf
Return

PWMDual:
For z = 0 to 1
Select Case z
Case 0
pwm Onled, Bright, 10
case 1
pwm secondled, bright, 10
end select
next
Return
[/My Attempt]

Many thanks for any help,

Steve

Tissy
- 18th May 2005, 13:41
The Code didn't format properly, hopefully this will make it easier to read !!

Fading LED


VariableSolidColour:
PWM onled,bright,10
If FuncSwitch=0 Then
While FuncSwitch = 0
If Flag = 0 Then
If Bright <255 Then Bright = Bright + 1
If bright = 255 Then Gosub flash
EndIf
If Flag = 1 Then
If Bright >0 Then Bright = Bright - 1
EndIf
PWM onled,Bright,10
WEnd
Flag = Flag + 1
EndIf
Return


RGB Sequence


RGBSequence:
rainbowval=0
While (rainbowval !=255) and (getout=0)
for RGBDelay = 1 to 10
gosub RGBRainbow
next RGBDelay
rainbowval=rainbowval+1
Wend
While rainbowval and (getout=0)
for RGBDelay = 1 to 10
gosub RGBRainbow
next RGBDelay
rainbowval=rainbowval-1
wend
Return


RGBRainbow:
rbg=0
While (rbg<=2) and (getout=0)
section = ((rainbowval + ((rbg * 2)*delta))/ delta) // 6 ' this gives
' what section
' it is in

Select case section
case 0
rbgarray[rbg] = (rainbowval // delta)*6 ' this is how far it
' has gone in its
' section
case 1
rbgarray[rbg] = 255
case 2
rbgarray[rbg] = 255
case 3
rbgarray[rbg] = 255 - ((rainbowval //delta)*6)
case 4
rbgarray[rbg] = 0
case 5
rbgarray[rbg] = 0
case 6 'this one is for pics bad math
rbgarray[rbg] = 0
end select
rbg=rbg+1
wend
Gosub ledpwm
Return


ledPWM:
y=0
While (y<=2) and (getout=0)
Select Case y
Case 0
PWM red, rbgarray[y], 1
Gosub ResetLEDs
Case 1
PWM green, rbgarray[y], 1
Gosub ResetLEDs
case 2
PWM blue, rbgarray[y], 1
Gosub ResetLEDs
End Select
y=y+1
wend
Return


My Attempt


VariableDualColour:
' PWM onled,Brightx,1
' PWM secondled,brightx,1
Gosub pwmdual
If FuncSwitch=0 Then
While FuncSwitch = 0
If Flag = 0 Then
If Bright <255 Then Bright = Bright + 1
If bright = 255 Then Gosub flash
EndIf
If Flag = 1 Then
If Bright >0 Then Bright = Bright - 1
EndIf
gosub pwmdual
' PWM onled,Brightx,1
' PWM secondled,brightx,1
WEnd
Flag = Flag + 1
EndIf
Return

PWMDual:
For z = 0 to 1
Select Case z
Case 0
pwm Onled, Bright, 10
case 1
pwm secondled, bright, 10
end select
next
Return


Many thanks for any help,

Steve

cosmomen
- 22nd September 2006, 15:58
Hi,

I have the same problem . any progress regarding this dual LED fading issue?
Thank you!

Doormatt
- 23rd September 2006, 01:34
If that's all your PIC is doing, and you don't mind doing it the easy way, then take a look at this.

http://www.pbpgroup.com/modules/wfsection/article.php?articleid=12

That'll do exactly what you need, all in software. (You just won't have many cycles left over for other things).