Code:
list p=16F628A
#include <p16F628A.inc>
__CONFIG _CP_OFF & _WDT_OFF & _PWRTE_ON & _INTOSC_OSC_NOCLKOUT & _LVP_OFF
errorlevel -302 ; supress banksel warning messages during assembly
errorlevel -311 ; supress HIGH operator warning messages during assembly
cblock 0x20
copyPORTA ;Declare variables used in the program
copyPORTB
FrameCount
LoopCount
Color1
Color2
Color3
endc
#define bank0 bcf STATUS,RP0
#define bank1 bsf STATUS,RP0
RESET_VECTOR org 0x000
START
movlw b'00000111' ;Set all ports as outputs
movwf CMCON ;Disable comparators
bank1
clrf TRISA
clrf TRISB
bank0
;-----------------------------------------------------------------------------------
; Main Program and Subroutines
;-----------------------------------------------------------------------------------
Sequence
call MegaColor
goto Sequence
;---------------------------------------------
MegaColor
movlw b'00010000'
movwf Color1
movlw b'01000000'
movwf Color2
movlw b'10000000'
movwf Color3
call Rainbow
;
movlw b'00010000'
movwf Color1
movlw b'01000000'
movwf Color2
movlw b'10000000'
movwf Color3
call Rainbow
;
movlw b'00010000'
movwf Color1
movlw b'01000000'
movwf Color2
movlw b'10000000'
movwf Color3
call Rainbow
;
movlw b'00010000'
movwf Color1
movlw b'11010000'
movwf Color2
movlw b'10010000'
movwf Color3
call Rainbow
;
movlw b'01010000'
movwf Color1
movlw b'00010000'
movwf Color2
movlw b'01010000'
movwf Color3
call Rainbow
;
movlw b'00010000'
movwf Color1
movlw b'01010000'
movwf Color2
movlw b'00010000'
movwf Color3
call Rainbow
;
movlw b'00010000'
movwf Color1
movlw b'01010000'
movwf Color2
movlw b'10010000'
movwf Color3
call Rainbow
;
movlw b'00010000'
movwf Color1
movlw b'01010000'
movwf Color2
movlw b'01000000'
movwf Color3
call Rainbow
;
movlw b'00010000'
movwf Color1
movlw b'01000000'
movwf Color2
movlw b'10000000'
movwf Color3
call Rainbow
;
movlw b'10010000'
movwf Color1
movlw b'11010000'
movwf Color2
movlw b'01000000'
movwf Color3
call Rainbow
;
movlw b'10000000'
movwf Color1
movlw b'11010000'
movwf Color2
movlw b'00010000'
movwf Color3
call Rainbow
;
movlw b'00010000'
movwf Color1
movlw b'11010000'
movwf Color2
movlw b'01000000'
movwf Color3
call Rainbow
;
movlw b'01000000'
movwf Color1
movlw b'11010000'
movwf Color2
movlw b'10000000'
movwf Color3
call Rainbow
;
return
;----------------------------------
Rainbow
call Frame1
Return
;--------------------------------------------------------------
Frame1 ;Each section modulates R,G,B
movlw d'160'
movwf FrameCount
FR1 call C1
call Sec1 ;Individual sections are shared by R, G,B calls
call C2
call Sec2
call C3
call Sec3
decfsz FrameCount,F
goto FR1
return
;--------------------------------------------------------------
Sec1
movlw d'100'
movwf LoopCount
loop1
movlw b'00001111' ;Values for Anodes (common) placed in B0-B7 & A0-A3
movwf copyPORTB
movlw b'00001110'
call Output
movlw b'00000111'
movwf copyPORTB
movlw b'00001100'
call Output
movlw b'00000011'
movwf copyPORTB
movlw b'00001000'
call Output
movlw b'00000001'
movwf copyPORTB
movlw b'00000000'
call Output
decfsz LoopCount,F
goto loop1
return
;---------------------------------------------
Sec2
movlw d'100'
movwf LoopCount
loop2
movlw b'11111110'
movwf copyPORTB
movlw b'00000000'
call Output
movlw b'01111100'
movwf copyPORTB
movlw b'00000000'
call Output
movlw b'00111000'
movwf copyPORTB
movlw b'00000000'
call Output
movlw b'00010000'
movwf copyPORTB
movlw b'00000000'
call Output
decfsz LoopCount,F
goto loop2
return
;---------------------------------------------
Sec3
movlw d'100'
movwf LoopCount
loop3
movlw b'11100000'
movwf copyPORTB
movlw b'00001111'
call Output
movlw b'11000000'
movwf copyPORTB
movlw b'00000111'
call Output
movlw b'10000000'
movwf copyPORTB
movlw b'00000011'
call Output
movlw b'00000000'
movwf copyPORTB
movlw b'00000001'
call Output
decfsz LoopCount,F
goto loop3
return
;-----------------------------------------------------------------------------------------
C1
movfw Color1 ;Sets the colors used in 3 chan, R,G,B
movwf copyPORTA ;Individual color for each section is stored
return ;in copyPORTA and added to anode bits (A0-A3)at "OUTPUT" call
;---------------------------------------------
C2
movfw Color3
movwf copyPORTA
return
;---------------------------------------------
C3
movfw Color2
movwf copyPORTA
return
;---------------------------------------------
Output
addwf copyPORTA,0 ;content of w from SEC# is added to copyPORTA which has color (Ex: 10000000 = Blue)
movwf PORTA ;the result of addition is placed in w and then transfered to PORTA directly
movfw copyPORTB
movwf PORTB
return
;---------------------------------------------
end
Here is the introduction of another code called: A12-Fix3Color
This may help clarify how ColorRot works. Fix3Color: Fix Static 3 colors faded into each other across 12 channels. (Same Circuit Layout, PIC, and Pin-out).
Basically this code is one Frame of ColorRot. Remember 12 Frames made the entire movie (ColorRot) which rotates 12 colors across 12 channels. In this case it is only 1 Frame so there is no movement, yet you have smooth color fading between 3 initial colors given in the begining of the program Fix3Color. Here is how it works:
Sequence: It is just a simple loop to have the program run over and over infinately. This calls the color generation subroutine called MegaColor. In MegaColor, user gives program 3 primary colors. (again you have the same selection of colors to choose from: R, RG, G, GB, B, BR, W, BLK). Colors are placed in 3 holders called C1, C2, C3. Program uses these colors to morph in between. When one complete cycle is completed, program comes back to MegaColor and picks another set of 3 colors and runs the program again. MegaColor calls on main routine called Rainbow. Rainbow calls on the Frame1. Frame1: Consists of 3 main Sections for 3 colors (C1, C2, C3). If primary colors used are Red, Green, Blue, then you have colors of rainbow across 12 channels. What does "call C1" do? It places the first color in copyPORTA (upper 4 bits). 1011-xxxx which is A7,A6, 0, A4-oooo. 4 right bits are used in the program and are set to zero at this point. At a later point we are adding the color to PortA and along with PortB we send them out to Output for display. We are still in the Frame1 subroutine; so we called on the C1 and placed it in Color1 then we tell it to go to Section1. In here data bits for PortB and PortA are set. Remember colors are in upper 4 bits of PortA. So we have 4 lower bits of PortA and 8 bits of PortB to place our data in which drives the anodes of RGB. While A7, A6, A4 sink the Cathodes thru transistors and run RGB. Section1: Consists of 4 sets [of two lines (PortA and PortB addresses) and an Output call]. 4 sets of lines create resolution or brightness of individual pins. Repeat bit 4 times (4 times "1" in each pin on each line) down the text and you have maximum brightness. Each time we give a value to PortA and PortB, we call the Output. We do this so rapidly that program sees the 4 lines as one unit average brightness across 12 channels. Output: Remember we placed our color in upper 4-bit of Color1 (as an example). Now we simply add the color to the individual data for PortA and push it out (data for PortA is read into W before Output call and then added to color in the Output routine). We read the data from PortB and push it out as well and now we have lit the appropriate LED and its associated color.
Another way of looking at it: we scan color1 across 12 channels, then scan color2 across 12 channels, then scan color3 across 12 channels. We do this so rapdily that we see each pin having a specific color mix and intensity. This creates a beautiful and smooth transition.