2008-03-14 Martin Thomas <mthomas@rhrk.uni-kl.de>
[avr_bc100.git] / BaseTinyFirmware / GCC / ADC.c
index 004ba8dc6cbcddd222e5eb368805e5ff740a9580..50814ca6413782eef7d73523cbbf2e590c972910 100644 (file)
@@ -45,7 +45,7 @@
 //******************************************************************************\r
 // ADC status struct.\r
 //! \brief Holds sampled data and ADC-status\r
-ADC_Status_t ADCS;\r
+volatile ADC_Status_t ADCS;\r
 \r
 \r
 /*! \brief Indicates maximum battery voltage.\r
@@ -170,10 +170,10 @@ ISR(ADC_vect)
 \r
                        // Scale sample according to jumper setting, handle negative numbers.\r
                        if (ADC > 511) {\r
-                          ADCS.IBAT = -(signed int)ScaleI(eeprom_read_byte(&VBAT_RANGE),\r
-                                            (1024 - (ADC-ADCS.ADC5_G20_OS)));\r
+                               ADCS.IBAT = -(signed int)ScaleI(eeprom_read_byte(&VBAT_RANGE),\r
+                                                               (1024 - (ADC-ADCS.ADC5_G20_OS)));\r
                        } else if (ADC > 0) {\r
-                          ADCS.IBAT = ScaleI(eeprom_read_byte(&VBAT_RANGE), (ADC-ADCS.ADC5_G20_OS));\r
+                               ADCS.IBAT = ScaleI(eeprom_read_byte(&VBAT_RANGE), (ADC-ADCS.ADC5_G20_OS));\r
                        } else {\r
                                ADCS.IBAT = 0;\r
                        }\r
@@ -206,15 +206,15 @@ ISR(ADC_vect)
        ADMUX = (1<<REFS0) + ADCS.MUX;\r
 \r
        if (Signed)     {\r
-         ADCSRB |= (1<<BIN);\r
+               ADCSRB |= (1<<BIN);\r
        } else {\r
-         ADCSRB &= ~(1<<BIN);\r
+               ADCSRB &= ~(1<<BIN);\r
        }\r
 \r
        // Re-enable the ADC unless a halt has been flagged and a conversion\r
        // cycle has completed.\r
        if (!((ADCS.Halt) && (ADCS.Flag))) {\r
-         ADCSRA |= (1<<ADEN)|(1<<ADSC);\r
+               ADCSRA |= (1<<ADEN)|(1<<ADSC);\r
        }\r
 }\r
 \r
@@ -333,7 +333,7 @@ void ADC_Wait(void)
        do {\r
        } while (ADCS.Flag == FALSE);\r
 \r
-       // Repeat, so we are sure the data beong to the same cycle.\r
+       // Repeat, so we are sure the data belongs to the same cycle.\r
        ADCS.Flag = FALSE;\r
        do {\r
        } while (ADCS.Flag == FALSE);\r
@@ -368,7 +368,9 @@ void ADC_Wait(void)
 void ADC_Init(void)\r
 {\r
        unsigned char i;\r
+       unsigned char sreg_saved;\r
 \r
+       sreg_saved = SREG;\r
        cli();\r
 \r
        ADCS.Halt = FALSE; // Enable consecutive runs of ADC.\r
@@ -423,4 +425,6 @@ void ADC_Init(void)
 \r
        // Get a complete cycle of data before returning.\r
        ADC_Wait();\r
+\r
+       SREG = sreg_saved;\r
 }\r