; ; infrastructure for fading LED's ; dvb 20070529 ; #include "P16F84.INC" ; -------------------------- ; 20070527dvb Step the dac ; state. dacState->sum, & ; dacState+1->value ; The state will be shifted onto ; the TOP bit of dest ; StepDac MACRO dacState,dest MOVFW dacState+1 ADDWF dacState,F RRF dest,F ENDM GoBank0 MACRO BCF STATUS,RP0 ; bitset bank 0 ENDM GoBank1 MACRO BSF STATUS,RP0 ; bitset bank 1 ENDM ; 0x0c to 0x4f is all for us. fDelay EQU 0x0c fTemp EQU 0x0d irqSaveW EQU 0x0e irqSaveS EQU 0x0f led0 EQU 0x10 led1 EQU 0x12 led2 EQU 0x14 led3 EQU 0x16 led4 EQU 0x18 led5 EQU 0x1a led6 EQU 0x1c led7 EQU 0x1e brightnessLow0 EQU 0x20 brightnessLow1 EQU 0x21 brightnessLow2 EQU 0x22 brightnessLow3 EQU 0x23 brightnessHigh0 EQU 0x24 brightnessHigh1 EQU 0x25 brightnessHigh2 EQU 0x26 brightnessHigh3 EQU 0x27 bump0 EQU 0x28 bump1 EQU 0x29 bump2 EQU 0x2a bump3 EQU 0x2b setBLED EQU 0x2c setBB EQU 0x2d bitsOut EQU 0x30 ; +-------------------------------- ; | The Origin. ; | ORG 0 GOTO Start ORG 4 ; <-- rather important! IrqHandler MOVWF irqSaveW SWAPF STATUS,W MOVWF irqSaveS ; saved swapped GoBank0 BSF PORTB,0 ; "inside irq" monitor MOVLW 0xa0 MOVWF TMR0 ; Until next irq... ; advance each dac CLRF bitsOut ;StepDac led7,bitsOut ;StepDac led6,bitsOut ;StepDac led5,bitsOut ;StepDac led4,bitsOut StepDac led3,bitsOut StepDac led2,bitsOut StepDac led1,bitsOut StepDac led0,bitsOut MOVFW bitsOut XORLW 0xff ; oh, we sink. MOVWF PORTB ; done with our timer-task. ; we now generously allow several instructions ; of the host code to execute. Not many... because ; holding the LED brightness is quite important! SWAPF irqSaveS,W ; unswaps MOVWF STATUS SWAPF irqSaveW,F SWAPF irqSaveW,W BCF INTCON,T0IF RETFIE Start GoBank0 BCF INTCON,GIE ; ; Set ports b for output ; CLRF PORTA ; zeroes on the outputs CLRF PORTB ; zeroes on the outputs GoBank1 MOVLW 0x0 ; all bits output MOVWF TRISA ; to tristate register. MOVWF TRISB ; to tristate register. GoBank0 ; Blink On Reset... FlashB MACRO v MOVLW v MOVWF PORTB MOVLW 0xf0 Call Delay ENDM Boo: FlashB 0 FlashB 0xff FlashB 0 FlashB 0xff FlashB 0 FlashB 0xff FlashB 0 FlashB 0xaa FlashB 0x55 FlashB 0x66 ;GOTO Boo ; ok. enable timer and stuff. GoBank1 BCF OPTION_REG,T0CS ; timer in clock-count mode GoBank0 BSF INTCON,T0IE BSF INTCON,GIE ; initialize our brightnesses and our bumps CLRF brightnessHigh0 CLRF brightnessHigh1 CLRF brightnessHigh2 CLRF brightnessHigh3 MOVLW 0x03 MOVWF bump0 MOVLW 0x04 MOVWF bump1 MOVLW 0x05 MOVWF bump2 MOVLW 0x06 MOVWF bump3 ; and do the main inline loop. FaderLoop CLRF setBLED ; led 0 MOVFW bump0 ADDWF brightnessLow0,f BTFSC STATUS,C INCF brightnessHigh0,f MOVFW brightnessHigh0 MOVWF setBB CALL SetBrightness INCF setBLED MOVFW bump1 ADDWF brightnessLow1,f BTFSC STATUS,C INCF brightnessHigh1,f MOVFW brightnessHigh1 MOVWF setBB CALL SetBrightness INCF setBLED MOVFW bump2 ADDWF brightnessLow2,f BTFSC STATUS,C INCF brightnessHigh2,f MOVFW brightnessHigh2 MOVWF setBB CALL SetBrightness INCF setBLED MOVFW bump3 ADDWF brightnessLow3,f BTFSC STATUS,C INCF brightnessHigh3,f MOVFW brightnessHigh3 MOVWF setBB CALL SetBrightness GOTO FaderLoop ;------------------------------ ; delay by W times inbuilt constant Delay MOVWF fDelay delay_a MOVLW 0xf0 MOVWF fTemp delay_b DECFSZ fTemp,f GOTO delay_b DECFSZ fDelay,f GOTO delay_a RETURN ORG 0x340 ; +---------------------------- ; | SetBrightness ; | LED index (0-7) in setBLED ; | Brightness (0-255) in setBB ; TODO: use RETLW for table lookup ; and use fewer bytes or get better ; resolution. SetBrightness: MOVFW setBLED ADDWF setBLED,W ADDLW led0 + 1 MOVWF FSR ; point where the result goes ;MOVFW setBB ;MOVWF INDF ;RETURN ; map 0..255 to 0..63 ; with a computed goto, ; among 64 pairs of instructions. MOVLW HIGH $ MOVWF PCLATH RRF setBB,W ANDLW 0x7e ADDWF PCL,F ; (exp-curve table for 64 steps --> 0..255) MOVLW d'0' ; <-- 0 of 64, 0 --> 0 GOTO gotExp MOVLW d'4' ; <-- 1 of 64, 4 --> 4 GOTO gotExp MOVLW d'4' ; <-- 2 of 64, 8 --> 4 GOTO gotExp MOVLW d'4' ; <-- 3 of 64, 12 --> 4 GOTO gotExp MOVLW d'4' ; <-- 4 of 64, 16 --> 4 GOTO gotExp MOVLW d'5' ; <-- 5 of 64, 20 --> 5 GOTO gotExp MOVLW d'6' ; <-- 6 of 64, 24 --> 6 GOTO gotExp MOVLW d'7' ; <-- 7 of 64, 28 --> 7 GOTO gotExp MOVLW d'8' ; <-- 8 of 64, 32 --> 8 GOTO gotExp MOVLW d'9' ; <-- 9 of 64, 36 --> 9 GOTO gotExp MOVLW d'11' ; <-- 10 of 64, 40 --> 11 GOTO gotExp MOVLW d'13' ; <-- 11 of 64, 44 --> 13 GOTO gotExp MOVLW d'15' ; <-- 12 of 64, 48 --> 15 GOTO gotExp MOVLW d'17' ; <-- 13 of 64, 52 --> 17 GOTO gotExp MOVLW d'19' ; <-- 14 of 64, 56 --> 19 GOTO gotExp MOVLW d'22' ; <-- 15 of 64, 60 --> 22 GOTO gotExp MOVLW d'24' ; <-- 16 of 64, 64 --> 24 GOTO gotExp MOVLW d'27' ; <-- 17 of 64, 68 --> 27 GOTO gotExp MOVLW d'30' ; <-- 18 of 64, 72 --> 30 GOTO gotExp MOVLW d'33' ; <-- 19 of 64, 76 --> 33 GOTO gotExp MOVLW d'36' ; <-- 20 of 64, 80 --> 36 GOTO gotExp MOVLW d'39' ; <-- 21 of 64, 85 --> 39 GOTO gotExp MOVLW d'42' ; <-- 22 of 64, 89 --> 42 GOTO gotExp MOVLW d'45' ; <-- 23 of 64, 93 --> 45 GOTO gotExp MOVLW d'49' ; <-- 24 of 64, 97 --> 49 GOTO gotExp MOVLW d'52' ; <-- 25 of 64, 101 --> 52 GOTO gotExp MOVLW d'56' ; <-- 26 of 64, 105 --> 56 GOTO gotExp MOVLW d'60' ; <-- 27 of 64, 109 --> 60 GOTO gotExp MOVLW d'64' ; <-- 28 of 64, 113 --> 64 GOTO gotExp MOVLW d'68' ; <-- 29 of 64, 117 --> 68 GOTO gotExp MOVLW d'72' ; <-- 30 of 64, 121 --> 72 GOTO gotExp MOVLW d'76' ; <-- 31 of 64, 125 --> 76 GOTO gotExp MOVLW d'80' ; <-- 32 of 64, 129 --> 80 GOTO gotExp MOVLW d'84' ; <-- 33 of 64, 133 --> 84 GOTO gotExp MOVLW d'89' ; <-- 34 of 64, 137 --> 89 GOTO gotExp MOVLW d'93' ; <-- 35 of 64, 141 --> 93 GOTO gotExp MOVLW d'98' ; <-- 36 of 64, 145 --> 98 GOTO gotExp MOVLW d'103' ; <-- 37 of 64, 149 --> 103 GOTO gotExp MOVLW d'107' ; <-- 38 of 64, 153 --> 107 GOTO gotExp MOVLW d'112' ; <-- 39 of 64, 157 --> 112 GOTO gotExp MOVLW d'117' ; <-- 40 of 64, 161 --> 117 GOTO gotExp MOVLW d'122' ; <-- 41 of 64, 165 --> 122 GOTO gotExp MOVLW d'127' ; <-- 42 of 64, 170 --> 127 GOTO gotExp MOVLW d'133' ; <-- 43 of 64, 174 --> 133 GOTO gotExp MOVLW d'138' ; <-- 44 of 64, 178 --> 138 GOTO gotExp MOVLW d'143' ; <-- 45 of 64, 182 --> 143 GOTO gotExp MOVLW d'149' ; <-- 46 of 64, 186 --> 149 GOTO gotExp MOVLW d'154' ; <-- 47 of 64, 190 --> 154 GOTO gotExp MOVLW d'160' ; <-- 48 of 64, 194 --> 160 GOTO gotExp MOVLW d'166' ; <-- 49 of 64, 198 --> 166 GOTO gotExp MOVLW d'172' ; <-- 50 of 64, 202 --> 172 GOTO gotExp MOVLW d'178' ; <-- 51 of 64, 206 --> 178 GOTO gotExp MOVLW d'184' ; <-- 52 of 64, 210 --> 184 GOTO gotExp MOVLW d'190' ; <-- 53 of 64, 214 --> 190 GOTO gotExp MOVLW d'196' ; <-- 54 of 64, 218 --> 196 GOTO gotExp MOVLW d'202' ; <-- 55 of 64, 222 --> 202 GOTO gotExp MOVLW d'208' ; <-- 56 of 64, 226 --> 208 GOTO gotExp MOVLW d'215' ; <-- 57 of 64, 230 --> 215 GOTO gotExp MOVLW d'221' ; <-- 58 of 64, 234 --> 221 GOTO gotExp MOVLW d'228' ; <-- 59 of 64, 238 --> 228 GOTO gotExp MOVLW d'234' ; <-- 60 of 64, 242 --> 234 GOTO gotExp MOVLW d'241' ; <-- 61 of 64, 246 --> 241 GOTO gotExp MOVLW d'248' ; <-- 62 of 64, 250 --> 248 GOTO gotExp MOVLW d'255' ; <-- 63 of 64, 255 --> 255 GOTO gotExp ; got the log value gotExp: MOVWF INDF ; and store indirect RETURN END