;	06-04-2008
;	Practica 3.ASM
;	Apertura y cierre de cortinas
;	PIC 16f84a
;	MPLAB 8.00
;	PROTEUS 7.2 SP2
;	Guido Carmona Girón
;--------------- Encabezado -------------------------------------------------------

	LIST	P=16F84A,				; usar PIC 16F84A
	#include <p16f84A.inc>
 	errorlevel      -302    			;Eliminar banco mensajes
	__CONFIG _CP_OFF&_PWRTE_ON&_WDT_OFF&_XT_OSC	; code protec		off
							; power up timer	on
							; watchdog		off
							; osc: 1 MHz		XT

;------------------------------------------------------------------------------------

CBLOCK 0x10   ; Temporary storage                 
                 pos
                 dc1
                 dc2
              ENDC
CBLOCK 0x10   ; Temporary storage
              ENDC

              ORG   0
entrypoint    goto  start

              ORG   4
intvector     goto  intvector
        
start         clrw                    ; Zero.

              movwf   PORTB           ; Ensure PORTB is zero before we enable it.
              bsf     STATUS,RP0      ; Select Bank 1
              movlw   0xF0            ; Set port B bits 0-3 as outputs
              movwf   TRISB           ; Set TRISB register.           
                                                               
              bcf     STATUS,RP0      ; Select Bank 0
                 
              movlw   3  	      ; Initialize the motor position 
              movwf   pos                                             
              movwf   PORTB
              call    delay
              clrf    PORTB	      ; Motor drive off	

;Main loop               
loop	      btfss   PORTA,0         ; Test clockwise button
	      call    stepcw
	      btfss   PORTA,1         ; Test anti-clockwise button
	      call    stepccw
	      goto loop
                               
;Rotate one step clockwise                               
stepcw        bcf    STATUS,C	      ; Clear the carry flag
 	      btfsc  pos,3            ; Set carry if this bit set
 	      bsf    STATUS,C
	      rlf    pos,W	      ; Pick up and rotate the motor's current position
              andlw  0x0F             ; Mask to lower nibble
              movwf  pos
              movwf  PORTB	      ; Drive the outputs
              call   delay	      ; Wait
              clrf   PORTB            ; Clear the output
              return

;Rotate one step counter clockwise                                                                             		
stepccw       bcf    STATUS,C	      ; Clear the carry flag
	      btfsc  pos,0
	      bsf    pos,4
	      rrf    pos,W	      ; Pick up and rotate the motor's current position
              andlw  0x0F             ; Mask to lower nibble
              movwf  pos
              movwf  PORTB	      ; Drive the outputs
              call   delay	      ; Wait
              clrf   PORTB            ; Clear the output
              return

; This routine implements the delay between steps,
; and thus controls the motor speed.
delay         movlw   18	     ; Outer loop iteration count
	      movwf   dc1		
dl1	      clrf    dc2            ; Initialize inner loop
dl2	      nop
	      nop
	      decfsz  dc2,F         
	      goto    dl2
	      decfsz  dc1,F
	      goto    dl1
	      return

	      END