
;****************************************************
;RGB LED Dice.asm                                     *
;  *
;                                        *
;2-6-2011                                          *
;****************************************************

	list	p=12F629
	radix	dec
	include	"p12f629.inc"
	
		errorlevel	-224	; Don't complain about tris
		errorlevel	-302	; Don't complain about BANK 1 Registers


	__CONFIG	_MCLRE_OFF & _CP_OFF & _WDT_OFF & _INTRC_OSC_NOCLKOUT  ;Internal osc.

;_MCLRE_OFF  - master clear must be off for gp3 to work as input pin 

;****************************************************************
; variables - names and files
;****************************************************************


temp1		equ 20h	;
temp2		equ 21h	;
temp3		equ 22h	;
temp4		equ	23h	; 
jump		equ	24h	;jump value for table1
fadeUp		equ	25h
fadeDwn		equ	26h
sequences	equ	27h
sw_duration	equ 28h
testing		equ 29h
loops		equ 2Ah ;


;****************************************************************
;Equates
;****************************************************************
status		equ	0x03
rp1			equ	0x06
rp0			equ	0x05
GPIO 		equ 0x05
			

status		equ	03h
option_reg	equ 81h


		; bits on GPIO
				
pin7		equ	0	;GP0  
pin6	 	equ	1	;GP1  
pin5		equ	2	;GP2  
pin4		equ	3	;GP3  "Roll Dice" Sw  
pin3		equ	4	;GP4  
pin2		equ	5	;GP5  "Effects" Sw and piezo   
 

		;bits
				
rp0			equ	5			;bit 5 of the status register



;****************************************************************
;Beginning of program
;****************************************************************
		org		0x00
		nop
		nop
		nop
		nop
		nop			
SetUp	;bsf		status, rp0 	;Bank 1		
		;bcf     option_reg,7    ;pull-ups enabled	
		;bcf		status, rp0		;bank 0
		movlw   07h         	;turn off Comparator ports
        movwf   CMCON       	;must be placed in bank 0  		
		goto 	Main	
		
						

;****************************************************************
;* Tables 			*
;****************************************************************

table1	addwf   PCL,F           ;02h,1  add W to program counter
		  
						

;********************
;* Delays 			*
;********************


_xuS	movwf	temp2
_uS		movlw	.10
		movwf	temp1
		decfsz	temp1,f
		goto	$-1				
		decfsz 	temp2,f
		goto 	_uS		
		retlw 	00
		
_ZuS	movwf	temp2
		goto	$+2
		goto	$+2		
		decfsz 	temp2,f
		goto 	$-3		
		retlw 	00		


_xmS	movwf	temp2
_x		nop
		decfsz 	temp1,f
		goto 	_x
		decfsz 	temp2,f
		goto 	_x	
		retlw 	00
		
		 
		
_1mS	movlw	01h
		movwf	temp2
		nop
		decfsz 	temp1,f
		goto 	$-2
		decfsz 	temp2,f
		goto 	$-4	
		retlw 	00	
		
		;2.5mS
		
_2mS5	movlw	03h
		movwf	temp2
		decfsz 	temp1,f
		goto 	$-1
		decfsz 	temp2,f
		goto 	$-3	
		retlw 	00				
		
				
		
		
_5mS	movlw	05h
		movwf	temp2
_5		nop
		decfsz 	temp1,f
		goto 	_5
		decfsz 	temp2,f
		goto 	_5	
		retlw 	00	
		
_7mS5	call    _5mS	
		call    _2mS5
		retlw 	00
		
		
_10mS	movlw	0Ah
		movwf	temp2
_10		nop
		decfsz 	temp1,f
		goto 	_10
		decfsz 	temp2,f
		goto 	_10	
		retlw 	00		
		

_50mS	movlw	.50
		movwf	temp2
_50		nop
		decfsz 	temp1,f
		goto 	_50
		decfsz 	temp2,f
		goto 	_50	
		retlw 	00
		
_100mS	movlw	.100
		movwf	temp2
_100	nop
		decfsz 	temp1,f
		goto 	_100
		decfsz 	temp2,f
		goto 	_100	
		retlw 	00	
		
		
;****************************
;* Sub Routines 			*
;****************************

        ;pulse LEDs at 100Hz  = 10,000uS  @25% 
        
				
			
_Ared	bsf		status, rp0 	;Bank 1		
		movlw	b'11111100'		;Set TRIS GP2,3,5 input   GP0,1,output
		movwf	TRISIO		
		bcf		status, rp0		;bank 0
		clrf	gpio
		bsf		gpio,0			;turn on LED A red		
		call	_2mS5        	;call 2.5mS
		bcf		gpio,0			;turn off red	
		retlw	00	

_Agreen	bsf		status, rp0 	;Bank 1		
		movlw	b'11111010'		;Set TRIS GP1,3,4,5 input   GP0,2,output
		movwf	TRISIO		
		bcf		status, rp0		;bank 0
		clrf	gpio
		bsf		gpio,0			;turn on LED A green		
		call	_2mS5        	;call 2.5mS
		bcf		gpio,0			;turn off green
        retlw	00	
        
_Ablue	bsf		status, rp0 	;Bank 1		
		movlw	b'11101110'		;Set TRIS GP1,2,3,5 input   GP0,4,output
		movwf	TRISIO		
		bcf		status, rp0		;bank 0
		clrf	gpio
		bsf		gpio,0			;turn on LED A blue		
		call	_2mS5        	;call 2.5mS
		bcf		gpio,0			;turn off blue      
        retlw	00
        
_Bred	bsf		status, rp0 	;Bank 1		
		movlw	b'11111100'		;Set TRIS GP2,3,5 input   GP0,1,output
		movwf	TRISIO		
		bcf		status, rp0		;bank 0
		clrf	gpio
		bsf		gpio,1			;turn on LED B red		
		call	_2mS5        	;call 2.5mS
		bcf		gpio,1			;turn off red	
	    retlw	00
	    
_Bgreen	bsf		status, rp0 	;Bank 1		
		movlw	b'11111001'		;Set TRIS GP0,3,4,5 input   GP1,2,output
		movwf	TRISIO		
		bcf		status, rp0		;bank 0
		clrf	gpio
		bsf		gpio,1			;turn on LED B green		
		call	_2mS5        	;call 2.5mS
		bcf		gpio,1			;turn off green		
	    retlw	00
	    
_Bblue	bsf		status, rp0 	;Bank 1		
		movlw	b'11101101'		;Set TRIS GP0,2,3,5 input   GP1,4,output
		movwf	TRISIO		
		bcf		status, rp0		;bank 0
		clrf	gpio
		bsf		gpio,1			;turn on LED B blue		
		call	_2mS5        	;call 2.5mS
		bcf		gpio,1			;turn off blue						
		retlw	00	      
           
        
_Cred	bsf		status, rp0 	;Bank 1		
		movlw	b'11111001'		;Set TRIS GP0,3,4,5 input   GP1,2,output
		movwf	TRISIO		
		bcf		status, rp0		;bank 0
		clrf	gpio
		bsf		gpio,2			;turn on LED C red		
		call	_2mS5        	;call 2.5mS
		bcf		gpio,2			;turn off red	
		retlw	00	
		
_Cgreen	bsf		status, rp0 	;Bank 1		
		movlw	b'11111010'		;Set TRIS GP1,3,4,5 input   GP0,2,output
		movwf	TRISIO		
		bcf		status, rp0		;bank 0
		clrf	gpio
		bsf		gpio,2			;turn on LED C green		
		call	_2mS5        	;call 2.5mS
		bcf		gpio,2			;turn off green	
		retlw	00
        
        
_Cblue  bsf		status, rp0 	;Bank 1		
		movlw	b'11101011'		;Set TRIS GP0,1,3,5 input   GP2,4,output
		movwf	TRISIO		
		bcf		status, rp0		;bank 0
		clrf	gpio
		bsf		gpio,2			;turn on LED C blue		
		call	_2mS5        	;call 2.5mS
		bcf		gpio,2			;turn off blue	
		retlw	00  
		
		      
        
_Dred  	bsf		status, rp0 	;Bank 1		
		movlw	b'11101101'		;Set TRIS GP0,2,3,5 input   GP1,4,output
		movwf	TRISIO		
		bcf		status, rp0		;bank 0
		clrf	gpio
		bsf		gpio,4			;turn on LED D red		
		call	_2mS5        	;call 2.5mS
		bcf		gpio,4			;turn off red      
        retlw	00
        
_Dgreen	bsf		status, rp0 	;Bank 1		
		movlw	b'11101011'		;Set TRIS GP0,1,3,5 input   GP2,4,output
		movwf	TRISIO		
		bcf		status, rp0		;bank 0
		clrf	gpio
		bsf		gpio,4			;turn on LED D green		
		call	_2mS5        	;call 2.5mS
		bcf		gpio,4			;turn off green	
        retlw	00
        
        
_Dblue  bsf		status, rp0 	;Bank 1		
		movlw	b'11101110'		;Set TRIS GP1,2,3,5 input   GP0,4,output
		movwf	TRISIO		
		bcf		status, rp0		;bank 0
		clrf	gpio
		bsf		gpio,4			;turn on LED D blue		
		call	_2mS5        	;call 2.5mS
		bcf		gpio,4			;turn off blue
        retlw	00       
 
Show1R 	call	_Dred			
		call	_7mS5        	;call 7.5mS						
		retlw	00			
		
Show1G 	call	_Dgreen
		call	_7mS5        	;call 7.5mS					
		retlw	00	
		
Show1B 	call    _Dblue	
		call	_7mS5        	;call 7.5mS					
		retlw	00	
		
Show1W	call	_Dred
		call	_Dgreen
        call	_Dgreen
        call	_5mS
        retlw	00
		
Show2R 	call    _Ared
		call	_7mS5        	;call 7.5mS				
		retlw	00			
		
Show2G 	call    _Agreen
		call	_7mS5        	;call 7.5mS				
		retlw	00			
		
Show2B 	call    _Ablue
		call	_7mS5        	;call 7.5mS			
		retlw	00	
		
Show2W	call	_Ared
		call	_Agreen
        call	_Ablue
        call	_5mS
        retlw	00
		
		
Show3R 	call	_Ared
		call	_5mS        	;call 5mS
		call	_Dred						
		retlw	00	
		
Show3G 	call	_Agreen
		call	_5mS        	;call 5mS
		call	_Dgreen		
		retlw	00			
				
Show3B 	call	_Ablue
		call	_5mS        	;call 5mS
		call	_Dblue		
		retlw	00		

				
Show4R 	call	_Ared
		call	_5mS        	;call 5mS
		call    _Bred
		retlw	00				
		
Show4G 	call	_Agreen
		call	_5mS        	;call 5mS
		call    _Bgreen
		retlw	00			
		
Show4B 	call	_Ablue
		call	_5mS        	;call 5mS		
		call    _Bblue		
		retlw	00						
				
				
Show5R 	call	_Ared				
		call    _Bred	
		call    _Dred	
		call	_2mS5        	;call 2.5mS				
		retlw	00			
		
Show5B 	call	_Ablue				
		call    _Bblue	
		call    _Dblue	
		call	_2mS5        	;call 2.5mS				
		retlw	00			
		
Show5G 	call	_Agreen				
		call    _Bgreen	
		call    _Dgreen	
		call	_2mS5        	;call 2.5mS				
		retlw	00	
		
		
Show6R 	call  	_Ared
		call  	_Bred
		call  	_Cred
		call	_2mS5        	;call 2.5mS	
		retlw	00	
		
Show6G 	call  	_Agreen
		call  	_Bgreen
		call  	_Cgreen
		call	_2mS5        	;call 2.5mS	
		retlw	00			
		
Show6B 	call  	_Ablue
		call  	_Bblue
		call  	_Cblue
		call	_2mS5        	;call 2.5mS	
		retlw	00	
		
						
;************************
;* Main 				*
;************************

Main			





		call	Show1R
		decfsz	loops,f
		goto	$-2		
		call	Show1G
		decfsz	loops,f
		goto	$-2		
		call	Show1B
		decfsz	loops,f
		goto	$-2
		
		call	Show1W
		decfsz	loops,f
		goto	$-2
		
		
		call	Show2R
		decfsz	loops,f
		goto	$-2		
		call	Show2G
		decfsz	loops,f
		goto	$-2		
		call	Show2B
		decfsz	loops,f
		goto	$-2
		
		call	Show2W
		decfsz	loops,f
		goto	$-2
	
		
		call	Show3R
		decfsz	loops,f
		goto	$-2		
		call	Show3G
		decfsz	loops,f
		goto	$-2		
		call	Show3B
		decfsz	loops,f
		goto	$-2
		
		call	Show4R
		decfsz	loops,f
		goto	$-2		
		call	Show4G
		decfsz	loops,f
		goto	$-2		
		call	Show4B
		decfsz	loops,f
		goto	$-2
		
		call	Show5R
		decfsz	loops,f
		goto	$-2		
		call	Show5G
		decfsz	loops,f
		goto	$-2		
		call	Show5B
		decfsz	loops,f
		goto	$-2
		
		call	Show6R
		decfsz	loops,f
		goto	$-2		
		call	Show6G
		decfsz	loops,f
		goto	$-2		
		call	Show6B
		decfsz	loops,f
		goto	$-2
		
		
		goto	Main		
	
				
;************************
;*EEPROM     	    	*
;************************
								
		org		2100h			
					
							
		END
		
		