X-Git-Url: http://git.kpe.io/?a=blobdiff_plain;ds=sidebyside;f=BaseTinyFirmware%2FIAR%2FADC.c;fp=BaseTinyFirmware%2FIAR%2FADC.c;h=6ccc245f3bd40ce440def17460f9e21e576cd590;hb=65612c4c7df34cdae10f9427ace6fd9e9e430d05;hp=b1229a24dd07ec793d6a6bcd7b4df03c88e47f37;hpb=89173ec9cd6f33843bf27c45be0f2f9be3f5f0ce;p=avr_bc100.git diff --git a/BaseTinyFirmware/IAR/ADC.c b/BaseTinyFirmware/IAR/ADC.c index b1229a2..6ccc245 100644 --- a/BaseTinyFirmware/IAR/ADC.c +++ b/BaseTinyFirmware/IAR/ADC.c @@ -33,6 +33,7 @@ #include #include +#include #include "structs.h" @@ -81,7 +82,7 @@ __eeprom unsigned char VBAT_RANGE = 1; * This ISR stores the sampled values in the ADC status-struct, then * updates the ADC MUX to the next channel in the scanning-sequence.\n * Once the sequence is completed, ADCS.Flag is set and unless - * ADCS.Halt has been set, the sequence starts over. Otherwise, the ADC + * ADCS.Halt has been set, the sequence starts over. Otherwise, the ADC * is disabled.\n * If the mains voltage is below minimum, ADCS.Mains gets set to FALSE. * @@ -116,11 +117,11 @@ __interrupt void ADC_ISR(void) static unsigned char avgIndex = 0; unsigned char i, Next, Signed; signed int temp = 0; - + Signed = FALSE; // Presume next conversion is unipolar. ADCSRA &= ~(1< ADC2 (PA2) = RID case 0x02: ADCS.rawRID = ADC; Next=0x03; break; - + // MUX = 0b000011 => ADC3 (PA4) = VIN- case 0x03: // Supply voltage is always divided by 16. ADCS.VIN = ScaleU(4, (unsigned int)ADC); // Cast because ADC is short. - + // Is mains failing? if (ADCS.VIN < VIN_MIN) { ADCS.Mains = FALSE; } else { ADCS.Mains = TRUE; } - + Next=0x05; break; - + // MUX = 0b000101 => ADC5 (PA6) = VBAT- case 0x05: ADCS.rawVBAT = ADC; - + // Scale voltage according to jumper setting. ADCS.VBAT = ScaleU(VBAT_RANGE, (unsigned int)ADC); // ADC is a short. + __no_operation(); Next=0x17; // Signed = TRUE; // Next conversion is bipolar. Halves sensitivity! break; @@ -168,7 +170,7 @@ __interrupt void ADC_ISR(void) case 0x17: // MUX = 0b010111 => 20 x [ADC6(PA7) - ADC5(PA6)] = IBAT // If bipolar, from -512 to 0, to 511: // 0x200 ... 0x3ff, 0x000, 0x001 ... 0x1FF - + // Scale sample according to jumper setting, handle negative numbers. if (ADC > 511) { ADCS.IBAT = -(signed int)ScaleI(VBAT_RANGE, @@ -187,35 +189,35 @@ __interrupt void ADC_ISR(void) for (i = 0; i < 4 ; i++) { temp += ADCS.discIBAT[i]; } - + ADCS.avgIBAT = (temp / 4); - + ADCS.Flag = TRUE; Next=0x01; Signed = FALSE; // This is the only bipolar conversion. break; - + default: // Should not happen. (Invalid MUX-channel) Next=0x01; // Start at the beginning of sequence. break; } - + // Update MUX to next channel in sequence, set a bipolar conversion if // this has been flagged. - ADCS.MUX = Next; - ADMUX = (1<> 4); - + if (setting <3) { // Jumper setting 0: mV/LSB = 4.883 = 39.06 / 8 // 1: mV/LSB = 9.766 = 39.06 / 4 @@ -298,7 +300,7 @@ unsigned int ScaleI(unsigned char setting, unsigned int data) { // Temporary variable needed. unsigned int scaled = 0; - + // Jumper setting 3: mA/LSB = 20.931mA ~= 21 - 1/16 + 1/128 if (setting == 3) { scaled = 21 * data; @@ -308,7 +310,7 @@ unsigned int ScaleI(unsigned char setting, unsigned int data) scaled = 28 * data; scaled -= (data >> 3); scaled += (data >> 5); - + if (setting <3) { // Jumper setting 0: mA/LSB = 3.489mA = 27.909 / 8 // 1: mA/LSB = 6.978mA = 27.909 / 4 @@ -316,7 +318,7 @@ unsigned int ScaleI(unsigned char setting, unsigned int data) scaled = (scaled >> (3-setting)); } } - + return(scaled); } @@ -325,19 +327,19 @@ unsigned int ScaleI(unsigned char setting, unsigned int data) * * This function clears the cycle complete-flag, then waits for it to be set * again. This is then repeated once before the function exits. - * + * */ void ADC_Wait(void) { // Clear ADC flag and wait for cycle to complete. - ADCS.Flag = FALSE; + ADCS.Flag = FALSE; do { - } while (ADCS.Flag == FALSE); - + } while (ADCS.Flag == FALSE); + // Repeat, so we are sure the data beong to the same cycle. - ADCS.Flag = FALSE; + ADCS.Flag = FALSE; do { - } while (ADCS.Flag == FALSE); + } while (ADCS.Flag == FALSE); } @@ -380,11 +382,11 @@ void ADC_Init(void) // Set ADC3 as reference, and MUX to measure the same pin. ADMUX = (1<