;**************************************************************************
;
;PIC-programmer and experiment board K8048 Velleman;
;
;**************************************************************************
;Author: Henny Reudink
;Call  : PA3BYB
;Data  : 2016 januari 06
;**************************************************************************
;Target device - PIC16F627
;Frequency : 4 MHz cristal
;**************************************************************************
;
;	    Led chaser sequense Sequense reverse switch SW1
;	    Based on idea Michael A. Covington
;
;**************************************************************************
;
;Version  : 0.1
;
;**************************************************************************
;Uitgang                      Ingang
;   Led LD1 RB0 (pin6)        Switch SW1 RA0 (pin17)
;   Led LD2 RB1 (pin7)        Switch SW2 RA1 (pin18)
;   Led LD3 RB2 (pin8)        Switch SW3 RA2 (pin1)
;   Led LD4 RB3 (pin9)        Switch SW4 RA3 (pin2)
;   Led LD5 RB4 (pin10)
;   Led LD6 RB5 (pin11)
;
;************************************************************************** 
;
;   CPU-configuaration  
;   Processor PIC16F627
;
;******************************************************************************
    processor   pic16f627
    include     p16f627.inc     ;include standard header for selected device

    __CONFIG _BODEN_OFF & _CP_OFF & DATA_CP_OFF & _LVP_OFF & _MCLRE_OFF & _PWRTE_ON & _XT_OSC & _WDT_OFF 

;******************************************************************************
;                          Setup and declare names
;******************************************************************************
Jcount   equ            H'7F'   ; J = address hex 7F
Kcount   equ            H'7E'   ; K = address hex 7E
   
; Program start
    org        0                ;Start adress is 0000h

;******************************************************************************
;                          Setup ports
;
; The PIC16F627A/628A/648A have two ports, PORTA and PORTB. Some pins for these
; I/O ports are multiplexed with alternate functions for the peripheral features
; on the device. In general, when a peripheral is enabled, that pin may not be
; used as a general purpose I/O pin.

; The PORTA pins are multiplexed with comparator and voltage reference functions
; The operation of these pins are selected by control bits in the CMCON 
; (Comparator Control register) register and the VRCON (Voltage Reference 
; Control register) register. When selected as a comparator input, these pins 
; will read as ?0?s.
;
; To use PORTA as I/O you need to declare the Port first.
; CLRF     PORTA     Initialize PORTA by setting output data latches
; MOVLW    0x07      Turn comperators off and enable pins for I/O functions
;                    Bit7 0000 0110 Bit0 
; MOVWF    CMCON     Bit RA<2:0> enable pins for I/O functions
;
;********************To make the PORTA pins output
; BCF    STATUS,RP1       Clear byte F
; BSF    STATUS,RP0       Select Bank 1
; MOVWF  0x85             F->TRISA = RA pin 0-3 input
; BCF    STATUS,RP0       Select Bank 0
;
;******************************************************************************
; Tristate PORTA := I/O
    movlw	0007		;Turn comparators off and enable pins for i/o
    movwf	CMCON		; b := 00000110

;******************************************************************************
; Pins RA0 - RA3 PORTA = input
; Switch on RA0 - RA3 normaly '1'

    bsf         STATUS,RP0      ;Select reg bank 1
    movlw       b'00001111'     ;w := b'0000 1111'
    movwf       85              ;F->TRISA = RA 0-3 input
    bcf         STATUS,RP0      ;Select reg bank 0

;******************************************************************************
; At first step all pins PORTB = output
; RB0 := '1' Led LD1 burn first    

    bsf         STATUS,RP0      ;select reg bank 1
    movlw       B'00000000'     ;w := binary 0000 0000 
    movwf       86              ;F->TRISB = PORTB output
    bcf         STATUS,RP0      ;Select reg bank 0
    movlw       B'00000001'     ;W := binary 0000 0001
    movwf       PORTB           ;PORTB0 := '1' First Led start Led

;******************************************************************************
; Clear carry flag

    bcf     STATUS,0            ;Clear carry flag

;Main loop. Check PORTA, bit = '0' and rotate eighter left or right till C:='1'
    
mloop:
    btfss   PORTA,0             ;skip next instuction if bit=1
    goto    m1
    rrf     PORTB,f             ;rotate PORTB bits to the left
    goto    m2
    
m1:
    rlf     PORTB,f             ;rotate PORTB bits to the right
m2:                             ; waste some time
    movlw   h'FF'               ;w := 50 decimaal
    movwf   Jcount
jloop:
    movwf   Kcount
kloop:
    decfsz  Kcount,f            ;K := K-1, skip next if zero
    goto    kloop
    decfsz  Jcount,f            ;J := J-1, skip next if zero
    goto    jloop
    goto    mloop               ; do it all again
    
ROUTINE  
COUNT   equ   h'FF'
LABEL:	decfsz	COUNT,1
        goto	LABEL
        RETURN
    
    end