#INCLUDE <P16F84.INC>

__CONFIG _CP_OFF & _WDT_OFF & _PWRTE_ON & _XT_OSC
CBLOCK 0X0C
Q0
ENDC
ORG 0

INICIO

BSF STATUS, RP0 	
CLRF TRISB		;LIMPIAMOS TRISB PARA QUE PORTB SEA SALIDA
MOVLW 0xFF
MOVWF TRISA		;PONEMOS EN 1 TRISA PARA QUE PORTA SEA ENTRADA
BCF STATUS, RP0

COMIENZA


ENTRADA
BTFSS PORTA,0
GOTO ENTRADA
MOVLW D'50'
MOVWF Q0

ENTRD
MOVLW B'00000001'
MOVWF PORTB
CALL DELAY_SUBIDA ;;2MS
MOVLW 0X00
MOVWF PORTB
CALL DELAY_SUBIDA_MS ;18MS
DECFSZ Q0,F
GOTO ENTRD
CALL BAJADA_1
GOTO COMIENZA

BAJADA_1
BTFSS PORTA,1
GOTO BAJADA_1
MOVLW D'50'
MOVWF Q0

BAJD
MOVLW B'00000001'
MOVWF PORTB
CALL DELAY_0G ;;1MS
MOVLW 0X00
MOVWF PORTB
CALL DELAY_0GB ;;19MS
DECFSZ Q0,F
GOTO BAJD
CALL BAJADA_2
GOTO COMIENZA

BAJADA_2
BTFSS PORTA,3
GOTO BAJADA_2
MOVLW D'50'
MOVWF Q0

BAJD2
MOVLW B'00000001'
MOVWF PORTB
CALL DELAY_8 ;;1.5MS
MOVLW 0X00
MOVWF PORTB
CALL DELAY_8B ;;19MS
DECFSZ Q0,F
GOTO BAJD2
GOTO COMIENZA


; Delay = 0.0015 seconds ;;0 GRADOS
; Clock frequency = 4 MHz

; Actual delay = 0.0015 seconds = 1500 cycles
; Error = 0 %
	cblock
	d1
	d2
	d3
	endc
DELAY_8
			;18493 cycles
	movlw	0x72
	movwf	d1
	movlw	0x0F
	movwf	d2
Delay_78
	decfsz	d1, f
	goto	$+2
	decfsz	d2, f
	goto	Delay_78

			;3 cycles
	goto	$+1
	nop

			;4 cycles (including call)
	return

DELAY_8B
			;1493 cycles
	movlw	0x2A
	movwf	d1
	movlw	0x02
	movwf	d2
Delay_B8
	decfsz	d1, f
	goto	$+2
	decfsz	d2, f
	goto	Delay_B8

			;3 cycles
	goto	$+1
	nop

			;4 cycles (including call)
	return

DELAY_0G
			;1493 cycles
movlw	0xC6
	movwf	d1
	movlw	0x01
	movwf	d2
Delay_0G
decfsz	d1, f
	goto	$+2
	decfsz	d2, f
	goto	Delay_0G

			;3 cycles
	goto	$+1
	nop

			;4 cycles (including call)
	return

; Delay = 0.0005 seconds
; Clock frequency = 4 MHz

; Actual delay = 0.0005 seconds = 500 cycles
; Error = 0 %


DELAY_0GB
			;496 cycles
movlw	0xD6
	movwf	d1
	movlw	0x0F
	movwf	d2
Delay_0B
decfsz	d1, f
	goto	$+2
	decfsz	d2, f
	goto	Delay_0B

			;3 cycles
	goto	$+1
	nop

			;4 cycles (including call)
	return


; Delay = 0.002 seconds  90 GRADOS
; Clock frequency = 4 MHz

; Actual delay = 0.002 seconds = 2000 cycles
; Error = 0 %



DELAY_SUBIDA
			;1993 cycles
	movlw	0x8E
	movwf	d1
	movlw	0x02
	movwf	d2
Delay_K
	decfsz	d1, f
	goto	$+2
	decfsz	d2, f
	goto	Delay_K

			;3 cycles
	goto	$+1
	nop

			;4 cycles (including call)
	return

; Delay = 0.018 seconds
; Clock frequency = 4 MHz

; Actual delay = 0.018 seconds = 18000 cycles
; Error = 0 %

DELAY_SUBIDA_MS
			;17993 cycles
	movlw	0x0E
	movwf	d1
	movlw	0x0F
	movwf	d2
Delay_q
	decfsz	d1, f
	goto	$+2
	decfsz	d2, f
	goto	Delay_q

			;3 cycles
	goto	$+1
	nop

			;4 cycles (including call)
	return

; Delay = 0.019 seconds
; Clock frequency = 4 MHz

; Actual delay = 0.019 seconds = 19000 cycles
; Error = 0 %

DELAY_SUBIDAPB
			;18993 cycles
	movlw	0xD6
	movwf	d1
	movlw	0x0F
	movwf	d2
Delay_f
	decfsz	d1, f
	goto	$+2
	decfsz	d2, f
	goto	Delay_f

			;3 cycles
	goto	$+1
	nop

			;4 cycles (including call)
	return

; Delay = 2 seconds
; Clock frequency = 4 MHz

; Actual delay = 2 seconds = 2000000 cycles
; Error = 0 %

DELAY_ESPERA
			;1999996 cycles
	movlw	0x11
	movwf	d1
	movlw	0x5D
	movwf	d2
	movlw	0x05
	movwf	d3
Delay_F
	decfsz	d1, f
	goto	$+2
	decfsz	d2, f
	goto	$+2
	decfsz	d3, f
	goto	Delay_F

			;4 cycles (including call)
	return

; Delay = 0.001 seconds --90 GRADOS A LA REVERSA
; Clock frequency = 4 MHz

; Actual delay = 0.001 seconds = 1000 cycles
; Error = 0 %


DELAY_BAJADA
			;993 cycles
	movlw	0xC6
	movwf	d1
	movlw	0x01
	movwf	d2
Delay_2
	decfsz	d1, f
	goto	$+2
	decfsz	d2, f
	goto	Delay_2

			;3 cycles
	goto	$+1
	nop

			;4 cycles (including call)
	return

Retardo_500ms
			;499994 cycles
	movlw	0x03
	movwf	d1
	movlw	0x18
	movwf	d2
	movlw	0x02
	movwf	d3
Delay_5
	decfsz	d1, f
	goto	$+2
	decfsz	d2, f
	goto	$+2
	decfsz	d3, f
	goto	Delay_5

			;2 cycles
	goto	$+1

			;4 cycles (including call)
	return

Delay_MEDIO
			;499994 cycles
	movlw	0x1E
	movwf	d1
	movlw	0x4F
	movwf	d2
Delay_79
decfsz	d1, f
	goto	$+2
	decfsz	d2, f
	goto	Delay_79

			;3 cycles
	goto	$+1
	nop

			;4 cycles (including call)
	return
END