Sidebar: Listing 1. KBE Source Code include "P16c84.inc" ;PIC-Based Keyboard Emulator ;Author: Tim Hamel ;Date: June 27th, 2k dta equ H'0001' clk equ H'0000' dela equ 12 pass equ 13 count equ 14 parity equ 15 send equ 16 delb equ 17 org 0 goto main ;****Delay routine for 25kHz Clock**** delay movlw .13 movwf dela delay1 decfsz dela goto delay1 return ;****** ;****Routine to Receive data from PC**** PC_rx movlw 9 ; Read in 8 bits + parity movwf count movlw 0x02 tris PORTA PC_rx1 call delay bcf PORTA,clk bcf STATUS, C ; What is the bit coming in? btfsc PORTA, dta bsf STATUS, C ; High Bit rrf pass ; Shift in the Bit call delay ; Wait for Bit to Go High bsf PORTA,clk decfsz count ; Are we done with 9 bits? goto PC_rx1 ; No, decrement 'count' and receive next bit bcf PORTA,clk ; Yes, clock out stop bit call delay bsf PORTA,clk wait bcf PORTA,clk call delay bsf PORTA,clk btfss PORTA,dta ; Wait for PC to take DATA line low. goto wait movlw 0 tris PORTA bcf PORTA,dta ; send ACK call delay bcf PORTA,clk call delay bsf PORTA,clk bsf PORTA,dta return ;****** ;****Transmit Routine**** KB_tx movwf send movlw 0 tris PORTA call delay bcf PORTA,dta bcf PORTA,clk ; Send a 0 start bit. call delay bsf PORTA,clk movlw .8 movwf count clrf parity sendnext ; Start sending response byte btfss send,0 goto shiftright incf parity,f bsf PORTA,dta goto sendzero shiftright bcf PORTA,dta sendzero call delay rrf send,f bcf PORTA,clk call delay bsf PORTA,clk decfsz count,f goto sendnext bsf PORTA,clk movlw 0xFF xorwf parity,f btfss parity,0 goto sendlow bsf PORTA,dta goto sendhigh sendlow bcf PORTA,dta sendhigh call delay bcf PORTA,clk call delay bsf PORTA,clk call delay ; send STOP bit (1) bcf PORTA,clk bsf PORTA,dta call delay bsf PORTA,clk call delay movlw 0x03 tris PORTA return ;****** ;****Start-up Delay**** longdly clrf dela movlw 9 movwf dela dele movlw 0xFF movwf delb delc decfsz delb goto delc deld decfsz dela goto dele return ;****** ;****Determine what the PC Sent**** Convert movf pass,W xorlw 0xFF ; Did the PC send a reset signal? btfsc STATUS,Z retlw 0xAA ; Yes, send Acknowledge movf pass,W xorlw 0xED ; No, maybe PC is trying to turn on Status LEDs btfsc STATUS,Z goto test ; EDh means "Set LEDs" retlw 0x00 ; We don't care at this point, send ; 'Acknowledge' anyway. test movlw 0xFA call KB_tx call PC_rx return ;***** main: movlw 0x00 ; Make PORTA output tris PORTA call longdly movlw 0xAA ; Initial start-up, send AAh call KB_tx ; Transmit it... loopi movwf 0x03 tris PORTA btfsc PORTA,dta ; Wait for DATA line to go low. goto $-1 ; Meaning PC wants to TX. call PC_rx ; Receive PC data. call Convert ; What is it? movwf pass movlw 0xFA call KB_tx ; Send 'Acknowledge'. movf pass,W call KB_tx goto loopi ; Repeat all this endlessly. end