; PIC 16F688 code for a simple "hello world": blink an LED
;
; ### license
;

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

        __config DEFAULT_CONFIG

 constant NODE_ADDRESS = 0x00

ADDRESS equ     0x20



        ; This code runs at reset (and powerup)
        org     0x0000
reset:
        ; STATUS starts with b00011xxx. (note: bank0)
        ; OPTION_REG starts with 0xFF.
        ; INTCON starts with 0x00.
        ; PIE1 is ignored due to PEIE == 0
        ; OSCCON starts with b-110x000. (note: SCS == 0)

        ; ANSEL starts with 0xFF. Disable the analog inputs:
        bank1
        clrf    ANSEL
        bank0s

        ; CMCON0 starts with 0x00. Disable the comparators:
        movlw   0x07
        movwf   CMCON0

        ; Initialize PORTA/C output latches to the LOW state.
        clrf    PORTA
        clrf    PORTC

        ; TRISA/C start with b--111111. Set RAx/RCx as (digital) outputs.
        ; Note: RA3 is a digital input, with a disabled pull-up.
        bank1s
        clrf    TRISA
        clrf    TRISC
        bank0s

        ; copy the node's I2C address from EEPROM into a register
        bank1s
        movlw   NODE_ADDRESS
        movwf   EEADR
        bcf     EECON1, EEPGD   ; select EEPROM
        bsf     EECON1, RD      ; perform the read
        movfw   EEDAT
        bank0s
        movwf   ADDRESS

blip:   macro
        bsf     PORTA, 1
        bcf     PORTA, 1
        endm

copybit:        macro BIT
        btfss   ADDRESS, BIT
        goto    $ + 3
        bsf     PORTA, 0
        goto    $ + 2
        bcf     PORTA, 0
        noop10
        bcf     PORTA, 0
        bsf     PORTA, 0
        bcf     PORTA, 0
        endm

noop10: macro
        nop
        nop
        nop
        nop
        nop
        nop
        nop
        nop
        nop
        nop
        endm

loop:
        blip
        noop10

        copybit 7
        copybit 6
        copybit 5
        copybit 4
        copybit 3
        copybit 2
        copybit 1
        copybit 0

        bcf     PORTA, 0

        goto loop

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