diff --git a/analog.c b/analog.c index 2a1b4e5..162b429 100644 --- a/analog.c +++ b/analog.c @@ -9,6 +9,32 @@ uint8_t adc_running_mask, adc_counter; +#if ANALOG_MASK & 1 + #define ANALOG_START 0 + #define ANALOG_START_MASK 1 +#elif ANALOG_MASK & 2 + #define ANALOG_START 1 + #define ANALOG_START_MASK 2 +#elif ANALOG_MASK & 4 + #define ANALOG_START 2 + #define ANALOG_START_MASK 4 +#elif ANALOG_MASK & 8 + #define ANALOG_START 3 + #define ANALOG_START_MASK 8 +#elif ANALOG_MASK & 16 + #define ANALOG_START 4 + #define ANALOG_START_MASK 16 +#elif ANALOG_MASK & 32 + #define ANALOG_START 5 + #define ANALOG_START_MASK 32 +#elif ANALOG_MASK & 64 + #define ANALOG_START 6 + #define ANALOG_START_MASK 64 +#elif ANALOG_MASK & 128 + #define ANALOG_START 7 + #define ANALOG_START_MASK 128 +#endif + volatile uint16_t adc_result[8] __attribute__ ((__section__ (".bss"))); void analog_init() { @@ -35,7 +61,7 @@ void analog_init() { #endif } -ISR(ADC_vect) { +ISR(ADC_vect, ISR_NOBLOCK) { // emulate free-running mode but be more deterministic about exactly which result we have, since this project has long-running interrupts adc_result[ADMUX & 0x0F] = ADC; // find next channel @@ -43,11 +69,8 @@ ISR(ADC_vect) { adc_counter++; adc_running_mask <<= 1; if (adc_counter == 8) { - adc_counter = 0; - adc_running_mask = 1; - - // relax interrupt use for analog subsystem- stop after last analog read - ADCSRA &= ~MASK(ADIE); + adc_counter = ANALOG_START; + adc_running_mask = ANALOG_START_MASK; } } while ((adc_running_mask & ANALOG_MASK) == 0); @@ -57,19 +80,19 @@ ISR(ADC_vect) { } uint16_t analog_read(uint8_t channel) { - uint8_t sreg; uint16_t r; + + uint8_t sreg; // save interrupt flag sreg = SREG; // disable interrupts cli(); + // atomic 16-bit copy r = adc_result[channel]; + // restore interrupt flag SREG = sreg; - - // re-enable analog read loop so we can get new values - ADCSRA |= MASK(ADIE); - + return r; } diff --git a/config.h.dist b/config.h.dist index c9b0538..a9ebbbd 100644 --- a/config.h.dist +++ b/config.h.dist @@ -178,7 +178,7 @@ struct { REFERENCE - which analog reference to use. see analog.h for choices ANALOG_MASK - which analog inputs we will be using, bitmask. eg; #define ANALOG_MASK MASK(AIO0_PIN) | MASK(3) for AIN0 and AIN3 */ -#define REFERENCE REFERENCE_AREF +#define REFERENCE REFERENCE_AVCC #ifndef ANALOG_MASK #define ANALOG_MASK 0 diff --git a/temp.c b/temp.c index e587a39..92edd3c 100644 --- a/temp.c +++ b/temp.c @@ -117,9 +117,6 @@ void temp_sensor_tick() { } else { uint16_t temp = 0; - #ifdef TEMP_THERMISTOR - uint8_t j; - #endif //time to deal with this temp sensor switch(temp_sensors[i].temp_type) { #ifdef TEMP_MAX6675 @@ -173,25 +170,26 @@ void temp_sensor_tick() { #ifdef TEMP_THERMISTOR case TT_THERMISTOR: - - //Read current temperature - temp = analog_read(temp_sensors[i].temp_pin); - - //Calculate real temperature based on lookup table - for (j = 1; j < NUMTEMPS; j++) { - if (pgm_read_word(&(temptable[j][0])) > temp) { - // multiply by 4 because internal temp is stored as 14.2 fixed point - temp = pgm_read_word(&(temptable[j][1])) + (pgm_read_word(&(temptable[j][0])) - temp) * 4 * (pgm_read_word(&(temptable[j-1][1])) - pgm_read_word(&(temptable[j][1]))) / (pgm_read_word(&(temptable[j][0])) - pgm_read_word(&(temptable[j-1][0]))); - break; + do { + uint8_t j; + //Read current temperature + temp = analog_read(temp_sensors[i].temp_pin); + + //Calculate real temperature based on lookup table + for (j = 1; j < NUMTEMPS; j++) { + if (pgm_read_word(&(temptable[j][0])) > temp) { + // multiply by 4 because internal temp is stored as 14.2 fixed point + temp = pgm_read_word(&(temptable[j][1])) * 4 + (pgm_read_word(&(temptable[j][0])) - temp) * 4 * (pgm_read_word(&(temptable[j-1][1])) - pgm_read_word(&(temptable[j][1]))) / (pgm_read_word(&(temptable[j][0])) - pgm_read_word(&(temptable[j-1][0]))); + break; + } } + + //Clamp for overflows + if (j == NUMTEMPS) + temp = temptable[NUMTEMPS-1][1] * 4; + + temp_sensors_runtime[i].next_read_time = 0; } - - //Clamp for overflows - if (j == NUMTEMPS) - temp = temptable[NUMTEMPS-1][1]; - - temp_sensors_runtime[i].next_read_time = 0; - break; #endif /* TEMP_THERMISTOR */