; ; Shared utility methods ; ; ; Put a (very psuedo) random number into ; the 'rnd' file register. Part of the magic ; of this is to use TMR0, which is always ; incrementing. By activating the watchdog ; timer, we can land up back at PC=0 with a ; new random value in TMR0, due to the ; unsynchronized clock. ; ; 9c (including call & return) Rnd: MOVFW rnd ADDWF TMR0,w RLF rnd,f RLF rnd,f ADDWF rnd,f RETLW 0 ; update all 5 leds ; cycles: 26 including the call RefreshLeds: ; advance each dac CLRF bitsOut StepDac led4,bitsOut ; 3 cycles StepDac led3,bitsOut ; 3 cycles ; just a zero for bit 3 BCF STATUS,C RLF bitsOut,f StepDac led2,bitsOut ; 3 cycles StepDac led1,bitsOut ; 3 cycles StepDac led0,bitsOut ; 3 cycles MOVFW bitsOut XORLW LEDSOFFVALUE ; invert for sink led MOVWF GPIO CLRWDT RETLW 0 ; move each LED to its target value, pausing ; d2 tween each step. ; do 256 steps to be sure. ; ; i_t0 -- number of steps (256 on the special entry) ; (i_t2,i_t1) -- 16 bits, countdown - 1 (0,0) is shortest) ; uses: d0, d1. ; ; cycles for GlideLeds: d0 * (196 + d2 * 33) ; GlideLeds256: ; glide LEDs all the way... CLRF d0 ; outer counter, make sure we do 256 altogether GlideLeds: ; glide LEDs by d0 steps ; inc16 the countdown _glideLedsLoop1: CALL RefreshLeds ; 26c MIGFPIN led0,led0Target ; 8c CALL RefreshLeds MIGFPIN led1,led1Target CALL RefreshLeds MIGFPIN led2,led2Target CALL RefreshLeds MIGFPIN led3,led3Target CALL RefreshLeds MIGFPIN led4,led4Target ; now delay by d2 refreshes... CALL RefreshLeds ; (147c so far) ; refresh leds by (i_t2,i_t1) times BANKSEL i_t1 MOVFF i_t1,d1 MOVFF i_t2,d2 BANKSEL 0 INCF d1,f ;IFC INCF d2,f ; countdown via (d2,d1) 16-bit value. _glideLedsLoop: CALL RefreshLeds ; 23c NOPS 5 ; 5c DECFSZ d1,f ; 1c usually GOTO _glideLedsLoop ; 2c DECFSZ d2,f ; 1c usually GOTO _glideLedsLoop ; 2c DECFSZ d0,f ; 1c usually GOTO _glideLedsLoop1 ; 2c RETLW 0 ; 2c HoldLeds256: CLRF d0 HoldLeds: MOVFF led0,led0Target MOVFF led1,led1Target MOVFF led2,led2Target MOVFF led3,led3Target GOTO GlideLeds Wait1: ; 1 s MOVLF16 333333/256,d0 GOTO Wait Wait01: ; .1 s MOVLF16 33333/256,d0 GOTO Wait Wait05: ; half second MOVLF16 333333/2/256,d0 ;and fall into Sleep ; sleep (d0,d1) cycles ; time ~ t / 333333 Wait: CLRF t INCF d0 INCF d1 _sleepLoop: DECFSZ t,f GOTO _sleepLoop DECFSZ d0,f GOTO _sleepLoop DECFSZ d1,f GOTO _sleepLoop RETLW 0 PowerupDisplay: MOVLF LEDSOFFVALUE,GPIO CALL Wait1 MOVLF LEDSONVALUE,GPIO CALL Wait1 MOVLF LEDSOFFVALUE,GPIO CALL Wait1 MOVLF 20,d3 _puflicker: MOVLF LEDSONVALUE,GPIO CALL Wait01 MOVLF LEDSOFFVALUE,GPIO CALL Wait01 DECFSZ d3,f GOTO _puflicker MOVLF LEDSOFFVALUE,GPIO CALL Wait1 CALL Wait1 ; count d2 up to 5 CLRF d2 MOVLF 1,bitsOut _puloop: ; bit number d2 is on bitsOut MOVFF d2,d3 INCF d3,f _pu_1: MOVFW bitsOut XORLW LEDSOFFVALUE MOVWF GPIO CALL Wait05 MOVLF LEDSOFFVALUE,GPIO CALL Wait05 DECFSZ d3,f GOTO _pu_1 BCF STATUS,C RLF bitsOut,f IFBS bitsOut,3 ; skip bit 3 RLF bitsOut,f INCF d2,f IFBC bitsOut,6 GOTO _puloop RETLW 0