Mercurial > public > hwos_code
annotate src/adc_lightsensor.asm @ 49:fe01bbaf6014
minor german update
author | heinrichsweikamp |
---|---|
date | Fri, 27 Sep 2013 14:17:21 +0200 |
parents | 18fe4e668baa |
children | f3062a611eef |
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: |
44
18fe4e668baa
BUGFIX: Percent display with 1,5V batteries
heinrichsweikamp
parents:
37
diff
changeset
|
153 movlw .100 |
18fe4e668baa
BUGFIX: Percent display with 1,5V batteries
heinrichsweikamp
parents:
37
diff
changeset
|
154 cpfslt lo |
18fe4e668baa
BUGFIX: Percent display with 1,5V batteries
heinrichsweikamp
parents:
37
diff
changeset
|
155 movwf lo |
18fe4e668baa
BUGFIX: Percent display with 1,5V batteries
heinrichsweikamp
parents:
37
diff
changeset
|
156 ; lo will be between 100 (Full) and 0 (empty) |
0 | 157 movf batt_percent,W |
158 cpfsgt lo ; keep batt_percent on the lowest value found | |
159 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
|
160 ; 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
|
161 ; movff lo,batt_percent ; store value |
0 | 162 bcf adc_running ; =1: The ADC is in use |
163 return | |
164 | |
165 get_battery_voltage4: | |
166 ; Use 1,5V battery voltage mode | |
167 ; Use approximation (batt_voltage:2-aa_15v_low)/4 = lo | |
168 movff batt_voltage+0,sub_a+0 | |
169 movff batt_voltage+1,sub_a+1 | |
170 movlw LOW aa_15v_low | |
171 movwf sub_b+0 | |
172 movlw HIGH aa_15v_low | |
173 movwf sub_b+1 | |
174 call subU16 ; sub_c = sub_a - sub_b | |
175 bcf STATUS,C | |
176 rrcf sub_c+1 | |
177 rrcf sub_c+0 ; /2 | |
178 bcf STATUS,C | |
179 rrcf sub_c+1 | |
180 rrcf sub_c+0 ; /4 | |
181 movff sub_c+0,lo | |
44
18fe4e668baa
BUGFIX: Percent display with 1,5V batteries
heinrichsweikamp
parents:
37
diff
changeset
|
182 bra get_battery_voltage3d ; Check limits and return |
0 | 183 |
184 global get_ambient_level | |
185 get_ambient_level: ; starts ADC and waits until finished | |
186 btfsc adc_running ; ADC in use? | |
187 return ; Yes, return | |
188 | |
189 movlw b'00000000' ; Vref+ = Vdd | |
190 movwf ADCON1 | |
191 movlw b'00011101' ; power on ADC, select AN7 | |
192 rcall wait_adc | |
193 | |
194 movff ADRESH,ambient_light+1 | |
195 movff ADRESL,ambient_light+0 | |
196 bcf ADCON0,0 ; power off ADC | |
197 | |
198 ; ambient_light:2 is between 4096 (direct sunlight) and about 200 (darkness) | |
199 ; First: Devide through 16 | |
200 bcf STATUS,C | |
201 rrcf ambient_light+1 | |
202 rrcf ambient_light+0 | |
203 bcf STATUS,C | |
204 rrcf ambient_light+1 | |
205 rrcf ambient_light+0 | |
206 bcf STATUS,C | |
207 rrcf ambient_light+1 | |
208 rrcf ambient_light+0 | |
209 bcf STATUS,C | |
210 rrcf ambient_light+1 | |
211 rrcf ambient_light+0 | |
212 ; Result: ambient_light:2/16 | |
213 ; Now, make sure to have value between ambient_light_low and ambient_light_max | |
214 | |
215 movlw .254 | |
216 tstfsz ambient_light+1 ; >255? | |
217 movwf ambient_light+0 ; avoid ADC clipping | |
218 | |
219 incfsz ambient_light+0,W ; =255? | |
220 bra get_ambient_level2 ; No, continue | |
221 | |
222 movlw .254 | |
223 movwf ambient_light+0 ; avoid ADC clipping | |
224 | |
225 get_ambient_level2: | |
226 banksel isr_backup ; Back to Bank0 ISR data | |
227 movff opt_brightness,isr1_temp | |
228 | |
229 btfsc RCSTA1,7 ; UART module on? | |
230 clrf isr1_temp ; Yes, set temporally to eco mode | |
231 | |
232 incf isr1_temp,F ; adjust 0-2 to 1-3 | |
233 | |
234 banksel common ; flag is in bank1 | |
235 movlw ambient_light_max_high ; brightest setting | |
236 btfsc battery_is_36v ; 3,6V battery in use? | |
237 movlw ambient_light_max_high_36V ; Yes... | |
238 banksel isr_backup ; Back to Bank0 ISR data | |
239 | |
240 dcfsnz isr1_temp,F | |
241 movlw ambient_light_max_eco ; brightest setting | |
242 dcfsnz isr1_temp,F | |
243 movlw ambient_light_max_medium; brightest setting | |
244 | |
245 banksel common ; ambient_light is in Bank1 | |
246 incf ambient_light+0,F ; +1 | |
247 cpfslt ambient_light+0 ; smaller then WREG? | |
248 movwf ambient_light+0 ; No, set to max. | |
249 | |
250 banksel isr_backup ; Back to Bank0 ISR data | |
251 movff opt_brightness,isr1_temp | |
252 incf isr1_temp,F ; adjust 0-2 to 1-3 | |
253 movlw ambient_light_min_high ; darkest setting | |
254 | |
255 dcfsnz isr1_temp,F | |
256 movlw ambient_light_min_eco ; darkest setting | |
257 dcfsnz isr1_temp,F | |
258 movlw ambient_light_min_medium; darkest setting | |
259 dcfsnz isr1_temp,F | |
260 movlw ambient_light_min_high ; darkest setting | |
261 | |
262 banksel common ; ambient_light is in Bank1 | |
263 cpfsgt ambient_light+0 ; bigger then WREG? | |
264 movwf ambient_light+0 ; No, set to min | |
265 | |
266 movff ambient_light+0,max_CCPR1L ; Store value for dimming in TMR7 interrupt | |
267 return | |
268 | |
269 global get_rssi_level | |
270 get_rssi_level: ; starts ADC and waits until fnished | |
271 bsf adc_running ; =1: The ADC is in use | |
272 movlw b'00100000' ; 2.048V Vref+ | |
273 movwf ADCON1 | |
274 movlw b'01000101' ; power on ADC, select AN17 | |
275 rcall wait_adc | |
276 | |
277 movff ADRESL,rssi_value | |
278 bcf ADCON0,0 ; power off ADC | |
279 bcf adc_running ; =1: The ADC is in use | |
280 return | |
281 | |
282 global reset_battery_pointer | |
283 reset_battery_pointer: ; Resets battery pointer 0x07-0x0C and battery_gauge:5 | |
284 clrf EEADRH | |
285 clrf EEDATA ; Delete to zero | |
286 write_int_eeprom 0x07 | |
287 write_int_eeprom 0x08 | |
288 write_int_eeprom 0x09 | |
289 write_int_eeprom 0x0A | |
290 write_int_eeprom 0x0B | |
291 write_int_eeprom 0x0C | |
292 banksel battery_gauge+0 | |
293 clrf battery_gauge+0 | |
294 clrf battery_gauge+1 | |
295 clrf battery_gauge+2 | |
296 clrf battery_gauge+3 | |
297 clrf battery_gauge+4 | |
298 clrf battery_gauge+5 | |
299 banksel common | |
300 movlw .100 | |
301 movwf batt_percent | |
302 return | |
303 | |
304 | |
305 END |