Beruflich Dokumente
Kultur Dokumente
Manjeera Jeedigunta http://blogs.cae.tntech.edu/msjeedigun21 Email: msjeedigun21@tntech.edu Tel: 931-372-6181, Prescott Hall 120
Prev
Interfacing with Switches
Today
Interfacing with Keypad
Interfacing to a Keyboard
A keyboard is arranged as an array of switches,
mechanical membrane capacitors Hall-effect in construction.
Lookup the ASCII table to find out the ASCII code of the pressed key.
Keypad Scanning
PA7~PA4 O/P, Row selection, row being [(0,1,2,3),(4,5,6,7)..] either one of PA7~PA4=0 Row being scanned is driven low
c3 c2 c1 c0
PA7 1 1 1 0
PA6 1 1 0 1
PA5 1 0 1 1
PA4 0 1 1 1
r0
r1
r2
r3
Debouncer will recognize that the switch is closed after the voltage is low for around 10ms and that the switch is open after the voltage is high for about 10 ms
VDD
4050
Vout
(b) CMOS gate debouncer VDD R H Vout C L Switch closed (c) Integrating RC circuit debouncer Figure 7.42 Hardware debouncing techniques Threshold level
Example 7.10 Write a program to perform keypad scanning, debouncing, and returns the ASCII code in accumulator A to the caller. Solution
Pins PA4..PA7 each control one row of four keys. Scanning is performed by setting one of the PA7PA4 pins to low, the other three pins to high and testing one key at a time.
#include c:\miniide\hcs12.inc" keyboard equ PTA get_char scan_r0 scan_k0 scan_k1 scan_k2 scan_k3 key0 key1 movb movb brclr brclr brclr brclr bra jmp jmp #$F0,DDRA #$EF,keyboard keyboard,$01,key0 keyboard,$02,key1 keyboard,$04,key2 keyboard,$08,key3 scan_r1 db_key0 db_key1 ; set PA7~PA4 for output, PA3~PA0 for input ; scan the row containing keys 0123 ; is key 0 pressed? ; is key 1 pressed? ; is key 2 pressed? ; is key 3 pressed?
key2 key3 scan_r1 scan_k4 scan_k5 scan_k6 scan_k7 key4 key5 key6 key7 scan_r2 scan_k8 scan_k9 scan_kA scan_kB key8 key9
jmp jmp movb brclr brclr brclr brclr bra jmp jmp jmp jmp movb bclr brclr brclr brclr brclr bra jmp jmp
db_key2 db_key3 #$DF,keyboard keyboard,$01,key4 keyboard,$02,key5 keyboard,$04,key6 keyboard,$08,key7 scan_r2 db_key4 db_key5 db_key6 db_key7 #$BF,keyboard keyboard,$40 keyboard,$01,key8 keyboard,$02,key9 keyboard,$04,keyA keyboard,$08,keyB scan_r3 db_key8 db_key9
; scan the row containing keys 4567 ; is key 4 pressed? ; is key 5 pressed? ; is key 6 pressed? ; is key 7 pressed?
; scan the row containing keys 89AB ; ; is key 8 pressed? ; is key 9 pressed? ; is key A pressed? ; is key B pressed?
jmp jmp movb brclr brclr brclr brclr jmp keyC jmp keyD jmp keyE jmp keyF jmp ; debounce key 0 db_key0 jsr brclr jmp getc0 ldaa rts ; debounce key 1
db_keyA db_keyB #$7F,keyboard keyboard,$01,keyC keyboard,$02,keyD keyboard,$04,keyE keyboard,$08,keyF scan_r0 db_keyC db_keyD db_keyE db_keyF
; scan the row containing keys CDEF ; is key C pressed? ; is key D pressed? ; is key E pressed? ; is key F pressed?
db_key1 jsr brclr jmp getc1 ldaa rts db_key2 jsr brclr jmp getc2 ldaa rts db_key3 jsr brclr jmp getc3 ldaa rts db_key4 jsr brclr
delay10ms keyboard,$02,getc1 scan_k2 #$31 delay10ms keyboard,$04,getc2 scan_k3 #$32 delay10ms keyboard,$08,getc3 scan_r1 #$33 delay10ms keyboard,$01,getc4
jmp getc4 ldaa rts db_key5 jsr brclr jmp getc5 ldaa rts db_key6 jsr brclr jmp getc6 ldaa rts db_key7 jsr brclr jmp
scan_k5 #$34
delay10ms keyboard,$02,getc5 scan_k6 #$35 ; return the ASCII code of 5 delay10ms keyboard,$04,getc6 scan_k7 #$36 ; return the ASCII code of 6 delay10ms keyboard,$08,getc7 scan_r2
getc7 db_key8
getc8 db_key9
getc9 db_keyA
getcA db_keyB
getcB
ldaa rts jsr brclr jmp ldaa rts jsr brclr jmp ldaa rts jsr brclr jmp ldaa rts jsr brclr jmp ldaa rts
delay10ms keyboard,$04,getcA scan_kB #$41 ; get the ASCII code of A delay10ms keyboard,$08,getcB scan_r3 #$42 ; get the ASCII code of B
db_keyC
getcC db_keyD
getcD db_keyE
jsr brclr jmp ldaa rts jsr brclr jmp ldaa rts jsr brclr jmp ldaa rts jsr brclr jmp ldaa rts
delay10ms keyboard,$01,getcC scan_kD #$43 ; get the ASCII code of C delay10ms keyboard,$02,getcD scan_kE #$44 ; get the ASCII code of D delay10ms keyboard,$04,getcE scan_kF #$45 ; get the ASCII code of E delay10ms keyboard,$08,getcF scan_r0 #$46 ; get the ASCII code of F
getcE db_keyF
getcF
delay10ms movb movb movb ldd addd std wait_lp2 brclr rts
; enable TCNT & fast flags clear ; configure prescale factor to 64 ; enable OC0 ; start an output compare operation ; with 10 ms time delay
Next
Interfacing with LCD Time-Multiplexing Class in BN 320 on Friday Nov 7th Read Chapter 7.6