to read a PS2 keyboard, you need something like...
Then, you need to build a HUGE lookup table and + conditions to fit the PS2 ScanCode which is store in KeyPS2 variable.Code:' ' PS2 Keypad signal ' ----------------- DEFINE PS2_SCLK PORTC DEFINE PS2_SCLK_BIT 1 DEFINE PS2_SDATA PORTC DEFINE PS2_SDATA_BIT 0 ' ' Variables definition ' ==================== KeyPS2 var BYTE ' Store pressed key ' some code here ' ' Keypad scan routine ' =================== ReadPS2: ' ' data is packed in a 11 Bit format ' Start, 8 bit data(LSB first), 1 parity, 1 stop. ' ' data is available on the falling edge of the clk ' gotsync=0 ' No synch received ScanIt: ASM CHK?RP PS2_SCLK CALL SkipPulse ; Skip the start bit CLRF _BitCounter ; clear bit counter ReadLoop BTFSC PS2_SCLK,PS2_SCLK_BIT ; wait for clk falling edge GOTO $-1 ; ENDASM @ MOVE?TB PS2_SDATA,PS2_SDATA_BIT,_SDATA KeyPS2.0[BitCounter]=SDATA ; Save current bit value ASM BTFSS PS2_SCLK,PS2_SCLK_BIT ; wait for clk falling edge GOTO $-1 ; INCF _BitCounter,F ; increment bit counter MOVLW 8 ; XORWF _BitCounter,W ; check if = 8 (end) BTFSS STATUS,Z ; GOTO ReadLoop ; if not read again CALL SkipPulse ; skip parity CALL SkipPulse ; skip stop bit GOTO GetOut ; get out of here to test the ; actual result SkipPulse BTFSC PS2_SCLK,PS2_SCLK_BIT ; wait for falling edge GOTO $-1 ; BTFSS PS2_SCLK,PS2_SCLK_BIT ; wait for rising edge GOTO $-1 ; RETURN ; Bye bye! GetOut ENDASM
The above don't check the parity, don't check the validity of the Start bit... it's just a plain and starter version.... sorry if it's mostly in ASM..
In PBP it should be something close to...
there's few other example here and there in the forum. Melanie also draw the whole PC ScanCode table.Code:ReadPS2: FOR COUNTERBIT=0 TO 10 WaitFallingEdge: if sclk then waitfallingedge KEY.0[COUNTERBit]=SDATA WaitRisingEdge: if sclk=0 then waitrisingedge NEXT KEY=(KEY>>1)&$ff return
at least
http://www.picbasic.co.uk/forum/show...ghlight=ps2%2A
http://www.picbasic.co.uk/forum/show...ight=scan+code





Bookmarks