
;*******************************
;;8x8 Module.asm
;  13-6-2013 
;*******************************


	list	p=12F629
	radix	dec
	include	"p12f629.inc"
	
		errorlevel	-302	; Dont complain about BANK 1 Registers during assembly

	__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
;****************************************************************

		Cblock  20h
temp1		    ;used in delays
temp2			;used in delays
temp3			;used in delays
flashes		    ;LED flashes to indicate key number

		endc

;****************************************************************
;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  LED - flashes to indicate key number
pin6	 	equ	1	;GP1  Data to 8x8 Module
pin5		equ	2	;GP2  Chip Select
pin4		equ	3	;GP3  
pin3		equ	4	;GP4  Clock
pin2		equ	5	;GP5  4 switches   
 

		;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			
       	movlw	b'11001000'		;Set TRIS GP0,1,2,4,5 out   GP3 not used
		movwf	TRISIO	
		bcf		status, rp0		;bank 0
		movlw   07h         	;turn off Comparator ports
        movwf   CMCON       	;must be placed in bank 0  
		clrf 	GPIO       		;Clear GPIO of junk	
		goto    SwPressed
		
		
;****************************************************************
;* Delays 			*
;****************************************************************
		
_6uS	goto	$+1		
		retlw   00


_10mS	movlw	.10
		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	
		
		
_500mS	movlw   5			
		movwf   temp3
		call	_100mS		
		decfsz  temp3,1
		goto    $-2
		retlw   00

;****************************************************************
;* Sub Routines 			*
;****************************************************************
			
  ;on start-up,program looks for switch pressed to set key-delay-value


SwPressed
		clrf	flashes
		bsf		status, rp0 	;Bank 1			
       	movlw	b'11001000'		;Set TRIS  GP0,5 out to charge capacitor
		movwf	TRISIO	
		bcf		status, rp0		;bank 0
		bsf		gpio,5			;make pin2 HIGH to charge cap. 
		call	_10mS
		bsf		status, rp0 	;Bank 1			
       	movlw	b'11101000'		;Set TRIS GP0,5 in to detect discharged cap
		movwf	TRISIO	
		bcf		status, rp0		;bank 0		
		call	_6uS
		incf	flashes,1
		btfsc   gpio,5
		goto	$-3
		bsf		gpio,0			;turn ON LED
		call	_500mS
		bcf		gpio,0			;turn OFF LED
		call	_500mS
		decfsz	flashes,1
		goto	$-5
		nop
		goto    $-1
				
					
							
		END
		
		