;-----------------------------------------------------------------------------------------------------------
;put the following at address 0000h (starting vector)
;-----------------------------------------------------------------------------------------------------------
org 0000h
;chip configuraton
movwf OSCCAL ; move the osc cal value stored at the end of code to the osccal reg MUST be first instruction
movlw 0xD6
OPTION ; TMR0 as a timer with 128 prescaler
goto INITREGS
;-----------------------------------------------------------------------------------------------------------
; This chunk of code is to fix a issue with the 12F parts not able to call subs in the lower half of the page...
;-----------------------------------------------------------------------------------------------------------
_RANDOM
goto _RANDOMLONG
;-----------------------------------------------------------------------------------------------------------
;init registers
INITREGS
;-----------------------------------------------------------------------------------------------------------
movlw 0xff
movwf GPIO ; set all bits high (leds off)
movlw 0x08
TRIS GPIO ; set LED pins as outputs
movlw 0x30 ; set up inital seed (MUST NOT BE 0x0000!)
movwf led0h
movlw 0x45
movwf led0l
movlw 0x05 ; set up inital seed (MUST NOT BE 0x0000!)
movwf led1h
movlw 0x30
movwf led1l
movlw 0xA2 ; set up inital seed (MUST NOT BE 0x0000!)
movwf led2h
movlw 0x11
movwf led2l
movlw 0xC5 ; set up inital seed (MUST NOT BE 0x0000!)
movwf led4h
movlw 0xEE
movwf led4l
movlw 0xDE ; set up inital seed (MUST NOT BE 0x0000!)
movwf led5h
movlw 0xAD
movwf led5l
movlw 0xFF ; Init the wind control vars
movwf wind
movwf windcntl
movlw 0x01
movwf windcnth
;-----------------------------------------------------------------------------------------------------------
;start of functional code
STARTOFCODE
;-----------------------------------------------------------------------------------------------------------
movf TMR0, W ; grab the current timer value
andlw DIMSTEPS ; mask off any upper bits not used for dimming the leds
movwf phase ; this move does NOT affect the status reg so next check should be valid
btfss STATUS, Z ; if the result is 0 turn on all the leds
goto CLEARDONE
movlw 0xFF
btfss GPIO, 3 ; check mode pin if high use active high outputs
clrf GPIO ; all outputs low for active low outputs
btfsc GPIO, 3 ; check mode pin if low use active low outputs
movwf GPIO ; all outputs high for active high outputs
CLEARDONE
incfsz wind, W ; check if wind is set add 1 if set goto windy code
goto WINDYLVS
; conditions are normal use full range
movf led0l, W ; grab the random output
andlw DIMSTEPS ; mask off unused bits
andlw CALMLEDMAX ; apply limits
iorlw CALMLEDMIN ; apply limits
subwf phase, W ; suptract the result from the current phase value
btfss STATUS, C ; if there is a carry (negitive) turn the leds off
goto LED0U
btfss GPIO, 3 ; check mode pin if high use active high outputs
bsf GPIO, 0 ; turn off the led set pin high for active low pins
btfsc GPIO, 3 ; check mode pin if low use active low outputs
bcf GPIO, 0 ; turn off the led set pin low for active high pins
LED0U
movf led1l, W ; SEE led0l description above for comments
andlw DIMSTEPS
andlw CALMLEDMAX
iorlw CALMLEDMIN
subwf phase, W
btfss STATUS, C
goto LED1U
btfss GPIO, 3 ; check mode pin
bsf GPIO, 1
btfsc GPIO, 3 ; check mode pin
bcf GPIO, 1
LED1U
movf led2l, W ; SEE led0l description above for comments
andlw DIMSTEPS
andlw CALMLEDMAX
iorlw CALMLEDMIN
subwf phase, W
btfss STATUS, C
goto LED2U
btfss GPIO, 3 ; check mode pin
bsf GPIO, 2
btfsc GPIO, 3 ; check mode pin
bcf GPIO, 2
LED2U
movf led4l, W ; SEE led0l description above for comments
andlw DIMSTEPS
andlw CALMLEDMAX
iorlw CALMLEDMIN
subwf phase, W
btfss STATUS, C
goto LED4U
btfss GPIO, 3 ; check mode pin
bsf GPIO, 4
btfsc GPIO, 3 ; check mode pin
bcf GPIO, 4
LED4U
movf led5l, W ; SEE led0l description above for comments
andlw DIMSTEPS
andlw CALMLEDMAX
iorlw CALMLEDMIN
subwf phase, W
btfss STATUS, C
goto LED5U
btfss GPIO, 3 ; check mode pin
bsf GPIO, 5
btfsc GPIO, 3 ; check mode pin
bcf GPIO, 5
LED5U
goto UPDATEDONE ; finished processing leds in normal conditions
WINDYLVS
; conditions are windy
movf led0l, W ; SEE led0l description above for comments
andlw DIMSTEPS
andlw WINDLEDMAX
iorlw WINDLEDMIN
subwf phase, W
btfss STATUS, C
goto LED0UW
btfss GPIO, 3 ; check mode pin
bsf GPIO, 0
btfsc GPIO, 3 ; check mode pin
bcf GPIO, 0
LED0UW
movf led1l, W ; SEE led0l description above for comments
andlw DIMSTEPS
andlw WINDLEDMAX
iorlw WINDLEDMIN
subwf phase, W
btfss STATUS, C
goto LED1UW
btfss GPIO, 3 ; check mode pin
bsf GPIO, 1
btfsc GPIO, 3 ; check mode pin
bcf GPIO, 1
LED1UW
movf led2l, W ; SEE led0l description above for comments
andlw DIMSTEPS
andlw WINDLEDMAX
iorlw WINDLEDMIN
subwf phase, W
btfss STATUS, C
goto LED2UW
btfss GPIO, 3 ; check mode pin
bsf GPIO, 2
btfsc GPIO, 3 ; check mode pin
bcf GPIO, 2
LED2UW
movf led4l, W ; SEE led0l description above for comments
andlw DIMSTEPS
andlw WINDLEDMAX
iorlw WINDLEDMIN
subwf phase, W
btfss STATUS, C
goto LED4UW
btfss GPIO, 3 ; check mode pin
bsf GPIO, 4
btfsc GPIO, 3 ; check mode pin
bcf GPIO, 4
LED4UW
movf led5l, W ; SEE led0l description above for comments
andlw DIMSTEPS
andlw WINDLEDMAX
iorlw WINDLEDMIN
subwf phase, W
btfss STATUS, C
goto UPDATEDONE
btfss GPIO, 3 ; check mode pin
bsf GPIO, 5
btfsc GPIO, 3 ; check mode pin
bcf GPIO, 5