annotate src/adc_lightsensor.asm @ 0:11d4fc797f74

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