'KEYB18XA.BAS -for Dec.03 "Silicon Chip" PICAXE-18X/A article.Ver 1.03 16/3/04 'Converts attached PS/2 PC computer keyboard codes for ASCII display.NB-NOT the 'normal Prog.Editor PC kbd but an "old PC" one - selected for low power demands. 'EEPROM command preloads ASCII values into 18X/A data memory for lookup table. 'When key pressed,it's "value" used as the data memory address via the read cmd. 'The correct ASCII value is then shifted from the data memory into variable b1 'For PICAXE-18X/A or 28X only - will NOT work PICAXE-08 (since no "key" cmds). 'Refer article & schematic for kbd V & I findings + 18X/A connection details. 'Ref PICNIK18 protoboard layout pix => www.picaxe.orcon.net.nz/keyb18xa.jpg 'Schematic=>www.picaxe.orcon.net.nz/keyb18xa.gif. Recommend "BananaCom" terminal 'Copy & paste download this listing via => www.picaxe.orcon.net.nz/keyb18xa.bas 'Via Stan.SWAN =>s.t.swan@massey.ac.nz. Comment lines (starting ')can be ignored. '-------------------------------------------------------------------------------- ' INPUTS 3-5V +supply OUTPUTS PS/2 (5)(3)(1)(4) '+ + + + + + + + + + + + + + + + + + + + + + + keyboard G S '| ,------------- | -typically C R U 'o o o o------- | | SEROUT _Piezo a recent L O D P 'KBd.| | | --------- | | one takes O U A P ' | 0 1 2 6 7 =| PICAXE |=0 1 2 3 4 5 6 7 just 2mA C N T L ' | =| 18X(A) |= L @ 3V-5V ! K D A Y ' |_0V --------- E ========== ' (SERTXD) |* | D ** =wire to 4.7k | | | | ' |||----------------/ * | | | | | | | | | pullup R & reset | | | | 'Prog. - - - - - - - - - - - - - - - - - - Ch OV Ch +5V 'input Common gnd for serial,kbd,piezo & supply PICAXE-18X/A => 6 7 '--------------------------------------------------------------------------------- serout 2,n2400,(12,"PC keyboard + PICAXE-18X/A terminal") 'Power switch on title wait 1: serout 2,n2400,(12) '1 sec.title display then FF(12=FormFeed) = new page for b0=128 to 255 'sweep thru' all upper 128 non volatile RAM locations read b0,b1 'stored data values readout to terminal/ LCD at switch on serout 2,n2400,(b1) 'EEPROM occupies first 128 bytes of course,so just 128 ch. next b0 '~20 spaced words,buffer stored for next power on display serout 2,n2400,(32) 'space indictes end of buffered text & fresh display start '--------------------------------------------------------------------------------- reset: b0=127 'reset for RAM storage -first 128(0-127) used EEPROM "keyvalues" kbd: 'decoding/display routine. SERTXD cmd ideal initial 18X tweaking for b0=128 to 255'increment for last 128 keystrokes capture.NB-overwrites earlier keyin 'Get the keyboard press.NB-all processing stops until received! read keyvalue,b1 'convert key value from keyin cmd into an ASCII character as b1 if keyvalue=$5A then crlf 'Carriage Return & Line feed routine for Enter key ($5A) if keyvalue=$66 then bksp 'backspace key routine for BkSp key ($66) 'sertxd (b1) 'display @4800bps ASCII ch.(via inbuilt F8 ?)NB:SERTXD 18X only! 'REFER COMMENT BELOW RE SERTXD BUG IF A SEROUT DIRECTLY FOLLOWS.Space by PAUSE 1? 'pulsout 2,500 'LED flash output 2 confirms key push. SEROUT flashes LED also serout 2,n2400,(b1)'pin 2 2400 bps serial output for terminal/LCD display. 18A/X sound 7,(100,5) 'key push sound- alter to suit (usual syntax).Remove if tedious ! pause 100 'may be needed to prevent double ch. sending-alter to suit ~150 ? write b0,b1 'stores last 128 raw keystrokes (less CRLF & BS) non volatile RAM if b0=255 then reset ' allow for overwriting initial stored keystroke buffer next b0 'increment RAM storage location until 128 ch. buffer is full goto kbd 'NB:18X SERTXD ideal tweaking- maybe 'rem out finally (as here)? '---------------------------------------------------------------------------------- crlf: 'CR & LF routine to action 'Enter' key press.(ASCII values) serout 2,n2400,(13,10) 'force a CR(=13) & LF(=10) for new line display on terminal sound 7,(80,20) 'old typewriter CR sound (!?)-alerts to different key press pause 200 '"typomatic" delay to prevent double action- alter to suit goto kbd 'loop back to main key decoding routine '---------------------------------------------------------------------------------- bksp: 'BackSpace routine to action 'BkSp' keypress.(ASCII values) serout 2,n2400,(8) 'force a BS(=8)on terminal.NB Some term. progs.may 'ignore' sound 7,(50,20) 'raspberry sound (!?)- alerts to different key press pause 10 'short delay prevents double BS keypress- alter to suit goto kbd 'loop back to main key decoding routine '---------------------------------------------------------------------------------- 'Keyvalue data via Picaxe Editor:->file ->open ->samples ->keyin.bas CAPITALS only! EEPROM $00,("?9?5312C?A864?'?") 'FN KEYS-all EEPROM values are hexadecimal(base 16) EEPROM $10,("?????Q1???ZSAW2?") 'MAIN KBD.Keyvalues run $10-$1F. So $1C=A,$1D=W etc EEPROM $20,("?CXDE43?? VFTR5?") 'NB "gap" before the "V" =SPACE BAR (keyvalue $29) EEPROM $30,("?NBHGY6???MJU78?") 'Refer keyvalue list =>Help- Picaxe-18 -Basic cmds EEPROM $40,("?,KIO09??./L;P-?") 'Use "If keyvalue =$xx then action" for 'capture' EEPROM $50,("??'?[=?????]????") 'further specific keys.Thus if keyvalue=$76 (= Esc) EEPROM $60,("?????????1?47???") 'force term.'Escape' action -> serout 2,n2400,(12) EEPROM $70,("0.2568??B+3-*9??") 'NUMERIC KEYPAD.where 12=FF (ASCII) to clear screen _____________________________________________________________________________________ Revision history: ---------------- Ver 1.00 mid Oct '03 Initial design - local use only while testing diverse keyboards Ver 1.01 27 Oct '03 CR/LF & ASCII art added & hosted=> picaxe.orcon.net.nz for feedback etc. Ver 1.02 1 Nov '03 "SiChip" version. Added 128 ch.buffer + BkSp. SERTXD comments tamed ! Ver 1.03 16 Mar '04 Comment on SERTXD bug added - as below --------------------------------------------------------------------------------------- NOTE: A Rev.Ed forum of mid March 2004 confirmed a SERTXD bug when a SEROUT follows directly ! Use a short pause (such as PAUSE 1) between sertxd & serout commands to workaround. Full details =>www.rev-ed.co.uk/picaxe/forum/topic.asp?topic_id=1067&forum_id=9&Topic_Title=Sertxd+problem&for um_title=PICAXE+Forum&M=False&S=True