annotate src/adc_lightsensor.asm @ 114:109df032cc54

minor fix: assert should pass for pure helium diluant
author heinrichsweikamp
date Mon, 23 Jun 2014 17:44:18 +0200
parents f3062a611eef
children 6c577737c34e
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
heinrichsweikamp
parents:
diff changeset
1 ;=============================================================================
heinrichsweikamp
parents:
diff changeset
2 ;
heinrichsweikamp
parents:
diff changeset
3 ; File adc.asm
heinrichsweikamp
parents:
diff changeset
4 ;
heinrichsweikamp
parents:
diff changeset
5 ;
heinrichsweikamp
parents:
diff changeset
6 ; Copyright (c) 2011, JD Gascuel, HeinrichsWeikamp, all right reserved.
heinrichsweikamp
parents:
diff changeset
7 ;=============================================================================
heinrichsweikamp
parents:
diff changeset
8 ; HISTORY
heinrichsweikamp
parents:
diff changeset
9 ; 2011-08-08 : [mH] moving from OSTC code
heinrichsweikamp
parents:
diff changeset
10
heinrichsweikamp
parents:
diff changeset
11 #include "ostc3.inc"
heinrichsweikamp
parents:
diff changeset
12 #include "math.inc"
heinrichsweikamp
parents:
diff changeset
13 #include "wait.inc"
heinrichsweikamp
parents:
diff changeset
14 #include "eeprom_rs232.inc"
113
heinrichsweikamp
parents: 44
diff changeset
15 #include "i2c.inc"
0
heinrichsweikamp
parents:
diff changeset
16
heinrichsweikamp
parents:
diff changeset
17 sensors CODE
heinrichsweikamp
parents:
diff changeset
18
heinrichsweikamp
parents:
diff changeset
19 wait_adc:
heinrichsweikamp
parents:
diff changeset
20 movwf ADCON0
heinrichsweikamp
parents:
diff changeset
21 nop
heinrichsweikamp
parents:
diff changeset
22 bsf ADCON0,1 ; start ADC
heinrichsweikamp
parents:
diff changeset
23 wait_adc2:
heinrichsweikamp
parents:
diff changeset
24 btfsc ADCON0,1 ; Wait...
heinrichsweikamp
parents:
diff changeset
25 bra wait_adc2
heinrichsweikamp
parents:
diff changeset
26 return
heinrichsweikamp
parents:
diff changeset
27
heinrichsweikamp
parents:
diff changeset
28 global get_battery_voltage
heinrichsweikamp
parents:
diff changeset
29 get_battery_voltage: ; starts ADC and waits until fnished
113
heinrichsweikamp
parents: 44
diff changeset
30 btfss c3_hardware
heinrichsweikamp
parents: 44
diff changeset
31 bra get_battery_voltage1 ; Normal ostc3 hardware
heinrichsweikamp
parents: 44
diff changeset
32
heinrichsweikamp
parents: 44
diff changeset
33 call lt2942_get_accumulated_charge
heinrichsweikamp
parents: 44
diff changeset
34 call lt2942_get_voltage
heinrichsweikamp
parents: 44
diff changeset
35 return
heinrichsweikamp
parents: 44
diff changeset
36
heinrichsweikamp
parents: 44
diff changeset
37 get_battery_voltage1:
0
heinrichsweikamp
parents:
diff changeset
38 bsf adc_running ; =1: The ADC is in use
heinrichsweikamp
parents:
diff changeset
39 movlw b'00100000' ; 2.048V Vref+ -> 1LSB = 500µV
heinrichsweikamp
parents:
diff changeset
40 movwf ADCON1
heinrichsweikamp
parents:
diff changeset
41 movlw b'00011001' ; power on ADC, select AN6
heinrichsweikamp
parents:
diff changeset
42 rcall wait_adc
heinrichsweikamp
parents:
diff changeset
43
heinrichsweikamp
parents:
diff changeset
44 movff ADRESH,batt_voltage+1 ; store value
heinrichsweikamp
parents:
diff changeset
45 movff ADRESL,batt_voltage+0 ; store value
heinrichsweikamp
parents:
diff changeset
46 bcf ADCON0,0 ; power off ADC
heinrichsweikamp
parents:
diff changeset
47
heinrichsweikamp
parents:
diff changeset
48 ; Multiply with 2,006 to be excact here...
heinrichsweikamp
parents:
diff changeset
49 ; bcf STATUS,C
heinrichsweikamp
parents:
diff changeset
50 ; rlcf xA+0,F
heinrichsweikamp
parents:
diff changeset
51 ;
heinrichsweikamp
parents:
diff changeset
52 ; rlcf xA+1,F ; x2
heinrichsweikamp
parents:
diff changeset
53
heinrichsweikamp
parents:
diff changeset
54 ; movff xA+0,batt_voltage+0 ; store value
heinrichsweikamp
parents:
diff changeset
55 ; movff xA+1,batt_voltage+1
heinrichsweikamp
parents:
diff changeset
56
heinrichsweikamp
parents:
diff changeset
57 movlw LOW lithium_36v_low
heinrichsweikamp
parents:
diff changeset
58 movwf sub_a+0
heinrichsweikamp
parents:
diff changeset
59 movlw HIGH lithium_36v_low
heinrichsweikamp
parents:
diff changeset
60 movwf sub_a+1
heinrichsweikamp
parents:
diff changeset
61 movff batt_voltage+0,sub_b+0
heinrichsweikamp
parents:
diff changeset
62 movff batt_voltage+1,sub_b+1
heinrichsweikamp
parents:
diff changeset
63 call subU16 ; sub_c = sub_a - sub_b
heinrichsweikamp
parents:
diff changeset
64 ; Battery is 3,6V (>lithium_36v_low?)
heinrichsweikamp
parents:
diff changeset
65 btfss neg_flag
heinrichsweikamp
parents:
diff changeset
66 bra get_battery_voltage4 ; No, use 1,5V
heinrichsweikamp
parents:
diff changeset
67
heinrichsweikamp
parents:
diff changeset
68 bsf battery_is_36v ; Yes, set flag (Cleared in power-on reset only!)
heinrichsweikamp
parents:
diff changeset
69
heinrichsweikamp
parents:
diff changeset
70 ; Check if the battery is near-dead already
heinrichsweikamp
parents:
diff changeset
71 movlw LOW lithium_36v_empty
heinrichsweikamp
parents:
diff changeset
72 movwf sub_a+0
heinrichsweikamp
parents:
diff changeset
73 movlw HIGH lithium_36v_empty
heinrichsweikamp
parents:
diff changeset
74 movwf sub_a+1
heinrichsweikamp
parents:
diff changeset
75 call subU16 ; sub_c = sub_a - sub_b
heinrichsweikamp
parents:
diff changeset
76 ; Battery is not dead yet (>lithium_36v_empty?)
heinrichsweikamp
parents:
diff changeset
77 btfsc neg_flag
heinrichsweikamp
parents:
diff changeset
78 bra get_battery_voltage2 ; Yes, battery is still ok
heinrichsweikamp
parents:
diff changeset
79
heinrichsweikamp
parents:
diff changeset
80 ; Battery is probably dead very soon
heinrichsweikamp
parents:
diff changeset
81 ; Set ">=24Ah used" into battery gauge registers
heinrichsweikamp
parents:
diff changeset
82 movlw .128
heinrichsweikamp
parents:
diff changeset
83 movff WREG,battery_gauge+5
heinrichsweikamp
parents:
diff changeset
84
heinrichsweikamp
parents:
diff changeset
85 get_battery_voltage2:
heinrichsweikamp
parents:
diff changeset
86 ; Use 3,6V battery gauging mode
heinrichsweikamp
parents:
diff changeset
87 movff battery_gauge+5,xC+3
heinrichsweikamp
parents:
diff changeset
88 movff battery_gauge+4,xC+2
heinrichsweikamp
parents:
diff changeset
89 movff battery_gauge+3,xC+1
heinrichsweikamp
parents:
diff changeset
90 movff battery_gauge+2,xC+0
heinrichsweikamp
parents:
diff changeset
91 ; battery_gauge:6 is nAs
heinrichsweikamp
parents:
diff changeset
92 ; devide through 65536
heinrichsweikamp
parents:
diff changeset
93 ; devide through 364
heinrichsweikamp
parents:
diff changeset
94 ; Result is in percent of a 2,4Ah Battery
heinrichsweikamp
parents:
diff changeset
95 movlw LOW .364
heinrichsweikamp
parents:
diff changeset
96 movwf xB+0
heinrichsweikamp
parents:
diff changeset
97 movlw HIGH .364
heinrichsweikamp
parents:
diff changeset
98 movwf xB+1
heinrichsweikamp
parents:
diff changeset
99 call div32x16 ; xC:4 / xB:2 = xC+3:xC+2 with xC+1:xC+0 as remainder
heinrichsweikamp
parents:
diff changeset
100 movff xC+0,lo
heinrichsweikamp
parents:
diff changeset
101 ; Limit to 100
heinrichsweikamp
parents:
diff changeset
102 movlw .100
heinrichsweikamp
parents:
diff changeset
103 cpfslt lo
heinrichsweikamp
parents:
diff changeset
104 movwf lo
heinrichsweikamp
parents:
diff changeset
105 ; lo will be between 0 (Full) and 100 (empty)
heinrichsweikamp
parents:
diff changeset
106 movf lo,W
heinrichsweikamp
parents:
diff changeset
107 sublw .100
heinrichsweikamp
parents:
diff changeset
108 movwf lo
heinrichsweikamp
parents:
diff changeset
109 get_battery_voltage3:
heinrichsweikamp
parents:
diff changeset
110 movlw .100
heinrichsweikamp
parents:
diff changeset
111 cpfslt lo
heinrichsweikamp
parents:
diff changeset
112 movwf lo
heinrichsweikamp
parents:
diff changeset
113 ; lo will be between 100 (Full) and 0 (empty)
37
0e1723f2761e use four additional data points for the battery monitor
mh@mh-THINK.fritz.box
parents: 0
diff changeset
114
0e1723f2761e use four additional data points for the battery monitor
mh@mh-THINK.fritz.box
parents: 0
diff changeset
115 ; use 3,6V battery sensing based on 50mA load
0e1723f2761e use four additional data points for the battery monitor
mh@mh-THINK.fritz.box
parents: 0
diff changeset
116 ; 75%
0e1723f2761e use four additional data points for the battery monitor
mh@mh-THINK.fritz.box
parents: 0
diff changeset
117 movff batt_voltage+0,sub_b+0
0e1723f2761e use four additional data points for the battery monitor
mh@mh-THINK.fritz.box
parents: 0
diff changeset
118 movff batt_voltage+1,sub_b+1
0e1723f2761e use four additional data points for the battery monitor
mh@mh-THINK.fritz.box
parents: 0
diff changeset
119 movlw LOW lithium_36v_75
0e1723f2761e use four additional data points for the battery monitor
mh@mh-THINK.fritz.box
parents: 0
diff changeset
120 movwf sub_a+0
0e1723f2761e use four additional data points for the battery monitor
mh@mh-THINK.fritz.box
parents: 0
diff changeset
121 movlw HIGH lithium_36v_75
0e1723f2761e use four additional data points for the battery monitor
mh@mh-THINK.fritz.box
parents: 0
diff changeset
122 movwf sub_a+1
0e1723f2761e use four additional data points for the battery monitor
mh@mh-THINK.fritz.box
parents: 0
diff changeset
123 call subU16 ; sub_c = sub_a - sub_b
0e1723f2761e use four additional data points for the battery monitor
mh@mh-THINK.fritz.box
parents: 0
diff changeset
124 btfsc neg_flag
0e1723f2761e use four additional data points for the battery monitor
mh@mh-THINK.fritz.box
parents: 0
diff changeset
125 bra get_battery_voltage3a
0e1723f2761e use four additional data points for the battery monitor
mh@mh-THINK.fritz.box
parents: 0
diff changeset
126 movlw .75
0e1723f2761e use four additional data points for the battery monitor
mh@mh-THINK.fritz.box
parents: 0
diff changeset
127 movwf lo
0e1723f2761e use four additional data points for the battery monitor
mh@mh-THINK.fritz.box
parents: 0
diff changeset
128 get_battery_voltage3a:
0e1723f2761e use four additional data points for the battery monitor
mh@mh-THINK.fritz.box
parents: 0
diff changeset
129 ; 50%
0e1723f2761e use four additional data points for the battery monitor
mh@mh-THINK.fritz.box
parents: 0
diff changeset
130 movlw LOW lithium_36v_50
0e1723f2761e use four additional data points for the battery monitor
mh@mh-THINK.fritz.box
parents: 0
diff changeset
131 movwf sub_a+0
0e1723f2761e use four additional data points for the battery monitor
mh@mh-THINK.fritz.box
parents: 0
diff changeset
132 movlw HIGH lithium_36v_50
0e1723f2761e use four additional data points for the battery monitor
mh@mh-THINK.fritz.box
parents: 0
diff changeset
133 movwf sub_a+1
0e1723f2761e use four additional data points for the battery monitor
mh@mh-THINK.fritz.box
parents: 0
diff changeset
134 call subU16 ; sub_c = sub_a - sub_b
0e1723f2761e use four additional data points for the battery monitor
mh@mh-THINK.fritz.box
parents: 0
diff changeset
135 btfsc neg_flag
0e1723f2761e use four additional data points for the battery monitor
mh@mh-THINK.fritz.box
parents: 0
diff changeset
136 bra get_battery_voltage3b
0e1723f2761e use four additional data points for the battery monitor
mh@mh-THINK.fritz.box
parents: 0
diff changeset
137 movlw .50
0e1723f2761e use four additional data points for the battery monitor
mh@mh-THINK.fritz.box
parents: 0
diff changeset
138 movwf lo
0e1723f2761e use four additional data points for the battery monitor
mh@mh-THINK.fritz.box
parents: 0
diff changeset
139 get_battery_voltage3b:
0e1723f2761e use four additional data points for the battery monitor
mh@mh-THINK.fritz.box
parents: 0
diff changeset
140 ; 25%
0e1723f2761e use four additional data points for the battery monitor
mh@mh-THINK.fritz.box
parents: 0
diff changeset
141 movlw LOW lithium_36v_25
0e1723f2761e use four additional data points for the battery monitor
mh@mh-THINK.fritz.box
parents: 0
diff changeset
142 movwf sub_a+0
0e1723f2761e use four additional data points for the battery monitor
mh@mh-THINK.fritz.box
parents: 0
diff changeset
143 movlw HIGH lithium_36v_25
0e1723f2761e use four additional data points for the battery monitor
mh@mh-THINK.fritz.box
parents: 0
diff changeset
144 movwf sub_a+1
0e1723f2761e use four additional data points for the battery monitor
mh@mh-THINK.fritz.box
parents: 0
diff changeset
145 call subU16 ; sub_c = sub_a - sub_b
0e1723f2761e use four additional data points for the battery monitor
mh@mh-THINK.fritz.box
parents: 0
diff changeset
146 btfsc neg_flag
0e1723f2761e use four additional data points for the battery monitor
mh@mh-THINK.fritz.box
parents: 0
diff changeset
147 bra get_battery_voltage3c
0e1723f2761e use four additional data points for the battery monitor
mh@mh-THINK.fritz.box
parents: 0
diff changeset
148 movlw .25
0e1723f2761e use four additional data points for the battery monitor
mh@mh-THINK.fritz.box
parents: 0
diff changeset
149 movwf lo
0e1723f2761e use four additional data points for the battery monitor
mh@mh-THINK.fritz.box
parents: 0
diff changeset
150 get_battery_voltage3c:
0e1723f2761e use four additional data points for the battery monitor
mh@mh-THINK.fritz.box
parents: 0
diff changeset
151 ; 10%
0e1723f2761e use four additional data points for the battery monitor
mh@mh-THINK.fritz.box
parents: 0
diff changeset
152 movlw LOW lithium_36v_10
0e1723f2761e use four additional data points for the battery monitor
mh@mh-THINK.fritz.box
parents: 0
diff changeset
153 movwf sub_a+0
0e1723f2761e use four additional data points for the battery monitor
mh@mh-THINK.fritz.box
parents: 0
diff changeset
154 movlw HIGH lithium_36v_10
0e1723f2761e use four additional data points for the battery monitor
mh@mh-THINK.fritz.box
parents: 0
diff changeset
155 movwf sub_a+1
0e1723f2761e use four additional data points for the battery monitor
mh@mh-THINK.fritz.box
parents: 0
diff changeset
156 call subU16 ; sub_c = sub_a - sub_b
0e1723f2761e use four additional data points for the battery monitor
mh@mh-THINK.fritz.box
parents: 0
diff changeset
157 btfsc neg_flag
0e1723f2761e use four additional data points for the battery monitor
mh@mh-THINK.fritz.box
parents: 0
diff changeset
158 bra get_battery_voltage3d
0e1723f2761e use four additional data points for the battery monitor
mh@mh-THINK.fritz.box
parents: 0
diff changeset
159 movlw .10
0e1723f2761e use four additional data points for the battery monitor
mh@mh-THINK.fritz.box
parents: 0
diff changeset
160 movwf lo
0e1723f2761e use four additional data points for the battery monitor
mh@mh-THINK.fritz.box
parents: 0
diff changeset
161 get_battery_voltage3d:
44
18fe4e668baa BUGFIX: Percent display with 1,5V batteries
heinrichsweikamp
parents: 37
diff changeset
162 movlw .100
18fe4e668baa BUGFIX: Percent display with 1,5V batteries
heinrichsweikamp
parents: 37
diff changeset
163 cpfslt lo
18fe4e668baa BUGFIX: Percent display with 1,5V batteries
heinrichsweikamp
parents: 37
diff changeset
164 movwf lo
18fe4e668baa BUGFIX: Percent display with 1,5V batteries
heinrichsweikamp
parents: 37
diff changeset
165 ; lo will be between 100 (Full) and 0 (empty)
0
heinrichsweikamp
parents:
diff changeset
166 movf batt_percent,W
heinrichsweikamp
parents:
diff changeset
167 cpfsgt lo ; keep batt_percent on the lowest value found
heinrichsweikamp
parents:
diff changeset
168 movff lo,batt_percent ; store value
37
0e1723f2761e use four additional data points for the battery monitor
mh@mh-THINK.fritz.box
parents: 0
diff changeset
169 ; btfsc battery_is_36v ; but always use computed value for 3,6V battery
0e1723f2761e use four additional data points for the battery monitor
mh@mh-THINK.fritz.box
parents: 0
diff changeset
170 ; movff lo,batt_percent ; store value
0
heinrichsweikamp
parents:
diff changeset
171 bcf adc_running ; =1: The ADC is in use
heinrichsweikamp
parents:
diff changeset
172 return
heinrichsweikamp
parents:
diff changeset
173
heinrichsweikamp
parents:
diff changeset
174 get_battery_voltage4:
heinrichsweikamp
parents:
diff changeset
175 ; Use 1,5V battery voltage mode
heinrichsweikamp
parents:
diff changeset
176 ; Use approximation (batt_voltage:2-aa_15v_low)/4 = lo
heinrichsweikamp
parents:
diff changeset
177 movff batt_voltage+0,sub_a+0
heinrichsweikamp
parents:
diff changeset
178 movff batt_voltage+1,sub_a+1
heinrichsweikamp
parents:
diff changeset
179 movlw LOW aa_15v_low
heinrichsweikamp
parents:
diff changeset
180 movwf sub_b+0
heinrichsweikamp
parents:
diff changeset
181 movlw HIGH aa_15v_low
heinrichsweikamp
parents:
diff changeset
182 movwf sub_b+1
heinrichsweikamp
parents:
diff changeset
183 call subU16 ; sub_c = sub_a - sub_b
heinrichsweikamp
parents:
diff changeset
184 bcf STATUS,C
heinrichsweikamp
parents:
diff changeset
185 rrcf sub_c+1
heinrichsweikamp
parents:
diff changeset
186 rrcf sub_c+0 ; /2
heinrichsweikamp
parents:
diff changeset
187 bcf STATUS,C
heinrichsweikamp
parents:
diff changeset
188 rrcf sub_c+1
heinrichsweikamp
parents:
diff changeset
189 rrcf sub_c+0 ; /4
heinrichsweikamp
parents:
diff changeset
190 movff sub_c+0,lo
44
18fe4e668baa BUGFIX: Percent display with 1,5V batteries
heinrichsweikamp
parents: 37
diff changeset
191 bra get_battery_voltage3d ; Check limits and return
0
heinrichsweikamp
parents:
diff changeset
192
heinrichsweikamp
parents:
diff changeset
193 global get_ambient_level
heinrichsweikamp
parents:
diff changeset
194 get_ambient_level: ; starts ADC and waits until finished
heinrichsweikamp
parents:
diff changeset
195 btfsc adc_running ; ADC in use?
heinrichsweikamp
parents:
diff changeset
196 return ; Yes, return
heinrichsweikamp
parents:
diff changeset
197
113
heinrichsweikamp
parents: 44
diff changeset
198 btfss c3_hardware
heinrichsweikamp
parents: 44
diff changeset
199 bra get_ambient_level1 ; Normal ostc3 hardware
heinrichsweikamp
parents: 44
diff changeset
200 movlw .250
heinrichsweikamp
parents: 44
diff changeset
201 movwf ambient_light+0
heinrichsweikamp
parents: 44
diff changeset
202 clrf ambient_light+1 ; Set to max
heinrichsweikamp
parents: 44
diff changeset
203 bra get_ambient_level2 ; Continue as normal
heinrichsweikamp
parents: 44
diff changeset
204
heinrichsweikamp
parents: 44
diff changeset
205 get_ambient_level1:
0
heinrichsweikamp
parents:
diff changeset
206 movlw b'00000000' ; Vref+ = Vdd
heinrichsweikamp
parents:
diff changeset
207 movwf ADCON1
heinrichsweikamp
parents:
diff changeset
208 movlw b'00011101' ; power on ADC, select AN7
heinrichsweikamp
parents:
diff changeset
209 rcall wait_adc
heinrichsweikamp
parents:
diff changeset
210
heinrichsweikamp
parents:
diff changeset
211 movff ADRESH,ambient_light+1
heinrichsweikamp
parents:
diff changeset
212 movff ADRESL,ambient_light+0
heinrichsweikamp
parents:
diff changeset
213 bcf ADCON0,0 ; power off ADC
heinrichsweikamp
parents:
diff changeset
214
heinrichsweikamp
parents:
diff changeset
215 ; ambient_light:2 is between 4096 (direct sunlight) and about 200 (darkness)
heinrichsweikamp
parents:
diff changeset
216 ; First: Devide through 16
heinrichsweikamp
parents:
diff changeset
217 bcf STATUS,C
heinrichsweikamp
parents:
diff changeset
218 rrcf ambient_light+1
heinrichsweikamp
parents:
diff changeset
219 rrcf ambient_light+0
heinrichsweikamp
parents:
diff changeset
220 bcf STATUS,C
heinrichsweikamp
parents:
diff changeset
221 rrcf ambient_light+1
heinrichsweikamp
parents:
diff changeset
222 rrcf ambient_light+0
heinrichsweikamp
parents:
diff changeset
223 bcf STATUS,C
heinrichsweikamp
parents:
diff changeset
224 rrcf ambient_light+1
heinrichsweikamp
parents:
diff changeset
225 rrcf ambient_light+0
heinrichsweikamp
parents:
diff changeset
226 bcf STATUS,C
heinrichsweikamp
parents:
diff changeset
227 rrcf ambient_light+1
heinrichsweikamp
parents:
diff changeset
228 rrcf ambient_light+0
heinrichsweikamp
parents:
diff changeset
229 ; Result: ambient_light:2/16
heinrichsweikamp
parents:
diff changeset
230 ; Now, make sure to have value between ambient_light_low and ambient_light_max
heinrichsweikamp
parents:
diff changeset
231
heinrichsweikamp
parents:
diff changeset
232 movlw .254
heinrichsweikamp
parents:
diff changeset
233 tstfsz ambient_light+1 ; >255?
heinrichsweikamp
parents:
diff changeset
234 movwf ambient_light+0 ; avoid ADC clipping
heinrichsweikamp
parents:
diff changeset
235
heinrichsweikamp
parents:
diff changeset
236 incfsz ambient_light+0,W ; =255?
heinrichsweikamp
parents:
diff changeset
237 bra get_ambient_level2 ; No, continue
heinrichsweikamp
parents:
diff changeset
238
heinrichsweikamp
parents:
diff changeset
239 movlw .254
heinrichsweikamp
parents:
diff changeset
240 movwf ambient_light+0 ; avoid ADC clipping
heinrichsweikamp
parents:
diff changeset
241
heinrichsweikamp
parents:
diff changeset
242 get_ambient_level2:
heinrichsweikamp
parents:
diff changeset
243 banksel isr_backup ; Back to Bank0 ISR data
heinrichsweikamp
parents:
diff changeset
244 movff opt_brightness,isr1_temp
heinrichsweikamp
parents:
diff changeset
245
heinrichsweikamp
parents:
diff changeset
246 btfsc RCSTA1,7 ; UART module on?
heinrichsweikamp
parents:
diff changeset
247 clrf isr1_temp ; Yes, set temporally to eco mode
heinrichsweikamp
parents:
diff changeset
248
heinrichsweikamp
parents:
diff changeset
249 incf isr1_temp,F ; adjust 0-2 to 1-3
heinrichsweikamp
parents:
diff changeset
250
heinrichsweikamp
parents:
diff changeset
251 banksel common ; flag is in bank1
heinrichsweikamp
parents:
diff changeset
252 movlw ambient_light_max_high ; brightest setting
heinrichsweikamp
parents:
diff changeset
253 btfsc battery_is_36v ; 3,6V battery in use?
heinrichsweikamp
parents:
diff changeset
254 movlw ambient_light_max_high_36V ; Yes...
heinrichsweikamp
parents:
diff changeset
255 banksel isr_backup ; Back to Bank0 ISR data
heinrichsweikamp
parents:
diff changeset
256
heinrichsweikamp
parents:
diff changeset
257 dcfsnz isr1_temp,F
heinrichsweikamp
parents:
diff changeset
258 movlw ambient_light_max_eco ; brightest setting
heinrichsweikamp
parents:
diff changeset
259 dcfsnz isr1_temp,F
heinrichsweikamp
parents:
diff changeset
260 movlw ambient_light_max_medium; brightest setting
heinrichsweikamp
parents:
diff changeset
261
heinrichsweikamp
parents:
diff changeset
262 banksel common ; ambient_light is in Bank1
heinrichsweikamp
parents:
diff changeset
263 incf ambient_light+0,F ; +1
heinrichsweikamp
parents:
diff changeset
264 cpfslt ambient_light+0 ; smaller then WREG?
heinrichsweikamp
parents:
diff changeset
265 movwf ambient_light+0 ; No, set to max.
heinrichsweikamp
parents:
diff changeset
266
heinrichsweikamp
parents:
diff changeset
267 banksel isr_backup ; Back to Bank0 ISR data
heinrichsweikamp
parents:
diff changeset
268 movff opt_brightness,isr1_temp
heinrichsweikamp
parents:
diff changeset
269 incf isr1_temp,F ; adjust 0-2 to 1-3
heinrichsweikamp
parents:
diff changeset
270 movlw ambient_light_min_high ; darkest setting
heinrichsweikamp
parents:
diff changeset
271
heinrichsweikamp
parents:
diff changeset
272 dcfsnz isr1_temp,F
heinrichsweikamp
parents:
diff changeset
273 movlw ambient_light_min_eco ; darkest setting
heinrichsweikamp
parents:
diff changeset
274 dcfsnz isr1_temp,F
heinrichsweikamp
parents:
diff changeset
275 movlw ambient_light_min_medium; darkest setting
heinrichsweikamp
parents:
diff changeset
276 dcfsnz isr1_temp,F
heinrichsweikamp
parents:
diff changeset
277 movlw ambient_light_min_high ; darkest setting
heinrichsweikamp
parents:
diff changeset
278
heinrichsweikamp
parents:
diff changeset
279 banksel common ; ambient_light is in Bank1
heinrichsweikamp
parents:
diff changeset
280 cpfsgt ambient_light+0 ; bigger then WREG?
heinrichsweikamp
parents:
diff changeset
281 movwf ambient_light+0 ; No, set to min
heinrichsweikamp
parents:
diff changeset
282
heinrichsweikamp
parents:
diff changeset
283 movff ambient_light+0,max_CCPR1L ; Store value for dimming in TMR7 interrupt
heinrichsweikamp
parents:
diff changeset
284 return
heinrichsweikamp
parents:
diff changeset
285
113
heinrichsweikamp
parents: 44
diff changeset
286 global get_analog_inputs
heinrichsweikamp
parents: 44
diff changeset
287 get_analog_inputs: ; starts ADC and waits until finished
0
heinrichsweikamp
parents:
diff changeset
288 bsf adc_running ; =1: The ADC is in use
113
heinrichsweikamp
parents: 44
diff changeset
289 btfsc TFT_PWM
heinrichsweikamp
parents: 44
diff changeset
290 bra get_analog_inputs ; Wait for PWM low
heinrichsweikamp
parents: 44
diff changeset
291 movlw b'00100000' ; 2.048V Vref+ -> 1LSB = 500µV
heinrichsweikamp
parents: 44
diff changeset
292 movwf ADCON1
heinrichsweikamp
parents: 44
diff changeset
293 movlw b'00100001' ; power on ADC, select AN8
heinrichsweikamp
parents: 44
diff changeset
294 rcall wait_adc
heinrichsweikamp
parents: 44
diff changeset
295 bcf STATUS,C
heinrichsweikamp
parents: 44
diff changeset
296 rrcf ADRESH,F ; /2
heinrichsweikamp
parents: 44
diff changeset
297 rrcf ADRESL,F
heinrichsweikamp
parents: 44
diff changeset
298 movff ADRESL,o2_mv_sensor1+0 ; in 0.1mV steps
heinrichsweikamp
parents: 44
diff changeset
299 movff ADRESH,o2_mv_sensor1+1
heinrichsweikamp
parents: 44
diff changeset
300 bcf STATUS,C
heinrichsweikamp
parents: 44
diff changeset
301 rlcf ADRESL,F
heinrichsweikamp
parents: 44
diff changeset
302 rlcf ADRESH,F
0
heinrichsweikamp
parents:
diff changeset
303 movlw b'00100000' ; 2.048V Vref+
heinrichsweikamp
parents:
diff changeset
304 movwf ADCON1
113
heinrichsweikamp
parents: 44
diff changeset
305 movlw b'00100101' ; power on ADC, select AN9
0
heinrichsweikamp
parents:
diff changeset
306 rcall wait_adc
113
heinrichsweikamp
parents: 44
diff changeset
307 bcf STATUS,C
heinrichsweikamp
parents: 44
diff changeset
308 rrcf ADRESH,F ; /2
heinrichsweikamp
parents: 44
diff changeset
309 rrcf ADRESL,F
heinrichsweikamp
parents: 44
diff changeset
310 movff ADRESL,o2_mv_sensor2+0 ; in 0.1mV steps
heinrichsweikamp
parents: 44
diff changeset
311 movff ADRESH,o2_mv_sensor2+1
heinrichsweikamp
parents: 44
diff changeset
312 movlw b'00100000' ; 2.048V Vref+
heinrichsweikamp
parents: 44
diff changeset
313 movwf ADCON1
heinrichsweikamp
parents: 44
diff changeset
314 movlw b'00101001' ; power on ADC, select AN10
heinrichsweikamp
parents: 44
diff changeset
315 rcall wait_adc
heinrichsweikamp
parents: 44
diff changeset
316 bcf STATUS,C
heinrichsweikamp
parents: 44
diff changeset
317 rrcf ADRESH,F ; /2
heinrichsweikamp
parents: 44
diff changeset
318 rrcf ADRESL,F
heinrichsweikamp
parents: 44
diff changeset
319 movff ADRESL,o2_mv_sensor3+0 ; in 0.1mV steps
heinrichsweikamp
parents: 44
diff changeset
320 movff ADRESH,o2_mv_sensor3+1
0
heinrichsweikamp
parents:
diff changeset
321 bcf ADCON0,0 ; power off ADC
heinrichsweikamp
parents:
diff changeset
322 bcf adc_running ; =1: The ADC is in use
heinrichsweikamp
parents:
diff changeset
323 return
heinrichsweikamp
parents:
diff changeset
324
113
heinrichsweikamp
parents: 44
diff changeset
325 global piezo_config ; Sets up piezo sensitivity of heinrichs weikamp Piezo buttons (~30ms)
heinrichsweikamp
parents: 44
diff changeset
326 piezo_config:
heinrichsweikamp
parents: 44
diff changeset
327 movlw .20
heinrichsweikamp
parents: 44
diff changeset
328 rcall piezo_config_tx ; Send one byte
heinrichsweikamp
parents: 44
diff changeset
329 movlw .20
heinrichsweikamp
parents: 44
diff changeset
330 rcall piezo_config_tx ; Send one byte
heinrichsweikamp
parents: 44
diff changeset
331 movlw .100
heinrichsweikamp
parents: 44
diff changeset
332 rcall piezo_config_tx ; Send one byte
heinrichsweikamp
parents: 44
diff changeset
333 movlw .200
heinrichsweikamp
parents: 44
diff changeset
334 rcall piezo_config_tx ; Send one byte
heinrichsweikamp
parents: 44
diff changeset
335 return
heinrichsweikamp
parents: 44
diff changeset
336
heinrichsweikamp
parents: 44
diff changeset
337 piezo_config_tx:
heinrichsweikamp
parents: 44
diff changeset
338 movwf uart1_temp ; Store byte
heinrichsweikamp
parents: 44
diff changeset
339 movlw .8
heinrichsweikamp
parents: 44
diff changeset
340 movwf uart2_temp ; Bit counter
heinrichsweikamp
parents: 44
diff changeset
341 bcf TX3_PIEZO_CFG ; Startbit
heinrichsweikamp
parents: 44
diff changeset
342 rcall piezo_config_wait_bit
heinrichsweikamp
parents: 44
diff changeset
343 piezo_config_tx_loop:
heinrichsweikamp
parents: 44
diff changeset
344 btfss uart1_temp,0 ; LSB first
heinrichsweikamp
parents: 44
diff changeset
345 bcf TX3_PIEZO_CFG
heinrichsweikamp
parents: 44
diff changeset
346 btfsc uart1_temp,0 ; LSB first
heinrichsweikamp
parents: 44
diff changeset
347 bsf TX3_PIEZO_CFG
heinrichsweikamp
parents: 44
diff changeset
348 rcall piezo_config_wait_bit
heinrichsweikamp
parents: 44
diff changeset
349 rrncf uart1_temp,F
heinrichsweikamp
parents: 44
diff changeset
350 decfsz uart2_temp,F
heinrichsweikamp
parents: 44
diff changeset
351 bra piezo_config_tx_loop
heinrichsweikamp
parents: 44
diff changeset
352 bsf TX3_PIEZO_CFG ; Stopbit
heinrichsweikamp
parents: 44
diff changeset
353 rcall piezo_config_wait_bit
heinrichsweikamp
parents: 44
diff changeset
354 return
heinrichsweikamp
parents: 44
diff changeset
355
heinrichsweikamp
parents: 44
diff changeset
356 piezo_config_wait_bit:
heinrichsweikamp
parents: 44
diff changeset
357 setf TMR5H
heinrichsweikamp
parents: 44
diff changeset
358 movlw .255-.26 ;26 x 31,5µs = 819us
heinrichsweikamp
parents: 44
diff changeset
359 movwf TMR5L
heinrichsweikamp
parents: 44
diff changeset
360 bcf PIR5,TMR5IF ; Clear flag
heinrichsweikamp
parents: 44
diff changeset
361 piezo_config_wait_bit3:
heinrichsweikamp
parents: 44
diff changeset
362 btfss PIR5,TMR5IF
heinrichsweikamp
parents: 44
diff changeset
363 bra piezo_config_wait_bit3 ; Wait loop
heinrichsweikamp
parents: 44
diff changeset
364 return
heinrichsweikamp
parents: 44
diff changeset
365
0
heinrichsweikamp
parents:
diff changeset
366 global reset_battery_pointer
heinrichsweikamp
parents:
diff changeset
367 reset_battery_pointer: ; Resets battery pointer 0x07-0x0C and battery_gauge:5
113
heinrichsweikamp
parents: 44
diff changeset
368 extern lt2942_charge_done
heinrichsweikamp
parents: 44
diff changeset
369 call lt2942_charge_done ; Reset accumulating registers to 0xFFFF
heinrichsweikamp
parents: 44
diff changeset
370 clrf EEADRH
0
heinrichsweikamp
parents:
diff changeset
371 clrf EEDATA ; Delete to zero
heinrichsweikamp
parents:
diff changeset
372 write_int_eeprom 0x07
heinrichsweikamp
parents:
diff changeset
373 write_int_eeprom 0x08
heinrichsweikamp
parents:
diff changeset
374 write_int_eeprom 0x09
heinrichsweikamp
parents:
diff changeset
375 write_int_eeprom 0x0A
heinrichsweikamp
parents:
diff changeset
376 write_int_eeprom 0x0B
heinrichsweikamp
parents:
diff changeset
377 write_int_eeprom 0x0C
heinrichsweikamp
parents:
diff changeset
378 banksel battery_gauge+0
heinrichsweikamp
parents:
diff changeset
379 clrf battery_gauge+0
heinrichsweikamp
parents:
diff changeset
380 clrf battery_gauge+1
heinrichsweikamp
parents:
diff changeset
381 clrf battery_gauge+2
heinrichsweikamp
parents:
diff changeset
382 clrf battery_gauge+3
heinrichsweikamp
parents:
diff changeset
383 clrf battery_gauge+4
heinrichsweikamp
parents:
diff changeset
384 clrf battery_gauge+5
heinrichsweikamp
parents:
diff changeset
385 banksel common
heinrichsweikamp
parents:
diff changeset
386 movlw .100
heinrichsweikamp
parents:
diff changeset
387 movwf batt_percent
heinrichsweikamp
parents:
diff changeset
388 return
heinrichsweikamp
parents:
diff changeset
389
heinrichsweikamp
parents:
diff changeset
390
heinrichsweikamp
parents:
diff changeset
391 END