1 /* This file has been prepared for Doxygen automatic documentation generation.*/
\r
2 /*! \file *********************************************************************
\r
5 * Functions for use of ADC
\r
7 * Contains high level functions for initializing the ADC, interrupt
\r
8 * handling, and treatment of samples.\n
\r
9 * The ADC is set to free running mode and uses an external reference
\r
11 * To make all sampling take at least 25 clock cycles the ADC is stopped
\r
12 * and restarted by the ISR.
\r
14 * \par Application note:
\r
15 * AVR458: Charging Li-Ion Batteries with BC100 \n
\r
16 * AVR463: Charging NiMH Batteries with BC100
\r
18 * \par Documentation:
\r
19 * For comprehensive code documentation, supported compilers, compiler
\r
20 * settings and supported devices see readme.html
\r
23 * Atmel Corporation: http://www.atmel.com \n
\r
24 * Support email: avr@atmel.com \n
\r
25 * Original author: \n
\r
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
31 * $Date: 2007-08-23 12:55:51 +0200 (to, 23 aug 2007) $\n
\r
32 ******************************************************************************/
\r
35 #include <avr/interrupt.h>
\r
37 #include "structs.h"
\r
43 //******************************************************************************
\r
45 //******************************************************************************
\r
46 // ADC status struct.
\r
47 //! \brief Holds sampled data and ADC-status
\r
48 volatile ADC_Status_t ADCS;
\r
51 /*! \brief Indicates maximum battery voltage.
\r
53 * This variable is stored in EEPROM and indicates how much the battery voltage
\r
54 * is downscaled by HW before it is sampled. The amount of downscaling depends
\r
55 * on the maximum battery voltage, and is necessary to avoid saturation of the
\r
56 * ADC (reference voltage is 2.5 V).
\r
58 * \note Used by the ADC ISR when calling ScaleU() and ScaleI().
\r
60 * \note Defaults to 1, which means 10 V max battery voltage.
\r
62 * \note Table of settings:
\r
64 * VBAT_RANGE | Max battery voltage | Jumper setting
\r
72 // Maximum battery voltage (affects scaling of samples).
\r
73 unsigned char EEMEM VBAT_RANGE = 1;
\r
76 //******************************************************************************
\r
78 //******************************************************************************
\r
79 /*! \brief Interrupt Service routine for ADC.
\r
81 * This ISR stores the sampled values in the ADC status-struct, then
\r
82 * updates the ADC MUX to the next channel in the scanning-sequence.\n
\r
83 * Once the sequence is completed, ADCS.Flag is set and unless
\r
84 * ADCS.Halt has been set, the sequence starts over. Otherwise, the ADC
\r
86 * If the mains voltage is below minimum, ADCS.Mains gets set to FALSE.
\r
88 * \note Table of scanning sequence:
\r
90 * Seq | MUX | pos I/P | neg I/P | gain | measure | signed
\r
91 * ----+--------+----------+----------+------+---------+-------
\r
92 * 01 | 000001 | ADC1/PA1 | n/a | 1x | NTC | no
\r
93 * 02 | 000010 | ADC2/PA2 | n/a | 1x | RID | no
\r
94 * 03 | 000011 | ADC3/PA4 | n/a | 1x | VIN- | no
\r
95 * 04 | 000100 | ADC4/PA5 | n/a | 1x | VIN+ | no
\r
96 * 05 | 000101 | ADC5/PA6 | n/a | 1x | VBAT- | no
\r
97 * 06 | 000110 | ADC6/PA7 | n/a | 1x | VBAT+ | no
\r
98 * 07 | 010010 | ADC4/PA5 | ADC3/PA4 | 20x | IIN | no
\r
99 * 08 | 010111 | ADC6/PA7 | ADC5/PA6 | 20x | IBAT | yes
\r
102 * \todo IIN (#7 in sequence) is never used.
\r
104 * \todo Signed is never set. Signed measurements of IBAT will halve the
\r
105 * measuring sensitivity, and is therefore not favourable. At the moment,
\r
106 * great currents (f.ex. if something happens with the battery) will be
\r
107 * interpreted as negative, which might cause unfavourable behaviour during
\r
108 * charging (depending on what PWM behaviour is defined), f.ex.
\r
109 * ConstantCurrent() will keep increasing the PWM output. This results in an
\r
110 * PWM controller error being flagged and the program going into
\r
111 * error-state and eventually reinitializing.
\r
115 static unsigned char avgIndex = 0;
\r
116 unsigned char i, Next, Signed;
\r
117 signed int temp = 0;
\r
119 Signed = FALSE; // Presume next conversion is unipolar.
\r
120 ADCSRA &= ~(1<<ADEN); // Stop conversion before handling. This makes all
\r
121 // conversions take at least 25 ADCCLK. (It is restarted later)
\r
123 // Handle the conversion, depending on what channel it is from, then
\r
124 // switch to the next channel in the sequence.
\r
126 // MUX = 0b000001 => ADC1 (PA1) = NTC
\r
133 // MUX = 0b000010 => ADC2 (PA2) = RID
\r
140 // MUX = 0b000011 => ADC3 (PA4) = VIN-
\r
142 // Supply voltage is always divided by 16.
\r
143 ADCS.VIN = ScaleU(4, (unsigned int)ADC); // Cast because ADC is short.
\r
145 // Is mains failing?
\r
146 if (ADCS.VIN < VIN_MIN) {
\r
147 ADCS.Mains = FALSE;
\r
156 // MUX = 0b000101 => ADC5 (PA6) = VBAT-
\r
158 ADCS.rawVBAT = ADC;
\r
160 // Scale voltage according to jumper setting.
\r
161 ADCS.VBAT = ScaleU(eeprom_read_byte(&VBAT_RANGE), (unsigned int)ADC); // ADC is a short.
\r
163 // Signed = TRUE; // Next conversion is bipolar. Halves sensitivity!
\r
167 case 0x17: // MUX = 0b010111 => 20 x [ADC6(PA7) - ADC5(PA6)] = IBAT
\r
168 // If bipolar, from -512 to 0, to 511:
\r
169 // 0x200 ... 0x3ff, 0x000, 0x001 ... 0x1FF
\r
171 // Scale sample according to jumper setting, handle negative numbers.
\r
173 ADCS.IBAT = -(signed int)ScaleI(eeprom_read_byte(&VBAT_RANGE),
\r
174 (1024 - (ADC-ADCS.ADC5_G20_OS)));
\r
175 } else if (ADC > 0) {
\r
176 ADCS.IBAT = ScaleI(eeprom_read_byte(&VBAT_RANGE), (ADC-ADCS.ADC5_G20_OS));
\r
181 // Insert sample of battery current into the averaging-array
\r
182 // (overwriting the oldest sample), then recalculate and store the
\r
183 // average. This is the last conversion in the sequence, so
\r
184 // flag a complete ADC-cycle and restart sequence.
\r
185 ADCS.discIBAT[(avgIndex++ & 0x03)] = ADCS.IBAT;
\r
186 for (i = 0; i < 4 ; i++) {
\r
187 temp += ADCS.discIBAT[i];
\r
190 ADCS.avgIBAT = (temp / 4);
\r
194 Signed = FALSE; // This is the only bipolar conversion.
\r
198 default: // Should not happen. (Invalid MUX-channel)
\r
199 Next=0x01; // Start at the beginning of sequence.
\r
203 // Update MUX to next channel in sequence, set a bipolar conversion if
\r
204 // this has been flagged.
\r
206 ADMUX = (1<<REFS0) + ADCS.MUX;
\r
209 ADCSRB |= (1<<BIN);
\r
211 ADCSRB &= ~(1<<BIN);
\r
214 // Re-enable the ADC unless a halt has been flagged and a conversion
\r
215 // cycle has completed.
\r
216 if (!((ADCS.Halt) && (ADCS.Flag))) {
\r
217 ADCSRA |= (1<<ADEN)|(1<<ADSC);
\r
222 /*! \brief Scales sample to represent "actual voltage" in mV.
\r
224 * This function returns the actual sampled voltage, scaled according
\r
225 * to the jumper settings.
\r
227 * \param setting Indicates what downscaling was used.
\r
228 * \param data The sampled value.
\r
230 * \note Table for setting-parameter:\n
\r
232 * Presume VREF = 2.5V and Gain = 1x.
\r
233 * => Resolution @ 1/1 = 2.5V / 1024 = 2.4414 mV/LSB
\r
234 * setting | source | R1 | R2/(R1+R2) | UADC(LSB) | U(MAX)
\r
235 * --------+--------+------+------------+-----------+-------
\r
236 * N/A | | - | - | 2.441mV | 2.50V
\r
237 * 0 | VBAT | 10k | 1/2 | 4.883mV | 5.00V
\r
238 * 1 | VBAT | 30k | 1/4 | 9.766mV | 9.99V
\r
239 * 2 | VBAT | 70k | 1/8 | 19.53mV | 19.98V
\r
240 * 3 | VBAT | 110k | 1/12 | 29.30mV | 29.97V
\r
241 * 4 | VBAT | 150k | 1/16 | 39.06mV | 39.96V
\r
242 * 4 | VIN | 150k | 1/16 | 39.06mV | 39.96V
\r
245 unsigned int ScaleU(unsigned char setting, unsigned int data)
\r
247 // Temporary variable needed.
\r
248 unsigned int scaled = 0;
\r
250 // Jumper setting 3: mV/LSB = 29.30 ~= 29 + 1/4 + 1/16
\r
251 if (setting == 3) {
\r
252 scaled = 29 * data;
\r
253 scaled += (data >> 2);
\r
254 scaled += (data >> 4);
\r
256 // Jumper setting 4: mV/LSB = 39.06 ~= 39 + 1/16
\r
257 scaled = 39 * data;
\r
258 scaled += (data >> 4);
\r
261 // Jumper setting 0: mV/LSB = 4.883 = 39.06 / 8
\r
262 // 1: mV/LSB = 9.766 = 39.06 / 4
\r
263 // 2: mV/LSB = 19.53 = 39.06 / 2
\r
264 scaled = (scaled >> (3-setting));
\r
272 /*! \brief Scales sample to represent "actual current" in mA.
\r
274 * This function returns the actual sampled current, scaled according
\r
275 * to the jumper settings.
\r
277 * \param setting Indicates what downscaling was used.
\r
278 * \param data The sampled value.
\r
280 * \note Table for setting-parameter:\n
\r
282 * Presume VREF = 2.5V and Gain = 1x or 20x.
\r
283 * => Resolution(U) @ (1/1 and 20x) = 2.5V / (GAIN x 1024) = 0.1221 mV/LSB
\r
284 * => Resolution(I) = Resolution(U) / Rshunt = Resolution(U) / 0.07
\r
285 * Setting | R1 | R2/(R1+R2) | U(LSB) | I(LSB) | I(MAX) | Gain
\r
286 * --------+------+------------+----------+----------+--------+-----
\r
287 * N/A | - | - | 0.1221mV | 1.744mA | 1.78A | 20x
\r
288 * 0 | 10k | 1/2 | 0.2442mV | 3.489mA | 3.57A | 20x
\r
289 * 1 | 30k | 1/4 | 0.4884mV | 6.978mA | 7.14A | 20x
\r
290 * 2 | 70k | 1/8 | 0.9768mV | 13.955mA | 14.3A | 20x
\r
291 * 3 | 110k | 1/12 | 1.4652mV | 20.931mA | 21.4A | 20x
\r
292 * 4 | 150k | 1/16 | 1.9536mV | 27.909mA | 28.5A | 20x
\r
293 * 5 | 10k | 1/2 | 2.4414mV | 34.877mA | 35.7A | 1x
\r
296 unsigned int ScaleI(unsigned char setting, unsigned int data)
\r
298 // Temporary variable needed.
\r
299 unsigned int scaled = 0;
\r
301 // Jumper setting 3: mA/LSB = 20.931mA ~= 21 - 1/16 + 1/128
\r
302 if (setting == 3) {
\r
303 scaled = 21 * data;
\r
304 scaled -= (data >> 4);
\r
305 scaled += (data >> 7);
\r
306 } else { // Jumper setting 4: mA/LSB = 27.909mA ~= 28 - 1/8 + 1/32
\r
307 scaled = 28 * data;
\r
308 scaled -= (data >> 3);
\r
309 scaled += (data >> 5);
\r
312 // Jumper setting 0: mA/LSB = 3.489mA = 27.909 / 8
\r
313 // 1: mA/LSB = 6.978mA = 27.909 / 4
\r
314 // 2: mA/LSB = 13.955mA = 27.909 / 2
\r
315 scaled = (scaled >> (3-setting));
\r
323 /*! \brief Waits for two full cycles of ADC-conversions to occur.
\r
325 * This function clears the cycle complete-flag, then waits for it to be set
\r
326 * again. This is then repeated once before the function exits.
\r
329 void ADC_Wait(void)
\r
331 // Clear ADC flag and wait for cycle to complete.
\r
334 } while (ADCS.Flag == FALSE);
\r
336 // Repeat, so we are sure the data belongs to the same cycle.
\r
339 } while (ADCS.Flag == FALSE);
\r
343 /*! \brief Initializes ADC and input pins.
\r
345 * This function initializes the ADC to free running mode, sampling from
\r
346 * PA1/2/4/5/6/7, and using an external reference voltage (PA3).\n
\r
347 * It also measures and stores calibration data for offset.
\r
349 * \todo Odd offset measurement for ADC3_G20_OS? It is never used anyway.
\r
351 * \note Table of MUX settings for offset measurement:
\r
353 * Ch | Pin | Gain | MUX
\r
354 * ------+-----+---------+-------
\r
355 * ADC1 | PA1 | 20x | 001101
\r
356 * ADC3 | PA4 | 20x | 010001
\r
357 * ADC5 | PA6 | 20x | 010110
\r
358 * ADC9 | PB6 | 20x | 011011
\r
359 * ADC0 | PA0 | 20x/32x | 111000
\r
360 * ADC0 | PA0 | 1x/8x | 111001
\r
361 * ADC1 | PA1 | 20x/32x | 111010
\r
362 * ADC2 | PA2 | 20x/32x | 111011
\r
363 * ADC4 | PA5 | 20x/32x | 111100
\r
364 * ADC5 | PA6 | 20x/32x | 111101
\r
365 * ADC6 | PA7 | 20x/32x | 111110
\r
368 void ADC_Init(void)
\r
371 unsigned char sreg_saved;
\r
376 ADCS.Halt = FALSE; // Enable consecutive runs of ADC.
\r
378 // Configure ADC pins (inputs and disabled pull-ups).
\r
379 DDRA &= ~((1<<PA1)|(1<<PA2)|(1<<PA4)|(1<<PA5)|(1<<PA6)|(1<<PA7));
\r
380 PORTA &= ~((1<<PA1)|(1<<PA2)|(1<<PA4)|(1<<PA5)|(1<<PA6)|(1<<PA7));
\r
382 // Set ADC3 as reference, and MUX to measure the same pin.
\r
383 ADMUX = (1<<REFS0) | (1<<MUX0) | (1<<MUX1);
\r
387 // Start conversion, no interrupt (disable ADC-ISR).
\r
388 ADCSRA = (1<<ADEN) | (1<<ADSC) | ADC_PRESCALER;
\r
390 do { // Wait for conversion to finish.
\r
391 } while (!(ADCSRA & (1<<ADIF)));
\r
393 ADCSRA |= (1<<ADIF); // Clear ADC interrupt flag manually.
\r
395 ADCS.ADC3_G20_OS = ADC; // Save the sampled offset.
\r
397 ADMUX = (1<<REFS0) | 0x16; // ADC5/ADC5 (external ref.), 20x
\r
399 // Start conversion, no interrupt. ADC_PRESCALER is defined in ADC.h.
\r
400 ADCSRA = (1<<ADEN) | (1<<ADSC) | ADC_PRESCALER;
\r
402 do { // Wait for conversion to finish.
\r
403 } while (!(ADCSRA & (1<<ADIF)));
\r
405 ADCSRA |= (1<<ADIF); // Clear ADC interrupt flag.
\r
407 ADCS.ADC5_G20_OS = ADC; // Save the sampled offset.
\r
409 // Reset the ADC-cycle.
\r
412 ADMUX = (1<<REFS0) | ADCS.MUX;
\r
414 // Clear averaged battery current and the discrete readings.
\r
417 for (i = 0; i < 4; i++) {
\r
418 ADCS.discIBAT[i] = 0;
\r
421 // Re-enable the ADC and ISR.
\r
422 ADCSRA=(1<<ADEN)|(1<<ADSC)|(1<<ADIE)|ADC_PRESCALER;
\r
426 // Get a complete cycle of data before returning.
\r