;Jeff Mucha
;pic microcontroler code for exercising multi color LEDs
;Porta bits 0 1 2 are inputs where active low switches are attached.
;proper debouncing is required.
;portb is pulse width modulated to provide n levels of output drive to the LEDs

	list	p=16f84
	radix	hex
;main

#define	width 	0x0d

f	equ	0x01
porta	equ	0x05
portb	equ	0x06
count	equ	0x0c
status	equ	0x03
countr	equ	0x20	;counter red
countg  equ	0x21	;counter green
countb	equ	0x22	;counter blue


pwm1	equ	0x10
pwm2	equ	0x11
pwm3	equ	0x12
pwm4	equ	0x13
pwm5	equ	0x14
pwm6	equ	0x15
pwm7	equ	0x16
pwm8	equ	0x17
pwm9	equ	0x18
pwm10	equ	0x19
pwm11	equ	0x1a
pwm12	equ	0x1b
pwm13	equ	0x1c

	org	0x000
start	movlw	0x00
	tris	portb
	movlw	0xff
	tris	porta

	movlw	b'00000111'
	movwf	pwm1
	movwf	pwm2
	movwf	pwm3
	movwf	pwm4
	movwf	pwm5
	movwf	pwm6
	movwf	pwm7
	movwf	pwm8
	movwf	pwm9
	movwf	pwm10
	movwf	pwm11
	movwf	pwm12

	movlw	width  ; 0x09
	movwf	count
	movwf	countr
	movwf	countg
	movwf	countb
	
loopa	movlw	0x07			;port a bits 0,1,2 set high means no buttons pushed	
	subwf	porta,w			;compare w to port a using the subtract and then z flag check
	btfss	status,2
	goto	select


	movf	pwm12,w
	movwf	portb
	movf	pwm11,w
	movwf	portb
	movf	pwm10,w
	movwf	portb
	movf	pwm9,w
	movwf	portb

	movf	pwm8,w
	movwf	portb	
	movf	pwm7,w
	movwf	portb
	movf	pwm6,w
	movwf	portb
	movf	pwm5,w
	movwf	portb
	movf	pwm4,w
	movwf	portb
	movf	pwm3,w
	movwf	portb
	movf	pwm2,w
	movwf	portb
	movf	pwm1,w
	movwf	portb

	goto	loopa	


select	btfss	porta,0	
	call	setr
	btfss	porta,1	
	call	setg
	btfss	porta,2	
	call	setb
	goto	loopa



setr	decfsz	countr
	goto	setr2
	goto	resetr
	
;	movwf	countr

setr2	movlw	0x0F		;this sets the bit for the value of the counter
	addwf	countr,w
	movwf	0x04
	bcf	0x00,0		;sets the red bit

testrl	btfss	porta,0		
	goto	testrl
	return

 
 


resetr	movlw	width	;0x09
	movwf	count

rloopr	movlw	0x0F		
	addwf	count,w
	movwf	0x04
	bsf	0x00,0		;clears red bit

	decfsz	count
	goto	rloopr
	movlw	width    ; 0x09
	movwf	countr
	goto	testrl






setg	decfsz	countg
	goto	setg2
	goto	resetg
	
;	movwf	countg

setg2	movlw	0x0F		
	addwf	countg,w
	movwf	0x04
	bcf	0x00,1		;set the green bit

testgl	btfss	porta,1		
	goto	testgl
	return

 
 


resetg	movlw	width	;0x09
	movwf	count

rloopg	movlw	0x0F		
	addwf	count,w
	movwf	0x04
	bsf	0x00,1		;clear the green bit

	decfsz	count
	goto	rloopg
	movlw	width    ; 0x09
	movwf	countg
	goto	testgl









setb	decfsz	countb
	goto	setb2
	goto	resetb
	
;	movwf	countb

setb2	movlw	0x0F		
	addwf	countb,w
	movwf	0x04
	bcf	0x00,2		;set the blue bit

testbl	btfss	porta,2		
	goto	testbl
	return

 
 


resetb	movlw	width	;0x09
	movwf	count

rloopb	movlw	0x0F		
	addwf	count,w
	movwf	0x04
	bsf	0x00,2		;clear the blue bit

	decfsz	count
	goto	rloopb
	movlw	width    ; 0x09
	movwf	countb
	goto	testbl















	

	end
