
;                                                                     *
;    Author: Ing. Roberto Moreno Elizalde                             *
;    Company:Electronica Steren SA de CV							  *
;    Este programa verifica el estado de los dip switches conectados  * 
;    al puerto A, asignando una cantidad de repeticiones dependiendo  *
;	 del switch activado, para posteriormente ejecutar las rutinas de *
;    dos semaforos en el puerto B, las repeticiones sirven para       *
;	 alargar o acortar los tiempos de activacin.                     *
;**********************************************************************

	list      p=16F84A            ; list directive to define processor
	#include <p16F84A.inc>        ; processor specific variable definitions

	__CONFIG   _CP_OFF & _WDT_OFF & _PWRTE_ON & _XT_OSC

;***** VARIABLE DEFINITIONS
w  		EQU     0        ; w=guardar en w
f	   	EQU     1        ; f=guardar en el registro
estado  EQU     0x03

 cblock	0x0C
	PDel0
	PDel1
	PDel2
	repeat
	repeaux
 endc
;**********************************************************************
		ORG     0x000           ; processor reset vector
  		goto    main            ; go to beginning of program
		ORG		0x04			;**Vector de interrupcin

main	bsf 	STATUS,RP0
		movlw	0x1F
		movwf	PORTA			;PA entradas
		movlw	0x00
		movwf 	PORTB			;PB salida 
	    bcf	    STATUS,RP0		
			
    	clrwdt
;***************************************************************************	
		clrf	PORTB	;Borra el puerto

swi0	btfsc	PORTA,0
		goto	swi1
		goto	tempo0

swi1	btfsc	PORTA,1
		goto	swi2
		goto	tempo1


swi2	btfsc	PORTA,2
		goto	swi3
		goto	tempo2

swi3	btfsc	PORTA,3
		goto	tempo4
		goto	tempo3

tempo4	movlw	0x05
		movwf	repeat
		goto	intreci

tempo0	movlw	0x01
		movwf	repeat
		goto	intreci

tempo1	movlw	0x02
		movwf	repeat
		goto	intreci

tempo2	movlw	0x03
		movwf	repeat
		goto	intreci

tempo3	movlw	0x04
		movwf	repeat

;*************************************************************

intreci bcf		PORTB,5	;Apaga el rojo2
		bsf		PORTB,0	;Enciende el rojo1
		bsf		PORTB,3	;Enciende el verde2
		call	PDelay
		call	PDelay
		call	PDelay
		call	PDelay

		bcf		PORTB,3 ;Apaga el verde2

		bsf		PORTB,4	;Enciende el Amarillo2
		call	DEMORA
	
		bcf		PORTB,4	;Apaga el Amarillo2
		call	DEMORA
	
		bsf		PORTB,4	;Enciende el Amarillo2
		call	DEMORA
		
		bcf		PORTB,4	;Apaga el Amarillo2
		call	DEMORA
		
		bsf		PORTB,4	;Enciende el Amarillo2
		call	DEMORA
		
		bcf		PORTB,4	;Apaga el Amarillo2
		call	DEMORA
		

		bcf		PORTB,0	;Apaga el rojo 1
		bsf		PORTB,2	;Enciende el Verde1
		bsf		PORTB,5 ;Enciende el Rojo2
		call	PDelay
		call	PDelay
		call	PDelay
		call	PDelay

		bcf		PORTB,2 ;Apaga el verde1

		bsf		PORTB,1	;Enciende el Amarillo1
		call	DEMORA
		
		bcf		PORTB,1	;Apaga el Amarillo1
		call	DEMORA
		
		bsf		PORTB,1	;Enciende el Amarillo1
		call	DEMORA
		
		bcf		PORTB,1	;Apaga el Amarillo1
		call	DEMORA
		
		bsf		PORTB,1	;Enciende el Amarillo1
		call	DEMORA
		
		bcf		PORTB,1	;Apaga el Amarillo1
		call	DEMORA
				
		goto    swi0
		
		
;***************************************************************************

PDelay	movf	  repeat,w
		movwf	  repeaux
PDelay1 movlw     .239      ; 1 set number of repetitions (B)
        movwf     PDel0     ; 1 |
PLoop1  movlw     .232      ; 1 set number of repetitions (A)
        movwf     PDel1     ; 1 |
PLoop2  clrwdt              ; 1 clear watchdog
PDelL1  goto PDelL2         ; 2 cycles delay
PDelL2  goto PDelL3         ; 2 cycles delay
PDelL3  clrwdt              ; 1 cycle delay
        decfsz    PDel1, 1  ; 1 + (1) is the time over? (A)
        goto      PLoop2    ; 2 no, loop
        decfsz    PDel0,  1 ; 1 + (1) is the time over? (B)
        goto      PLoop1    ; 2 no, loop
PDelL4  goto PDelL5         ; 2 cycles delay
PDelL5  goto PDelL6         ; 2 cycles delay
PDelL6  goto PDelL7         ; 2 cycles delay
PDelL7  clrwdt              ; 1 cycle delay
		decfsz	repeaux,f
		goto	PDelay1
        return              ; 2+2 Done
;***********************************************************************************

DEMORA  movlw     .14       ; 1 set numero de repeticion  (C)
        movwf     PDel0     ; 1 |
PLoop01  movlw     .72       ; 1 set numero de repeticion  (B)
        movwf     PDel1     ; 1 |
PLoop11  movlw     .247      ; 1 set numero de repeticion  (A)
        movwf     PDel2     ; 1 |
PLoop21  clrwdt              ; 1 clear watchdog
        decfsz    PDel2, 1  ; 1 + (1) es el tiempo 0  ? (A)
        goto      PLoop21    ; 2 no, loop
        decfsz    PDel1,  1 ; 1 + (1) es el tiempo 0  ? (B)
        goto      PLoop11    ; 2 no, loop
        decfsz    PDel0,  1 ; 1 + (1) es el tiempo 0  ? (C)
        goto      PLoop01    ; 2 no, loop
PDelL11  goto PDelL21         ; 2 ciclos delay
PDelL21  clrwdt              ; 1 ciclo delay
        return              ; 2+2 Fin.


 end