1 ///////////////////////////////////////////////////////////////////////////////
\r
3 // IAR Atmel AVR C/C++ Compiler V4.30F/W32 13/Mar/2008 04:49:35 /
\r
4 // Copyright 1996-2007 IAR Systems. All rights reserved. /
\r
6 // Source file = C:\home\kevin\pub\src\bc100\IAR\ADC.c /
\r
7 // Command line = C:\home\kevin\pub\src\bc100\IAR\ADC.c /
\r
8 // --cpu=tiny861 -ms -o C:\home\kevin\pub\src\bc1 /
\r
9 // 00\IAR\Release\Obj\ -D NDEBUG -lCN /
\r
10 // C:\home\kevin\pub\src\bc100\IAR\Release\List\ /
\r
11 // -lB C:\home\kevin\pub\src\bc100\IAR\Release\Li /
\r
12 // st\ --initializers_in_flash -s9 /
\r
13 // --no_cross_call --no_tbaa /
\r
14 // -DENABLE_BIT_DEFINITIONS -e -I "C:\Program /
\r
15 // Files\IAR Systems\Embedded Workbench /
\r
16 // 4.0\avr\INC\" -I "C:\Program Files\IAR /
\r
17 // Systems\Embedded Workbench 4.0\avr\INC\CLIB\" /
\r
18 // --eeprom_size 512 --misrac=5-9,11-12,14,16-17, /
\r
19 // 19-21,24-26,29-32,34-35,38-39,42-43,46,50, /
\r
20 // 52-54,56-59,61-62,64-65,68-80,83-84,87-91, /
\r
21 // 94-95,98-100,103-110,112-126 /
\r
22 // Enabled MISRA C rules = 5-9,11-12,14,16-17,19-21,24-26,29-32,34-35, /
\r
23 // 38-39,42-43,46,50,52-54,56-59,61-62,64-65, /
\r
24 // 68-80,83-84,87-91,94-95,98-100,103-110,112-126 /
\r
25 // Checked = 5,7-9,11-12,14,17,19-21,24,29-32,34-35,38-39, /
\r
26 // 42,46,50,52-54,56-59,61-62,64,68-69,71-80, /
\r
27 // 83-84,87-89,91,94-95,98,100,104-105,108-109, /
\r
28 // 112-115,118-126 /
\r
29 // Not checked = 6,16,25-26,43,65,70,90,99,103,106-107,110, /
\r
31 // List file = C:\home\kevin\pub\src\bc100\IAR\Release\List\A /
\r
35 ///////////////////////////////////////////////////////////////////////////////
\r
39 RSEG CSTACK:DATA:NOROOT(0)
\r
40 RSEG RSTACK:DATA:NOROOT(0)
\r
42 EXTERN ?Register_R4_is_cg_reg
\r
43 EXTERN ?SS_DIVMOD_L02
\r
46 EXTERN ?need_segment_init
\r
49 PUBWEAK `?<Segment init: NEAR_Z>`
\r
50 PUBWEAK `??ADC_ISR??INTVEC 22`
\r
69 ADC_ISR SYMBOL "ADC_ISR"
\r
70 `??ADC_ISR??INTVEC 22` SYMBOL "??INTVEC 22", ADC_ISR
\r
72 // C:\home\kevin\pub\src\bc100\IAR\ADC.c
\r
73 // 1 /* This file has been prepared for Doxygen automatic documentation generation.*/
\r
74 // 2 /*! \file *********************************************************************
\r
77 // 5 * Functions for use of ADC
\r
79 // 7 * Contains high level functions for initializing the ADC, interrupt
\r
80 // 8 * handling, and treatment of samples.\n
\r
81 // 9 * The ADC is set to free running mode and uses an external reference
\r
83 // 11 * To make all sampling take at least 25 clock cycles the ADC is stopped
\r
84 // 12 * and restarted by the ISR.
\r
86 // 14 * \par Application note:
\r
87 // 15 * AVR458: Charging Li-Ion Batteries with BC100 \n
\r
88 // 16 * AVR463: Charging NiMH Batteries with BC100
\r
90 // 18 * \par Documentation:
\r
91 // 19 * For comprehensive code documentation, supported compilers, compiler
\r
92 // 20 * settings and supported devices see readme.html
\r
95 // 23 * Atmel Corporation: http://www.atmel.com \n
\r
96 // 24 * Support email: avr@atmel.com \n
\r
97 // 25 * Original author: \n
\r
100 // 28 * $Revision: 2299 $
\r
102 // 30 * $URL: http://svn.norway.atmel.com/AppsAVR8/avr458_Charging_Li-Ion_Batteries_with_BC100/tag/20070904_release_1.0/code/IAR/ADC.c $
\r
103 // 31 * $Date: 2007-08-23 12:55:51 +0200 (to, 23 aug 2007) $\n
\r
104 // 32 ******************************************************************************/
\r
106 // 34 #include <ioavr.h>
\r
108 ASEGN ABSOLUTE:DATA:NOROOT,03bH
\r
109 // <unnamed> volatile __io _A_PORTA
\r
113 ASEGN ABSOLUTE:DATA:NOROOT,03aH
\r
114 // <unnamed> volatile __io _A_DDRA
\r
118 ASEGN ABSOLUTE:DATA:NOROOT,027H
\r
119 // <unnamed> volatile __io _A_ADMUX
\r
123 ASEGN ABSOLUTE:DATA:NOROOT,026H
\r
124 // <unnamed> volatile __io _A_ADCSRA
\r
128 ASEGN ABSOLUTE:DATA:NOROOT,024H
\r
129 // <unnamed> volatile __io _A_ADC
\r
133 ASEGN ABSOLUTE:DATA:NOROOT,023H
\r
134 // <unnamed> volatile __io _A_ADCSRB
\r
137 // 35 #include <inavr.h>
\r
139 // 37 #include "structs.h"
\r
141 // 39 #include "main.h"
\r
142 // 40 #include "ADC.h"
\r
145 // 43 //******************************************************************************
\r
147 // 45 //******************************************************************************
\r
148 // 46 // ADC status struct.
\r
149 // 47 //! \brief Holds sampled data and ADC-status
\r
150 // 48 ADC_Status_t ADCS;
\r
153 // 51 /*! \brief Indicates maximum battery voltage.
\r
155 // 53 * This variable is stored in EEPROM and indicates how much the battery voltage
\r
156 // 54 * is downscaled by HW before it is sampled. The amount of downscaling depends
\r
157 // 55 * on the maximum battery voltage, and is necessary to avoid saturation of the
\r
158 // 56 * ADC (reference voltage is 2.5 V).
\r
160 // 58 * \note Used by the ADC ISR when calling ScaleU() and ScaleI().
\r
162 // 60 * \note Defaults to 1, which means 10 V max battery voltage.
\r
164 // 62 * \note Table of settings:
\r
166 // 64 * VBAT_RANGE | Max battery voltage | Jumper setting
\r
167 // 65 * 0 | 5V | 1/2
\r
168 // 66 * 1 | 10V | 1/4
\r
169 // 67 * 2 | 20V | 1/8
\r
170 // 68 * 3 | 30V | 1/12
\r
171 // 69 * 4 | 40V | 1/16
\r
174 // 72 // Maximum battery voltage (affects scaling of samples).
\r
176 RSEG EEPROM_I:XDATA:NOROOT(0)
\r
177 // 73 __eeprom unsigned char VBAT_RANGE = 1;
\r
182 // 76 //******************************************************************************
\r
184 // 78 //******************************************************************************
\r
185 // 79 /*! \brief Interrupt Service routine for ADC.
\r
187 // 81 * This ISR stores the sampled values in the ADC status-struct, then
\r
188 // 82 * updates the ADC MUX to the next channel in the scanning-sequence.\n
\r
189 // 83 * Once the sequence is completed, ADCS.Flag is set and unless
\r
190 // 84 * ADCS.Halt has been set, the sequence starts over. Otherwise, the ADC
\r
191 // 85 * is disabled.\n
\r
192 // 86 * If the mains voltage is below minimum, ADCS.Mains gets set to FALSE.
\r
194 // 88 * \note Table of scanning sequence:
\r
196 // 90 * Seq | MUX | pos I/P | neg I/P | gain | measure | signed
\r
197 // 91 * ----+--------+----------+----------+------+---------+-------
\r
198 // 92 * 01 | 000001 | ADC1/PA1 | n/a | 1x | NTC | no
\r
199 // 93 * 02 | 000010 | ADC2/PA2 | n/a | 1x | RID | no
\r
200 // 94 * 03 | 000011 | ADC3/PA4 | n/a | 1x | VIN- | no
\r
201 // 95 * 04 | 000100 | ADC4/PA5 | n/a | 1x | VIN+ | no
\r
202 // 96 * 05 | 000101 | ADC5/PA6 | n/a | 1x | VBAT- | no
\r
203 // 97 * 06 | 000110 | ADC6/PA7 | n/a | 1x | VBAT+ | no
\r
204 // 98 * 07 | 010010 | ADC4/PA5 | ADC3/PA4 | 20x | IIN | no
\r
205 // 99 * 08 | 010111 | ADC6/PA7 | ADC5/PA6 | 20x | IBAT | yes
\r
208 // 102 * \todo IIN (#7 in sequence) is never used.
\r
210 // 104 * \todo Signed is never set. Signed measurements of IBAT will halve the
\r
211 // 105 * measuring sensitivity, and is therefore not favourable. At the moment,
\r
212 // 106 * great currents (f.ex. if something happens with the battery) will be
\r
213 // 107 * interpreted as negative, which might cause unfavourable behaviour during
\r
214 // 108 * charging (depending on what PWM behaviour is defined), f.ex.
\r
215 // 109 * ConstantCurrent() will keep increasing the PWM output. This results in an
\r
216 // 110 * PWM controller error being flagged and the program going into
\r
217 // 111 * error-state and eventually reinitializing.
\r
219 // 113 #pragma vector=ADC_vect
\r
221 RSEG CODE:CODE:NOROOT(1)
\r
222 // 114 __interrupt void ADC_ISR(void)
\r
245 REQUIRE ?Register_R4_is_cg_reg
\r
246 // 116 static unsigned char avgIndex = 0;
\r
247 // 117 unsigned char i, Next, Signed;
\r
248 // 118 signed int temp = 0;
\r
252 // 120 Signed = FALSE; // Presume next conversion is unipolar.
\r
253 // 121 ADCSRA &= ~(1<<ADEN); // Stop conversion before handling. This makes all
\r
255 // 122 // conversions take at least 25 ADCCLK. (It is restarted later)
\r
257 // 124 // Handle the conversion, depending on what channel it is from, then
\r
258 // 125 // switch to the next channel in the sequence.
\r
259 // 126 switch (ADCS.MUX){
\r
261 LDI R27, (ADCS) >> 8
\r
275 // 127 // MUX = 0b000001 => ADC1 (PA1) = NTC
\r
277 // 129 ADCS.rawNTC = ADC;
\r
281 MOVW R31:R30, R27:R26
\r
290 // 134 // MUX = 0b000010 => ADC2 (PA2) = RID
\r
292 // 136 ADCS.rawRID = ADC;
\r
296 MOVW R31:R30, R27:R26
\r
305 // 141 // MUX = 0b000011 => ADC3 (PA4) = VIN-
\r
307 // 143 // Supply voltage is always divided by 16.
\r
308 // 144 ADCS.VIN = ScaleU(4, (unsigned int)ADC); // Cast because ADC is short.
\r
314 MOVW R31:R30, R27:R26
\r
318 // 146 // Is mains failing?
\r
319 // 147 if (ADCS.VIN < VIN_MIN) {
\r
324 // 148 ADCS.Mains = FALSE;
\r
328 // 150 ADCS.Mains = TRUE;
\r
341 // 157 // MUX = 0b000101 => ADC5 (PA6) = VBAT-
\r
343 // 159 ADCS.rawVBAT = ADC;
\r
347 MOVW R31:R30, R27:R26
\r
351 // 161 // Scale voltage according to jumper setting.
\r
352 // 162 ADCS.VBAT = ScaleU(VBAT_RANGE, (unsigned int)ADC); // ADC is a short.
\r
355 LDI R20, LOW(VBAT_RANGE)
\r
356 LDI R21, (VBAT_RANGE) >> 8
\r
359 MOVW R31:R30, R27:R26
\r
365 // 164 // Signed = TRUE; // Next conversion is bipolar. Halves sensitivity!
\r
369 // 168 case 0x17: // MUX = 0b010111 => 20 x [ADC6(PA7) - ADC5(PA6)] = IBAT
\r
370 // 169 // If bipolar, from -512 to 0, to 511:
\r
371 // 170 // 0x200 ... 0x3ff, 0x000, 0x001 ... 0x1FF
\r
373 // 172 // Scale sample according to jumper setting, handle negative numbers.
\r
374 // 173 if (ADC > 511) {
\r
380 // 174 ADCS.IBAT = -(signed int)ScaleI(VBAT_RANGE,
\r
381 // 175 (1024 - (ADC-ADCS.ADC5_G20_OS)));
\r
388 MOVW R31:R30, R27:R26
\r
395 LDI R20, LOW(VBAT_RANGE)
\r
396 LDI R21, (VBAT_RANGE) >> 8
\r
403 MOVW R31:R30, R27:R26
\r
407 // 176 } else if (ADC > 0) {
\r
413 // 177 ADCS.IBAT = ScaleI(VBAT_RANGE, (ADC-ADCS.ADC5_G20_OS));
\r
416 MOVW R31:R30, R27:R26
\r
422 LDI R20, LOW(VBAT_RANGE)
\r
423 LDI R21, (VBAT_RANGE) >> 8
\r
428 // 179 ADCS.IBAT = 0;
\r
430 MOVW R31:R30, R27:R26
\r
435 // 182 // Insert sample of battery current into the averaging-array
\r
436 // 183 // (overwriting the oldest sample), then recalculate and store the
\r
437 // 184 // average. This is the last conversion in the sequence, so
\r
438 // 185 // flag a complete ADC-cycle and restart sequence.
\r
439 // 186 ADCS.discIBAT[(avgIndex++ & 0x03)] = ADCS.IBAT;
\r
450 MOVW R31:R30, R27:R26
\r
454 // 187 for (i = 0; i < 4 ; i++) {
\r
457 // 188 temp += ADCS.discIBAT[i];
\r
467 // 191 ADCS.avgIBAT = (temp / 4);
\r
468 MOVW R17:R16, R25:R24
\r
471 RCALL ?SS_DIVMOD_L02
\r
472 MOVW R31:R30, R27:R26
\r
476 // 193 ADCS.Flag = TRUE;
\r
481 // 195 Signed = FALSE; // This is the only bipolar conversion.
\r
485 // 199 default: // Should not happen. (Invalid MUX-channel)
\r
486 // 200 Next=0x01; // Start at the beginning of sequence.
\r
492 // 204 // Update MUX to next channel in sequence, set a bipolar conversion if
\r
493 // 205 // this has been flagged.
\r
494 // 206 ADCS.MUX = Next;
\r
501 // 207 ADMUX = (1<<REFS0) + ADCS.MUX;
\r
506 // 209 if (Signed) {
\r
507 // 210 ADCSRB |= (1<<BIN);
\r
509 // 212 ADCSRB &= ~(1<<BIN);
\r
513 // 215 // Re-enable the ADC unless a halt has been flagged and a conversion
\r
514 // 216 // cycle has completed.
\r
515 // 217 if (!((ADCS.Halt) && (ADCS.Flag))) {
\r
520 // 218 ADCSRA |= (1<<ADEN)|(1<<ADSC);
\r
553 RSEG NEAR_Z:DATA:NOROOT(0)
\r
554 REQUIRE `?<Segment init: NEAR_Z>`
\r
560 // 223 /*! \brief Scales sample to represent "actual voltage" in mV.
\r
562 // 225 * This function returns the actual sampled voltage, scaled according
\r
563 // 226 * to the jumper settings.
\r
565 // 228 * \param setting Indicates what downscaling was used.
\r
566 // 229 * \param data The sampled value.
\r
568 // 231 * \note Table for setting-parameter:\n
\r
570 // 233 * Presume VREF = 2.5V and Gain = 1x.
\r
571 // 234 * => Resolution @ 1/1 = 2.5V / 1024 = 2.4414 mV/LSB
\r
572 // 235 * setting | source | R1 | R2/(R1+R2) | UADC(LSB) | U(MAX)
\r
573 // 236 * --------+--------+------+------------+-----------+-------
\r
574 // 237 * N/A | | - | - | 2.441mV | 2.50V
\r
575 // 238 * 0 | VBAT | 10k | 1/2 | 4.883mV | 5.00V
\r
576 // 239 * 1 | VBAT | 30k | 1/4 | 9.766mV | 9.99V
\r
577 // 240 * 2 | VBAT | 70k | 1/8 | 19.53mV | 19.98V
\r
578 // 241 * 3 | VBAT | 110k | 1/12 | 29.30mV | 29.97V
\r
579 // 242 * 4 | VBAT | 150k | 1/16 | 39.06mV | 39.96V
\r
580 // 243 * 4 | VIN | 150k | 1/16 | 39.06mV | 39.96V
\r
584 RSEG CODE:CODE:NOROOT(1)
\r
585 // 246 unsigned int ScaleU(unsigned char setting, unsigned int data)
\r
589 MOVW R23:R22, R25:R24
\r
591 MOVW R25:R24, R19:R18
\r
592 // 248 // Temporary variable needed.
\r
593 // 249 unsigned int scaled = 0;
\r
595 // 251 // Jumper setting 3: mV/LSB = 29.30 ~= 29 + 1/4 + 1/16
\r
596 // 252 if (setting == 3) {
\r
598 MOVW R21:R20, R19:R18
\r
600 // 253 scaled = 29 * data;
\r
601 // 254 scaled += (data >> 2);
\r
602 // 255 scaled += (data >> 4);
\r
624 // 257 // Jumper setting 4: mV/LSB = 39.06 ~= 39 + 1/16
\r
625 // 258 scaled = 39 * data;
\r
626 // 259 scaled += (data >> 4);
\r
642 // 261 if (setting <3) {
\r
645 // 262 // Jumper setting 0: mV/LSB = 4.883 = 39.06 / 8
\r
646 // 263 // 1: mV/LSB = 9.766 = 39.06 / 4
\r
647 // 264 // 2: mV/LSB = 19.53 = 39.06 / 2
\r
648 // 265 scaled = (scaled >> (3-setting));
\r
649 MOVW R17:R16, R25:R24
\r
653 MOVW R25:R24, R17:R16
\r
657 // 269 return(scaled);
\r
659 MOVW R17:R16, R25:R24
\r
660 MOVW R25:R24, R23:R22
\r
666 // 273 /*! \brief Scales sample to represent "actual current" in mA.
\r
668 // 275 * This function returns the actual sampled current, scaled according
\r
669 // 276 * to the jumper settings.
\r
671 // 278 * \param setting Indicates what downscaling was used.
\r
672 // 279 * \param data The sampled value.
\r
674 // 281 * \note Table for setting-parameter:\n
\r
676 // 283 * Presume VREF = 2.5V and Gain = 1x or 20x.
\r
677 // 284 * => Resolution(U) @ (1/1 and 20x) = 2.5V / (GAIN x 1024) = 0.1221 mV/LSB
\r
678 // 285 * => Resolution(I) = Resolution(U) / Rshunt = Resolution(U) / 0.07
\r
679 // 286 * Setting | R1 | R2/(R1+R2) | U(LSB) | I(LSB) | I(MAX) | Gain
\r
680 // 287 * --------+------+------------+----------+----------+--------+-----
\r
681 // 288 * N/A | - | - | 0.1221mV | 1.744mA | 1.78A | 20x
\r
682 // 289 * 0 | 10k | 1/2 | 0.2442mV | 3.489mA | 3.57A | 20x
\r
683 // 290 * 1 | 30k | 1/4 | 0.4884mV | 6.978mA | 7.14A | 20x
\r
684 // 291 * 2 | 70k | 1/8 | 0.9768mV | 13.955mA | 14.3A | 20x
\r
685 // 292 * 3 | 110k | 1/12 | 1.4652mV | 20.931mA | 21.4A | 20x
\r
686 // 293 * 4 | 150k | 1/16 | 1.9536mV | 27.909mA | 28.5A | 20x
\r
687 // 294 * 5 | 10k | 1/2 | 2.4414mV | 34.877mA | 35.7A | 1x
\r
691 RSEG CODE:CODE:NOROOT(1)
\r
692 // 297 unsigned int ScaleI(unsigned char setting, unsigned int data)
\r
696 MOVW R23:R22, R25:R24
\r
698 MOVW R25:R24, R19:R18
\r
699 // 299 // Temporary variable needed.
\r
700 // 300 unsigned int scaled = 0;
\r
702 // 302 // Jumper setting 3: mA/LSB = 20.931mA ~= 21 - 1/16 + 1/128
\r
703 // 303 if (setting == 3) {
\r
705 MOVW R21:R20, R19:R18
\r
707 // 304 scaled = 21 * data;
\r
708 // 305 scaled -= (data >> 4);
\r
709 // 306 scaled += (data >> 7);
\r
731 // 307 } else { // Jumper setting 4: mA/LSB = 27.909mA ~= 28 - 1/8 + 1/32
\r
732 // 308 scaled = 28 * data;
\r
733 // 309 scaled -= (data >> 3);
\r
734 // 310 scaled += (data >> 5);
\r
739 MOVW R19:R18, R17:R16
\r
740 MOVW R17:R16, R25:R24
\r
749 MOVW R17:R16, R25:R24
\r
752 MOVW R25:R24, R17:R16
\r
756 // 312 if (setting <3) {
\r
759 // 313 // Jumper setting 0: mA/LSB = 3.489mA = 27.909 / 8
\r
760 // 314 // 1: mA/LSB = 6.978mA = 27.909 / 4
\r
761 // 315 // 2: mA/LSB = 13.955mA = 27.909 / 2
\r
762 // 316 scaled = (scaled >> (3-setting));
\r
763 MOVW R17:R16, R25:R24
\r
767 MOVW R25:R24, R17:R16
\r
771 // 320 return(scaled);
\r
773 MOVW R17:R16, R25:R24
\r
774 MOVW R25:R24, R23:R22
\r
780 // 324 /*! \brief Waits for two full cycles of ADC-conversions to occur.
\r
782 // 326 * This function clears the cycle complete-flag, then waits for it to be set
\r
783 // 327 * again. This is then repeated once before the function exits.
\r
787 RSEG CODE:CODE:NOROOT(1)
\r
788 // 330 void ADC_Wait(void)
\r
792 // 332 // Clear ADC flag and wait for cycle to complete.
\r
793 // 333 ADCS.Flag = FALSE;
\r
795 // 335 } while (ADCS.Flag == FALSE);
\r
798 // 337 // Repeat, so we are sure the data beong to the same cycle.
\r
799 // 338 ADCS.Flag = FALSE;
\r
801 // 340 } while (ADCS.Flag == FALSE);
\r
805 // 344 /*! \brief Initializes ADC and input pins.
\r
807 // 346 * This function initializes the ADC to free running mode, sampling from
\r
808 // 347 * PA1/2/4/5/6/7, and using an external reference voltage (PA3).\n
\r
809 // 348 * It also measures and stores calibration data for offset.
\r
811 // 350 * \todo Odd offset measurement for ADC3_G20_OS? It is never used anyway.
\r
813 // 352 * \note Table of MUX settings for offset measurement:
\r
815 // 354 * Ch | Pin | Gain | MUX
\r
816 // 355 * ------+-----+---------+-------
\r
817 // 356 * ADC1 | PA1 | 20x | 001101
\r
818 // 357 * ADC3 | PA4 | 20x | 010001
\r
819 // 358 * ADC5 | PA6 | 20x | 010110
\r
820 // 359 * ADC9 | PB6 | 20x | 011011
\r
821 // 360 * ADC0 | PA0 | 20x/32x | 111000
\r
822 // 361 * ADC0 | PA0 | 1x/8x | 111001
\r
823 // 362 * ADC1 | PA1 | 20x/32x | 111010
\r
824 // 363 * ADC2 | PA2 | 20x/32x | 111011
\r
825 // 364 * ADC4 | PA5 | 20x/32x | 111100
\r
826 // 365 * ADC5 | PA6 | 20x/32x | 111101
\r
827 // 366 * ADC6 | PA7 | 20x/32x | 111110
\r
831 RSEG CODE:CODE:NOROOT(1)
\r
832 // 369 void ADC_Init(void)
\r
835 // 371 unsigned char i;
\r
837 // 373 __disable_interrupt();
\r
840 // 375 ADCS.Halt = FALSE; // Enable consecutive runs of ADC.
\r
842 LDI R31, (ADCS) >> 8
\r
847 // 377 // Configure ADC pins (inputs and disabled pull-ups).
\r
848 // 378 DDRA &= ~((1<<PA1)|(1<<PA2)|(1<<PA4)|(1<<PA5)|(1<<PA6)|(1<<PA7));
\r
852 // 379 PORTA &= ~((1<<PA1)|(1<<PA2)|(1<<PA4)|(1<<PA5)|(1<<PA6)|(1<<PA7));
\r
857 // 381 // Set ADC3 as reference, and MUX to measure the same pin.
\r
858 // 382 ADMUX = (1<<REFS0) | (1<<MUX0) | (1<<MUX1);
\r
866 // 386 // Start conversion, no interrupt (disable ADC-ISR).
\r
867 // 387 ADCSRA = (1<<ADEN) | (1<<ADSC) | ADC_PRESCALER;
\r
871 // 389 do { // Wait for conversion to finish.
\r
872 // 390 } while (!(ADCSRA & (1<<ADIF)));
\r
877 // 392 ADCSRA |= (1<<ADIF); // Clear ADC interrupt flag manually.
\r
880 // 394 ADCS.ADC3_G20_OS = ADC; // Save the sampled offset.
\r
889 // 396 ADMUX = (1<<REFS0) | 0x16; // ADC5/ADC5 (external ref.), 20x
\r
893 // 398 // Start conversion, no interrupt. ADC_PRESCALER is defined in ADC.h.
\r
894 // 399 ADCSRA = (1<<ADEN) | (1<<ADSC) | ADC_PRESCALER;
\r
898 // 401 do { // Wait for conversion to finish.
\r
899 // 402 } while (!(ADCSRA & (1<<ADIF)));
\r
904 // 404 ADCSRA |= (1<<ADIF); // Clear ADC interrupt flag.
\r
907 // 406 ADCS.ADC5_G20_OS = ADC; // Save the sampled offset.
\r
916 // 408 // Reset the ADC-cycle.
\r
917 // 409 ADCS.Flag = FALSE;
\r
918 // 410 ADCS.MUX = 0x01;
\r
923 // 411 ADMUX = (1<<REFS0) | ADCS.MUX;
\r
927 // 413 // Clear averaged battery current and the discrete readings.
\r
928 // 414 ADCS.avgIBAT = 0;
\r
933 // 416 for (i = 0; i < 4; i++) {
\r
936 // 417 ADCS.discIBAT[i] = 0;
\r
944 // 420 // Re-enable the ADC and ISR.
\r
945 // 421 ADCSRA=(1<<ADEN)|(1<<ADSC)|(1<<ADIE)|ADC_PRESCALER;
\r
949 // 423 __enable_interrupt();
\r
952 // 425 // Get a complete cycle of data before returning.
\r
964 ASEGN ABSOLUTE:DATA:NOROOT,01cH
\r
967 ASEGN ABSOLUTE:DATA:NOROOT,01dH
\r
970 ASEGN ABSOLUTE:DATA:NOROOT,01eH
\r
973 ASEGN ABSOLUTE:DATA:NOROOT,01fH
\r
976 COMMON INTVEC:CODE:ROOT(1)
\r
978 `??ADC_ISR??INTVEC 22`:
\r
981 RSEG INITTAB:CODE:NOROOT(0)
\r
982 `?<Segment init: NEAR_Z>`:
\r
983 DW SFE(NEAR_Z) - SFB(NEAR_Z)
\r
986 REQUIRE ?need_segment_init
\r
990 // 7 bytes in segment ABSOLUTE
\r
991 // 746 bytes in segment CODE
\r
992 // 1 byte in segment EEPROM_I
\r
993 // 6 bytes in segment INITTAB
\r
994 // 2 bytes in segment INTVEC
\r
995 // 25 bytes in segment NEAR_Z
\r
997 // 746 bytes of CODE memory (+ 8 bytes shared)
\r
998 // 25 bytes of DATA memory (+ 7 bytes shared)
\r
999 // 1 byte of XDATA memory
\r