Mercurial > public > mk2
comparison code_part1/OSTC_code_asm_part1/adc_rtc.asm @ 0:96a35aeda5f2
Initial setup
| author | heinrichsweikamp |
|---|---|
| date | Tue, 12 Jan 2010 15:05:59 +0100 |
| parents | |
| children | 588a969a0cae |
comparison
equal
deleted
inserted
replaced
| -1:000000000000 | 0:96a35aeda5f2 |
|---|---|
| 1 | |
| 2 ; OSTC - diving computer code | |
| 3 ; Copyright (C) 2008 HeinrichsWeikamp GbR | |
| 4 | |
| 5 ; This program is free software: you can redistribute it and/or modify | |
| 6 ; it under the terms of the GNU General Public License as published by | |
| 7 ; the Free Software Foundation, either version 3 of the License, or | |
| 8 ; (at your option) any later version. | |
| 9 | |
| 10 ; This program is distributed in the hope that it will be useful, | |
| 11 ; but WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 12 ; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
| 13 ; GNU General Public License for more details. | |
| 14 | |
| 15 ; You should have received a copy of the GNU General Public License | |
| 16 ; along with this program. If not, see <http://www.gnu.org/licenses/>. | |
| 17 | |
| 18 | |
| 19 ; routines for AD converter, Realtime clock initialisation | |
| 20 ; written by: Matthias Heinrichs, info@heinrichsweikamp.com | |
| 21 ; written: 10/30/05 | |
| 22 ; last updated: 05/15/08 | |
| 23 ; known bugs: | |
| 24 ; ToDo: | |
| 25 | |
| 26 get_battery_voltage: ; starts ADC and waits until fnished | |
| 27 bsf ADCON0,0 ; power on ADC | |
| 28 nop | |
| 29 bsf ADCON0,1 ; start ADC | |
| 30 get_battery_voltage2: | |
| 31 btfsc ADCON0,1 ; Wait... | |
| 32 bra get_battery_voltage2 | |
| 33 | |
| 34 ; 3.3V/1024=3,2227mV Input/Bit=9,6680mV Battery/Bit. | |
| 35 ; Example: 434*9,6680mV=4195,9mV Battery. | |
| 36 | |
| 37 movff ADRESH,xA+1 | |
| 38 movff ADRESL,xA+0 | |
| 39 movlw LOW d'966' | |
| 40 movwf xB+0 | |
| 41 movlw HIGH d'966' | |
| 42 movwf xB+1 | |
| 43 call mult16x16 ; AD_Result*966 | |
| 44 movlw d'100' | |
| 45 movwf xB+0 | |
| 46 clrf xB+1 | |
| 47 call div32x16 ;xC:4 / xB:2 = xC+3:xC+2 with xC+1:xC+0 as remainder | |
| 48 movff xC+0,batt_voltage+0 ; store value | |
| 49 movff xC+1,batt_voltage+1 | |
| 50 bcf ADCON0,0 ; power off ADC | |
| 51 | |
| 52 ; Check if we should enter deep-sleep mode | |
| 53 | |
| 54 movff batt_voltage+0,sub_b+0 | |
| 55 movff batt_voltage+1,sub_b+1 | |
| 56 movlw LOW d'2600' ; must be greater then 2600mV... | |
| 57 movwf sub_a+0 | |
| 58 movlw HIGH d'2600' | |
| 59 movwf sub_a+1 | |
| 60 call sub16 ; sub_c = sub_a - sub_b | |
| 61 bcf enter_error_sleep ; Clear flag | |
| 62 btfsc neg_flag ; neg_flag=1 if eeprom40:41 < 2000 | |
| 63 bra get_battery_voltage3 ; Battery in OK range | |
| 64 | |
| 65 movlw d'2' | |
| 66 movwf fatal_error_code ; Battery very low! | |
| 67 bsf enter_error_sleep ; enter error routine | |
| 68 | |
| 69 get_battery_voltage3: | |
| 70 movff amb_pressure+0,sub_b+0 | |
| 71 movff amb_pressure+1,sub_b+1 | |
| 72 movlw LOW d'15001' ; must be lower then 15001mBar | |
| 73 movwf sub_a+0 | |
| 74 movlw HIGH d'15001' | |
| 75 movwf sub_a+1 | |
| 76 call sub16 ; sub_c = sub_a - sub_b | |
| 77 bcf enter_error_sleep ; Clear flag | |
| 78 btfss neg_flag ; | |
| 79 bra get_battery_voltage4 ; Pressure in OK range | |
| 80 | |
| 81 movlw d'3' | |
| 82 movwf fatal_error_code ; too deep | |
| 83 bsf enter_error_sleep ; enter error routine | |
| 84 ; Continue with rest of routine | |
| 85 | |
| 86 get_battery_voltage4: | |
| 87 ; check if the battery control memory needs to be initialised! | |
| 88 bcf initialize_battery1 ; clear check-flags | |
| 89 bcf initialize_battery2 | |
| 90 | |
| 91 read_int_eeprom d'40' ; get lowest battery voltage seen in mV | |
| 92 movff EEDATA,sub_b+0 | |
| 93 read_int_eeprom d'41' | |
| 94 movff EEDATA,sub_b+1 | |
| 95 | |
| 96 movlw LOW d'2000' ; must be greater then 2000mV... | |
| 97 movwf sub_a+0 | |
| 98 movlw HIGH d'2000' | |
| 99 movwf sub_a+1 | |
| 100 call sub16 ; sub_c = sub_a - sub_b | |
| 101 btfss neg_flag ; neg_flag=1 if eeprom40:41 < 2000 | |
| 102 bsf initialize_battery1 ; battery need to be initialised | |
| 103 | |
| 104 movlw LOW d'4500' ; must be lower then 4500mV... | |
| 105 movwf sub_a+0 | |
| 106 movlw HIGH d'4500' | |
| 107 movwf sub_a+1 | |
| 108 call sub16 ; sub_c = sub_a - sub_b | |
| 109 btfss neg_flag ; neg_flag=1 if eeprom40:41 < 4500 | |
| 110 bsf initialize_battery2 ; battery need to be initialised | |
| 111 | |
| 112 btfss initialize_battery1 ; battery need to be initialised? | |
| 113 bra get_battery_no_init ; No, we have already valid values, just check for new extremas | |
| 114 | |
| 115 btfss initialize_battery2 ; battery need to be initialised? | |
| 116 bra get_battery_no_init ; No, we have already valid values, just check for new extremas | |
| 117 | |
| 118 ; Init EEPROM for battery control | |
| 119 ; Reset lowest battery seen | |
| 120 movlw LOW d'4200' ; reset to 4.2V | |
| 121 movwf EEDATA | |
| 122 write_int_eeprom d'40' | |
| 123 movlw HIGH d'4200' ; reset to 4.2V | |
| 124 movwf EEDATA | |
| 125 write_int_eeprom d'41' | |
| 126 movff month,EEDATA | |
| 127 write_int_eeprom d'42' | |
| 128 movff day,EEDATA | |
| 129 write_int_eeprom d'43' | |
| 130 movff year,EEDATA | |
| 131 write_int_eeprom d'44' | |
| 132 movff temperature+0,EEDATA | |
| 133 write_int_eeprom d'45' | |
| 134 movff temperature+1,EEDATA | |
| 135 write_int_eeprom d'46' | |
| 136 ; Reset charge statistics | |
| 137 clrf EEDATA ; last complete charge | |
| 138 write_int_eeprom d'47' | |
| 139 clrf EEDATA ; last complete charge | |
| 140 write_int_eeprom d'48' | |
| 141 clrf EEDATA ; last complete charge | |
| 142 write_int_eeprom d'49' | |
| 143 clrf EEDATA ; total cycles | |
| 144 write_int_eeprom d'50' | |
| 145 clrf EEDATA ; total cycles | |
| 146 write_int_eeprom d'51' | |
| 147 clrf EEDATA ; total complete cycles | |
| 148 write_int_eeprom d'52' | |
| 149 clrf EEDATA ; total complete cycles | |
| 150 write_int_eeprom d'53' | |
| 151 ; Reset temperature extremas | |
| 152 movff temperature+0,EEDATA ; Reset mimimum extrema | |
| 153 write_int_eeprom d'54' | |
| 154 movff temperature+1,EEDATA | |
| 155 write_int_eeprom d'55' | |
| 156 movff month,EEDATA | |
| 157 write_int_eeprom d'56' | |
| 158 movff day,EEDATA | |
| 159 write_int_eeprom d'57' | |
| 160 movff year,EEDATA | |
| 161 write_int_eeprom d'58' | |
| 162 movff temperature+0,EEDATA ; Reset maximum extrema | |
| 163 write_int_eeprom d'59' | |
| 164 movff temperature+1,EEDATA | |
| 165 write_int_eeprom d'60' | |
| 166 movff month,EEDATA | |
| 167 write_int_eeprom d'61' | |
| 168 movff day,EEDATA | |
| 169 write_int_eeprom d'62' | |
| 170 movff year,EEDATA | |
| 171 write_int_eeprom d'63' | |
| 172 | |
| 173 get_battery_no_init: | |
| 174 read_int_eeprom d'40' ; get lowest battery voltage seen in mV | |
| 175 movff EEDATA,sub_b+0 | |
| 176 read_int_eeprom d'41' | |
| 177 movff EEDATA,sub_b+1 | |
| 178 movff batt_voltage+0,sub_a+0 | |
| 179 movff batt_voltage+1,sub_a+1 | |
| 180 call sub16 ; sub_c = sub_a - sub_b | |
| 181 btfss neg_flag ; new lowest battery voltage? | |
| 182 return ; no, quit routine | |
| 183 ; Yes, store new value together with the date and temperature values | |
| 184 movff batt_voltage+0,EEDATA | |
| 185 write_int_eeprom d'40' | |
| 186 movff batt_voltage+1,EEDATA | |
| 187 write_int_eeprom d'41' | |
| 188 movff month,EEDATA | |
| 189 write_int_eeprom d'42' | |
| 190 movff day,EEDATA | |
| 191 write_int_eeprom d'43' | |
| 192 movff year,EEDATA | |
| 193 write_int_eeprom d'44' | |
| 194 movff temperature+0,EEDATA | |
| 195 write_int_eeprom d'45' | |
| 196 movff temperature+1,EEDATA | |
| 197 write_int_eeprom d'46' | |
| 198 return | |
| 199 | |
| 200 RTCinit: ; resets RTC | |
| 201 movlw 0x80 | |
| 202 movwf TMR1H | |
| 203 clrf TMR1L | |
| 204 | |
| 205 ; Reset RTC if any part of the time/date is out of range | |
| 206 movlw d'60' ; Limit | |
| 207 cpfslt secs ; Check part | |
| 208 bra RTCinit2 ; Reset time... | |
| 209 movlw d'60' ; Limit | |
| 210 cpfslt mins ; Check part | |
| 211 bra RTCinit2 ; Reset time... | |
| 212 movlw d'24' ; Limit | |
| 213 cpfslt hours ; Check part | |
| 214 bra RTCinit2 ; Reset time... | |
| 215 movlw d'32' ; Limit | |
| 216 cpfslt day ; Check part | |
| 217 bra RTCinit2 ; Reset time... | |
| 218 movlw d'12' ; Limit | |
| 219 cpfslt month ; Check part | |
| 220 bra RTCinit2 ; Reset time... | |
| 221 movlw d'100' ; Limit | |
| 222 cpfslt year ; Check part | |
| 223 bra RTCinit2 ; Reset time... | |
| 224 | |
| 225 bsf PIE1, TMR1IE | |
| 226 return | |
| 227 | |
| 228 RTCinit2: | |
| 229 movlw .00 | |
| 230 movwf secs | |
| 231 movlw .00 | |
| 232 movwf mins | |
| 233 movlw .12 | |
| 234 movwf hours | |
| 235 movlw .2 | |
| 236 movwf day | |
| 237 movlw .8 | |
| 238 movwf month | |
| 239 movlw .09 | |
| 240 movwf year | |
| 241 bsf PIE1, TMR1IE | |
| 242 return |
