; PIC 16F688 code to roll blinky across 9 outputs
;
; See pwm2.asm for LED pinout.
;

        list p = 16f688
        include p16f688.inc
        errorlevel  -302        ; suppress banksel warning (302) from listing

        constant USING_WAIT_MACROS = 1
        include ../include/handy.inc

        __config DEFAULT_CONFIG

        ; This code runs at reset (and powerup)
.reset: org     0x0000
        goto    main

.main:  code                    ; relocatable
main:
        ; Note: bank is ENOCARE. This will force bank 0.
        ; Disable comparators. We're going to use all RA* as OUTPUTS.
        disable_comparators
        ; Now in Bank 0

        ; Start with everything "off" (active LOW).
        ; Note: this sets some pins to 0 which which actually remain
        ;   in HIGH-Z, but ... meh.
        movlw   0xFF
        iorwf   PORTA, F
        iorwf   PORTC, F

        bank1s
        run_at_8MHz
        no_analog               ; We want digital pins, not analog.

        ; At reset time, all ports are INPUT/HIGH-Z. Set all RA*,
        ; RC0/1/2/3 pins as (digital) outputs. Leave RC4/RC5 alone
        ; (these are typically I2C). Note TRISA3 is never writeable.
        clrf    TRISA
        movlw   0xF0
        andwf   TRISC, F

        ; Set up for 32.8ms timer busywait loops.
        prepare_timer0

        ; Back to Bank 0 for blockwait() and PORTA
        bank0s

short_pause: macro
        ; How many loops of 32.6ms should we run?
        movlw   D'1000' / D'326' ; 100.0ms / 32.6ms
        call    blockwait
        endm

blink:
        bcf     PORTC, RC3      ; LED1 on
        bsf     PORTC, RC1      ; LED9 off

        short_pause

        bcf     PORTC, RC2      ; LED2 on
        bsf     PORTC, RC3      ; LED1 off

        short_pause

        bcf     PORTA, RA4      ; LED3 on
        bsf     PORTC, RC2      ; LED2 off

        short_pause

        bcf     PORTA, RA5      ; LED4 on
        bsf     PORTA, RA4      ; LED3 off

        short_pause

        bcf     PORTA, RA0      ; LED5 on
        bsf     PORTA, RA5      ; LED4 off

        short_pause

        bcf     PORTA, RA1      ; LED6 on
        bsf     PORTA, RA0      ; LED5 off

        short_pause

        bcf     PORTA, RA2      ; LED7 on
        bsf     PORTA, RA1      ; LED6 off

        short_pause

        bcf     PORTC, RC0      ; LED8 on
        bsf     PORTA, RA2      ; LED7 off

        short_pause

        bcf     PORTC, RC1      ; LED9 on
        bsf     PORTC, RC0      ; LED8 off

        short_pause

        goto    blink


; stupid assemblers don't realize EOF means *end*
        end
