; copyright 2008 all rights reserved

red equ 10h ; register 10h will be red value 8 bits, same for green and blue
green equ 11h 
blue equ 12h  

cred equ 13h
cgreen equ 14h
cblue equ 15h


dval equ 16h ; register 16h will be a counter 8 bits etc.

ct1 equ 17h
ct2 equ 18h

BSF          03h,5           ; bank 1
MOVLW   b'00000000'          ; port rb to output 
MOVWF   86h                   
BCF          03h,5           ; and back to bank 0

MOVLW   b'00000000'          ; port rb to low
MOVWF   06  


mainloop


movlw d'255' ; set cred, cblue and cgreen to 2
movwf cred
movlw d'1'
movwf cgreen
movlw d'1'
movwf cblue

movlw d'30' ; set speed
movwf dval

call fadegreenup   ; rg
call delayloop     
call green2blue    ;rb
call delayloop
call delayloop
call red2green     ;bg
call delayloop
call fadebluedown   ;g
call green2blue     ;b
call delayloop
call delayloop
call blue2red       ;r
call fadegreenup     ;rg
call fadereddown ;g
call green2purple  ; rb
call delayloop
call delayloop
call delayloop
call fadebluedown ;r
call red2yellow ;gb
call delayloop
call faderedup ;rgb
call fadegreendown ;rb
call fadebluedown ;r
call delayloop 
call red2green ;g
call green2purple ;rb
call fadereddown ;b
call delayloop   
call blue2red
call delayloop
call fadegreenup   ; rg
call delayloop     
call green2blue    ;rb
call delayloop
call delayloop
call red2green     ;bg
call delayloop
call fadebluedown   ;g
call green2blue     ;b
call blue2red
call delayloop
call red2yellow ;gb
call delayloop
call faderedup ;rgb
call fadegreendown ;rb
call fadebluedown ;r
call delayloop 
call red2green ;g
call green2purple ;rb
call fadereddown ;b
call delayloop   
call blue2red
call delayloop
call delayloop
call fadegreenup   ; rg
call delayloop     
call green2blue    ;rb
call red2green     ;bg
call delayloop
call fadebluedown   ;g
call green2blue     ;b
call blue2red
call delayloop
goto mainloop

; sub routines start here

faderedup
movf cred,W ; move cred to red, cblue to blue etc. 
movwf red
movf cblue,W
movwf blue
movf cgreen,W
movwf green
movf dval,W
movwf ct2
call delayloop ;call delayloop, delays by value of ct2
INCFSZ cred,1 ; increase cred by 1, skip next command if 0
goto faderedup
return

fadereddown
movf cred,W ; move cred to red, cblue to blue etc. 
movwf red
movf cblue,W
movwf blue
movf cgreen,W
movwf green
movf dval,W
movwf ct2
call delayloop ;call delayloop, delays by value of ct2
decFSZ cred,1 ; decrease cred by 1, skip next command if 0
goto fadereddown
movlw d'1' ; set cred to 1
movwf cred
return
 
fadeblueup
movf cred,W ; move cred to red, cblue to blue etc. 
movwf red
movf cblue,W
movwf blue
movf cgreen,W
movwf green
movf dval,W
movwf ct2
call delayloop ;call delayloop, delays by value of ct2
INCFSZ cblue,1 ; increase cred by 1, skip next command if 0
goto fadeblueup
return

fadebluedown
movf cred,W ; move cred to red, cblue to blue etc. 
movwf red
movf cblue,W
movwf blue
movf cgreen,W
movwf green
movf dval,W
movwf ct2
call delayloop ;call delayloop, delays by value of ct2
decFSZ cblue,1 ; decrease cred by 1, skip next command if 0
goto fadebluedown
movlw d'1' ; set cblue to 1
movwf cblue
return

fadegreenup
movf cred,W ; move cred to red, cblue to blue etc. 
movwf red
movf cblue,W
movwf blue
movf cgreen,W
movwf green
movf dval,W
movwf ct2
call delayloop ;call delayloop, delays by value of ct2
INCFSZ cgreen,1 ; increase cred by 1, skip next command if 0
goto fadegreenup
return

fadegreendown
movf cred,W ; move cred to red, cblue to blue etc. 
movwf red
movf cblue,W
movwf blue
movf cgreen,W
movwf green
movf dval,W
movwf ct2
call delayloop ;call delayloop, delays by value of ct2
decFSZ cgreen,1 ; decrease cred by 1, skip next command if 0
goto fadegreendown
movlw d'1' ; set cgreen to 1
movwf cgreen
return

red2green
movf cred,W ; move cred to red, cblue to blue etc. 
movwf red
movf cblue,W
movwf blue
movf cgreen,W
movwf green
movf dval,W
movwf ct2
call delayloop ;call delayloop, delays by value of ct2
incf cgreen,1
decFSZ cred,1 ; decrease cred by 1, skip next command if 0
goto red2green
movlw d'1' ; set cred to 1
movwf cred
movlw d'255' ; set cgreen to 255
movwf cgreen
return

green2blue
movf cred,W ; move cred to red, cblue to blue etc. 
movwf red
movf cblue,W
movwf blue
movf cgreen,W
movwf green
movf dval,W
movwf ct2
call delayloop ;call delayloop, delays by value of ct2
incf cblue,1
decFSZ cgreen,1 ; decrease cred by 1, skip next command if 0
goto green2blue
movlw d'1' ; set cgreen to 1
movwf cgreen
movlw d'255' ; set cblue to 1
movwf cblue
return

blue2red
movf cred,W ; move cred to red, cblue to blue etc. 
movwf red
movf cblue,W
movwf blue
movf cgreen,W
movwf green
movf dval,W
movwf ct2
call delayloop ;call delayloop, delays by value of ct2
incf cred,1
decFSZ cblue,1 ; decrease cred by 1, skip next command if 0
goto blue2red
movlw d'1' ; set cblue to 1
movwf cblue
movlw d'255' ; set cred to 255
movwf cred
return

green2purple
movf cred,W ; move cred to red, cblue to blue etc. 
movwf red
movf cblue,W
movwf blue
movf cgreen,W
movwf green
movf dval,W
movwf ct2
call delayloop ;call delayloop, delays by value of ct2
incf cred,1
incf cblue,1
decFSZ cgreen,1 ; decrease cred by 1, skip next command if 0
goto green2purple
movlw d'1' ; set cblue to 1
movwf cgreen
movlw d'255' ; set cred to 255
movwf cred
movlw d'255' ; set cred to 255
movwf cblue
return

red2yellow
movf cred,W ; move cred to red, cblue to blue etc. 
movwf red
movf cblue,W
movwf blue
movf cgreen,W
movwf green
movf dval,W
movwf ct2
call delayloop ;call delayloop, delays by value of ct2
incf cgreen,1
incf cblue,1
decFSZ cred,1 ; decrease cred by 1, skip next command if 0
goto red2yellow
movlw d'1' ; set cblue to 1
movwf cred
movlw d'255' ; set cred to 255
movwf cblue
movlw d'255' ; set cred to 255
movwf cgreen
return

delayloop
call rgbrefresh
decfsz ct2,1
goto delayloop
return

rgbrefresh
movlw b'111' ; setting pin 0,1,2 to high
movwf 06h
mainpwmloop ; defining the name (for jumps etc.)
decfsz blue,1 ; reduce red value by 1, skip next command if result is 0. 
goto skip1 ; skip next command
BCF 06h,0 ; turn off port rb0 
skip1 
decfsz red,1 ; reduce red value by 1, skip next command if result is 0. 
goto skip2 ; jump to skip2
BCF 06h,1  ; turn off port rb1 
skip2
decfsz green,1 ; reduce red value by 1, skip next command if result is 0. 
goto skip3 ; jump to skip3
BCF 06h,2 ; turn off port rb2
skip3
decfsz ct1,1 ; reduce counter by one, if afterwards zero will skip next command
goto mainpwmloop ; jump mainpwmloop
return


end







