PIC Microcontroler based Keyboards

7 switches with 1 A/D pin

by Bob Axtell of
http://www.platinumtechnical.com

;********************************************************;
; Code snippet for reading 7 switches on a single PIN
; Routine assumes that ADCON0 and ADCON1 are set to
; appropriate values for the PIC used.
; The routine tests 8bit ADC value against the voltage
; level presented. If it is LOWER than the test then
; that switch is grounded. NOTE: This can be minimized
; further. NOTE: this should be executed during an
; interrupt to make sure that the snippet is not missed.
;********************************************************;
;process keyboard: inside ints only
keybd:
	clrf	status
	rrf	adresh,w
	movwf	tmp1_
	bank1
	rrf	adresl,w
	clrf	status
	movwf	tmp_
	rrf	tmp1_,f
	rrf	tmp_,f 		;now fixed to 8 bits, NOT 10 bits
	clrf	tmp1_ 		;used to locate switch number
	incf 	tmp1_,f		;=SW1
	movlw	-d'18'		; 0.0V + 0.7V/2 = 0.35V Test limit
	addwf	tmp_,w		;OK Pressed?
	btfss	status,c	;check if LESS
	goto	keybd0 		;jump if OK SW pressed
	incf	tmp1_,f
	movlw	-d'54' 		; 0.7V + 0.35V = 1.05V
	addwf	tmp_,w 		;DN Pressed?
	btfss	status,c 	;check if LESS
	goto	keybd0 		;jump if DN SW pressed
	incf	tmp1_,f
	movlw	-d'90' 		; 1.4v + 0.35V = 1.75V
	addwf	tmp_,w 		;<< Pressed?
	btfss	status,c 	;check if LESS
	goto	keybd0 		;jump if << SW pressed
	incf	tmp1_,f
	movlw	-d'125' 	; 2.1V + 0.35V = 2.45V
	addwf	tmp_,w 		;< pressed?
	btfss	status,c 	;check if LESS
	goto	keybd0 		;jump if < SW pressed
	incf 	tmp1_,f
	movlw 	-d'161' 	;2.8V + 0.35V = 3.15V
	addwf 	tmp_,w 		;> pressed?
	btfss 	status,c 	;check if LESS
	goto 	keybd0 		;jump if > SW pressed
	incf 	tmp1_,f
	movlw 	-d'197' 	;3.5V + 0.35V = 3.85V
	addwf 	AN0,w 		;>> pressed?
	btfss 	status,c 	;check if LESS
	goto 	keybd0 		;jump if >> SW pressed
	incf 	tmp1_,f
	movlw 	-d'233' 	;4.2V + 0.35V = 4.55V
	addwf 	AN0,w 		;UP pressed?
	btfsc 	status,c 	;check if LESS
; no switches are pressed, so done
	goto 	keybd1
; now process the switches
keybd0:
	incf 	tmp1_,w
	movwf 	KEY 		;value will read 1 to 7 for appropriate SW
	bsf 	flags,KYPRESS 	;tell system it is time to service keypress
keybd1:
	bsf 	adcon0,GO 	;read for next time
	return
;********************************************************;
; end of snippet
;********************************************************;