;---Inicio---
		list	p=16f877
		radix	hex
	
;---Mapa de memoria---
status	equ		0x03
porta	equ		0x05
portb	equ		0x06
portc	equ		0x07
portd	equ		0x08
porte	equ		0x09

reg1	equ		0x0C
reg2	equ		0x0D
reg3	equ		0x0E
reg4	equ		0x0B
bits1	equ		0x1C
bits2	equ		0x1F
tecla1	equ		0x1D
tecla2	equ		0x1E

;---Configuracion de puertos---
reset	org		0x00
		goto	inicio
		org		0x05

inicio	bsf		status,5
		movlw	b'11111'
		movwf	porta			;Puerto A entrada
		movlw	b'11111111'
		movwf	portb			;Puerto B entrada
		movlw	b'11111111'
		movwf	portc			;Puerto C entrada
		movlw	b'11111111'	
		movwf	portd			;Puerto D entrada
		movlw	b'000'
		movwf	porte			;Puerto E salida
		bcf		status,5

;---Espera---
espera	btfsc	portb,0
		call	tecla_up
		btfsc	portb,1
		call	tecla_down
		btfsc	portb,2
		call	tecla_right
		btfsc	portb,3
		call	tecla_left
		btfsc	portb,4
		call	tecla_shift
		btfsc	portb,5
		call	tecla_control
		btfsc	portb,6
		call	tecla_Z
		btfsc	portb,7
		call	tecla_X
		btfsc	portc,0
		call	tecla_M
		btfsc	portc,1
		call	tecla_N
		btfsc	portc,2
		call	tecla_B
		btfsc	portc,3
		call	tecla_H
		btfsc	portc,4
		call	tecla_Q
		btfsc	portc,5
		call	tecla_W
		btfsc	portc,6
		call	tecla_A
		btfsc	portc,7
		call	tecla_S
		btfsc	portd,0
		call	tecla_R
		btfsc	portd,1
		call	tecla_T
		btfsc	portd,2
		call	tecla_1
		btfsc	portd,3
		call	tecla_2
		btfsc	portd,4
		call	tecla_5
		btfsc	portd,5
		call	tecla_6
		btfsc	portd,6
		call	tecla_esc
		btfsc	portd,7
		call	tecla_O
		btfsc	porte,0
		call	tecla_K
		btfsc	porte,1
		call	tecla_enter
		goto 	espera


;==================================================================
; PROTOCOLO PS/2
; 1 bit de inicio: siempre 0
; 8 bit de datos: menos significativo primero
; 1 bit de paridad (paridad impar -odd-) (0 si 1 impar, 1 si 1 par)
; 1 bit de parada: siempre 1
;
; VARIABLE TECLA1 = 0110 0100
; 0-3, teclas con paridad 1 (1 par)
; 4-7, teclas con paridad 0 (1 impar)
;==================================================================

;---Teclas---
tecla_up 
		bcf		porte,1 ;0 (Inicio)
		call	pausa
		bcf		porte,1	;0 (Datos)
		call	pausa
		bsf		porte,1	;1
		call	pausa
		bcf		porte,1	;1
		call	pausa
		bcf		porte,1	;0
		call	pausa
		bcf		porte,1	;0
		call	pausa
		bsf		porte,1 ;1
		call	pausa
		bcf		porte,1	;0
		call	pausa
		bcf		porte,1	;0
		call	pausa
		bcf		porte,1	;1 (Paridad)
		call	pausa
		bsf		portd,1 ;1 (Parada)
		call	pausa2
		return	

tecla_down 
		bcf		porte,1 ;0 (Inicio)
		call	pausa
		bcf		porte,1	;0 (Datos)
		call	pausa
		bcf		porte,1	;0
		call	pausa
		bcf		porte,1	;0
		call	pausa
		bsf		porte,1	;1
		call	pausa
		bcf		porte,1	;0
		call	pausa
		bsf		porte,1 ;1
		call	pausa
		bcf		porte,1	;0
		call	pausa
		bcf		porte,1	;0
		call	pausa
		bcf		porte,1	;1 (Paridad)
		call	pausa
		bsf		portd,1 ;1 (Parada)
		call	pausa2
		return	

tecla_right
		bcf		porte,1 ;0 (Inicio)
		call	pausa
		bcf		porte,1	;0 (Datos)
		call	pausa
		bsf		porte,1	;1
		call	pausa
		bcf		porte,1	;1
		call	pausa
		bcf		porte,1	;0
		call	pausa
		bcf		porte,1	;0
		call	pausa
		bsf		porte,1 ;1
		call	pausa
		bcf		porte,1	;0
		call	pausa
		bcf		porte,1	;0
		call	pausa
		bcf		porte,1	;1 (Paridad)
		call	pausa
		bsf		portd,1 ;1 (Parada)
		call	pausa2
		return	

tecla_left
		bcf		porte,1 ;0 (Inicio)
		call	pausa
		bcf		porte,1	;0 (Datos)
		call	pausa
		bsf		porte,1	;1
		call	pausa
		bcf		porte,1	;1
		call	pausa
		bcf		porte,1	;0
		call	pausa
		bcf		porte,1	;0
		call	pausa
		bsf		porte,1 ;1
		call	pausa
		bcf		porte,1	;0
		call	pausa
		bcf		porte,1	;0
		call	pausa
		bcf		porte,1	;1 (Paridad)
		call	pausa
		bsf		portd,1 ;1 (Parada)
		call	pausa2
		return	

tecla_shift
		bcf		porte,1 ;0 (Inicio)
		call	pausa
		bcf		porte,1	;0 (Datos)
		call	pausa
		bsf		porte,1	;1
		call	pausa
		bcf		porte,1	;1
		call	pausa
		bcf		porte,1	;0
		call	pausa
		bcf		porte,1	;0
		call	pausa
		bsf		porte,1 ;1
		call	pausa
		bcf		porte,1	;0
		call	pausa
		bcf		porte,1	;0
		call	pausa
		bcf		porte,1	;1 (Paridad)
		call	pausa
		bsf		portd,1 ;1 (Parada)
		call	pausa2
		return	

tecla_control
		bcf		porte,1 ;0 (Inicio)
		call	pausa
		bcf		porte,1	;0 (Datos)
		call	pausa
		bsf		porte,1	;1
		call	pausa
		bcf		porte,1	;1
		call	pausa
		bcf		porte,1	;0
		call	pausa
		bcf		porte,1	;0
		call	pausa
		bsf		porte,1 ;1
		call	pausa
		bcf		porte,1	;0
		call	pausa
		bcf		porte,1	;0
		call	pausa
		bcf		porte,1	;1 (Paridad)
		call	pausa
		bsf		portd,1 ;1 (Parada)
		call	pausa2
		return	

tecla_Z	;bsf		portd,0
		bcf		porte,1 ;0 (Inicio)
		call	pausa
		bcf		porte,1	;0 (Datos)
		call	pausa
		bsf		porte,1	;1
		call	pausa
		bcf		porte,1	;0
		call	pausa
		bsf		porte,1	;1
		call	pausa
		bsf		porte,1	;1
		call	pausa
		bcf		porte,1 ;0
		call	pausa
		bsf		porte,1	;1
		call	pausa
		bcf		porte,1	;0
		call	pausa
		bcf		porte,1	;1 (Paridad)
		call	pausa
		bsf		portd,1 ;1 (Parada)
		call	pausa2
		return	

tecla_X bcf		porte,1 ;0 (Inicio)
		call	pausa
		bcf		porte,1	;0 (Datos)
		call	pausa
		bsf		porte,1	;1
		call	pausa
		bcf		porte,1	;1
		call	pausa
		bcf		porte,1	;0
		call	pausa
		bcf		porte,1	;0
		call	pausa
		bsf		porte,1 ;1
		call	pausa
		bcf		porte,1	;0
		call	pausa
		bcf		porte,1	;0
		call	pausa
		bcf		porte,1	;1 (Paridad)
		call	pausa
		bsf		portd,1 ;1 (Parada)
		call	pausa2
		return	

tecla_M bcf		porte,1 ;0 (Inicio)
		call	pausa
		bcf		porte,1	;0 (Datos)
		call	pausa
		bsf		porte,1	;1
		call	pausa
		bcf		porte,1	;1
		call	pausa
		bcf		porte,1	;0
		call	pausa
		bcf		porte,1	;0
		call	pausa
		bsf		porte,1 ;1
		call	pausa
		bcf		porte,1	;0
		call	pausa
		bcf		porte,1	;0
		call	pausa
		bcf		porte,1	;1 (Paridad)
		call	pausa
		bsf		portd,1 ;1 (Parada)
		call	pausa2
		return	

tecla_N bcf		porte,1 ;0 (Inicio)
		call	pausa
		bcf		porte,1	;0 (Datos)
		call	pausa
		bsf		porte,1	;1
		call	pausa
		bcf		porte,1	;1
		call	pausa
		bcf		porte,1	;0
		call	pausa
		bcf		porte,1	;0
		call	pausa
		bsf		porte,1 ;1
		call	pausa
		bcf		porte,1	;0
		call	pausa
		bcf		porte,1	;0
		call	pausa
		bcf		porte,1	;1 (Paridad)
		call	pausa
		bsf		portd,1 ;1 (Parada)
		call	pausa2
		return	

tecla_B	bcf		porte,1 ;0 (Inicio)
		call	pausa
		bcf		porte,1	;0 (Datos)
		call	pausa
		bsf		porte,1	;1
		call	pausa
		bcf		porte,1	;1
		call	pausa
		bcf		porte,1	;0
		call	pausa
		bcf		porte,1	;0
		call	pausa
		bsf		porte,1 ;1
		call	pausa
		bcf		porte,1	;0
		call	pausa
		bcf		porte,1	;0
		call	pausa
		bcf		porte,1	;1 (Paridad)
		call	pausa
		bsf		portd,1 ;1 (Parada)
		call	pausa2
		return	

tecla_H bcf		porte,1 ;0 (Inicio)
		call	pausa
		bcf		porte,1	;0 (Datos)
		call	pausa
		bsf		porte,1	;1
		call	pausa
		bcf		porte,1	;1
		call	pausa
		bcf		porte,1	;0
		call	pausa
		bcf		porte,1	;0
		call	pausa
		bsf		porte,1 ;1
		call	pausa
		bcf		porte,1	;0
		call	pausa
		bcf		porte,1	;0
		call	pausa
		bcf		porte,1	;1 (Paridad)
		call	pausa
		bsf		portd,1 ;1 (Parada)
		call	pausa2
		return	

tecla_Q	;bsf		portd,0
		bcf		porte,1 ;0 (Inicio)
		call	pausa
		bsf		porte,1	;1 (Datos)
		call	pausa
		bcf		porte,1	;0
		call	pausa
		bsf		porte,1	;1
		call	pausa
		bcf		porte,1	;0
		call	pausa
		bsf		porte,1	;1
		call	pausa
		bcf		porte,1 ;0
		call	pausa
		bcf		porte,1	;0
		call	pausa
		bcf		porte,1	;0
		call	pausa
		bcf		porte,1	;0 (Paridad)
		call	pausa
		bsf		portd,1 ;1 (Parada)
		call	pausa2
		return	

tecla_W bcf		porte,1 ;0 (Inicio)
		call	pausa
		bcf		porte,1	;0 (Datos)
		call	pausa
		bsf		porte,1	;1
		call	pausa
		bcf		porte,1	;1
		call	pausa
		bcf		porte,1	;0
		call	pausa
		bcf		porte,1	;0
		call	pausa
		bsf		porte,1 ;1
		call	pausa
		bcf		porte,1	;0
		call	pausa
		bcf		porte,1	;0
		call	pausa
		bcf		porte,1	;1 (Paridad)
		call	pausa
		bsf		portd,1 ;1 (Parada)
		call	pausa2
		return	

tecla_A bcf		porte,1 ;0 (Inicio)
		call	pausa
		bcf		porte,1	;0 (Datos)
		call	pausa
		bsf		porte,1	;1
		call	pausa
		bcf		porte,1	;1
		call	pausa
		bcf		porte,1	;0
		call	pausa
		bcf		porte,1	;0
		call	pausa
		bsf		porte,1 ;1
		call	pausa
		bcf		porte,1	;0
		call	pausa
		bcf		porte,1	;0
		call	pausa
		bcf		porte,1	;1 (Paridad)
		call	pausa
		bsf		portd,1 ;1 (Parada)
		call	pausa2
		return	

tecla_S bcf		porte,1 ;0 (Inicio)
		call	pausa
		bcf		porte,1	;0 (Datos)
		call	pausa
		bsf		porte,1	;1
		call	pausa
		bcf		porte,1	;1
		call	pausa
		bcf		porte,1	;0
		call	pausa
		bcf		porte,1	;0
		call	pausa
		bsf		porte,1 ;1
		call	pausa
		bcf		porte,1	;0
		call	pausa
		bcf		porte,1	;0
		call	pausa
		bcf		porte,1	;1 (Paridad)
		call	pausa
		bsf		portd,1 ;1 (Parada)
		call	pausa2
		return	

tecla_R bcf		porte,1 ;0 (Inicio)
		call	pausa
		bcf		porte,1	;0 (Datos)
		call	pausa
		bsf		porte,1	;1
		call	pausa
		bcf		porte,1	;1
		call	pausa
		bcf		porte,1	;0
		call	pausa
		bcf		porte,1	;0
		call	pausa
		bsf		porte,1 ;1
		call	pausa
		bcf		porte,1	;0
		call	pausa
		bcf		porte,1	;0
		call	pausa
		bcf		porte,1	;1 (Paridad)
		call	pausa
		bsf		portd,1 ;1 (Parada)
		call	pausa2
		return	

tecla_T	bcf		porte,1 ;0 (Inicio)
		call	pausa
		bcf		porte,1	;0 (Datos)
		call	pausa
		bsf		porte,1	;1
		call	pausa
		bcf		porte,1	;1
		call	pausa
		bcf		porte,1	;0
		call	pausa
		bcf		porte,1	;0
		call	pausa
		bsf		porte,1 ;1
		call	pausa
		bcf		porte,1	;0
		call	pausa
		bcf		porte,1	;0
		call	pausa
		bcf		porte,1	;1 (Paridad)
		call	pausa
		bsf		portd,1 ;1 (Parada)
		call	pausa2
		return	

tecla_1	bcf		porte,1 ;0 (Inicio)
		call	pausa
		bcf		porte,1	;0 (Datos)
		call	pausa
		bsf		porte,1	;1
		call	pausa
		bcf		porte,1	;1
		call	pausa
		bcf		porte,1	;0
		call	pausa
		bcf		porte,1	;0
		call	pausa
		bsf		porte,1 ;1
		call	pausa
		bcf		porte,1	;0
		call	pausa
		bcf		porte,1	;0
		call	pausa
		bcf		porte,1	;1 (Paridad)
		call	pausa
		bsf		portd,1 ;1 (Parada)
		call	pausa2
		return	

tecla_2	bcf		porte,1 ;0 (Inicio)
		call	pausa
		bcf		porte,1	;0 (Datos)
		call	pausa
		bsf		porte,1	;1
		call	pausa
		bcf		porte,1	;1
		call	pausa
		bcf		porte,1	;0
		call	pausa
		bcf		porte,1	;0
		call	pausa
		bsf		porte,1 ;1
		call	pausa
		bcf		porte,1	;0
		call	pausa
		bcf		porte,1	;0
		call	pausa
		bcf		porte,1	;1 (Paridad)
		call	pausa
		bsf		portd,1 ;1 (Parada)
		call	pausa2
		return	

tecla_5	bcf		porte,1 ;0 (Inicio)
		call	pausa
		bcf		porte,1	;0 (Datos)
		call	pausa
		bsf		porte,1	;1
		call	pausa
		bcf		porte,1	;1
		call	pausa
		bcf		porte,1	;0
		call	pausa
		bcf		porte,1	;0
		call	pausa
		bsf		porte,1 ;1
		call	pausa
		bcf		porte,1	;0
		call	pausa
		bcf		porte,1	;0
		call	pausa
		bcf		porte,1	;1 (Paridad)
		call	pausa
		bsf		portd,1 ;1 (Parada)
		call	pausa2
		return	

tecla_6	bcf		porte,1 ;0 (Inicio)
		call	pausa
		bcf		porte,1	;0 (Datos)
		call	pausa
		bsf		porte,1	;1
		call	pausa
		bcf		porte,1	;1
		call	pausa
		bcf		porte,1	;0
		call	pausa
		bcf		porte,1	;0
		call	pausa
		bsf		porte,1 ;1
		call	pausa
		bcf		porte,1	;0
		call	pausa
		bcf		porte,1	;0
		call	pausa
		bcf		porte,1	;1 (Paridad)
		call	pausa
		bsf		portd,1 ;1 (Parada)
		call	pausa2
		return	

tecla_esc
		bcf		porte,1 ;0 (Inicio)
		call	pausa
		bcf		porte,1	;0 (Datos)
		call	pausa
		bsf		porte,1	;1
		call	pausa
		bcf		porte,1	;1
		call	pausa
		bcf		porte,1	;0
		call	pausa
		bcf		porte,1	;0
		call	pausa
		bsf		porte,1 ;1
		call	pausa
		bcf		porte,1	;0
		call	pausa
		bcf		porte,1	;0
		call	pausa
		bcf		porte,1	;1 (Paridad)
		call	pausa
		bsf		portd,1 ;1 (Parada)
		call	pausa2
		return	

tecla_O	bcf		porte,1 ;0 (Inicio)
		call	pausa
		bcf		porte,1	;0 (Datos)
		call	pausa
		bsf		porte,1	;1
		call	pausa
		bcf		porte,1	;1
		call	pausa
		bcf		porte,1	;0
		call	pausa
		bcf		porte,1	;0
		call	pausa
		bsf		porte,1 ;1
		call	pausa
		bcf		porte,1	;0
		call	pausa
		bcf		porte,1	;0
		call	pausa
		bcf		porte,1	;1 (Paridad)
		call	pausa
		bsf		portd,1 ;1 (Parada)
		call	pausa2
		return	

tecla_K	bcf		porte,1 ;0 (Inicio)
		call	pausa
		bcf		porte,1	;0 (Datos)
		call	pausa
		bsf		porte,1	;1
		call	pausa
		bcf		porte,1	;1
		call	pausa
		bcf		porte,1	;0
		call	pausa
		bcf		porte,1	;0
		call	pausa
		bsf		porte,1 ;1
		call	pausa
		bcf		porte,1	;0
		call	pausa
		bcf		porte,1	;0
		call	pausa
		bcf		porte,1	;1 (Paridad)
		call	pausa
		bsf		portd,1 ;1 (Parada)
		call	pausa2
		return	

tecla_enter
		bcf		porte,1 ;0 (Inicio)
		call	pausa
		bcf		porte,1	;0 (Datos)
		call	pausa
		bsf		porte,1	;1
		call	pausa
		bcf		porte,1	;1
		call	pausa
		bcf		porte,1	;0
		call	pausa
		bcf		porte,1	;0
		call	pausa
		bsf		porte,1 ;1
		call	pausa
		bcf		porte,1	;0
		call	pausa
		bcf		porte,1	;0
		call	pausa
		bcf		porte,1	;1 (Paridad)
		call	pausa
		bsf		portd,1 ;1 (Parada)
		call	pausa2
		return	


;---Retardo 50us----
;PIC Time Delay = 0,00005000 s with Osc = 4000000 Hz
pausa	movlw	D'15'
		movwf	reg1
loop	decfsz	reg1,1
		goto	loop
		retlw	0


;---Retardo 150us---
pausa2	movlw	D'45'
		movwf	reg1
loop2	decfsz	reg1,1
		goto	loop2
		retlw	0


		
;---Retardo---
;pausa	movlw	10	     	; Aquí se cargan los registros
;		movwf	reg1	    ; reg1, reg2 y reg3
;			     			; con los valores 10, 20 y 30
;tres	movlw	10	     	; respectivamente
;		movwf	reg2
;
;dos		movlw	5
;		movwf	reg3
;
;uno		decfsz	reg3,1	    ; Aquí se comienza a decrementar
;		goto	uno	     	; Cuando reg3 llegue a 0 
;		decfsz	reg2,1	    ; le quitare 1 a reg2
;		goto	dos	     	; cuando reg2 llegue a 0	
;		decfsz	reg1,1	    ; le quitare 1 a reg1
;		goto	tres	    ; cuando reg1 llegue a 0
;		bsf		porte,1
		;retlw	00     		; regresare al lugar 
;		return	     			; de donde se hizo la llamada

;---FIN---
		end
