Mercurial > public > hwos_code
annotate src/adc_lightsensor.asm @ 42:92aa5238a99c
minor
author | mh@mh-THINK.fritz.box |
---|---|
date | Thu, 15 Aug 2013 15:58:48 +0200 |
parents | 0e1723f2761e |
children | 18fe4e668baa |
rev | line source |
---|---|
0 | 1 ;============================================================================= |
2 ; | |
3 ; File adc.asm | |
4 ; | |
5 ; | |
6 ; Copyright (c) 2011, JD Gascuel, HeinrichsWeikamp, all right reserved. | |
7 ;============================================================================= | |
8 ; HISTORY | |
9 ; 2011-08-08 : [mH] moving from OSTC code | |
10 | |
11 #include "ostc3.inc" | |
12 #include "math.inc" | |
13 #include "wait.inc" | |
14 #include "eeprom_rs232.inc" | |
15 | |
16 sensors CODE | |
17 | |
18 wait_adc: | |
19 movwf ADCON0 | |
20 nop | |
21 bsf ADCON0,1 ; start ADC | |
22 wait_adc2: | |
23 btfsc ADCON0,1 ; Wait... | |
24 bra wait_adc2 | |
25 return | |
26 | |
27 global get_battery_voltage | |
28 get_battery_voltage: ; starts ADC and waits until fnished | |
29 bsf adc_running ; =1: The ADC is in use | |
30 movlw b'00100000' ; 2.048V Vref+ -> 1LSB = 500µV | |
31 movwf ADCON1 | |
32 movlw b'00011001' ; power on ADC, select AN6 | |
33 rcall wait_adc | |
34 | |
35 movff ADRESH,batt_voltage+1 ; store value | |
36 movff ADRESL,batt_voltage+0 ; store value | |
37 bcf ADCON0,0 ; power off ADC | |
38 | |
39 ; Multiply with 2,006 to be excact here... | |
40 ; bcf STATUS,C | |
41 ; rlcf xA+0,F | |
42 ; | |
43 ; rlcf xA+1,F ; x2 | |
44 | |
45 ; movff xA+0,batt_voltage+0 ; store value | |
46 ; movff xA+1,batt_voltage+1 | |
47 | |
48 movlw LOW lithium_36v_low | |
49 movwf sub_a+0 | |
50 movlw HIGH lithium_36v_low | |
51 movwf sub_a+1 | |
52 movff batt_voltage+0,sub_b+0 | |
53 movff batt_voltage+1,sub_b+1 | |
54 call subU16 ; sub_c = sub_a - sub_b | |
55 ; Battery is 3,6V (>lithium_36v_low?) | |
56 btfss neg_flag | |
57 bra get_battery_voltage4 ; No, use 1,5V | |
58 | |
59 bsf battery_is_36v ; Yes, set flag (Cleared in power-on reset only!) | |
60 | |
61 ; Check if the battery is near-dead already | |
62 movlw LOW lithium_36v_empty | |
63 movwf sub_a+0 | |
64 movlw HIGH lithium_36v_empty | |
65 movwf sub_a+1 | |
66 call subU16 ; sub_c = sub_a - sub_b | |
67 ; Battery is not dead yet (>lithium_36v_empty?) | |
68 btfsc neg_flag | |
69 bra get_battery_voltage2 ; Yes, battery is still ok | |
70 | |
71 ; Battery is probably dead very soon | |
72 ; Set ">=24Ah used" into battery gauge registers | |
73 movlw .128 | |
74 movff WREG,battery_gauge+5 | |
75 | |
76 get_battery_voltage2: | |
77 ; Use 3,6V battery gauging mode | |
78 movff battery_gauge+5,xC+3 | |
79 movff battery_gauge+4,xC+2 | |
80 movff battery_gauge+3,xC+1 | |
81 movff battery_gauge+2,xC+0 | |
82 ; battery_gauge:6 is nAs | |
83 ; devide through 65536 | |
84 ; devide through 364 | |
85 ; Result is in percent of a 2,4Ah Battery | |
86 movlw LOW .364 | |
87 movwf xB+0 | |
88 movlw HIGH .364 | |
89 movwf xB+1 | |
90 call div32x16 ; xC:4 / xB:2 = xC+3:xC+2 with xC+1:xC+0 as remainder | |
91 movff xC+0,lo | |
92 ; Limit to 100 | |
93 movlw .100 | |
94 cpfslt lo | |
95 movwf lo | |
96 ; lo will be between 0 (Full) and 100 (empty) | |
97 movf lo,W | |
98 sublw .100 | |
99 movwf lo | |
100 get_battery_voltage3: | |
101 movlw .100 | |
102 cpfslt lo | |
103 movwf lo | |
104 ; 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
|
105 |
0e1723f2761e
use four additional data points for the battery monitor
mh@mh-THINK.fritz.box
parents:
0
diff
changeset
|
106 ; 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
|
107 ; 75% |
0e1723f2761e
use four additional data points for the battery monitor
mh@mh-THINK.fritz.box
parents:
0
diff
changeset
|
108 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
|
109 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
|
110 movlw LOW lithium_36v_75 |
0e1723f2761e
use four additional data points for the battery monitor
mh@mh-THINK.fritz.box
parents:
0
diff
changeset
|
111 movwf sub_a+0 |
0e1723f2761e
use four additional data points for the battery monitor
mh@mh-THINK.fritz.box
parents:
0
diff
changeset
|
112 movlw HIGH lithium_36v_75 |
0e1723f2761e
use four additional data points for the battery monitor
mh@mh-THINK.fritz.box
parents:
0
diff
changeset
|
113 movwf sub_a+1 |
0e1723f2761e
use four additional data points for the battery monitor
mh@mh-THINK.fritz.box
parents:
0
diff
changeset
|
114 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
|
115 btfsc neg_flag |
0e1723f2761e
use four additional data points for the battery monitor
mh@mh-THINK.fritz.box
parents:
0
diff
changeset
|
116 bra get_battery_voltage3a |
0e1723f2761e
use four additional data points for the battery monitor
mh@mh-THINK.fritz.box
parents:
0
diff
changeset
|
117 movlw .75 |
0e1723f2761e
use four additional data points for the battery monitor
mh@mh-THINK.fritz.box
parents:
0
diff
changeset
|
118 movwf lo |
0e1723f2761e
use four additional data points for the battery monitor
mh@mh-THINK.fritz.box
parents:
0
diff
changeset
|
119 get_battery_voltage3a: |
0e1723f2761e
use four additional data points for the battery monitor
mh@mh-THINK.fritz.box
parents:
0
diff
changeset
|
120 ; 50% |
0e1723f2761e
use four additional data points for the battery monitor
mh@mh-THINK.fritz.box
parents:
0
diff
changeset
|
121 movlw LOW lithium_36v_50 |
0e1723f2761e
use four additional data points for the battery monitor
mh@mh-THINK.fritz.box
parents:
0
diff
changeset
|
122 movwf sub_a+0 |
0e1723f2761e
use four additional data points for the battery monitor
mh@mh-THINK.fritz.box
parents:
0
diff
changeset
|
123 movlw HIGH lithium_36v_50 |
0e1723f2761e
use four additional data points for the battery monitor
mh@mh-THINK.fritz.box
parents:
0
diff
changeset
|
124 movwf sub_a+1 |
0e1723f2761e
use four additional data points for the battery monitor
mh@mh-THINK.fritz.box
parents:
0
diff
changeset
|
125 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
|
126 btfsc neg_flag |
0e1723f2761e
use four additional data points for the battery monitor
mh@mh-THINK.fritz.box
parents:
0
diff
changeset
|
127 bra get_battery_voltage3b |
0e1723f2761e
use four additional data points for the battery monitor
mh@mh-THINK.fritz.box
parents:
0
diff
changeset
|
128 movlw .50 |
0e1723f2761e
use four additional data points for the battery monitor
mh@mh-THINK.fritz.box
parents:
0
diff
changeset
|
129 movwf lo |
0e1723f2761e
use four additional data points for the battery monitor
mh@mh-THINK.fritz.box
parents:
0
diff
changeset
|
130 get_battery_voltage3b: |
0e1723f2761e
use four additional data points for the battery monitor
mh@mh-THINK.fritz.box
parents:
0
diff
changeset
|
131 ; 25% |
0e1723f2761e
use four additional data points for the battery monitor
mh@mh-THINK.fritz.box
parents:
0
diff
changeset
|
132 movlw LOW lithium_36v_25 |
0e1723f2761e
use four additional data points for the battery monitor
mh@mh-THINK.fritz.box
parents:
0
diff
changeset
|
133 movwf sub_a+0 |
0e1723f2761e
use four additional data points for the battery monitor
mh@mh-THINK.fritz.box
parents:
0
diff
changeset
|
134 movlw HIGH lithium_36v_25 |
0e1723f2761e
use four additional data points for the battery monitor
mh@mh-THINK.fritz.box
parents:
0
diff
changeset
|
135 movwf sub_a+1 |
0e1723f2761e
use four additional data points for the battery monitor
mh@mh-THINK.fritz.box
parents:
0
diff
changeset
|
136 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
|
137 btfsc neg_flag |
0e1723f2761e
use four additional data points for the battery monitor
mh@mh-THINK.fritz.box
parents:
0
diff
changeset
|
138 bra get_battery_voltage3c |
0e1723f2761e
use four additional data points for the battery monitor
mh@mh-THINK.fritz.box
parents:
0
diff
changeset
|
139 movlw .25 |
0e1723f2761e
use four additional data points for the battery monitor
mh@mh-THINK.fritz.box
parents:
0
diff
changeset
|
140 movwf lo |
0e1723f2761e
use four additional data points for the battery monitor
mh@mh-THINK.fritz.box
parents:
0
diff
changeset
|
141 get_battery_voltage3c: |
0e1723f2761e
use four additional data points for the battery monitor
mh@mh-THINK.fritz.box
parents:
0
diff
changeset
|
142 ; 10% |
0e1723f2761e
use four additional data points for the battery monitor
mh@mh-THINK.fritz.box
parents:
0
diff
changeset
|
143 movlw LOW lithium_36v_10 |
0e1723f2761e
use four additional data points for the battery monitor
mh@mh-THINK.fritz.box
parents:
0
diff
changeset
|
144 movwf sub_a+0 |
0e1723f2761e
use four additional data points for the battery monitor
mh@mh-THINK.fritz.box
parents:
0
diff
changeset
|
145 movlw HIGH lithium_36v_10 |
0e1723f2761e
use four additional data points for the battery monitor
mh@mh-THINK.fritz.box
parents:
0
diff
changeset
|
146 movwf sub_a+1 |
0e1723f2761e
use four additional data points for the battery monitor
mh@mh-THINK.fritz.box
parents:
0
diff
changeset
|
147 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
|
148 btfsc neg_flag |
0e1723f2761e
use four additional data points for the battery monitor
mh@mh-THINK.fritz.box
parents:
0
diff
changeset
|
149 bra get_battery_voltage3d |
0e1723f2761e
use four additional data points for the battery monitor
mh@mh-THINK.fritz.box
parents:
0
diff
changeset
|
150 movlw .10 |
0e1723f2761e
use four additional data points for the battery monitor
mh@mh-THINK.fritz.box
parents:
0
diff
changeset
|
151 movwf lo |
0e1723f2761e
use four additional data points for the battery monitor
mh@mh-THINK.fritz.box
parents:
0
diff
changeset
|
152 get_battery_voltage3d: |
0 | 153 movf batt_percent,W |
154 cpfsgt lo ; keep batt_percent on the lowest value found | |
155 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
|
156 ; 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
|
157 ; movff lo,batt_percent ; store value |
0 | 158 bcf adc_running ; =1: The ADC is in use |
159 return | |
160 | |
161 get_battery_voltage4: | |
162 ; Use 1,5V battery voltage mode | |
163 ; Use approximation (batt_voltage:2-aa_15v_low)/4 = lo | |
164 movff batt_voltage+0,sub_a+0 | |
165 movff batt_voltage+1,sub_a+1 | |
166 movlw LOW aa_15v_low | |
167 movwf sub_b+0 | |
168 movlw HIGH aa_15v_low | |
169 movwf sub_b+1 | |
170 call subU16 ; sub_c = sub_a - sub_b | |
171 bcf STATUS,C | |
172 rrcf sub_c+1 | |
173 rrcf sub_c+0 ; /2 | |
174 bcf STATUS,C | |
175 rrcf sub_c+1 | |
176 rrcf sub_c+0 ; /4 | |
177 movff sub_c+0,lo | |
178 bra get_battery_voltage3 ; Check limits and return | |
179 | |
180 global get_ambient_level | |
181 get_ambient_level: ; starts ADC and waits until finished | |
182 btfsc adc_running ; ADC in use? | |
183 return ; Yes, return | |
184 | |
185 movlw b'00000000' ; Vref+ = Vdd | |
186 movwf ADCON1 | |
187 movlw b'00011101' ; power on ADC, select AN7 | |
188 rcall wait_adc | |
189 | |
190 movff ADRESH,ambient_light+1 | |
191 movff ADRESL,ambient_light+0 | |
192 bcf ADCON0,0 ; power off ADC | |
193 | |
194 ; ambient_light:2 is between 4096 (direct sunlight) and about 200 (darkness) | |
195 ; First: Devide through 16 | |
196 bcf STATUS,C | |
197 rrcf ambient_light+1 | |
198 rrcf ambient_light+0 | |
199 bcf STATUS,C | |
200 rrcf ambient_light+1 | |
201 rrcf ambient_light+0 | |
202 bcf STATUS,C | |
203 rrcf ambient_light+1 | |
204 rrcf ambient_light+0 | |
205 bcf STATUS,C | |
206 rrcf ambient_light+1 | |
207 rrcf ambient_light+0 | |
208 ; Result: ambient_light:2/16 | |
209 ; Now, make sure to have value between ambient_light_low and ambient_light_max | |
210 | |
211 movlw .254 | |
212 tstfsz ambient_light+1 ; >255? | |
213 movwf ambient_light+0 ; avoid ADC clipping | |
214 | |
215 incfsz ambient_light+0,W ; =255? | |
216 bra get_ambient_level2 ; No, continue | |
217 | |
218 movlw .254 | |
219 movwf ambient_light+0 ; avoid ADC clipping | |
220 | |
221 get_ambient_level2: | |
222 banksel isr_backup ; Back to Bank0 ISR data | |
223 movff opt_brightness,isr1_temp | |
224 | |
225 btfsc RCSTA1,7 ; UART module on? | |
226 clrf isr1_temp ; Yes, set temporally to eco mode | |
227 | |
228 incf isr1_temp,F ; adjust 0-2 to 1-3 | |
229 | |
230 banksel common ; flag is in bank1 | |
231 movlw ambient_light_max_high ; brightest setting | |
232 btfsc battery_is_36v ; 3,6V battery in use? | |
233 movlw ambient_light_max_high_36V ; Yes... | |
234 banksel isr_backup ; Back to Bank0 ISR data | |
235 | |
236 dcfsnz isr1_temp,F | |
237 movlw ambient_light_max_eco ; brightest setting | |
238 dcfsnz isr1_temp,F | |
239 movlw ambient_light_max_medium; brightest setting | |
240 | |
241 banksel common ; ambient_light is in Bank1 | |
242 incf ambient_light+0,F ; +1 | |
243 cpfslt ambient_light+0 ; smaller then WREG? | |
244 movwf ambient_light+0 ; No, set to max. | |
245 | |
246 banksel isr_backup ; Back to Bank0 ISR data | |
247 movff opt_brightness,isr1_temp | |
248 incf isr1_temp,F ; adjust 0-2 to 1-3 | |
249 movlw ambient_light_min_high ; darkest setting | |
250 | |
251 dcfsnz isr1_temp,F | |
252 movlw ambient_light_min_eco ; darkest setting | |
253 dcfsnz isr1_temp,F | |
254 movlw ambient_light_min_medium; darkest setting | |
255 dcfsnz isr1_temp,F | |
256 movlw ambient_light_min_high ; darkest setting | |
257 | |
258 banksel common ; ambient_light is in Bank1 | |
259 cpfsgt ambient_light+0 ; bigger then WREG? | |
260 movwf ambient_light+0 ; No, set to min | |
261 | |
262 movff ambient_light+0,max_CCPR1L ; Store value for dimming in TMR7 interrupt | |
263 return | |
264 | |
265 global get_rssi_level | |
266 get_rssi_level: ; starts ADC and waits until fnished | |
267 bsf adc_running ; =1: The ADC is in use | |
268 movlw b'00100000' ; 2.048V Vref+ | |
269 movwf ADCON1 | |
270 movlw b'01000101' ; power on ADC, select AN17 | |
271 rcall wait_adc | |
272 | |
273 movff ADRESL,rssi_value | |
274 bcf ADCON0,0 ; power off ADC | |
275 bcf adc_running ; =1: The ADC is in use | |
276 return | |
277 | |
278 global reset_battery_pointer | |
279 reset_battery_pointer: ; Resets battery pointer 0x07-0x0C and battery_gauge:5 | |
280 clrf EEADRH | |
281 clrf EEDATA ; Delete to zero | |
282 write_int_eeprom 0x07 | |
283 write_int_eeprom 0x08 | |
284 write_int_eeprom 0x09 | |
285 write_int_eeprom 0x0A | |
286 write_int_eeprom 0x0B | |
287 write_int_eeprom 0x0C | |
288 banksel battery_gauge+0 | |
289 clrf battery_gauge+0 | |
290 clrf battery_gauge+1 | |
291 clrf battery_gauge+2 | |
292 clrf battery_gauge+3 | |
293 clrf battery_gauge+4 | |
294 clrf battery_gauge+5 | |
295 banksel common | |
296 movlw .100 | |
297 movwf batt_percent | |
298 return | |
299 | |
300 | |
301 END |