EmbeddedRelated.com
Forums
Memfault Beyond the Launch

PS/2 Keyboard on 8051

Started by raje...@gmail.com June 5, 2006
Hi all 8051 Gurus,
I am new to this group and to 8051 as well. I tried to connect a ps/2
keyboard to my 8051 board with lcd(hd44780, 2x16) and wrote a program
to print the ascii character of the key pressed on lcd. I could detect
the make code and getting the key pressed but the break codes or some
other stuff adds some funny character after the real one. How to avoid
that ? and what should be the logic to detect the key ascii?
pls. help.
the code is :
; KBD1.ASM
;===============================================================
; AA (hex) i.e. 170 dec is power on test successfull

;
; H/W CONNECTIONS : KBD CLOCK connected to P3.2(INT0)
;                   KBD DATA  connected to P3.4
;
; pc-kbd, 89C52,
; assembler : asm51
;===============================================================

$MOD51  ; ASSEMBLER DIRECTIVE FOR ASM51 ASSEMBLER

	BYTE_IDX	EQU	30H

       CR       EQU     0
       L_SHIFT  EQU     0
       R_SHIFT  EQU     0
       CAPS_LOCK EQU     0
       NUM_LOCK EQU     0
       BACK_SPACE EQU     0
       ESC      EQU     0
       L_CTRL   EQU     0


ORG 0000H
        JMP START
ORG 0003H
        JMP EXT_INT0

START:
	CLR BYTE_IDX	;1ST BYTE IS MAKE CODE, THEN COMES TWO BYTES OF BREAK
CODE

        MOV  P3,#0FFH             ;Port 3 set for second function
        MOV IE,#81H             ;ExtInt0 ENABLED
        SETB IT0                ;EDGE TRIGGERED INT 0
        MOV DPTR, #COMMANDS
SEND_CMD:
        CLR A
        MOVC A,@A+DPTR
        JZ SEND_DATA
        ACALL control_word
        ACALL ddelay
        INC DPTR
        SJMP SEND_CMD

SEND_DATA:
AGAIN:
        mov     a,#08Fh ; 1st row  16TH COL
	acall	control_word
	acall	ddelay

        MOV DPTR, #MSG
NXT:    CLR A
        MOVC A,@A+DPTR
        JZ DISP1
        ACALL data_word
        ACALL ddelay
        INC DPTR
        SJMP NXT
DISP1:  SJMP AGAIN

WAIT:   SETB P2.2
        JMP WAIT
;--------------------------------------------
control_word:

	mov	r0,a
	anl	a,#0F0h
	swap 	a
        orl     a,#0e0h
        mov P2, A
	anl	a,#0cfh
        mov P2, A
	mov	a,r0
	anl	a,#0fh
        orl     a,#0e0h
        mov P2, A
	anl	a,#0cfh
        mov P2, A
        ret

data_word:
	mov	r0,a
	anl	a,#0F0h
	swap 	a
	orl	a,#0f0h
        mov P2, A
        anl     a,#0dfh
        mov P2, A
	mov	a,r0
	anl	a,#0fh
	orl	a,#0f0h
        mov P2, A
        anl     a,#0dfh
        mov P2, A
        ret

;***************************** isr
EXT_INT0:
;        MOV A, BYTE_IDX
;        CJNE A, #10,EXIT_ISR
KBD:       MOV R3,#8
           MOV R7,#0
KP1:       MOV C,P3.2
           JC KP1
     K4:   MOV C,P3.2
           JNC K4
K5:       MOV C,P3.2
           JC K5
           MOV C,P3.4
           MOV A,R7
           RRC A
           MOV R7,A
K6:        MOV C,P3.2
           JNC K6
           DJNZ R3,K5
           ACALL DELAY1
           MOV R5,A
           ;acc contains the key-scan code  **/*/*/*/*/*/*

        MOV DPTR,#SCAN2ASCII_LOOK_UP
        MOVC A,@A+DPTR

        ACALL data_word
        ACALL ddelay
	INC BYTE_IDX
SEC_INT:
EXIT_ISR:  RETI

;*************************************
DELAY1:    MOV R4,#80H
          DJNZ R4,$
          RET

;*************************************************
dDELAY:  MOV R3,#250     ;50
HERE:   MOV R4,#255
        DJNZ R4, $
        DJNZ R3, HERE
        RET

DELAY:
        MOV R1,#0FFH
        MOV R2,#0FFH
LP:     NOP
        NOP
        DJNZ R1,$
        DJNZ R2,LP
        RET

L_DELAY:
        MOV R1,#0FFH
        MOV R2,#0FFH
        MOV R3,#04h
LP1:    NOP
        NOP
        DJNZ R1,LP1
        DJNZ R2,LP1
        DJNZ R3,LP1
        RET

;##########################################################################
;BIN2BCD
;       CONVERTS 8-BIT BINARY VARIABLE IN ACC TO 3-DIGIT PACKED BCD
FORMAT.
;       HUNDRED'S PLACE LEFT IN VARIABLE 'HUND',
;       TEN'S AND ONE'S PLACES IN 'TENONE'.
;       USES/AFFECTS : ACC, B

        HUND    DATA 21H
        TENS    DATA 22H
        ONES    DATA 23H
BIN2BCD:
        MOV B,#100
        DIV AB
        MOV HUND,A
        MOV A,#10
        XCH A,B
        DIV AB
        MOV TENS,A
        MOV ONES,B
;        SWAP A
;        ADD A,B
;        MOV TENONE,A
        RET
;##########################################################################

COMMANDS:
        DB      028H, 028H      ;
        DB      0EH
        DB      01H
        DB      06H
        DB      07H
        DB      0

MSG:    DB      'KBD ', 0
SCAN2ASCII_LOOK_UP:
        DB      0       ;FOR SCAN-CODE 0 WHICH DOES NOT EXIST, I
SUPPOSE
        DB      0,0,0,0,0,0,0,0,0,0     ; scan code 001 to 010
        DB      0,0,0,0,0,0,0,0,L_SHIFT,L_CTRL     ; scan code 011 to
020
        DB      'Q','!',0,0,0,'Z','S','A','W','@'     ; scan code 021
to 030
        DB      0,0,'C','X','D','E','$','#',0,0     ; scan code 031 to
040
        DB      SP,'V','F','T','R','%',0,0,'N','B'     ; scan code 041
to 050
        DB      'H','G','Y','^',0,0,0,'M','J','U'     ; scan code 051
to 060
        DB      '&','*',0,0,'<','K','I','O',')','('     ; scan code 061
to 070
        DB      0,0,'>','?','L',':','P','_',0,0     ; scan code 071 to
080
        DB      0,'"',0,'{','+',0,0,CAPS_LOCK,R_SHIFT,CR     ; scan
code 081 to 090
        DB      '}',0,'|',0,0,0,0,0,0,0     ; scan code 091 to 100
        DB      0,BACK_SPACE,0,0,0,0,0,0,0,0     ; scan code 101 to 110
        DB      0,0,0,0,0,0,0,ESC,NUM_LOCK,0     ; scan code 111 to 120
        DB      0,0,0,0,0,0,0,0,0,0     ; scan code 121 to 130
        DB      0,0,0,0,0,0,0,0,0,0     ; scan code 131 to 140
        DB      0,0,0,0,0,0,0,0,0,0     ; scan code 141 to 150
        DB      0,0,0,0,0,0,0,0,0,0     ; scan code 151 to 160
        DB      0,0,0,0,0,0,0,0,0,0     ; scan code 161 to 170
        DB      0,0,0,0,0,0,0,0,0,0     ; scan code 171 to 180
        DB      0,0,0,0,0,0,0,0,0,0     ; scan code 181 to 190
        DB      0,0,0,0,0,0,0,0,0,0     ; scan code 191 to 200
        DB      0,0,0,0,0,0,0,0,0,0     ; scan code 201 to 210
        DB      0,0,0,0,0,0,0,0,0,0     ; scan code 211 to 220
        DB      0,0,0,0,0,0,0,0,0,0     ; scan code 221 to 230
        DB      0,0,0,0,0,0,0,0,0,0     ; scan code 231 to 240
        DB      0,0,0,0,0,0,0,0,0,0     ; scan code 241 to 250
        DB      0,0,0,0,0,0,0,0,0,0     ; scan code 251 to 260

;=========================================================================


END

thanks in anticipation.

Rajesh Bij

rajeshbij@gmail.com wrote:

> Hi all 8051 Gurus, > I am new to this group and to 8051 as well.
I would recommend you join the message board at 8052.com. There are lot's of 8051 developers there and the question about PS2 keyboards has been covered several times so the info you need should be there. Ian
rajeshbij@gmail.com wrote:
> Hi all 8051 Gurus, > I am new to this group and to 8051 as well. I tried to connect a ps/2 > keyboard to my 8051 board with lcd(hd44780, 2x16) and wrote a program > to print the ascii character of the key pressed on lcd. I could detect > the make code and getting the key pressed but the break codes or some > other stuff adds some funny character after the real one. How to avoid > that ? and what should be the logic to detect the key ascii? > pls. help. > thanks in anticipation. > > Rajesh Bij
Check this site: http://www.beyondlogic.org/keyboard/keybrd.htm It has everything you need to know. I did this myself a while ago, but I used a PIC16F628. I had absolutely no problems using this document. It gives you the scan codes for the keys. Also as a note, I didn't look over your code, you should always read the response byte after you pass a byte to the keyboard. If you don't it will likely reject (or just ignore) any subsequent commands you pass to it (Did reject on one of my keyboards although it is not supposed to I believe). -Isaac

Memfault Beyond the Launch