Mercurial > public > hwos_code
annotate src/adc_lightsensor.asm @ 656:8af5aefbcdaf default tip
Update to 3.31 beta
| author | heinrichsweikamp |
|---|---|
| date | Thu, 27 Nov 2025 18:32:58 +0100 |
| parents | 75e90cd0c2c3 |
| children |
| rev | line source |
|---|---|
| 0 | 1 ;============================================================================= |
| 2 ; | |
| 640 | 3 ; File adc_lightsensor.asm * combined next generation V3.11.1 |
| 0 | 4 ; |
| 5 ; | |
| 654 | 6 ; Copyright (c) 2011, JD Gascuel, heinrichs weikamp gmbh, all right reserved. |
| 0 | 7 ;============================================================================= |
| 8 ; HISTORY | |
| 9 ; 2011-08-08 : [mH] moving from OSTC code | |
| 10 | |
| 275 | 11 #include "hwos.inc" |
| 0 | 12 #include "math.inc" |
| 13 #include "wait.inc" | |
| 14 #include "eeprom_rs232.inc" | |
| 113 | 15 #include "i2c.inc" |
| 0 | 16 |
| 582 | 17 |
| 634 | 18 ;============================================================================= |
| 623 | 19 adc_light CODE |
| 582 | 20 ;============================================================================= |
| 0 | 21 |
| 634 | 22 |
| 23 ;----------------------------------------------------------------------------- | |
| 24 ; Helper Function - wait on ADC to finish conversion | |
| 25 ; | |
| 26 ; bank safe | |
| 27 ; | |
| 643 | 28 global wait_adc |
| 634 | 29 wait_adc: |
| 631 | 30 movwf ADCON0 ; select ADC channel |
| 31 nop ; wait a short moment | |
| 634 | 32 nop ; ... |
| 33 nop ; ..... | |
| 34 nop ; ....... | |
| 582 | 35 bsf ADCON0,1 ; start ADC |
| 0 | 36 wait_adc2: |
| 631 | 37 btfsc ADCON0,1 ; ADC done? |
| 38 bra wait_adc2 ; NO - wait | |
| 39 return ; YES - done | |
| 0 | 40 |
| 634 | 41 |
| 42 ;----------------------------------------------------------------------------- | |
| 43 ; get Battery Voltage | |
| 44 ; | |
| 0 | 45 global get_battery_voltage |
| 634 | 46 get_battery_voltage: |
| 623 | 47 btfss battery_gauge_available ; battery gauge IC available? |
| 48 bra get_battery_voltage_2 ; NO - OSTC hardware without gauge IC | |
| 631 | 49 bsf battery_is_36v ; YES - gauge IC available, 3.6V battery |
| 656 | 50 call lt2942_get_voltage ; - read battery voltage (and coulomb counter) |
| 623 | 51 call lt2942_get_temperature ; - read battery temperature |
| 52 tstfsz batt_voltage+1 ; - read voltage < 256 mV ? | |
| 53 bra get_battery_voltage_1 ; NO - proceed | |
| 656 | 54 call lt2942_get_voltage ; - re-read battery voltage (and coulomb counter) |
| 623 | 55 call lt2942_get_temperature ; - re-read battery temperature |
| 56 ;bra get_battery_voltage_1 ; - proceed | |
| 582 | 57 |
| 623 | 58 get_battery_voltage_1: |
| 656 | 59 ; rcall double_check_battery_percent ; make sure batt_percent is adjusted when voltage does not comply with the computed value |
| 631 | 60 rcall get_battery_voltage_low ; check for battery low condition |
| 623 | 61 btfsc divemode ; in dive mode? |
| 62 return ; YES - done | |
| 120 | 63 |
| 623 | 64 bcf cv_active ; clear CV charing status by default |
| 65 bcf cc_active ; clear CC charing status ny default | |
| 66 bcf LEDr ; switch off red LED | |
| 67 bcf TRISJ,2 ; activate Chrg-Out output | |
| 68 bsf CHRG_OUT ; start charger | |
| 640 | 69 nop ; ignore some noise |
| 70 nop ; ... | |
| 71 nop ; ... | |
| 72 nop ; ... | |
| 623 | 73 btfss CHRG_IN ; charging? |
| 74 bra charge_cc_active ; YES - charging in CC mode | |
| 75 bcf CHRG_OUT ; NO - stop charger | |
| 76 bsf TRISJ,2 ; - set Chrg-Out output to high impedance | |
| 77 WAITMS d'1' ; - wait 1 ms | |
| 78 btfsc CHRG_IN ; - still charging? | |
| 79 return ; NO - done | |
| 80 ;bra charge_cv_active ; YES - charging in CV mode | |
| 120 | 81 |
| 603 | 82 charge_cv_active: |
| 650 | 83 btfss analog_switches ; does the OSTC have analog switches? |
| 84 bra charge_cv_active1 ; NO | |
| 85 btfsc surfmode_menu ; YES - in surface menus? | |
| 86 bsf trigger_timeout ; YES - exit to surface mode during charging | |
| 87 charge_cv_active1: | |
| 631 | 88 decfsz get_bat_volt_counter,F ; decrement counter, became zero? |
| 89 return ; NO - not yet, done | |
| 656 | 90 movlw .16 ; YES - battery voltage >= 16*256mV (4.096V) and < 17*256mV (4.352V) |
| 91 cpfseq batt_voltage+1 ; - ... ? | |
| 631 | 92 bra charge_cc_active ; NO |
| 93 bsf cc_active ; YES - set CC charging status | |
| 94 bsf cv_active ; - set CV charging status | |
| 95 bsf LEDr ; - indicate charging | |
| 96 call lt2942_charge_done ; - reset accumulating registers to 0xFFFF | |
| 654 | 97 btfsc lock_cycle_counter ; cycle counted already? |
| 98 bra charge_cv_active2 ; Yes, skip | |
| 99 banksel charge_cycles | |
| 100 INCI charge_cycles ; increase charge_cycles counter by one | |
| 101 banksel common | |
| 102 bsf lock_cycle_counter ; lock cycle counter | |
| 103 charge_cv_active2: | |
| 631 | 104 WAITMS d'10' ; - wait 10 ms |
| 105 bcf LEDr ; - indicate charging | |
| 106 bsf get_bat_volt_counter,0 ; - set counter to 1 | |
| 107 return ; - done | |
| 120 | 108 |
| 207 | 109 charge_cc_active: |
| 650 | 110 btfss analog_switches ; does the OSTC have analog switches? |
| 111 bra charge_cc_active1 ; NO | |
| 112 btfsc surfmode_menu ; YES - in surface menus? | |
| 113 bsf trigger_timeout ; YES - exit to surface mode during charging | |
| 114 charge_cc_active1: | |
| 631 | 115 bsf cc_active ; set CC charging mode |
| 604 | 116 bsf LEDr ; indicate charging |
| 631 | 117 bcf CHRG_OUT ; |
| 623 | 118 bsf TRISJ,2 ; set chrg-Out output to high impedance |
| 631 | 119 movlw .15 ; battery voltage >= 16*256mV (4.096 V) |
| 120 cpfsgt batt_voltage+1 ; ... ? | |
| 604 | 121 bra charge_cc_active2 ; NO |
| 631 | 122 movlw .81 ; YES - battery voltage >= 80mV (+4096mV from batt_voltage+1) |
| 123 cpfslt batt_voltage+0 ; - ... ? | |
| 124 bra charge_cv_active ; YES | |
| 603 | 125 charge_cc_active2: |
| 631 | 126 movlw .10 ; NO - set counter to 10 |
| 127 movwf get_bat_volt_counter ; - ... | |
| 128 return ; - done | |
| 656 | 129 |
| 130 double_check_battery_percent: ; make sure batt_percent is adjusted when voltage does not comply with the computed value | |
| 131 ; batt_voltage:2 is in mV | |
| 132 ; use thresholds rechargeable_36V_80 to rechargeable_36V_00 | |
| 133 ; return, if computed value is lower then fixed threshold | |
| 134 ; overwrite batt_percent if computed value is bigger then threshold | |
| 135 ; in 2024, it's no longer enough to explain the old behavior in the manual... | |
| 136 ; used up as temp here | |
| 137 movlw .100 | |
| 138 movwf up ; initialize | |
| 139 | |
| 140 MOVII batt_voltage,sub_b | |
| 141 MOVLI rechargeable_36V_80,sub_a ; load threshold 80% | |
| 142 call cmpU16 ; sub_a - sub_b | |
| 143 movlw .80 | |
| 144 btfss neg_flag ; battery voltage > rechargeable_36V_80 ? | |
| 145 movwf up ; no, overwrite up with a fix value | |
| 146 MOVII batt_voltage,sub_b | |
| 147 MOVLI rechargeable_36V_60,sub_a ; load threshold 60% | |
| 148 call cmpU16 ; sub_a - sub_b | |
| 149 movlw .60 | |
| 150 btfss neg_flag ; battery voltage > rechargeable_36V_60 ? | |
| 151 movwf up ; no, overwrite up with a fix value | |
| 152 MOVII batt_voltage,sub_b | |
| 153 MOVLI rechargeable_36V_40,sub_a ; load threshold 40% | |
| 154 call cmpU16 ; sub_a - sub_b | |
| 155 movlw .40 | |
| 156 btfss neg_flag ; battery voltage > rechargeable_36V_40 ? | |
| 157 movwf up ; no, overwrite up with a fix value | |
| 158 MOVII batt_voltage,sub_b | |
| 159 MOVLI rechargeable_36V_20,sub_a ; load threshold 20% | |
| 160 call cmpU16 ; sub_a - sub_b | |
| 161 movlw .20 | |
| 162 btfss neg_flag ; battery voltage > rechargeable_36V_20 ? | |
| 163 movwf up ; no, overwrite up with a fix value | |
| 164 MOVII batt_voltage,sub_b | |
| 165 MOVLI rechargeable_36V_00,sub_a ; load threshold 0% | |
| 166 call cmpU16 ; sub_a - sub_b | |
| 167 movlw .0 | |
| 168 btfss neg_flag ; battery voltage > rechargeable_36V_0 ? | |
| 169 movwf up ; no, overwrite up with a fix value | |
| 170 | |
| 171 movf up,W | |
| 172 cpfsgt batt_percent ; up < batt_percent | |
| 173 return | |
| 174 movwf batt_percent ; YES, overwritze | |
| 175 return | |
| 113 | 176 |
| 623 | 177 get_battery_voltage_2: ; no gauge IC available, use ADC to measure battery voltage |
| 178 ; additional charging disable in software | |
| 179 bsf charge_disable ; set charging-inhibit signal | |
| 180 bcf charge_enable ; activate charging-inhibit signal | |
| 615 | 181 |
| 623 | 182 bsf adc_is_running ; =1: the ADC is in use |
| 183 movlw b'00100000' ; 2.048 Volt Vref+ -> 1 LSB = 500 µV | |
| 631 | 184 movwf ADCON1 ; ... |
| 582 | 185 movlw b'00011001' ; power on ADC, select AN6 |
| 631 | 186 rcall wait_adc ; take measurement |
| 623 | 187 MOVII ADRESL,batt_voltage ; store value |
| 582 | 188 bcf ADCON0,0 ; power off ADC |
| 0 | 189 |
| 634 | 190 ; ; multiply with 2.006 to be exact here... |
| 0 | 191 ; bcf STATUS,C |
| 192 ; rlcf xA+0,F | |
| 582 | 193 ; rlcf xA+1,F ; x2 |
| 623 | 194 ; MOVII xA,batt_voltage ; store value |
| 0 | 195 |
| 623 | 196 bcf battery_is_36v ; by default assume it is a 1.5 battery |
| 197 MOVII batt_voltage,sub_b ; load measured battery voltage | |
| 0 | 198 |
| 634 | 199 ; check if the battery is a 3.6V lithium |
| 623 | 200 MOVLI lithium_36v_low,sub_a ; load threshold for 3.6 Volt lithium battery |
| 201 call cmpU16 ; sub_a - sub_b | |
| 202 btfss neg_flag ; battery voltage > 3.6 V lithium threshold ? | |
| 203 bra get_battery_voltage_9 ; NO - keep assumption of 1.5V battery | |
| 204 bsf battery_is_36v ; YES - set flag | |
| 0 | 205 |
| 634 | 206 ; check if the battery is near-dead already |
| 623 | 207 MOVLI lithium_36v_empty,sub_a ; load threshold for near-dead lithium battery |
| 208 call cmpU16 ; sub_a - sub_b | |
| 209 btfsc neg_flag ; battery voltage > dead-battery threshold ? | |
| 210 bra get_battery_voltage_3 ; YES - battery is still ok | |
| 211 movlw .128 ; NO - battery is probably dead very soon, set ">=24Ah used" | |
| 212 movff WREG,battery_gauge+5 ; - into battery gauge registers | |
| 0 | 213 |
| 623 | 214 get_battery_voltage_3: ; 3.6V battery gauge mode |
| 631 | 215 ; SMOVQQ "by hand" as the macro does not work with arguments that have a '+something' with them |
| 623 | 216 bcf trigger_isr_updates ; clear flag, it will be set by the ISR in case it had kicked in |
| 0 | 217 movff battery_gauge+5,xC+3 |
| 218 movff battery_gauge+4,xC+2 | |
| 219 movff battery_gauge+3,xC+1 | |
| 220 movff battery_gauge+2,xC+0 | |
| 623 | 221 btfsc trigger_isr_updates ; did the ISR kicked in since we cleared the flag? |
| 222 bra get_battery_voltage_3 ; YES - retry copy | |
| 223 | |
| 604 | 224 ; battery_gauge: 6 is nAs |
| 225 ; divide through 65536 | |
| 226 ; divide through battery_capacity:2 | |
| 227 ; result is in percent | |
| 623 | 228 MOVII battery_capacity_internal,xB |
| 604 | 229 call div32x16 ; xC:4 = xC:4 / xB:2 with xA as remainder |
| 0 | 230 movff xC+0,lo |
| 604 | 231 ; limit to 100 |
| 582 | 232 movlw .100 |
| 233 cpfslt lo | |
| 234 movwf lo | |
| 604 | 235 ; lo will be between 0 (full) and 100 (empty) |
| 0 | 236 movf lo,W |
| 237 sublw .100 | |
| 238 movwf lo | |
| 623 | 239 get_battery_voltage_4: |
| 240 movlw .100 ; start with default of 100% | |
| 241 cpfslt lo ; > 100% | |
| 242 movwf lo ; YES - limit to 100% | |
| 604 | 243 ; lo will be between 100 (full) and 0 (empty) |
| 244 ; use 3.6V battery sensing based on 50 mA load | |
| 623 | 245 MOVLI lithium_36v_75,sub_a ; load threshold for > 75% |
| 246 call cmpU16 ; sub_a - sub_b | |
| 247 btfsc neg_flag ; battery voltage above 75% level? | |
| 248 bra get_battery_voltage_5 ; YES | |
| 249 movlw .75 ; NO - set to 75% | |
| 582 | 250 movwf lo |
| 623 | 251 get_battery_voltage_5: |
| 252 ; 50% | |
| 253 MOVLI lithium_36v_50,sub_a ; load threshold for > 50% | |
| 254 call cmpU16 ; sub_a - sub_b | |
| 255 btfsc neg_flag ; battery voltage above 75% level? | |
| 256 bra get_battery_voltage_6 ; YES | |
| 257 movlw .50 ; NO - set to 50% | |
| 582 | 258 movwf lo |
| 623 | 259 get_battery_voltage_6: |
| 582 | 260 ; 25% |
| 623 | 261 MOVLI lithium_36v_25,sub_a ; load threshold for > 25% |
| 262 call cmpU16 ; sub_a - sub_b | |
| 263 btfsc neg_flag ; battery voltage above 25% level? | |
| 264 bra get_battery_voltage_7 ; YES | |
| 265 movlw .25 ; NO - set to 25% | |
| 582 | 266 movwf lo |
| 623 | 267 get_battery_voltage_7: |
| 582 | 268 ; 10% |
| 623 | 269 MOVLI lithium_36v_10,sub_a ; load threshold for > 10% |
| 270 call cmpU16 ; sub_a - sub_b | |
| 271 btfsc neg_flag ; battery voltage above 10% level? | |
| 272 bra get_battery_voltage_8 ; YES | |
| 273 movlw .10 ; NO - set to 10% | |
| 582 | 274 movwf lo |
| 623 | 275 get_battery_voltage_8: |
| 276 movlw .100 ; maximum is 100 (%) | |
| 277 cpfslt lo ; > 100% ? | |
| 278 movwf lo ; YES - limit to 100% | |
| 279 movf batt_percent,W ; get last battery percentage | |
| 280 cpfsgt lo ; current battery % < last battery % ? | |
| 281 movff lo,batt_percent ; YES - take new value (keep batt_percent on the lowest value found) | |
| 282 btfsc battery_is_36v ; using a 3.6 volt battery? | |
| 283 movff lo,batt_percent ; YES - take new value (always use computed value for 3.6V battery) | |
| 284 bcf adc_is_running ; done with ADC | |
| 631 | 285 ;bra get_battery_voltage_low ; check for battery low condition (and return) |
| 286 | |
| 287 get_battery_voltage_low: | |
| 288 ; check for battery low condition | |
| 289 movlw battery_warn_level_36+1 ; get threshold for 3.6 Volt battery warning, incremented by 1 | |
| 290 btfss battery_is_36v ; actually a 3.6 Volt battery detected? | |
| 291 movlw battery_warn_level_15+1 ; NO - replace with 1.5 Volt battery warning, incremented by 1 | |
| 292 bsf battery_low_condition ; set battery low condition by default | |
| 293 cpfslt batt_percent ; current battery level <= warning threshold ? | |
| 294 bcf battery_low_condition ; NO - clear battery low condition | |
| 295 return ; - done | |
| 296 | |
| 623 | 297 get_battery_voltage_9: |
| 604 | 298 ; use 1.5V battery voltage mode |
| 623 | 299 ; use approximation (batt_voltage-aa_15v_low)/4 = lo |
| 300 MOVII batt_voltage,sub_a ; load battery voltage | |
| 301 MOVLI aa_15v_low, sub_b ; load offset | |
| 582 | 302 call subU16 ; sub_c = sub_a - sub_b |
| 623 | 303 bcf STATUS,C ; shift right to divide / 2 |
| 582 | 304 rrcf sub_c+1 |
| 623 | 305 rrcf sub_c+0 |
| 306 bcf STATUS,C ; another shift right to divide / 4 | |
| 582 | 307 rrcf sub_c+1 |
| 623 | 308 rrcf sub_c+0 |
| 309 movff sub_c+0,lo ; store result | |
| 310 bra get_battery_voltage_8 ; check limits and return | |
| 0 | 311 |
| 623 | 312 |
| 634 | 313 ;----------------------------------------------------------------------------- |
| 314 ; get ambient Light Level | |
| 315 ; | |
| 316 ; called from ISR only, in context bank isr_backup | |
| 317 ; | |
| 318 global get_ambient_level | |
| 628 | 319 get_ambient_level: |
| 623 | 320 btfsc sleepmode ; in sleep mode? |
| 321 return ; YES - done | |
| 322 btfsc adc_is_running ; NO - ADC in use? | |
| 640 | 323 return ; YES - abort |
| 650 | 324 TSTOSS opt_use_AmbientSensor ; =1: Use the ambient sensor |
| 325 bra get_ambient_level0 ; NO, skip and use fixed value | |
| 623 | 326 banksel HW_descriptor ; NO - select bank where hardware descriptor is stored |
| 327 btfsc ambient_sensor ; - ambient sensor available? | |
| 328 bra get_ambient_level1 ; YES - use sensor | |
| 650 | 329 get_ambient_level0: |
| 623 | 330 banksel isr_backup ; NO - back to ISR default bank |
| 645 | 331 movff brightness,isr_lo ; - get brightness selection |
| 656 | 332 incf isr_lo,F ; - 0-2 -> 1-3 |
| 333 movlw ambient_no_sensor_high ; - default selection to brightest setting | |
| 334 dcfsnz isr_lo,F ; - level 0 (eco) selected? | |
| 335 movlw ambient_no_sensor_eco ; YES - select eco brightness | |
| 336 dcfsnz isr_lo,F ; - level 1 (medium) selected? | |
| 337 movlw ambient_no_sensor_medium ; YES - select medium brightness | |
| 338 movwf ambient_light+0 ; - store selection | |
| 339 movwf max_CCPR1L ; - store value for dimming in TMR7 interrupt | |
| 340 return ; - done | |
| 203 | 341 |
| 623 | 342 get_ambient_level1: ; using ambient sensor |
| 343 banksel isr_backup ; back to ISR default bank | |
| 629 | 344 clrf ADCON1 ; Vref+ = Vdd |
| 582 | 345 movlw b'00011101' ; power on ADC, select AN7 |
| 346 rcall wait_adc | |
| 623 | 347 MOVII ADRESL,ambient_light |
| 656 | 348 ; MOVII ADRESL,gp_debug ; for debugging only |
| 643 | 349 bcf ADCON0,0 ; power off ADC |
| 0 | 350 |
| 643 | 351 btfsc ambient_light+1,7 ; result negative? |
| 352 return ; Yes, skip this measurement | |
| 0 | 353 ; ambient_light:2 is between 4096 (direct sunlight) and about 200 (darkness) |
| 604 | 354 ; first: divide by 16 |
| 656 | 355 movlw .4 ; divide by 2^4 = 16 |
| 356 btfsc dn_flag | |
| 357 movlw .1 ; divide by 1^4 = 4, increased sensitivity for dn hardware | |
| 634 | 358 get_ambient_level1_loop: |
| 359 bcf STATUS,C ; clear carry | |
| 360 rrcf ambient_light+1 ; rotate right high byte, carry into MSB, LSB into carry | |
| 361 rrcf ambient_light+0 ; rotate right low byte, carry into MSB, LSB into carry | |
| 362 decfsz WREG ; decrement counter, done? | |
| 363 bra get_ambient_level1_loop ; NO - loop | |
| 364 | |
| 604 | 365 ; result: ambient_light:2/16 |
| 366 ; now make sure to have value between ambient_light_low and ambient_light_max | |
| 0 | 367 |
| 582 | 368 movlw .254 |
| 604 | 369 tstfsz ambient_light+1 ; > 255 ? |
| 370 movwf ambient_light+0 ; YES - avoid ADC clipping | |
| 0 | 371 |
| 604 | 372 incfsz ambient_light+0,W ; = 255 ? |
| 643 | 373 bra get_ambient_level2 ; NO - continue |
| 0 | 374 |
| 582 | 375 movlw .254 |
| 376 movwf ambient_light+0 ; avoid ADC clipping | |
| 0 | 377 |
| 378 get_ambient_level2: | |
| 645 | 379 movff brightness,isr_lo ; get brightness setting |
| 0 | 380 |
| 582 | 381 btfsc RCSTA1,7 ; UART module on? |
| 623 | 382 clrf isr_lo ; YES - set temporary to eco mode |
| 634 | 383 |
| 623 | 384 incf isr_lo,F ; adjust 0-2 to 1-3 |
| 0 | 385 |
| 640 | 386 banksel HW_descriptor ; select bank where hardware descriptor and model variant are stored |
| 387 movlw ambient_light_max_high_cr ; default to cR and 2 hardware brightest setting | |
| 388 btfss battery_gauge_available ; battery gauge available? | |
| 389 movlw ambient_light_max_high_15V ; NO - change to 1.5V battery brightest setting | |
| 656 | 390 btfsc battery_is_36v ; 3.6V battery in use? |
| 640 | 391 movlw ambient_light_max_high_36V ; YES - change to 3.6V battery brightest setting |
| 656 | 392 btfsc dn_flag ; dn hardware |
| 393 movlw ambient_light_max_high_dn ; YES - change to dn hardware brightest setting | |
| 623 | 394 banksel isr_backup ; back to ISR default bank |
| 0 | 395 |
| 634 | 396 dcfsnz isr_lo,F ; eco setting? |
| 397 movlw ambient_light_max_eco ; YES | |
| 398 dcfsnz isr_lo,F ; medium setting? | |
| 399 movlw ambient_light_max_medium ; YES | |
| 0 | 400 |
| 582 | 401 incf ambient_light+0,F ; +1 |
| 604 | 402 cpfslt ambient_light+0 ; smaller than WREG? |
| 403 movwf ambient_light+0 ; NO - set to max. | |
| 0 | 404 |
| 645 | 405 movff brightness,isr_lo ; get brightness setting |
| 623 | 406 incf isr_lo,F ; adjust 0-2 to 1-3 |
| 634 | 407 movlw ambient_light_min_high ; default to highest setting |
| 408 dcfsnz isr_lo,F ; eco setting? | |
| 409 movlw ambient_light_min_eco ; YES | |
| 410 dcfsnz isr_lo,F ; medium setting? | |
| 411 movlw ambient_light_min_medium ; YES | |
| 412 dcfsnz isr_lo,F ; highest setting? | |
| 413 movlw ambient_light_min_high ; YES | |
| 0 | 414 |
| 604 | 415 cpfsgt ambient_light+0 ; bigger than WREG? |
| 416 movwf ambient_light+0 ; NO - set to min | |
| 582 | 417 |
| 604 | 418 movff ambient_light+0,max_CCPR1L ; store value for dimming in TMR7 interrupt |
| 634 | 419 return ; done |
| 0 | 420 |
| 623 | 421 |
| 634 | 422 ;----------------------------------------------------------------------------- |
| 423 ; Read analog external Sensors | |
| 424 ; | |
| 425 ; called from outside ISR only | |
| 623 | 426 ; |
| 656 | 427 IFDEF _external_sensor_eccr |
| 623 | 428 |
| 634 | 429 global get_analog_inputs |
| 430 get_analog_inputs: | |
| 623 | 431 bsf adc_is_running ; =1: the ADC is in use |
| 640 | 432 btfsc screen_type3 ; display 3 ? |
| 433 bra get_analog_inputs2 ; yes, skip here | |
| 634 | 434 btfsc TFT_PWM ; PWM active? |
| 435 bra get_analog_inputs ; YES - wait for PWM low | |
| 436 | |
| 640 | 437 get_analog_inputs2: |
| 604 | 438 movlw b'00100000' ; 2.048V Vref+ -> 1 LSB = 500 µV |
| 634 | 439 movwf ADCON1 ; ... |
| 440 | |
| 623 | 441 ; Sensor 1 |
| 582 | 442 movlw b'00100001' ; power on ADC, select AN8 |
| 634 | 443 MOVII sensor1_mv,mpr ; get last sensor voltage |
| 444 rcall get_analog_inputs_common ; update value | |
| 445 MOVII mpr,sensor1_mv ; store new value | |
| 623 | 446 |
| 447 ; Sensor 2 | |
| 582 | 448 movlw b'00100101' ; power on ADC, select AN9 |
| 634 | 449 MOVII sensor2_mv,mpr ; get last sensor voltage |
| 450 rcall get_analog_inputs_common ; update value | |
| 451 MOVII mpr,sensor2_mv ; store new value | |
| 623 | 452 |
| 453 ; Sensor 3 | |
| 582 | 454 movlw b'00101001' ; power on ADC, select AN10 |
| 634 | 455 MOVII sensor3_mv,mpr ; get last sensor voltage |
| 456 rcall get_analog_inputs_common ; update value | |
| 457 MOVII mpr,sensor3_mv ; store new value | |
| 458 | |
| 582 | 459 bcf ADCON0,0 ; power off ADC |
| 623 | 460 bcf adc_is_running ; done with ADC |
| 634 | 461 return ; done |
| 462 | |
| 463 get_analog_inputs_common: | |
| 464 rcall wait_adc ; wait for ADC | |
| 465 bcf STATUS,C ; clear carry flag | |
| 466 rrcf ADRESH,F ; divide /2 | |
| 467 rrcf ADRESL,W ; ... | |
| 468 addwf mpr+0,F ; add to sensor_mv:2 | |
| 469 movf ADRESH,W ; ... | |
| 470 addwfc mpr+1,F ; ... | |
| 471 bcf STATUS,C ; clear carry flag | |
| 472 rrcf mpr+1,F ; divide /2 | |
| 473 rrcf mpr+0,F ; ... | |
| 474 movlw HIGH ignore_mv_above ; get upper limit of operational range | |
| 475 cpfsgt mpr+1 ; > limit ? | |
| 476 bra get_analog_inputs_common_1 ; NO - ok to use | |
| 477 CLRI mpr ; YES - ignore this reading | |
| 478 return ; - done | |
| 479 get_analog_inputs_common_1: | |
| 480 tstfsz mpr+1 ; > 25.5 mV ? | |
| 481 return ; YES - ok to use anyway | |
| 482 movlw ignore_mv_below ; NO - get lower limit of operational range | |
| 483 cpfsgt mpr+0 ; - > limit ? | |
| 484 clrf mpr+0 ; NO - ignore this reading | |
| 485 return ; - done | |
| 0 | 486 |
| 656 | 487 ENDIF ; _external_sensor_eccr |
| 623 | 488 |
| 489 | |
| 634 | 490 ;----------------------------------------------------------------------------- |
| 491 ; Set the Sensitivity of the Piezo Buttons | |
| 492 ; | |
| 493 ; called from outside ISR only, ~ 30 ms | |
| 494 ; | |
| 495 global piezo_config | |
| 496 piezo_config: | |
| 623 | 497 clrf TMR5H ; clear TMR5H first |
| 498 clrf TMR5L ; clear TMR5L thereafter | |
| 499 bcf PIR5,TMR5IF ; clear timer 5 overrun flag, will take ~ 2 seconds to overrun after reset | |
| 500 bcf switch_right ; clear left-over button events | |
| 501 bcf switch_left ; ... | |
|
416
4389fe9673b2
NEW: Support for new logbook memory chip (minor hardware change)
heinrichsweikamp
parents:
357
diff
changeset
|
502 piezo_config0: |
| 623 | 503 btfsc switch_right ; user still pressing the right button? |
| 504 bra piezo_config ; YES - loop to wait for release | |
| 505 btfsc switch_left ; user still pressing the left button? | |
| 506 bra piezo_config ; YES - loop to wait for release | |
| 582 | 507 |
| 623 | 508 btfss PIR5,TMR5IF ; timeout? |
| 509 bra piezo_config0 ; NO - not yet, loop | |
| 468 | 510 |
| 634 | 511 bcf INTCON,GIE ; disable all interrupts |
| 604 | 512 |
| 582 | 513 movff opt_cR_button_right,WREG ; right button |
| 514 btfsc flip_screen ; 180° rotation ? | |
| 604 | 515 movff opt_cR_button_left,WREG ; YES - left button |
| 634 | 516 rcall piezo_config_tx ; send to button |
|
343
34e2f5bdc5a2
BUGFIX: Rotate button sensitivity with screen (OSTC 2 and cR)
heinrichsweikamp
parents:
275
diff
changeset
|
517 |
| 582 | 518 movff opt_cR_button_left,WREG ; left button |
| 519 btfsc flip_screen ; 180° rotation ? | |
| 604 | 520 movff opt_cR_button_right,WREG ; YES - right button |
| 634 | 521 rcall piezo_config_tx ; send to button |
|
343
34e2f5bdc5a2
BUGFIX: Rotate button sensitivity with screen (OSTC 2 and cR)
heinrichsweikamp
parents:
275
diff
changeset
|
522 |
| 582 | 523 movlw .20 ; reserved |
| 634 | 524 rcall piezo_config_tx ; send to button |
| 582 | 525 movlw .20 ; reserved |
| 634 | 526 rcall piezo_config_tx ; send to button |
| 527 | |
| 528 bsf INTCON,GIE ; re-enable all interrupts | |
| 529 return ; done | |
| 530 | |
| 604 | 531 |
| 634 | 532 ;----------------------------------------------------------------------------- |
| 533 ; Helper Function - send 1 Byte to the Piezo Buttons | |
| 534 ; | |
| 535 piezo_config_tx: | |
| 536 movwf lo ; store byte to be sent | |
| 537 movlw .8 ; set up bit count | |
| 538 movwf hi ; ... | |
| 582 | 539 bcf TX3_PIEZO_CFG ; start bit |
| 634 | 540 rcall piezo_config_wait_bit ; wait |
| 541 piezo_config_tx_loop: ; LSB first | |
| 542 btfss lo,0 ; bit = 0 ? | |
| 543 bcf TX3_PIEZO_CFG ; YES | |
| 544 btfsc lo,0 ; bit = 1 ? | |
| 545 bsf TX3_PIEZO_CFG ; YES | |
| 546 rcall piezo_config_wait_bit ; wait | |
| 547 rrncf lo,F ; shift lo to access next bit | |
| 548 decfsz hi,F ; decrement bit counter, all done? | |
| 549 bra piezo_config_tx_loop ; NO - loop | |
| 550 bsf TX3_PIEZO_CFG ; YES - stop bit | |
| 551 rcall piezo_config_wait_bit ; - wait | |
| 552 return ; - done | |
| 113 | 553 |
| 554 piezo_config_wait_bit: | |
| 623 | 555 setf TMR5H ; set TMR5H first (to 255) |
| 634 | 556 movlw .255-.26 ; 26 x 31.5 µs = 819 µs |
| 623 | 557 movwf TMR5L ; set TMR5L thereafter |
| 558 bcf PIR5,TMR5IF ; clear timer 5 overrun flag | |
| 634 | 559 piezo_config_wait_bit_loop: |
| 623 | 560 btfss PIR5,TMR5IF ; timeout? |
| 634 | 561 bra piezo_config_wait_bit_loop ; NO - loop |
| 623 | 562 return ; YES - done |
| 582 | 563 |
| 564 | |
| 634 | 565 ;----------------------------------------------------------------------------- |
| 566 ; get the analog Switches | |
| 567 ; | |
| 568 ; called from ISR and from sleep mode, returns in bank common | |
| 569 ; | |
| 570 global get_analog_switches | |
| 571 get_analog_switches: | |
| 604 | 572 btfsc analog_switches ; does the OSTC have analog switches? |
| 623 | 573 bra get_analog_switches_1 ; YES |
| 634 | 574 |
| 629 | 575 get_analog_switches0: |
| 634 | 576 bcf analog_sw1_pressed ; clear flag for analog switch 1 |
| 577 bcf analog_sw2_pressed ; clear flag for analog switch 2 | |
| 578 return ; done | |
| 623 | 579 |
| 580 get_analog_switches_1: | |
| 581 btfsc adc_is_running ; ADC in use? | |
| 604 | 582 return ; YES - abort |
| 650 | 583 btfsc cc_active ; NO - charging constant current? |
| 584 bra get_analog_switches0 ; YES - abort (and clear both flags) | |
| 585 btfsc cv_active ; NO - charging constant voltage? | |
| 631 | 586 bra get_analog_switches0 ; YES - abort (and clear both flags) |
| 623 | 587 |
| 588 get_analog_switches_2: | |
| 640 | 589 ; reset the latch register in case it's externally pulled down. mH This is a test |
| 590 bsf power_sw1 ; switch on power supply for switch 1 | |
| 591 bsf power_sw2 ; switch on power supply for switch 2 | |
| 592 | |
| 634 | 593 bsf adc_is_running ; flag that ADC is in use |
| 631 | 594 bcf ADCON2,ADFM ; left justified |
| 634 | 595 clrf ADCON1 ; |
| 582 | 596 movlw b'00100101' ; power on ADC, select AN9 |
| 634 | 597 rcall wait_adc ; wait for ADC |
| 623 | 598 |
| 599 banksel isr_backup ; select bank ISR data | |
| 634 | 600 |
| 601 movlw .250 ; set upper limit | |
| 602 cpfslt ADRESH ; above upper limit? | |
| 603 movff analog_sw2,ADRESH ; YES - use (last) average instead | |
| 604 | |
| 605 movf ADRESH,W ; get ADC result (high byte) | |
| 606 addwf analog_sw2_raw+0 ; add to averaging register | |
| 607 movlw .0 ; ... | |
| 608 addwfc analog_sw2_raw+1 ; ... | |
| 582 | 609 decfsz analog_counter,F ; continue averaging? |
| 623 | 610 bra get_analog_switches_2a ; YES |
| 634 | 611 |
| 612 ; compute average - divide by 16 | |
| 613 swapf analog_sw2_raw+1,F ; swap nibbles in high byte | |
| 614 movlw b'11110000' ; keep only upper nibble | |
| 615 andwf analog_sw2_raw+1,W ; ... | |
| 616 movwf analog_sw2 ; copy ex lower nibble of high byte to upper nibble of result | |
| 617 swapf analog_sw2_raw+0,F ; swap nibble in low byte | |
| 618 movlw b'00001111' ; keep only lower nibble | |
| 619 andwf analog_sw2_raw+0,W ; ... | |
| 620 iorwf analog_sw2,F ; copy ex upper nibble of low byte to lower nibble of result | |
| 621 | |
| 622 clrf analog_sw2_raw+1 ; reset average registers | |
| 623 clrf analog_sw2_raw+0 ; ... | |
| 623 | 624 |
| 625 get_analog_switches_2a: | |
| 626 bcf analog_sw2_pressed ; default to not pressed | |
| 634 | 627 movff opt_cR_button_left,WREG ; get button sensitivity (20-100) |
| 628 bcf STATUS,C ; clear carry bit | |
| 604 | 629 rrcf WREG ; /2 -> 10-50 |
| 634 | 630 bcf STATUS,C ; clear carry bit |
| 631 rrcf WREG ; /4 -> 5-25 | |
| 604 | 632 decf WREG,W ; -1 |
| 633 decf WREG,W ; -1 | |
| 634 decf WREG,W ; -1 -> 2-22 | |
| 635 btfss button_polarity,1 ; (1= normal, 0=inverted) | |
| 634 | 636 bra get_analog_switches_sw2_inv ; 0 |
| 637 addwf analog_sw2,W ; 1 - add average (~128) | |
| 638 cpfsgt ADRESH ; - pressed? | |
| 639 bra get_analog_switches_3 ; NO - continue with other button | |
| 640 bra get_analog_switches_sw2_pressed ; YES - (left button normal) | |
| 623 | 641 |
| 642 get_analog_switches_sw2_inv: | |
| 634 | 643 subwf analog_sw2,W ; subtract average (~128) |
| 623 | 644 cpfslt ADRESH ; pressed? |
| 634 | 645 bra get_analog_switches_3 ; NO - continue with other button |
| 646 ;bra get_analog_switches_sw2_pressed ; YES - (left button inverted) | |
| 623 | 647 |
| 648 get_analog_switches_sw2_pressed: | |
| 649 bsf analog_sw2_pressed ; set left button as pressed | |
| 650 | |
| 651 get_analog_switches_3: | |
| 582 | 652 movlw b'00101001' ; power on ADC, select AN10 |
| 634 | 653 rcall wait_adc ; wait on ADC |
| 654 | |
| 655 movlw .250 ; set upper limit | |
| 656 cpfslt ADRESH ; above upper limit? | |
| 657 movff analog_sw1,ADRESH ; YES - use (last) average instead | |
| 658 | |
| 659 movf ADRESH,W ; get ADC result (high byte) | |
| 660 addwf analog_sw1_raw+0 ; add to averaging register | |
| 661 movlw .0 ; ... | |
| 662 addwfc analog_sw1_raw+1 ; ... | |
| 582 | 663 tstfsz analog_counter ; continue averaging? |
| 623 | 664 bra get_analog_switches_3a ; YES |
| 634 | 665 |
| 666 ; compute average - divide by 16 | |
| 667 swapf analog_sw1_raw+1,F ; swap nibbles in high byte | |
| 668 movlw b'11110000' ; keep only upper nibble | |
| 669 andwf analog_sw1_raw+1,W ; ... | |
| 670 movwf analog_sw1 ; copy ex lower nibble of high byte to upper nibble of result | |
| 671 swapf analog_sw1_raw+0,F ; swap nibble in low byte | |
| 672 movlw b'00001111' ; keep only lower nibble | |
| 673 andwf analog_sw1_raw+0,W ; ... | |
| 674 iorwf analog_sw1,F ; copy ex upper nibble of low byte to lower nibble of result | |
| 675 | |
| 676 clrf analog_sw1_raw+1 ; reset average registers | |
| 677 clrf analog_sw1_raw+0 ; ... | |
| 678 | |
| 679 ; set up averaging counter for next call-up | |
| 680 movlw .16 ; 16 averaging rounds | |
| 681 movwf analog_counter ; ... | |
| 623 | 682 |
| 683 get_analog_switches_3a: | |
| 684 bcf analog_sw1_pressed ; default to not pressed | |
| 604 | 685 movff opt_cR_button_right,WREG ; 20-100 |
| 634 | 686 bcf STATUS,C ; clear carry bit |
| 604 | 687 rrcf WREG ; /2 -> 10-50 |
| 634 | 688 bcf STATUS,C ; clear carry bit |
| 604 | 689 rrcf WREG ; /2 -> 5-25 |
| 690 decf WREG,W ; -1 | |
| 691 decf WREG,W ; -1 | |
| 692 decf WREG,W ; -1 -> 2-22 | |
| 693 btfss button_polarity,0 ; (1= normal, 0=inverted) | |
| 634 | 694 bra get_analog_switches_sw1_inv ; 0 |
| 695 addwf analog_sw1,W ; 1 - add to average (~128) | |
| 696 cpfsgt ADRESH ; - pressed? | |
| 697 bra get_analog_switches_4 ; NO - continue with clean-up | |
| 698 bra get_analog_switches_sw1_pressed ; YES - (right button normal) | |
| 623 | 699 |
| 700 get_analog_switches_sw1_inv: | |
| 634 | 701 subwf analog_sw1,W ; subtract from average (~128) |
| 623 | 702 cpfslt ADRESH ; pressed? |
| 634 | 703 bra get_analog_switches_4 ; NO - continue with clean-up |
| 704 ;bra get_analog_switches_sw1_pressed ; YES - (right button inverted) | |
| 623 | 705 |
| 706 get_analog_switches_sw1_pressed: | |
| 707 bsf analog_sw1_pressed ; set right button as pressed | |
| 708 | |
| 709 get_analog_switches_4: | |
| 631 | 710 bsf ADCON2,ADFM ; restore to right justified |
| 634 | 711 bcf adc_is_running ; flag ADC not in use any more |
| 712 | |
| 623 | 713 banksel common ; back to bank common |
| 634 | 714 |
| 623 | 715 btfsc analog_sw1_pressed ; right button pressed? |
| 716 return ; YES - done | |
| 717 btfsc analog_sw2_pressed ; left button pressed? | |
| 718 return ; YES - done | |
| 719 setf TMR1H ; NO to both - no button pressed, set timer1 to overflow quickly | |
| 634 | 720 setf TMR1L ; - ... |
| 721 return ; - done | |
| 448 | 722 |
| 634 | 723 ;----------------------------------------------------------------------------- |
| 724 | |
| 725 END |
