Learning project for Control sw1 – sw2

Als onderdeel van een leerproces om de Programmable Integrated Controller PIC een beetje onder de knie te krijgen is gestart met de PIC16F627 dat meegeleverd wordt met het experimenteer bordje P8048 van Velleman. Het onderstaande programma is ook op dat bordje geschreven.
Bij ingebruikname van dat experimenteerbordje liep ik op tegen het fenomeen dat de PIC slechts een keer was te programmeren. Herprogrammeren leverde een foutmelding op.
Na enig zoeken bleek de fout te vinden in het bij het bordje meegeleverde software. Gebruik dan ook de door Velleman beschikbare gestelde versie uit 2009 die je hier vindt. Die werkt prima. 
Het programma laat op elke toetsdruk een ander led-patroon zien van de op het experimenteerbord gemonteerde led's
 
;******************************************************************
;
;PIC-programmer and experiment board K8048 Velleman;
;
;******************************************************************
;Author: Henny Reudink
;Call  : PA3BYB
;Data  : 2016 januari 06
;******************************************************************
;Processor :- PIC16F627
;Frequency : 4 MHz cristal
;******************************************************************
;
;        Learning project for control sw1 - sw4     
;        Version  : 0.1
;
;******************************************************************
;Output pins                  Input pins
;   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)
;******************************************************************
;**********************Setup and declare names
    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 

Jcount   equ            H'20'   ; J = address hex 7F
Kcount   equ            H'21'   ; K = address hex 7E

;**********************Program start
    org        0                ;Start adress is 0000h

;***********************Setup ports
; 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 RA<3-0> PORTA = input
; Switch on RA<0-3> normaly '0'
    bsf         STATUS,RP0      ;Select reg bank 1
    movlw       b'00001111'     ;w := b'0000 1111'
    movwf       85              ;F->TRISA = RA<3-0> input
    bcf         STATUS,RP0      ;Select reg bank 0

;******************************************************************
; At first step pinsRB<7-0> PORTB = output
; When use rrf or rlf on PORTB then all ports need to be output!!!
    bsf         STATUS,RP0      ;select reg bank 1
    movlw       B'00000000'     ;w := binary 0000 0000 
    movwf       86              ;F->TRISB = PORTB<5-0> output
    bcf         STATUS,RP0      ;Select reg bank 0

;******************************************************************
; Clear carry flag f reg
    bcf     STATUS,0            ;Clear carry flag

; Main loop

mloop:
    btfsc   PORTA,0             ;skip next instuction if bit=0
    call    switch1             ;test op switch1
    btfsc   PORTA,1
    call    switch2             ;test op switch2
    btfsc   PORTA,2
    call    switch3             ;test op switch3
    btfsc   PORTA,3
    call    switch4             ;test op switch4
    goto    mloop               ; begin opnieuw

switch1:                        ;Rotate Portb bit to the right
    movlw   B'00000001'
    movwf   PORTB
switch11:
    rrf     PORTB,f             ;Flash led 7 as first of secuence
    call    waste
    btfss   PORTB,0             ;All bit's Flash?
    goto    switch11
    btfsc   PORTA,0             ;Switch1 pushed?
    goto    switch1
    movlw   b'00000000'
    movwf   PORTB               ;Switch all led's off
    return

switch2:                        ;Rotate PORTB bit to the left
    movlw   B'10000000'
    movwf   PORTB
switch12:
    rlf     PORTB,f             ;Flash led 0 as first of sequence
    call    waste               ;Let led burn sometime
    btfss   PORTB,7             ;All bit's flash?
    goto    switch12
    btfsc   PORTA,1             ;Switch2 pushed?
    goto    switch2
    movlw   b'00000000'
    movwf   PORTB               ;Switch all led's off
    return

switch3:                        ;Led's burn alternately
    movlw   B'01010101'
    movwf   PORTB
switch31:
    movlw   b'11111111'
    xorwf   PORTB,f
    call    waste
    btfsc   PORTA,2             ;Switch 3 pushed?
    goto    switch31
    clrf    PORTB
    return

switch4:                        ;opposite loop light
    movlw   b'00100001'
    movwf   PORTB               ;bit0 en bit7 '1'
    call    waste
    movlw   b'00010010'
    movwf   PORTB               ;bit1 en bit6 '1'
    call    waste
    movlw   b'00001100'
    movwf   PORTB               ;bit2 en bit5 '1'
    call    waste
    movlw   b'00010010'
    movwf   PORTB               ;bit3 en bit4 '1'
    call    waste
    movlw   b'00100001'
    movwf   PORTB               ;bit2 en bit5 '1'
    call    waste
    clrf    PORTB
    return

;******************Waste Time
waste:                          ; waste some time
    movlw   h'FF'               ;w := 50 decimaal
    movwf   Jcount
delay1:
    movwf   Kcount
delay2:
    decfsz  Kcount,f            ;Kcount := K-1, skip next if zero
    goto    delay2
    decfsz  Jcount,f            ;Jcount := J-1, skip next if zero
    goto    delay1
    RETURN

    end