Rgb dimmers are most of the time dimming according figure 1. It’s simple code, just increase al the colors 1 step a time,  but the disadvange is that blue is ready first and red the last and that looks not that realistic. It would be neater when all 3 colors will start and finishing together as seen in figure 2.
This code will fade all colors together.
Timer3 is used to controle the 3 pwm channels.
Timer0 is the delay for the steps. It’s with this xtal and preload an delay of 20ms. If you are going to use a other crystal then you need recalculate the timer0 preload.

 

Syntax

Fade Red,Green,Blue,Steps

Remarks

 Red  Constant or variable with new RED value.
 Green  Constant or variable with new GREEN value.
 Blue  Constant or variable with new BLUE value.
 Steps  Number of steps to fade between old and new value

 

 

 

 

 

 

Dowload the code here 

See the code below in action;

 

$regfile = "m128def.DAT"
$crystal = 7372800
$baud = 19200


Config Timer3 = Pwm , Prescale = 64 , Pwm = 8 , Compare A Pwm = Clear Down , Compare B Pwm = Clear Down , Compare C Pwm = Clear Down
Config Timer0 = Timer , Prescale = 1024

On Ovf0 Rgbint
Enable Timer3
Enable Ovf0
Enable Interrupts
Start Timer3
Timer0 = &H70
Stop Timer0


Declare Sub Fade(byval Newrood As Byte , Byval Newgroen As Byte , Byval Newblauw As Byte , Byval Stappen As Byte)
Dim Staprood As Single
Dim Stapgroen As Single
Dim Stapblauw As Single
Dim Roodw As Single
Dim Blauww As Single
Dim Groenw As Single
Dim Stappen As Byte
Dim Temps As Single
Dim Tempint As Integer
Dim Stap2 As Byte

Rood Alias Compare3a
Blauw Alias Compare3b
Groen Alias Compare3c

Rood = 0
Blauw = 0
Groen = 0


Do
Fade 25 , 25 , 25 , 10
Wait 2
Fade 255 , 0 , 0 , 50
Wait 2
Fade 0 , 255 , 0 , 50
Wait 2
Fade 0 , 0 , 255 , 50
Wait 2
Fade 150 , 150 , 75 , 50
Wait 2
Fade 0 , 0 , 0 , 10
Wait 2
Fade 225 , 225 , 225 , 100
Wait 2
Loop




Sub Fade(newrood As Byte , Newgroen As Byte , Newblauw As Byte , Stappen As Byte )
Stap2 = Stappen
Roodw = Rood : Groenw = Groen : Blauww = Blauw
Temps = Newrood - Rood
Staprood = Temps / Stappen
Temps = Newgroen - Groen
Stapgroen = Temps / Stappen
Temps = Newblauw - Blauw
Stapblauw = Temps / Stappen
Start Timer0
End Sub


Rgbint:
Roodw = Roodw + Staprood
Groenw = Groenw + Stapgroen
Blauww = Blauww + Stapblauw
Tempint = Round(roodw)
Rood = Tempint
Tempint = Round(blauww)
Blauw = Tempint
Tempint = Round(groenw)
Groen = Tempint
Decr Stap2
If Stap2 = 0 Then
 Stop Timer0
End If
Timer0 = &H70
Return