//============================================================================== // // Simple PC Keyboard - Driver // //============================================================================== // // This firmware source code is for free. You can redistribute, copy and // modify it. It is distributed in the hope that it will be useful, // but without any warranty. // // Check the applications section of www.avrcard.com for updates and further // information on this project. // // Have fun. // // Hans Kallen, www.avrcard.com, August 2003 // //============================================================================== // // Version Date Author Changes // ------- ---------- ------- ------------------------------ // 1.0 03.10.2002 HK Initial release // // Target: ATmega128 at 16MHz // Compiler: AVRco (by www.e-lab.de) // // Hardware: INT4 - Clock // PE.5 - Data // // User program must provide a pipe: // var Keyboard: Pipe[16] of Char; // and a process that receives the keyboard chars from that pipe. // Procedure InitKeyb must be called before using this driver. // // //////////////////////////////////////////////////////////////////////////////// const CodeTab: array[$0..$FF] of Char = // unshifted (#0, #0, #0, #0, #0, #0, #0, #0, #0, #0, #0, #0, #0, #9, '|', #0, #0, #0, #0, #0, #0, 'q', '1', #0, #0, #0, 'y', 's', 'a', 'w', '2', #0, #0, 'c', 'x', 'd', 'e', '4', '3', #0, #0, ' ', 'v', 'f', 't', 'r', '5', #0, #0, 'n', 'b', 'h', 'g', 'z', '6', #0, #0, ',', 'm', 'j', 'u', '7', '8', #0, #0, ',', 'k', 'i', 'o', '0', '9', #0, #0, '.', '-', 'l', 'ø', 'p', '+', #0, #0, #0, 'æ', #0, 'å', '^', #0, #0, #0, #0, #13, '¨', #0, '$', #0, #0, #0, '<', #0, #0, #0, #0, #8, #0, #0, '1', #0, '4', '7', #0, #0, #0, '0',',', '2', '5', '6', '8', #0, #0, #0,#$60, '3', '-', '*', '9', #0, #0, // shifted #0, #0, #0, #0, #0, #0, #0, #0, #0, #0, #0, #0, #0, #9, '§', #0, #0, #0, #0, #0, #0, 'Q', '+', #0, #0, #0, 'Y', 'S', 'A', 'W', '"', #0, #0, 'C', 'X', 'D', 'E', '¤', '*', #0, #0, ' ', 'V', 'F', 'T', 'R', '%', #0, #0, 'N', 'B', 'H', 'G', 'Z', '&', #0, #0, 'L', 'M', 'J', 'U', '/', '(', #0, #0, ';', 'K', 'I', 'O', '=', ')', #0, #0, ':', '_', 'L', 'Ø', 'P', '?', #0, #0, #0, 'Æ', #0, '!', '`', #0, #0, #0, #0, #13, '^', '*', '£', #0, #0, #0, '>', #0, #0, #0, #0, #8, #0, #0, '1', #0, '4', '7', #0, #0, #0, '0',',', '2', '5', '6', '8', #0, #0, #0, '+', '3', '-', '*', '9', #0, #0); // Extended keyboard codes = scan code + $80 kbdPgUp: Byte = $FD; kbdPgDn: Byte = $FA; kbdUp: Byte = $F5; kbdDn: Byte = $F2; kbdLeft: Byte = $EB; kbdRight: Byte = $F4; kbdEnter: Byte = $0D; kbdSpace: Byte = $20; kbdBS: Byte = $08; kbdTab: Byte = $09; type KeybStat = (up, spec, shift, goup, goupext); var // PC keyboard handler BitCntr: Byte; tmpByte: Byte; Keyb: KeybStat; Procedure InitKeyb; //------------------------------------------------------------------------------ begin BitCntr:= 11; Keyb:= up; PipeFlush (Keyboard); // init port interrupt INT4 incl (EICR, 1); excl (EICR, 0); // falling edge INT4 excl (EIFR, 4); // reset INT4 incl (EIMSK, 4); // enable INT4 end InitKeyb; Interrupt INT4; //============================================================================== begin if (BitCntr < 11) and (BitCntr > 2) then // Bit 3 to 10 is data. Parity bit, tmpByte:= tmpByte shr 1; // start and stop bits are ignored. if (PinE and %00100000) > 0 then incl (tmpByte, 7); else excl (tmpByte, 7); endif; endif; dec (BitCntr); if BitCntr = 0 then // All bits received BitCntr:= 11; // Decode scancodes into char case Keyb of up: case tmpByte of $E0: Keyb:= spec; | $F0: Keyb:= goup; | $12: Keyb:= shift; | else PipeSend (Keyboard, CodeTab [tmpByte]); endcase; | shift: case tmpByte of $F0: Keyb:= goup; | else tmpByte:= tmpByte + $80; // add offset to code tab PipeSend (Keyboard, CodeTab [tmpByte]); Keyb:= goup; endcase; | goup: Keyb:= up; | goupext: case tmpByte of $E0: Keyb:= up; | endcase; | spec: case tmpByte of $F0: Keyb:= goup; | $E0: | else PipeSend (Keyboard, Char (tmpByte + $80)); endcase; | endcase; // KeybStat endif; // BitCntr = 0 end;