; Keyboard Encoder ; ; Written by:Steven Easley ; Email: seasley@robotdungeon.com ; Webpage: www.robotdungeon.com ; ; ; list p=16f877a ; list directive to define processor #include ; processor specific variable definitions errorlevel -302 __CONFIG _CP_OFF & _WDT_OFF & _BODEN_OFF & _PWRTE_ON & _HS_OSC & _LVP_OFF & _CPD_OFF ;Define the port data #DEFINE KB_DATo PORTA,0 #DEFINE KB_CLKo PORTA,1 #DEFINE KB_DATi PORTA,2 #DEFINE KB_CLKi PORTA,3 #DEFINE stallr_a PORTA,5 #DEFINE scan_enable PORTB,7 #define KB_ERROR 0xFE cblock 0x20 temp ;misc scratch byte counter ;used in the SPI routines ; Used by the PS2 keyboard routines parity kb_data host_data RX_error KB_init wait_counter temp0 ; RAM to hold the current and last button states portA_mirror portA_last portB_mirror portB_last portC_mirror portC_last portD_mirror portD_last portE_mirror portE_last ;Delay variables d1 d2 d3 endc org 0x0000 goto init org 0x0004 goto ISR ISR bcf INTCON, GIE ; disable interrupts bcf INTCON, INTF bsf INTCON, GIE ; enable interrupts retfie ;---------------------------------------------------------- Receive_device: clrf kb_data btfss KB_CLKi ;Test for Request-to-send retlw 0xFE btfsc KB_DATi retlw 0xFE movlw 0x08 ;Count 8 rising edges movwf counter movlw 0xFF clrf wait_counter Start_bit: decfsz wait_counter, f goto here0 retlw 0xFE here0: btfsc KB_CLKi ;Wait for clock to go low goto Start_bit btfsc KB_DATi ;Is start bit low retlw 0xFE ;If not exit with error movlw 0xFF clrf wait_counter Wait_Start_bit: decfsz wait_counter, f goto here1 retlw 0xFE here1: btfss KB_CLKi ;Wait for clock to go high goto Wait_Start_bit RX_data: decfsz wait_counter, f goto here2 retlw 0xFE here2: btfsc KB_CLKi ;Wait for clock to go low goto RX_data ;Data is valid when clock is low bcf STATUS, C btfsc KB_DATi bsf STATUS, C rrf kb_data,f movlw 0xFF clrf wait_counter Wait_RX_data: decfsz wait_counter, f goto here3 retlw 0xFE here3: btfss KB_CLKi ;Wait for clock to go high goto Wait_RX_data movlw 0xFF clrf wait_counter decfsz counter, f goto RX_data movlw 0xFF clrf wait_counter Parity_bit: decfsz wait_counter, f goto here4 retlw 0xFE here4: btfsc KB_CLKi ;Wait for clock to go low goto Parity_bit clrf parity ;Check and record parity btfsc KB_DATi incf parity, f movlw 0xFF clrf wait_counter Wait_Parity_bit: decfsz wait_counter, f goto here5 retlw 0xFE here5: btfss KB_CLKi ;Wait for clock to go high goto Wait_Parity_bit movlw 0xFF clrf wait_counter Stop_bit: decfsz wait_counter, f goto here6 retlw 0xFE here6: btfsc KB_CLKi ;Wait for clock to go low goto Stop_bit movlw 0xFF clrf wait_counter Wait_Stop_bit: decfsz wait_counter, f goto here7 retlw 0xFE here7: btfss KB_CLKi ;Wait for clock to go high goto Wait_Stop_bit return ;---------------------------------------------------------- Send_device: movwf temp0 btfss KB_CLKi ;Test for Request-to-send retlw 0xFE btfss KB_DATi retlw 0xFE movlw 0x08 ;Count 8 rising edges movwf counter bsf STATUS, RP0 ;pull clock low bcf KB_CLKi bcf STATUS, RP0 call delay_20us ;100us delay call delay_20us call delay_20us call delay_20us call delay_20us bsf STATUS, RP0 ;pull data low and release clock bcf KB_DATi bsf KB_CLKi bcf STATUS, RP0 RX_data0: btfsc KB_CLKi ;Wait for clock to go low goto RX_data0 ;Data is valid when clock is high movf temp0, w xorwf parity, f bsf STATUS, RP0 andlw 0x01 btfss STATUS, Z bsf KB_DATi btfsc STATUS, Z bcf KB_DATi bcf STATUS, RP0 rrf temp0, f RX_data1: btfss KB_CLKi ;Wait for clock to go high goto RX_data1 decfsz counter, f goto RX_data0 Parity_bit0: btfsc KB_CLKi ;Wait for clock to go low goto Parity_bit0 bsf STATUS, RP0 ;Set parity btfss parity, 0 bsf KB_DATi btfsc parity, 0 bcf KB_DATi bcf STATUS, RP0 Wait_Parity_bit0: btfss KB_CLKi ;Wait for clock to go high goto Wait_Parity_bit0 Stop_bit0: btfsc KB_CLKi ;Wait for clock to go low goto Stop_bit0 bsf STATUS, RP0 ;Set stop and release data line bsf KB_DATi bcf STATUS, RP0 Wait_Stop_bit0: btfss KB_CLKi ;Wait for clock to go high goto Wait_Stop_bit0 Ack_bit0: btfsc KB_CLKi ;Wait for clock to go low goto Ack_bit0 btfss KB_DATi retlw 0xFE Ack_bit1: btfss KB_CLKi ;Wait for clock to go high goto Ack_bit1 retlw 0x00 ;---------------------------------------------------------- Send_host: movwf temp0 InhibitLoop btfss KB_CLKo ;Test for inhibit goto InhibitLoop call delay_20us call delay_20us btfss KB_CLKo goto InhibitLoop btfss KB_DATo ;Test for inhibit retlw 0xFE clrf parity movlw 0x08 movwf counter movlw 0x00 call KBBitOut ;Start bit (0) btfss KB_CLKo ;Test for inhibit goto Send_host_end nop nop nop nop Send_host_data: movf temp0, w xorwf parity, f call KBBitOut ;Data bits btfss KB_CLKo ;Test for inhibit goto Send_host_end rrf temp0, f decfsz counter, f goto Send_host_data nop nop comf parity, w call KBBitOut ;Parity bit btfss KB_CLKo ;Test for inhibit goto Send_host_end nop nop nop nop nop movlw 0xFF call KBBitOut ;Stop bit (1) call delay_20us retlw 0x00 Send_host_end: ;Release clock and data and abort transmission bsf STATUS, RP0 bsf KB_DATo bsf KB_CLKo bcf STATUS, RP0 retlw 0xFE KBBitOut: bsf STATUS, RP0 andlw 0x01 btfss STATUS, Z bsf KB_DATo btfsc STATUS, Z bcf KB_DATo call delay_20us bcf KB_CLKo call delay_20us call delay_20us bsf KB_CLKo bcf STATUS, RP0 nop nop nop nop nop return ;---------------------------------------------------------- Receive_host: clrf host_data btfss KB_CLKo ;Test for clock low retlw 0xFE btfsc KB_DATo ;Test for inhibit retlw 0xFE clrf parity movlw 0x08 movwf counter call delay_20us Receive_host_data: call KBBitIn ;Data bits btfss KB_CLKo ;Test for inhibit retlw 0xFE bcf STATUS, C rrf host_data, f iorwf host_data, f xorwf parity,f decfsz counter, f goto Receive_host_data nop call KBBitIn ;Parity bit btfss KB_CLKo ;Test for inhibit retlw 0xFE xorwf parity, f nop nop nop nop nop ByteInLoop1 nop call KBBitIn ;Stop bit btfss KB_CLKo ;Test for inhibit retlw 0xFE xorlw 0x00 btfsc STATUS, Z clrf parity btfsc STATUS, Z ;Stop bit = 1? goto ByteInLoop1 ; No--keep clocking. bsf STATUS, RP0 ;Acknowledge bcf KB_DATo call delay_20us bcf KB_CLKo call delay_20us call delay_20us bsf KB_CLKo call delay_20us bsf KB_DATo bcf STATUS, RP0 ;btfss parity, 7 ;Parity correct? ;retlw 0xFF ; No--return error call delay_20us retlw 0x00 KBBitIn: call delay_20us bsf STATUS, RP0 bcf KB_CLKo call delay_20us call delay_20us bsf KB_CLKo bcf STATUS, RP0 call delay_20us btfsc KB_DATo retlw 0x80 retlw 0x00 ;---------------------------------------------------------- ;delay for 100 us delay_between_TX ;call delay_1ms return ;---------------------------------------------------------- ;delay for 20 us delay_20us movlw 0x21 movwf d1 Delay_20 decfsz d1, f goto Delay_20 return ;---------------------------------------------------------- delay_5us: movlw 0x08 movwf d1 Delay_5 decfsz d1, f goto Delay_5 return ;---------------------------------------------------------- ;delay for 8000 cycles delay_8000 movlw 0x3E movwf d1 movlw 0x07 movwf d2 delay_8000_0 decfsz d1, f goto $+2 decfsz d2, f goto delay_8000_0 goto $+1 nop return ;Delay for .5 seconds Delay_500ms: movlw 0x16 ;2499999 cycles movwf d1 movlw 0x74 movwf d2 movlw 0x06 movwf d3 Delay_500 decfsz d1, f goto $+2 decfsz d2, f goto $+2 decfsz d3, f goto Delay_500 return delay_1ms: movlw 0xE7 ;4998 cycles movwf d1 movlw 0x04 movwf d2 Delay_1: decfsz d1, f goto $+2 decfsz d2, f goto Delay_1 nop nop return delay_50ms: movlw 0x4F ;249998 cycles movwf d1 movlw 0xC4 movwf d2 Delay_50 decfsz d1, f goto $+2 decfsz d2, f goto Delay_50 goto $+1 return ;********************************************************** ; Initialize all the ports and stuff init ; bank 0 bcf STATUS, RP0 ;Goto the right page ;Setup default port states movlw b'00000000' movwf PORTA movlw b'00000000' movwf PORTB movlw b'00000000' movwf PORTC movlw b'00000000' movwf PORTD movlw b'00000000' movwf PORTE ;Disable A2D movlw b'00000000' ; Setup A2D on all portA pins and set osc. bits movwf ADCON0 ; bank 1 bsf STATUS, RP0 ; Goto the right page ;Setup the data direction registers movlw b'11111111' ; Set portA to all inputs movwf TRISA movlw b'11111111' ; Set SPI port to inputs movwf TRISB movlw b'11111111' ; Set RX pin to input all oters output movwf TRISC movlw b'11111111' ; Set SPI port to inputs movwf TRISD movlw b'00000111' ; Set RX pin to input all oters output movwf TRISE ;Disable the A2D movlw b'00000110' ; Setup A2D right justified movwf ADCON1 bcf STATUS, RP0 ;Goto the right page bcf INTCON, GIE ; Disable interrupts call Delay_500ms movlw 0xAA call Send_host ; keyboard and host ; H K ; AA ; FF FA ; AA ; ED FA ; 00 FA ; FF FA ; AA ; F3 FA ; 00 FA ; ED FA ; 00 FA ; ED FA ; 00 FA ; F3 FA ; 20 FA ; Button Maping ; PA4 => a => P2_RIGHT => KEYCODE_G ; PA5 => b => P2_DOWN => KEYCODE_F ; PB0 => c => P1_BUTTON5 => KEYCODE_Z ; PB1 => d => P1_COIN => KEYCODE_5 ; PB2 => e => P1_START => KEYCODE_1 ; PB3 => f => P1_DOWN => KEYCODE_DOWN ; PB4 => g => P1_RIGHT => KEYCODE_RIGHT ; PB5 => h => P1_UP => KEYCODE_UP ; PB6 => i => P1_LEFT => KEYCODE_LEFT ; PC0 => k => P2_BUTTON2 => KEYCODE_S ; PC1 => l => P2_BUTTON3 => KEYCODE_D ; PC2 => m => P2_BUTTON4 => KEYCODE_W ; PC3 => n => P2_START => KEYCODE_2 ; PC4 => o => P2_BUTTON6 => KEYCODE_Y ; PC5 => ESC ; PC6 => q => P1_BUTTON4 => KEYCODE_LSHIFT ; PC7 => r => P1_BUTTON3 => KEYCODE_SPACE ; PD0 => s => P2_COIN => KEYCODE_6 ; PD1 => t => P2_BUTTON5 => KEYCODE_T ; PD2 => u => ; PD3 => v => P2_BUTTON7 => KEYCODE_A ; PD4 => w => P1_BUTTON2 => KEYCODE_LALT ; PD5 => x => P1_BUTTON1 => KEYCODE_LCONTROL ; PD6 => y => P1_BUTTON7 => KEYCODE_LCONTROL ; PD7 => z => P1_BUTTON6 => KEYCODE_X ; PE0 => 0 => P2_LEFT => KEYCODE_D ; PE1 => 1 => P2_UP => KEYCODE_R ; PE2 => 2 => P2_BUTTON1 => KEYCODE_A ;-------------------------------------------------------------------- ;Start of the main program, aka were it all goes down. ;-------------------------------------------------------------------- clrf KB_init main: btfss scan_enable goto Skip_delay call delay_1ms call delay_1ms call delay_1ms call delay_1ms call delay_1ms Skip_delay: ;Check to see if keyboard wants to talk btfsc scan_enable goto PC_to_device call Receive_device sublw KB_ERROR bz PC_to_device ;Forward data to PC movf kb_data, w call Send_host movf KB_init, w sublw 0x00 bz state0 movf KB_init, w sublw 0x01 bz state1 goto PC_to_device state0: ;First reset the keybord call delay_1ms movlw 0xFF call Send_device incf KB_init, f goto PC_to_device state1: ;Second send the ED command movlw 0xED call Send_device wait_for_ack1: btfss KB_CLKi goto wait_for_ack1 btfsc KB_DATi goto wait_for_ack1 call Receive_device movlw 0x00 call Send_device incf KB_init, f goto PC_to_device PC_to_device: call Receive_host movwf RX_error sublw 0x00 bnz show_error call delay_1ms movlw 0xFA call Send_host movf host_data, w sublw 0xFF bnz main call Delay_500ms movlw 0xAA call Send_host goto main show_error: movf RX_error, w sublw 0xFE bz check_inputs call delay_50ms movf RX_error, w goto main check_inputs: btfss scan_enable goto main ;Scan the keys for presses and releases ;Check the buttons that are on PORTA<-------------------- CheckA: movf PORTA, w andlw 0x30; movwf portA_mirror xorwf portA_last, w bz CheckB checkA4_pressed: btfsc portA_mirror, 4 goto checkA4_released btfss portA_last, 4 goto checkA4_released movlw 0x34 ;Do pressed message call Send_host checkA4_released: btfss portA_mirror, 4 goto checkA5_pressed btfsc portA_last, 4 goto checkA5_pressed movlw 0xF0 ;Do released message call Send_host call delay_between_TX movlw 0x34 call Send_host checkA5_pressed: btfsc portA_mirror, 5 goto checkA5_released btfss portA_last, 5 goto checkA5_released movlw 0x2B ;Do pressed message call Send_host checkA5_released: btfss portA_mirror, 5 goto end_checkA btfsc portA_last, 5 goto end_checkA movlw 0xF0 ;Do released message call Send_host call delay_between_TX movlw 0x2B call Send_host end_checkA: movf portA_mirror, w movwf portA_last ;Check the buttons that are on PORTB<-------------------- CheckB: movf PORTB, w movwf portB_mirror xorwf portB_last, w bz CheckC checkB0_pressed: btfsc portB_mirror, 0 goto checkB0_released btfss portB_last, 0 goto checkB0_released movlw 0x1A ;Do pressed message call Send_host checkB0_released: btfss portB_mirror, 0 goto checkB1_pressed btfsc portB_last, 0 goto checkB1_pressed movlw 0xF0 ;Do released message call Send_host call delay_between_TX movlw 0x1A call Send_host checkB1_pressed: btfsc portB_mirror, 1 goto checkB1_released btfss portB_last, 1 goto checkB1_released movlw 0x2E ;Do pressed message call Send_host checkB1_released: btfss portB_mirror, 1 goto checkB2_pressed btfsc portB_last, 1 goto checkB2_pressed movlw 0xF0 ;Do released message call Send_host call delay_between_TX movlw 0x2E call Send_host checkB2_pressed: btfsc portB_mirror, 2 goto checkB2_released btfss portB_last, 2 goto checkB2_released movlw 0x16 ;Do pressed message call Send_host checkB2_released: btfss portB_mirror, 2 goto checkB3_pressed btfsc portB_last, 2 goto checkB3_pressed movlw 0xF0 ;Do released message call Send_host call delay_between_TX movlw 0x16 call Send_host checkB3_pressed: btfsc portB_mirror, 3 goto checkB3_released btfss portB_last, 3 goto checkB3_released movlw 0xE0 ;Do released message call Send_host call delay_between_TX movlw 0x72 call Send_host checkB3_released: btfss portB_mirror, 3 goto checkB4_pressed btfsc portB_last, 3 goto checkB4_pressed movlw 0xF0 ;Do released message call Send_host call delay_between_TX movlw 0xE0 ;Do released message call Send_host call delay_between_TX movlw 0x72 call Send_host checkB4_pressed: btfsc portB_mirror, 4 goto checkB4_released btfss portB_last, 4 goto checkB4_released movlw 0xE0 ;Do released message call Send_host call delay_between_TX movlw 0x74 call Send_host checkB4_released: btfss portB_mirror, 4 goto checkB5_pressed btfsc portB_last, 4 goto checkB5_pressed movlw 0xF0 ;Do released message call Send_host call delay_between_TX movlw 0xE0 ;Do released message call Send_host call delay_between_TX movlw 0x74 call Send_host checkB5_pressed: btfsc portB_mirror, 5 goto checkB5_released btfss portB_last, 5 goto checkB5_released movlw 0xE0 ;Do released message call Send_host call delay_between_TX movlw 0x75 call Send_host checkB5_released: btfss portB_mirror, 5 goto checkB6_pressed btfsc portB_last, 5 goto checkB6_pressed movlw 0xF0 ;Do released message call Send_host call delay_between_TX movlw 0xE0 ;Do released message call Send_host call delay_between_TX movlw 0x75 call Send_host checkB6_pressed: btfsc portB_mirror, 6 goto checkB6_released btfss portB_last, 6 goto checkB6_released movlw 0xE0 ;Do released message call Send_host call delay_between_TX movlw 0x6B call Send_host checkB6_released: btfss portB_mirror, 6 goto end_checkB btfsc portB_last, 6 goto end_checkB movlw 0xF0 ;Do released message call Send_host call delay_between_TX movlw 0xE0 ;Do released message call Send_host call delay_between_TX movlw 0x6B call Send_host end_checkB: movf portB_mirror, w movwf portB_last ;Check the buttons that are on PORTC<-------------------- CheckC: movf PORTC, w movwf portC_mirror xorwf portC_last, w bz CheckD checkC0_pressed: btfsc portC_mirror, 0 goto checkC0_released btfss portC_last, 0 goto checkC0_released movlw 0x1B ;Do pressed message call Send_host checkC0_released: btfss portC_mirror, 0 goto checkC1_pressed btfsc portC_last, 0 goto checkC1_pressed movlw 0xF0 ;Do released message call Send_host call delay_between_TX movlw 0x1B call Send_host checkC1_pressed: btfsc portC_mirror, 1 goto checkC1_released btfss portC_last, 1 goto checkC1_released movlw 0x23 ;Do pressed message call Send_host checkC1_released: btfss portC_mirror, 1 goto checkC2_pressed btfsc portC_last, 1 goto checkC2_pressed movlw 0xF0 ;Do released message call Send_host call delay_between_TX movlw 0x23 call Send_host checkC2_pressed: btfsc portC_mirror, 2 goto checkC2_released btfss portC_last, 2 goto checkC2_released movlw 0x1D ;Do pressed message call Send_host checkC2_released: btfss portC_mirror, 2 goto checkC3_pressed btfsc portC_last, 2 goto checkC3_pressed movlw 0xF0 ;Do released message call Send_host call delay_between_TX movlw 0x1D call Send_host checkC3_pressed: btfsc portC_mirror, 3 goto checkC3_released btfss portC_last, 3 goto checkC3_released movlw 0x1E ;Do pressed message call Send_host checkC3_released: btfss portC_mirror, 3 goto checkC4_pressed btfsc portC_last, 3 goto checkC4_pressed movlw 0xF0 ;Do released message call Send_host call delay_between_TX movlw 0x1E call Send_host checkC4_pressed: btfsc portC_mirror, 4 goto checkC4_released btfss portC_last, 4 goto checkC4_released movlw 0x35 ;Do pressed message call Send_host checkC4_released: btfss portC_mirror, 4 goto checkC5_pressed btfsc portC_last, 4 goto checkC5_pressed movlw 0xF0 ;Do released message call Send_host call delay_between_TX movlw 0x35 call Send_host checkC5_pressed: btfsc portC_mirror, 5 goto checkC5_released btfss portC_last, 5 goto checkC5_released movlw 0x76 ;Do pressed message call Send_host checkC5_released: btfss portC_mirror, 5 goto checkC6_pressed btfsc portC_last, 5 goto checkC6_pressed movlw 0xF0 ;Do released message call Send_host call delay_between_TX movlw 0x76 call Send_host checkC6_pressed: btfsc portC_mirror, 6 goto checkC6_released btfss portC_last, 6 goto checkC6_released movlw 0x12 ;Do pressed message call Send_host checkC6_released: btfss portC_mirror, 6 goto checkC7_pressed btfsc portC_last, 6 goto checkC7_pressed movlw 0xF0 ;Do released message call Send_host call delay_between_TX movlw 0x12 call Send_host checkC7_pressed: btfsc portC_mirror, 7 goto checkC7_released btfss portC_last, 7 goto checkC7_released movlw 0x29 ;Do pressed message call Send_host checkC7_released: btfss portC_mirror, 7 goto end_checkC btfsc portC_last, 7 goto end_checkC movlw 0xF0 ;Do released message call Send_host call delay_between_TX movlw 0x29 call Send_host end_checkC: movf portC_mirror, w movwf portC_last ;Check the buttons that are on PORTD<-------------------- CheckD: movf PORTD, w movwf portD_mirror xorwf portD_last, w bz CheckE checkD0_pressed: btfsc portD_mirror, 0 goto checkD0_released btfss portD_last, 0 goto checkD0_released movlw 0x36 ;Do pressed message call Send_host checkD0_released: btfss portD_mirror, 0 goto checkD1_pressed btfsc portD_last, 0 goto checkD1_pressed movlw 0xF0 ;Do released message call Send_host call delay_between_TX movlw 0x36 call Send_host checkD1_pressed: btfsc portD_mirror, 1 goto checkD1_released btfss portD_last, 1 goto checkD1_released movlw 0x2C ;Do pressed message call Send_host checkD1_released: btfss portD_mirror, 1 goto checkD2_pressed btfsc portD_last, 1 goto checkD2_pressed movlw 0xF0 ;Do released message call Send_host call delay_between_TX movlw 0x2C call Send_host checkD2_pressed: btfsc portD_mirror, 2 goto checkD2_released btfss portD_last, 2 goto checkD2_released movlw 0x3C ;Do pressed message call Send_host checkD2_released: btfss portD_mirror, 2 goto checkD3_pressed btfsc portD_last, 2 goto checkD3_pressed movlw 0xF0 ;Do released message call Send_host call delay_between_TX movlw 0x3C call Send_host checkD3_pressed: btfsc portD_mirror, 3 goto checkD3_released btfss portD_last, 3 goto checkD3_released movlw 0x1C ;Do pressed message call Send_host checkD3_released: btfss portD_mirror, 3 goto checkD4_pressed btfsc portD_last, 3 goto checkD4_pressed movlw 0xF0 ;Do released message call Send_host call delay_between_TX movlw 0x1C call Send_host checkD4_pressed: btfsc portD_mirror, 4 goto checkD4_released btfss portD_last, 4 goto checkD4_released movlw 0x11 ;Do pressed message call Send_host checkD4_released: btfss portD_mirror, 4 goto checkD5_pressed btfsc portD_last, 4 goto checkD5_pressed movlw 0xF0 ;Do released message call Send_host call delay_between_TX movlw 0x11 call Send_host checkD5_pressed: btfsc portD_mirror, 5 goto checkD5_released btfss portD_last, 5 goto checkD5_released movlw 0x14 ;Do pressed message call Send_host checkD5_released: btfss portD_mirror, 5 goto checkD6_pressed btfsc portD_last, 5 goto checkD6_pressed movlw 0xF0 ;Do released message call Send_host call delay_between_TX movlw 0x14 call Send_host checkD6_pressed: btfsc portD_mirror, 6 goto checkD6_released btfss portD_last, 6 goto checkD6_released movlw 0x14 ;Do pressed message call Send_host checkD6_released: btfss portD_mirror, 6 goto checkD7_pressed btfsc portD_last, 6 goto checkD7_pressed movlw 0xF0 ;Do released message call Send_host call delay_between_TX movlw 0x14 call Send_host checkD7_pressed: btfsc portD_mirror, 7 goto checkD7_released btfss portD_last, 7 goto checkD7_released movlw 0x22 ;Do pressed message call Send_host checkD7_released: btfss portD_mirror, 7 goto end_checkD btfsc portD_last, 7 goto end_checkD movlw 0xF0 ;Do released message call Send_host call delay_between_TX movlw 0x22 call Send_host end_checkD: movf portD_mirror, w movwf portD_last ;Check the buttons that are on PORTE<-------------------- CheckE: movf PORTE, w movwf portE_mirror xorwf portE_last, w bz End_check checkE0_pressed: btfsc portE_mirror, 0 goto checkE0_released btfss portE_last, 0 goto checkE0_released movlw 0x23 ;Do pressed message call Send_host checkE0_released: btfss portE_mirror, 0 goto checkE1_pressed btfsc portE_last, 0 goto checkE1_pressed movlw 0xF0 ;Do released message call Send_host call delay_between_TX movlw 0x23 call Send_host checkE1_pressed: btfsc portE_mirror, 1 goto checkE1_released btfss portE_last, 1 goto checkE1_released movlw 0x2D ;Do pressed message call Send_host checkE1_released: btfss portE_mirror, 1 goto checkE2_pressed btfsc portE_last, 1 goto checkE2_pressed movlw 0xF0 ;Do released message call Send_host call delay_between_TX movlw 0x2D call Send_host checkE2_pressed: btfsc portE_mirror, 2 goto checkE2_released btfss portE_last, 2 goto checkE2_released movlw 0x1C ;Do pressed message call Send_host checkE2_released: btfss portE_mirror, 2 goto end_checkE btfsc portE_last, 2 goto end_checkE movlw 0xF0 ;Do released message call Send_host call delay_between_TX movlw 0x1C call Send_host end_checkE: movf portE_mirror, w movwf portE_last End_check: goto main end