Ce document correspond à la version en cache proposée par G o o g l e pour la page http://www.technology.niagarac.on.ca/courses/comp630/Projects/KeyboardEmulator/keyboard.asm.
La version « En cache » proposée par G o o g l e correspond à la page telle qu’elle se présentait lors de la dernière consultation effectuée par Google.
Il se peut que la page ait été modifiée depuis cette date. Cliquez ici pour consulter la page actuelle (sans mises en valeur).
Pour créer un lien avec cette page ou l'inclure dans vos favoris/signets, utilisez l'adresse suivante : http://www.google.com/search?q=cache:r4eK-3MJ7KUJ:www.technology.niagarac.on.ca/courses/comp630/Projects/KeyboardEmulator/keyboard.asm+&hl=fr&ie=UTF-8.


Google n'est ni affilié aux auteurs de cette page ni responsable de son contenu.

;*******************************************************************
;	KEYBOARD.ASM
;	Caroline Jones, 9503929
;	December 9, 1999
;	Keyboard Emulator - receives ASCII data and outputs
;			    keyboard scan codes
;*******************************************************************
;   Target: PIC16F84 on PICPROTO-II Board
;
;   Hardware:
;	RA0	T1OUT of MAX232
;	RB0	Pin 2 of DIN Connector
;	RB1	Pin 1 of DIN Connector
;
;*******************************************************************
        LIST P=16F84
        include "P16F84.INC"

;*******REGISTER DEFINES********************************************
temp		equ	10	;temporary storage
count		equ	11	;used in delay loops
count2		equ	12
count3		equ	13
bit_count       equ     14	;used in loop to receive & send 
				;eight bits of data
keypressed	equ	15	;ASCII value received
parity		equ	16	;parity bit
scan_code	equ	17	;keyboard scan code sent

;*******VARIABLES***************************************************
CLOCKBIT	equ	H'0001'
DATABIT		equ	H'0000'
RECEIVE		equ	H'0000'

	goto   main

;*******Keyboard Scan Code Table************************************
ScanCodeTable
        ADDWF  PCL,F
	retlw   29
        retlw   16
        retlw   52
        retlw   26
        retlw   25
        retlw   2E
        retlw   3D
        retlw   52
        retlw   46
        retlw   45
	retlw	3E
	retlw	79
	retlw	41
	retlw	7B
	retlw	49
	retlw	4A
	retlw	70
	retlw	69
	retlw	72
	retlw	7A
	retlw	6B
	retlw	73
	retlw	74
	retlw	6C
	retlw	75
	retlw	7D
	retlw	4C
	retlw	4C
	retlw	41
	retlw	55
	retlw	49
	retlw	4A
	retlw	1E
	retlw	1C
	retlw	32
	retlw	21
	retlw	23
	retlw	24
	retlw	2B
	retlw	34
	retlw	33
	retlw	43
	retlw	3B
	retlw	42
	retlw	4B
	retlw	3A
	retlw	31
	retlw	44
	retlw	4D
	retlw	15
	retlw	2D
	retlw	1B
	retlw	2C
	retlw	3C
	retlw	2A
	retlw	1D
	retlw	22
	retlw	35
	retlw	1A
	retlw	54
	retlw	5D
	retlw	5B
	retlw	36
	retlw	4E
	retlw	0E
	retlw	1C
	retlw	32
	retlw	21
	retlw	23
	retlw	24
	retlw	2B
	retlw	34
	retlw	33
	retlw	43
	retlw	3B
	retlw	42
	retlw	4B
	retlw	3A
	retlw	31
	retlw	44
	retlw	4D
	retlw	15
	retlw	2D
	retlw	1B
	retlw	2C
	retlw	3C
	retlw	2A
	retlw	1D
	retlw	22
	retlw	35
	retlw	1A
	retlw	54
	retlw	5D
	retlw	5B
	retlw	0E
        RETLW	80

;*******Function Keys Code Table************************************
FunctionKeyCodeTable
        ADDWF  PCL,F
	retlw   00
        retlw   05
        retlw   06
        retlw   04
        retlw   0C
        retlw   03
        retlw   0B
        retlw   83
        retlw   66
        retlw   0D
	retlw	0A
	retlw	01
	retlw	09
	retlw	5A
	RETLW	80

;*******MAIN PROGRAM************************************************
main 
        clrf    PORTB		;make portb lines low
        bsf     STATUS,RP0	;select page 1
        movlw   0xFF            ;portb lines all inputs
        movwf   TRISB
        bcf     STATUS,RP0

GetNextKeyPress
        clrf    PORTA		;make porta lines low
        bsf     STATUS,RP0	;select page 1
        movlw   b'00000001'		;porta lines all input
        movwf   TRISA
        bcf     STATUS,RP0	

;receive RS232 data
	movlw	.8
	movwf	bit_count

PollForRS232StartBit
	btfsc	PORTA,RECEIVE
	goto	PollForRS232StartBit

;receive the data at 1200 baud rate
	call	Delay3
	call	Delay3

ReceiveNextBit
	btfss	PORTA,RECEIVE
	goto	ReceiveZero
	bsf	STATUS,C
	goto	ShiftIncomingData
ReceiveZero
	bcf	STATUS,C
ShiftIncomingData
	rrf	keypressed,f
	call	Delay3
	call	Delay3
	decfsz  bit_count,f
	goto	ReceiveNextBit

;*******************************************************************
;ASCII values must be checked to see if they are the secondary 
;values of the keys, ex. capital letters.  If they are, a shift code ;must be sent before the scan code for the actual key. The following ;code checks for secondary key values.
;*******************************************************************
;Must check ASCII value to see if an extended key has been entered.

	movlw	0E
	subwf	keypressed,w
	btfss	STATUS,C
	goto	FunctionKeys

	movlw	1A
	subwf	keypressed,w
	btfsc	STATUS,Z
	goto	CtrlAltDel

	movlw	1B
	subwf	keypressed,w
	btfsc	STATUS,Z
	goto	EscPressed

	movlw	7F
	subwf	keypressed,w
	btfsc	STATUS,Z
	goto	DeletePressed

;If the key pressed is not one of the extended keys, and its ASCII ;value is below 20 hex, it is not being dealt with and must be ;ignored.
	movlw	20
	subwf	keypressed,w
	btfss	STATUS,C
	goto	GetNextKeyPress


;Check for individual keys, exceptions to patterns
	movlw	20
	call	CheckAsciiValue

	movlw	27
	call	CheckAsciiValue

	movlw	3B
	call	CheckAsciiValue
	
	movlw	3D
	call	CheckAsciiValue

	movlw	5E
	subwf	keypressed,w
	btfsc	STATUS,Z
	goto	ShiftNeeded

	movlw	5F
	subwf	keypressed,w
	btfsc	STATUS,Z
	goto	ShiftNeeded	

;Check groups of keys, sections of the ASCII table that either need ;the shift key or don't.
	movlw	2B
	subwf	keypressed,w
	btfss	STATUS,C
	goto	ShiftNeeded

	movlw	3A
	subwf	keypressed,w
	btfss	STATUS,C
	goto	NoShiftNeeded

	movlw	5B
	subwf	keypressed,w
	btfss	STATUS,C
	goto	ShiftNeeded

	movlw	7B
	subwf	keypressed,w
	btfss	STATUS,C
	goto	NoShiftNeeded

	movlw	7F
	subwf	keypressed,w
	btfss	STATUS,C
	goto	ShiftNeeded

;If the ASCII value received is not in any of these ranges, it's not ;being dealt with, and program goes back to scanning for data.

	goto	GetNextKeyPress

;If a the ESC or DELETE key was sent load the keyboard scan code for
;appropriate key.
EscPressed
	movlw	76
	movwf	scan_code
	call	TransmitWithoutShift
	goto	GetNextKeyPress
DeletePressed
	movlw	0xE0
	movwf	scan_code
	call	Transmit
	movlw	71
	movwf	scan_code
	call	Transmit
	goto	GetNextKeyPress
NoShiftNeeded
	call	Convert
	call	TransmitWithoutShift
	goto	GetNextKeyPress

ShiftNeeded
	call	Convert
	call	TransmitWithShift
	goto    GetNextKeyPress

FunctionKeys
	movf	keypressed,w
        call    FunctionKeyCodeTable
        movwf   scan_code
	call	TransmitWithoutShift
	goto	GetNextKeyPress

CtrlAltDel
	movlw	0x14		;tell the PC the CTRL key is being pressed
	movwf	scan_code
        call	Transmit
	call	Delay2		;short delay between transmissions
	movlw	0x11		;ALT key is pressed
	movwf	scan_code	
	call	Transmit
	call	Delay2
	movlw	0xE0		;delete key has been pressed
	movwf	scan_code	;delete is a 2 byte code, EO71
	call	Transmit
	call	Delay2
	movlw	0x71
	movwf	scan_code
	call	Delay2
	call	Transmit
	call	ReleaseKey
	movlw	0x14		;CTRL key has been released
	movwf	scan_code
        call	Transmit
	call	Delay2		;short delay between transmissions
	call	ReleaseKey
	movlw	0x11		;ALT key is released
	movwf	scan_code	
	call	Transmit
	call	Delay2
	call	ReleaseKey
	movlw	0xE0		;delete key has been released
	movwf	scan_code	;delete is a 2 byte code, EO71
	call	Transmit
	call	Delay2
	movlw	0x71
	movwf	scan_code
	call	Transmit
	goto	GetNextKeyPress



;*******FUNCTIONS***************************************************
;*******Delay-provides delay required for the frequency expected by ;	      the PC from the keyboard
Delay
        movlw   .2
        movwf   count
GoAgain
        nop
        decfsz  count,f
        goto    GoAgain        
        return
;*******************************************************************
;*******Delay2-0.6ms delay
Delay2                    
        movlw   .250          ;Load count2 with constant
        movwf   count2 
delaya  decfsz  count2,f      ;Dec count2 until zero
        goto    delaya 
        movlw   .250          ;Load count2 with constant
        movwf   count2 
delayb  decfsz  count2,f      ;Dec count2 until zero
        goto    delayb 
        return
;*******************************************************************
;*******Delay3-used for RS232, provides a baud rate of 1200
Delay3
	movlw	.138
	movwf	count3
dec3	decfsz	count3,f
	goto	dec3
	return       
;*******************************************************************
;*******Transmit-sends data stored in scan_code register to the keyboard
Transmit
;poll portb to check both clock & data lines are high (idle), when 
;both are high the keyboard is free to send data
GetKeyboardStatus
	movf	PORTB,w
	btfsc	PORTB,CLOCKBIT
	btfss	PORTB,DATABIT
	goto	GetKeyboardStatus

	clrf    PORTB
        bsf     STATUS,RP0
        movlw   b'11111100'	;set clock & data lines to output
        movwf   TRISB
        bcf     STATUS,RP0

        ;set up counter for sending 8 bits of data
	movlw	.8
        movwf   bit_count
	clrf	parity		;clear parity register
        
        ;send start bit, 0
        bcf     PORTB,DATABIT
        nop     ;small delay then bring clock low
        nop

        bcf     PORTB,CLOCKBIT
        call    Delay
        call    Delay
        nop
        nop
        
SendNextBit
        bsf     PORTB,CLOCKBIT
        call    Delay
	btfss	scan_code,0
	goto	ShiftToRight
	bsf	PORTB,DATABIT
	incf	parity,f
	goto	SendZero
ShiftToRight
	nop
	bcf	PORTB,DATABIT
SendZero
	call	Delay
        rrf     scan_code,f
	
        bcf     PORTB,CLOCKBIT
	call	Delay
	nop
	nop
        nop
	nop
	call	Delay
        decfsz  bit_count,f
	goto	SendNextBit


        bsf     PORTB,CLOCKBIT
	call	Delay

        ;calculate parity bit and send
	movlw	0xFF
	xorwf	parity,f
	btfss	parity,0
	goto	SendZeroParityBit
        bsf     PORTB,DATABIT

	goto	SentHighParity	
SendZeroParityBit
	bcf	PORTB,DATABIT
SentHighParity
	call	Delay
        bcf     PORTB,CLOCKBIT

        call    Delay
        call    Delay
        nop
        nop
        bsf     PORTB,CLOCKBIT

	;send stop bit
        call    Delay
        nop
        bsf     PORTB,DATABIT
	call	Delay
        nop
        bcf     PORTB,CLOCKBIT
        call    Delay
        call    Delay
        nop
        nop
        nop
        bsf     PORTB,CLOCKBIT

	return
;*******************************************************************
;*******TransmitWithShift-sends a shift to the keyboard followed by ;			  the data in the scan_code register
TransmitWithShift
	movf	scan_code,w	;save the value in scan_code
	movwf	temp
	movlw	0x12		;tell the PC the shift key is being pressed
	movwf	scan_code
        call	Transmit
	call	Delay2		;short delay between transmissions
	movf	temp,w
	movwf	scan_code	;send required data
	call	Transmit
	call	Delay2
	call	ReleaseKey
	movf	temp,w
	movwf	scan_code
	call	Transmit
	call	Delay2
	call	ReleaseKey
	movlw	0x12
	movwf	scan_code
	call	Transmit
	return
;*******************************************************************
;*******TransmitWithoutShift-sends data to the keyboard, and tells 
;			     it that the key has been released
TransmitWithoutShift
	movf	scan_code,w	;save the value in scan_code
	movwf	temp
	call	Transmit
	call	Delay2
	call	ReleaseKey
	movf	temp,w
	movwf	scan_code
	call	Transmit
	return
;*******************************************************************
;*******Convert-convert ASCII code in keypressed register received 
;		via RS232 into the scan code expected from the 
;		keyboard and save in scan_code register
Convert
	movlw	20
	subwf	keypressed,w
        call    ScanCodeTable
        movwf   scan_code
	return
;*******************************************************************
;*******CheckAsciiValue-checks ASCII value in w to see if the 
;			keyboard scan code will need a shift
CheckAsciiValue
	subwf	keypressed,w
	btfsc	STATUS,Z
	goto	NoShiftNeeded
	return
;*******************************************************************
;*******ReleaseKey-sends data to tell PC a key has been released
ReleaseKey
	movlw	0xF0
	movwf	scan_code
	call	Transmit
	call	Delay2
	return
;*******************************************************************
       END