Keyboard Decode Mail

Date: Sun, 03 Nov 1996 16:50:54 -0500 (EST)
From: Roger Coleman <101722.3022@compuserve.com>
Subject: Re: keyboard ScanCodes
To: Multiple recipients of list PICLIST

Hi!

:I'm developping an PIC aplication that use an IBM AT keyboard,
:the routine to read the keyboard is ok, I can receive the scan
:codes.

:What I need is a easy way to transform the scancode to ASCII,
:i.e. 0X1c --> 'A'.

I worked out a way to do this - it's not very elegant and basically just uses an enormous lookup table to translate the code from the keyboard into ASCII. It works, though!

I had a lot of fun getting it all to work!

I use the keyboard with a Hitachi LCD, so I have other routines to handle the shift and delete keys too, + handling keys being 'unpressed' + reading the data from the keyboard - email me if you need these.

'key_data' contains the data from the keyboard

get_k_letter

; nb this routine only deals with letters and numbers
; use a different routine to trap 'delete', 'return' and
; 'shift'.
movlw b'00010101' ; puts 21 in w
subwf key_data,f ; takes 21 off the keyboard input
movf key_data,w ; gets the new key number
andlw b'11000000' ; checks if number is big
btfss status,z ; skips if it isn't
retlw 0 ; returns 0 if it is
movlw b'00000111' ;
addwf key_data,w ; adds it to key number
movwf temp_store ; stores it temporarily
btfsc temp_store,6 ; skips if it isn't too big
retlw 0 ; returns 0 if it is
movf key_data,w ; gets key_data into w
addwf pc,f ; jumps to the corresponding line
retlw 'q' ; 21 is q
retlw '1' ; 22 is 1
retlw 0 ; 23 is unused
retlw 0 ; 24 is unused
retlw 0 ; 25 is unused
retlw 'z' ; 26 is z
retlw 's' ; 27 is s
retlw 'a' ; 28 is a
retlw 'w' ; 29 is w
retlw '2' ; 30 is 2
retlw 0 ; 31 is unused
retlw 0 ; 32 is unused
retlw 'c' ; 33 is c
retlw 'x' ; 34 is x
retlw 'd' ; 35 is d
retlw 'e' ; 36 is e
retlw '4' ; 37 is 4
retlw '3' ; 38 is 3
retlw 0 ; 39 is unused
retlw 0 ; 40 is unused
retlw ' ' ; 41 is space
retlw 'v' ; 42 is v
retlw 'f' ; 43 is f
retlw 't' ; 44 is t
retlw 'r' ; 45 is r
retlw '5' ; 46 is 5
retlw 0 ; 47 is unused
retlw 0 ; 48 is unused
retlw 'n' ; 49 is n
retlw 'b' ; 50 is b
retlw 'h' ; 51 is h
retlw 'g' ; 52 is g
retlw 'y' ; 53 is y
retlw '6' ; 54 is 6
retlw 0 ; 55 is unused
retlw 0 ; 56 is unused
retlw 0 ; 57 is unused
retlw 'm' ; 58 is m
retlw 'j' ; 59 is j
retlw 'u' ; 60 is u
retlw '7' ; 61 is 7
retlw '8' ; 62 is 8
retlw 0 ; 63 is unused
retlw 0 ; 64 is unused
retlw ',' ; 65 is ,
retlw 'k' ; 66 is k
retlw 'i' ; 67 is i
retlw 'o' ; 68 is o
retlw '0' ; 69 is 0 (zero)
retlw '9' ; 70 is 9
retlw 0 ; 71 is unused
retlw 0 ; 72 is unused
retlw '.' ; 73 is .
retlw '/' ; 74 is ?
retlw 'l' ; 75 is l
retlw 0 ; 76 is unused
retlw 'p' ; 77 is p
retlw 0 ; 78 onwards are unused
The above routine returns the lower case for most of the keys pressed. You can easily make it upper case or a symbol by clearing bit 4 or 5:

(ascii_data is the ASCII code returned by the above routine)

btfsc ascii_data,6 ; checks for a letter
bcf ascii_data,5 ; makes it upper case
btfsc ascii_data,5 ; checks for a number
bcf ascii_data,4 ; makes it a symbol

Ta Daaaaa!

Best wishes

Roger.

-- Roger Coleman,

101722.3022@compuserve.com