PICの出力\DATA | HI | LOW |
HI | LOW | LOW |
LOW | HI | LOW |
;------------------------------------ ;------KEYBOARD EMULATER BETA0------- ;------------------------------------ ;------COPYRIGHT GRAFZ.NET 2009------ ;------------------------------------ LIST P=16F84A #INCLUDE ERRORLEVEL -302 __CONFIG _CP_OFF & _WDT_OFF & _PWRTE_ON & _XT_OSC CBLOCK 0x0C WAIT_COUNT1 WAIT_COUNT2 COM_STATUS PARITY COUNT_BYTE BYTE_IN BYTE_OUT LASTDATA SW0_STATUS CNT_LOOP CNT_WAIT BUFFER_LETTER ENDC #DEFINE PS2_CLKOUT D'0' #DEFINE PS2_CLKIN D'1' #DEFINE PS2_DATAOUT D'2' #DEFINE PS2_DATAIN D'3' #DEFINE CONST_WAIT_MS D'250' #DEFINE CONST_WAIT_US D'8' #DEFINE F_VALIDDATA D'3' ;in COM_STATUS #DEFINE PS2BREAK D'5' ORG 0x00 GOTO START ORG 0x04 RETFIE START BSF STATUS,RP0 ;BANK1 MOVLW B'11111111' MOVWF TRISA MOVLW B'11111010';RB0 AND RB2 ARE OUTPUT MOVWF TRISB CLRF COM_STATUS MOVLW B'11111111' MOVWF SW0_STATUS BCF STATUS,RP0 ;BANK0 INITPOR MOVLW D'200' CALL WAIT MOVLW D'150' CALL WAIT MOVLW D'150' CALL WAIT BCF STATUS,RP0;bank0 BTFSS PORTB,PS2_DATAIN GOTO MAIN ;WHEN HOST MAKES DATA LO MOVLW H'AA' ;BAT IS OK CALL TRANSMITDATA MAIN CALL RECEIVEDATA BTFSC COM_STATUS,F_VALIDDATA GOTO HOSTCOMMAND ;WHEN NO HOSTCOMMAND RECIEVED CALL KEYMAIN1 CALL KEYMAIN2 CALL KEYMAIN3 CALL KEYMAIN4 CALL KEYMAIN5 CALL KEYMAIN6 CALL KEYMAIN7 CALL KEYMAIN8 GOTO MAIN HOSTCOMMAND MOVLW D'10' CALL WAIT ;------------------------------- ;--------HOSTCOMMAND------------ ;------------------------------- HC_START HC_RESET MOVLW H'FF' SUBWF BYTE_IN,0 BTFSS STATUS,Z GOTO HC_LED MOVLW H'FA' ;ACK CALL TRANSMITDATA MOVLW D'100' ;emulation of por of PIC itself CALL WAIT GOTO INITPOR ;BAT HC_LED MOVLW H'ED' SUBWF BYTE_IN,0 BTFSS STATUS,Z GOTO HC_ECHO MOVLW H'FA' ;ACK CALL TRANSMITDATA CALL WAIT100US HC_LED_LOOP CALL RECEIVEDATA BTFSS COM_STATUS,F_VALIDDATA GOTO HC_LED_LOOP BTFSC BYTE_IN,7;IF RESERVED BIT ISN'T 0, GOTO HC_START;IT'S NOT OPTION BYTE CALL WAIT100US MOVLW H'FA' ;ACK CALL TRANSMITDATA GOTO MAIN HC_ECHO MOVLW H'EE' SUBWF BYTE_IN,0 BTFSS STATUS,Z GOTO HC_SCANCODE MOVLW H'EE' CALL TRANSMITDATA GOTO MAIN HC_SCANCODE MOVLW H'F0' SUBWF BYTE_IN,0 BTFSS STATUS,Z GOTO HC_SETID MOVLW H'FA' ;ACK CALL TRANSMITDATA CALL WAIT100US HC_SCANCODE_LOOP CALL RECEIVEDATA BTFSS COM_STATUS,F_VALIDDATA GOTO HC_SCANCODE_LOOP BTFSC BYTE_IN,7;IF RESERVED BIT ISN'T 0, GOTO HC_START;IT'S NOT OPTION BYTE CALL WAIT100US MOVLW H'00' SUBWF BYTE_IN,0 BTFSS STATUS,Z GOTO HC_SCANCODE2 MOVLW H'FA' CALL TRANSMITDATA CALL WAIT100US MOVLW H'02' CALL TRANSMITDATA GOTO MAIN HC_SCANCODE2 MOVLW H'FA' CALL TRANSMITDATA GOTO MAIN HC_SETID MOVLW H'F2' SUBWF BYTE_IN,0 BTFSS STATUS,Z GOTO HC_REPEATRATE MOVLW H'FA' ;ACK CALL TRANSMITDATA CALL WAIT100US MOVLW H'AB' CALL TRANSMITDATA CALL WAIT100US MOVLW H'83' CALL TRANSMITDATA GOTO MAIN HC_REPEATRATE MOVLW H'F3' SUBWF BYTE_IN,0 BTFSS STATUS,Z GOTO HC_ENABLE MOVLW H'FA' ;ACK CALL TRANSMITDATA CALL WAIT100US HC_REPEATRATE_LOOP CALL RECEIVEDATA BTFSS COM_STATUS,F_VALIDDATA GOTO HC_REPEATRATE_LOOP BTFSC BYTE_IN,7;IF RESERVED BIT ISN'T 0, GOTO HC_START;IT'S NOT OPTION BYTE CALL WAIT100US MOVLW H'FA' ;ACK CALL TRANSMITDATA GOTO MAIN HC_ENABLE MOVLW H'F4' SUBWF BYTE_IN,0 BTFSS STATUS,Z GOTO HC_DISABLE MOVLW H'FA' ;ACK CALL TRANSMITDATA GOTO MAIN HC_DISABLE MOVLW H'F5' SUBWF BYTE_IN,0 BTFSS STATUS,Z GOTO HC_DEFAULT MOVLW H'FA' ;ACK CALL TRANSMITDATA GOTO MAIN HC_DEFAULT MOVLW H'F6' SUBWF BYTE_IN,0 BTFSS STATUS,Z GOTO HC_RESEND MOVLW H'FA' ;ACK CALL TRANSMITDATA GOTO MAIN HC_RESEND MOVLW H'FE' SUBWF BYTE_IN,0 BTFSS STATUS,Z GOTO HC_SETKEY0 MOVF LASTDATA,0 CALL TRANSMITDATA GOTO MAIN HC_SETKEY0 MOVLW H'FD' SUBWF BYTE_IN,0 BTFSS STATUS,Z GOTO HC_SETKEY1 MOVLW H'FA' ;ACK CALL TRANSMITDATA GOTO MAIN HC_SETKEY1 MOVLW H'FC' SUBWF BYTE_IN,0 BTFSS STATUS,Z GOTO HC_SETKEY2 MOVLW H'FA' ;ACK CALL TRANSMITDATA GOTO MAIN HC_SETKEY2 MOVLW H'FB' SUBWF BYTE_IN,0 BTFSS STATUS,Z GOTO HC_SETKEY3 MOVLW H'FA' ;ACK CALL TRANSMITDATA GOTO MAIN HC_SETKEY3 MOVLW H'FA' SUBWF BYTE_IN,0 BTFSS STATUS,Z GOTO HC_SETKEY4 MOVLW H'FA' ;ACK CALL TRANSMITDATA GOTO MAIN HC_SETKEY4 MOVLW H'F9' SUBWF BYTE_IN,0 BTFSS STATUS,Z GOTO HC_SETKEY5 MOVLW H'FA' ;ACK CALL TRANSMITDATA GOTO MAIN HC_SETKEY5 MOVLW H'F8' SUBWF BYTE_IN,0 BTFSS STATUS,Z GOTO HC_SETKEY6 MOVLW H'FA' ;ACK CALL TRANSMITDATA GOTO MAIN HC_SETKEY6 MOVLW H'F7' SUBWF BYTE_IN,0 BTFSS STATUS,Z GOTO HC_END MOVLW H'FA' ;ACK CALL TRANSMITDATA GOTO MAIN HC_END MOVLW H'FE' CALL TRANSMITDATA GOTO MAIN ;------------------------------------ ;-------------RECEIVE---------------- ;------------------------------------ RECEIVEDATA BCF COM_STATUS,F_VALIDDATA ;STARTBIT BCF STATUS,RP0 ;bank0 BTFSC PORTB,PS2_DATAIN RETURN ;WHEN NO STARTBIT FOUND MOVLW D'1' MOVWF PARITY ;INITIALISE MOVLW D'8' MOVWF COUNT_BYTE MOVLW D'15' CALL WAIT ;DATA RECEIVEDATA_data RRF BYTE_IN,1 CALL SENDCLK BTFSC PORTB,PS2_DATAIN ;CHECK HI OR LOW GOTO RECEIVEDATA_b1 RECEIVEDATA_b0 BCF BYTE_IN,7 GOTO RECEIVEDATA_data_end RECEIVEDATA_b1 BSF BYTE_IN,7 INCF PARITY,1 RECEIVEDATA_data_end BTFSS PORTB,PS2_CLKIN RETURN ;WHEN HOST MAKES CLK LOW, ABORT DECFSZ COUNT_BYTE,1 GOTO RECEIVEDATA_data ;PARITY BTFSS PORTB,PS2_CLKIN RETURN ;WHEN HOST MAKE CLK LOW, ABORT MOVLW B'00000001' ANDWF PARITY,1 ;BIT0 OF PARITY CALL SENDCLK BTFSC PORTB,PS2_DATAIN ;CHECK HI OR LOW GOTO RECEIVEDATA_PARITY1 ;RECEIVEDATA_PARITY0 MOVF PARITY,1 BTFSS STATUS,Z GOTO RECEIVEDATA_REQ_RESEND GOTO RECEIVEDATA_PARITYEND RECEIVEDATA_PARITY1 MOVF PARITY,1 BTFSC STATUS,Z GOTO RECEIVEDATA_REQ_RESEND RECEIVEDATA_PARITYEND ;STOPBIT CALL SENDCLK BTFSS PORTB,PS2_DATAIN ;CHECK HI OR LOW GOTO RECEIVEDATA_CONTROL ;WHEN NO STOPBIT FOUND ;OK BSF COM_STATUS,F_VALIDDATA ;FLAG = TRUE BSF PORTB,PS2_DATAOUT ;CONTROL BIT ON CALL SENDCLK BCF PORTB,PS2_DATAOUT ;CONTROL BIT OFF RETURN RECEIVEDATA_CONTROL RECEIVEDATA_CONTROL2 BTFSC PORTB,PS2_DATAIN GOTO RECEIVEDATA_REQ_RESEND ;WHEN DATA TURNS HI CALL SENDCLK ;SEND CLK UNTIL DATA BECOMES HI GOTO RECEIVEDATA_CONTROL2 RECEIVEDATA_REQ_RESEND BTFSS PORTB,PS2_CLKIN ;WAIT UNTIL CLK BECOMES HI GOTO RECEIVEDATA_REQ_RESEND RECEIVEDATA_REQ_RESEND1 CALL WAIT100US BTFSS PORTB,PS2_DATAIN GOTO RECEIVEDATA MOVLW H'FE' ;RESEND CALL TRANSMITDATA BTFSC COM_STATUS,PS2BREAK GOTO RECEIVEDATA_REQ_RESEND1 ;TRY ONCE AGAIN RETURN ;------------------------------------ ;-------------TRANSMIT--------------- ;------------------------------------ TRANSMITDATA MOVWF BYTE_OUT MOVWF LASTDATA BSF COM_STATUS,PS2BREAK MOVLW D'1' MOVWF PARITY MOVLW D'8' MOVWF COUNT_BYTE BCF STATUS,RP0 ;BANK0 ;STARTBIT BTFSS PORTB,PS2_CLKIN GOTO TRANSMITDATA_BREAK ;ABORT BTFSS PORTB,PS2_DATAIN GOTO TRANSMITDATA_BREAK ;ABORT BSF PORTB,PS2_DATAOUT ;STARTBIT IS LOW CALL SENDCLK ;DATA TRANSMITDATA_DATA BTFSC BYTE_OUT,0 GOTO TRANSMITDATA_DATA1 ;TRANSMITDATA_DATA0 BSF PORTB,PS2_DATAOUT GOTO TRANSMITDATA_CLK TRANSMITDATA_DATA1 BCF PORTB,PS2_DATAOUT INCF PARITY,1 TRANSMITDATA_CLK CALL SENDCLK BTFSS PORTB,PS2_CLKIN GOTO TRANSMITDATA_BREAK ;ABORT RRF BYTE_OUT,1 ;SHIFT DECFSZ COUNT_BYTE,1 GOTO TRANSMITDATA_DATA ;REPEATS 8 TIMES ;PARITY BTFSC PARITY,0 GOTO TRANSMITDATA_PARITY1 ;TRANSMITDATA_PARITY0 BSF PORTB,PS2_DATAOUT GOTO TRANSMITDATA_PARITY_CLK TRANSMITDATA_PARITY1 BCF PORTB,PS2_DATAOUT TRANSMITDATA_PARITY_CLK BTFSS PORTB,PS2_CLKIN GOTO TRANSMITDATA_BREAK ;ABORT CALL SENDCLK ;STOPBIT BCF PORTB,PS2_DATAOUT CALL SENDCLK ;OK BCF COM_STATUS,PS2BREAK RETURN TRANSMITDATA_BREAK BSF COM_STATUS,PS2BREAK BCF PORTB,PS2_DATAOUT RETURN ;------------------------------------ ;-------------UTILS------------------ ;------------------------------------ SENDCLK CALL WAIT10US CALL WAIT10US BSF PORTB,PS2_CLKOUT CALL WAIT10US CALL WAIT10US CALL WAIT10US BCF PORTB,PS2_CLKOUT CALL WAIT10US RETURN WAIT10MS MOVLW D'90' MOVWF CNT_WAIT LOOP_WAIT10MS CALL WAIT100US CALL WAIT10US DECFSZ CNT_WAIT,F GOTO LOOP_WAIT10MS RETURN WAIT1MS MOVLW D'9' MOVWF CNT_WAIT LOOP_WAIT1MS CALL WAIT100US CALL WAIT10US DECFSZ CNT_WAIT,F GOTO LOOP_WAIT1MS RETURN WAIT100US MOVLW D'49' MOVWF CNT_LOOP LOOP_WAIT100US GOTO $+1 DECFSZ CNT_LOOP,F GOTO LOOP_WAIT100US RETURN WAIT10US MOVLW D'4' MOVWF CNT_LOOP LOOP_WAIT10US GOTO $+1 DECFSZ CNT_LOOP,F GOTO LOOP_WAIT10US RETURN WAIT6US MOVLW D'2' MOVWF CNT_LOOP LOOP_WAIT6US GOTO $+1 DECFSZ CNT_LOOP,F GOTO LOOP_WAIT6US RETURN WAIT MOVWF WAIT_COUNT1 WAIT_LOOP1 MOVLW CONST_WAIT_MS MOVWF WAIT_COUNT2 WAIT_LOOP2 NOP NOP NOP NOP NOP NOP NOP DECFSZ WAIT_COUNT2,1 GOTO WAIT_LOOP2 DECFSZ WAIT_COUNT1,1 GOTO WAIT_LOOP1 RETURN WAITUS MOVWF WAIT_COUNT1 WAITUS_LOOP1 MOVLW CONST_WAIT_US MOVWF WAIT_COUNT2 WAITUS_LOOP2 DECFSZ WAIT_COUNT2,1 GOTO WAITUS_LOOP2 DECFSZ WAIT_COUNT1,1 GOTO WAITUS_LOOP1 RETURN ;------------------------------------ ;----------AUTOMATION---------------- ;------------------------------------ KEYMAIN1 BCF STATUS,RP0 ;BANK0 IF4 BTFSS PORTB,4 GOTO IF4FALSE IF4TRUE ;WHEN CURRENT IS 1 BTFSS SW0_STATUS,0 CALL BREAK_P;CURRENT 1, LAST 0 BSF SW0_STATUS,0 RETURN IF4FALSE ;WHEN CURRENT IS 0 BCF SW0_STATUS,0 RETURN BREAK_P CALL WAIT10MS BTFSC PORTB,4 CALL MAKE_PHRASE1 ;SKIPPED WHEN CHATTERING RETURN KEYMAIN2 BCF STATUS,RP0 ;BANK0 BTFSS PORTB,5 GOTO KEYMAIN2_CURRENT_LO ;CURRENT IS HI BTFSS SW0_STATUS,1 ;LASTDATA CALL KEYMAIN2_BREAK BSF SW0_STATUS,1 RETURN KEYMAIN2_CURRENT_LO BCF SW0_STATUS,1 RETURN KEYMAIN2_BREAK CALL WAIT10MS BTFSC PORTB,5 CALL MAKE_PHRASE2 RETURN KEYMAIN3 BCF STATUS,RP0 ;BANK0 BTFSS PORTB,6 GOTO KEYMAIN3_CURRENT_LO ;CURRENT IS HI BTFSS SW0_STATUS,2 ;LASTDATA CALL KEYMAIN3_BREAK BSF SW0_STATUS,2 RETURN KEYMAIN3_CURRENT_LO BCF SW0_STATUS,2 RETURN KEYMAIN3_BREAK CALL WAIT10MS BTFSC PORTB,6 CALL MAKE_PHRASE3 RETURN KEYMAIN4 BCF STATUS,RP0 ;BANK0 BTFSS PORTB,7 GOTO KEYMAIN4_CURRENT_LO ;CURRENT IS HI BTFSS SW0_STATUS,3 ;LASTDATA CALL KEYMAIN4_BREAK BSF SW0_STATUS,3 RETURN KEYMAIN4_CURRENT_LO BCF SW0_STATUS,3 RETURN KEYMAIN4_BREAK CALL WAIT10MS BTFSC PORTB,7 CALL MAKE_PHRASE4 RETURN KEYMAIN5 BCF STATUS,RP0 ;BANK0 BTFSS PORTA,0 GOTO KEYMAIN5_CURRENT_LO ;CURRENT IS HI BTFSS SW0_STATUS,4 ;LASTDATA CALL KEYMAIN5_BREAK BSF SW0_STATUS,4 RETURN KEYMAIN5_CURRENT_LO BCF SW0_STATUS,4 RETURN KEYMAIN5_BREAK CALL WAIT10MS BTFSC PORTA,0 CALL MAKE_PHRASE5 RETURN KEYMAIN6 BCF STATUS,RP0 ;BANK0 BTFSS PORTA,1 GOTO KEYMAIN6_CURRENT_LO ;CURRENT IS HI BTFSS SW0_STATUS,5 ;LASTDATA CALL KEYMAIN6_BREAK BSF SW0_STATUS,5 RETURN KEYMAIN6_CURRENT_LO BCF SW0_STATUS,5 RETURN KEYMAIN6_BREAK CALL WAIT10MS BTFSC PORTA,1 CALL MAKE_PHRASE6 RETURN KEYMAIN7 BCF STATUS,RP0 ;BANK0 BTFSS PORTA,2 GOTO KEYMAIN7_CURRENT_LO ;CURRENT IS HI BTFSS SW0_STATUS,6 ;LASTDATA CALL KEYMAIN7_BREAK BSF SW0_STATUS,6 RETURN KEYMAIN7_CURRENT_LO BCF SW0_STATUS,6 RETURN KEYMAIN7_BREAK CALL WAIT10MS BTFSC PORTA,2 CALL MAKE_PHRASE7 RETURN KEYMAIN8 BCF STATUS,RP0 ;BANK0 BTFSS PORTA,3 GOTO KEYMAIN8_CURRENT_LO ;CURRENT IS HI BTFSS SW0_STATUS,7 ;LASTDATA CALL KEYMAIN8_BREAK BSF SW0_STATUS,7 RETURN KEYMAIN8_CURRENT_LO BCF SW0_STATUS,7 RETURN KEYMAIN8_BREAK CALL WAIT10MS BTFSC PORTA,3 CALL MAKE_PHRASE8 RETURN ;--------------- MAKE_LETTER MOVWF BUFFER_LETTER CALL TRANSMITDATA MOVLW D'4' CALL WAIT MOVLW H'F0' CALL TRANSMITDATA CALL WAIT1MS MOVF BUFFER_LETTER,0 CALL TRANSMITDATA MOVLW D'4' CALL WAIT RETURN MAKE_LETTER_EX MOVWF BUFFER_LETTER MOVLW H'E0' CALL TRANSMITDATA CALL WAIT1MS MOVF BUFFER_LETTER,0 CALL TRANSMITDATA MOVLW D'4' CALL WAIT MOVLW H'E0' CALL TRANSMITDATA CALL WAIT1MS MOVLW H'F0' CALL TRANSMITDATA CALL WAIT1MS MOVF BUFFER_LETTER,0 CALL TRANSMITDATA MOVLW D'4' CALL WAIT RETURN MAKE_SHIFT MOVLW H'12' CALL TRANSMITDATA MOVLW D'4' CALL WAIT RETURN BREAK_SHIFT MOVLW H'F0' CALL TRANSMITDATA CALL WAIT1MS MOVLW H'12' CALL TRANSMITDATA MOVLW D'4' CALL WAIT RETURN ;--------------- MAKE_PHRASE1 MOVLW H'1C' ;a CALL MAKE_LETTER RETURN MAKE_PHRASE2 CALL MAKE_SHIFT MOVLW H'1C' ;A CALL BREAK_SHIFT RETURN MAKE_PHRASE3 RETURN MAKE_PHRASE4 RETURN MAKE_PHRASE5 RETURN MAKE_PHRASE6 RETURN MAKE_PHRASE7 RETURN MAKE_PHRASE8 RETURN END |