IFD2343Code ; ;Controller Maps Electric Drive Battery Voltage to Thermal Fuel Gage ; William Grill ; 16F506 PC equ 2 ; STATUS equ 3 ; #define CARRY STATUS,0 ; #define ZERO STATUS,2 ; SFR equ 4 Calib equ 5 GPIOB equ 6 GPIOC equ 7 ADCTL equ 9 ; #define ADready ADCTL,1 ; ADDATA equ 0xA ; #define meterdrive GPIOC,0 #define sensorenable GPIOC,1 #define contact GPIOC,4 #define syncin GPIOC,5 ; #define emptylite GPIOB,4 ;#define alarm optional GPIOB,5 ; #define fulloffset .25 ; 26/256*5 ; setEmpty equ 0x10 ; empty setting setFail equ 0x11 ; fail setting ;display equ 0x12 delayK equ 0x13 cnt equ 0x15 D equ 0x16 M1 equ 0x17 M2 equ 0x18 RL equ 0x19 Rh equ 0x1A Battin equ 0x1C ; battery in temp equ 0x1D M1H equ 0x1E ; start here init ; ************************************** ; movwf Calib movlw 0xC0 ; 1100 0000 OPTION ; movlw 0xF7 ; disable cmp0,1 movwf 8 movwf 0xB ; clrf SFR movlw 0x20 movwf 0xC clrf GPIOB clrf GPIOC movlw 0xEF TRIS GPIOB ; 1110 1111 movlw 0xE4 ; 1110 0100 TRIS GPIOC movlw .145 movwf delayK movlw .100 movwf Battin movwf cnt movwf D movlw 0xE1 ; 11 10 00 01 *** set up fail value movwf ADCTL bsf ADCTL,1 init3: btfsc ADready goto init3 movfw ADDATA movwf setFail movlw 0xE5 ; 11 10 01 01 set up empty value movwf ADCTL bsf ADCTL,1 init4: btfsc ADready goto init4 movfw ADDATA movwf setEmpty bsf contact init1: btfsc syncin goto init1 init2: btfss syncin ; loop till rising edge goto init2 ; *** EVALUATE LITE and PROCESS TO BE USED movfw Battin subwf setEmpty,w btfsc CARRY goto continue movfw Battin subwf setFail,w btfsc CARRY goto init6 bcf emptylite bsf alarm ; possible service before disconnect ; shut down bcf contact ; also avaiable as an emptylite die: goto die ; **************************** init6: bsf emptylite ; assert emptylite movlw 1 bsf meterdrive ; assert output within 5uS **** 0 *** goto init7 continue: ; battery is in scope bsf meterdrive ; assert output within 5uS **** 0 *** nop bcf emptylite ; turn off emptylitenop nop nop nop init7: ; *** delay using delayK movfw delayK movwf temp init7a: nop ; origianl copy of D nop decfsz temp goto init7a ; decfsz cnt goto init7 ; bcf meterdrive nop ; wait for transistor to switch and syncin to be readable nop nop nop nop nop nop nop nop nop nop btfsc syncin goto init7b decf D btfsc ZERO incf D goto init7e init7b: movfw delayK movwf temp init7bb: nop nop decfsz temp goto init7bb incf D btfsc syncin goto init7b decf D init7e: movfw D movwf M1 ; ***************** get fuel data bsf sensorenable nop ; give coupler chnace to respond nop nop nop nop nop nop nop movlw 0xE9 ; 11 10 10 01 set up Battery input movwf ADCTL nop nop nop bsf ADCTL,1 init5: btfsc ADready goto init5 movfw ADDATA movwf Battin ; movlw fulloffset subwf Battin,w btfsc CARRY goto init5a movlw fulloffset ; limit Battin movwf Battin init5a: bcf sensorenable ; scalerprocess: ; multiple M1 * M2 movfw Battin subwf setEmpty,w btfsc CARRY goto scale1 movlw 1 scale1: movwf M2 movlw 8 movwf cnt clrf M1H clrf RL clrf Rh mult1: bcf CARRY rrf M2 btfss CARRY goto mult2 ; movfw M1H addwf Rh movfw M1 addwf RL btfsc CARRY incf Rh bcf CARRY mult2: rlf M1 rlf M1H decfsz cnt goto mult1 ; done : result in RhRL movf RL btfss ZERO goto Dividethis movf Rh btfsc ZERO incf RL ; ********************* Dividethis: ; RhRL/divisor clrf cnt movlw fulloffset ; fixed setting for full A/D value subwf setEmpty,w Divide1: incf cnt subwf RL btfsc CARRY goto Divide1 movf Rh btfsc ZERO goto Divide1a decf Rh goto Divide1 ; done cnt has number of scaled delayloops to display Divide1a: movfw cnt movwf D goto init2 ; retlw 0 END