changes by jv4779 via pull request 14
This commit is contained in:
parent
38cf934594
commit
1e287dd6c3
43
analog.c
43
analog.c
|
|
@ -9,6 +9,32 @@
|
||||||
|
|
||||||
uint8_t adc_running_mask, adc_counter;
|
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")));
|
volatile uint16_t adc_result[8] __attribute__ ((__section__ (".bss")));
|
||||||
|
|
||||||
void analog_init() {
|
void analog_init() {
|
||||||
|
|
@ -35,7 +61,7 @@ void analog_init() {
|
||||||
#endif
|
#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
|
// 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;
|
adc_result[ADMUX & 0x0F] = ADC;
|
||||||
// find next channel
|
// find next channel
|
||||||
|
|
@ -43,11 +69,8 @@ ISR(ADC_vect) {
|
||||||
adc_counter++;
|
adc_counter++;
|
||||||
adc_running_mask <<= 1;
|
adc_running_mask <<= 1;
|
||||||
if (adc_counter == 8) {
|
if (adc_counter == 8) {
|
||||||
adc_counter = 0;
|
adc_counter = ANALOG_START;
|
||||||
adc_running_mask = 1;
|
adc_running_mask = ANALOG_START_MASK;
|
||||||
|
|
||||||
// relax interrupt use for analog subsystem- stop after last analog read
|
|
||||||
ADCSRA &= ~MASK(ADIE);
|
|
||||||
}
|
}
|
||||||
} while ((adc_running_mask & ANALOG_MASK) == 0);
|
} while ((adc_running_mask & ANALOG_MASK) == 0);
|
||||||
|
|
||||||
|
|
@ -57,19 +80,19 @@ ISR(ADC_vect) {
|
||||||
}
|
}
|
||||||
|
|
||||||
uint16_t analog_read(uint8_t channel) {
|
uint16_t analog_read(uint8_t channel) {
|
||||||
uint8_t sreg;
|
|
||||||
uint16_t r;
|
uint16_t r;
|
||||||
|
|
||||||
|
uint8_t sreg;
|
||||||
// save interrupt flag
|
// save interrupt flag
|
||||||
sreg = SREG;
|
sreg = SREG;
|
||||||
// disable interrupts
|
// disable interrupts
|
||||||
cli();
|
cli();
|
||||||
|
|
||||||
// atomic 16-bit copy
|
// atomic 16-bit copy
|
||||||
r = adc_result[channel];
|
r = adc_result[channel];
|
||||||
|
|
||||||
// restore interrupt flag
|
// restore interrupt flag
|
||||||
SREG = sreg;
|
SREG = sreg;
|
||||||
|
|
||||||
// re-enable analog read loop so we can get new values
|
|
||||||
ADCSRA |= MASK(ADIE);
|
|
||||||
|
|
||||||
return r;
|
return r;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -178,7 +178,7 @@ struct {
|
||||||
REFERENCE - which analog reference to use. see analog.h for choices
|
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
|
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
|
#ifndef ANALOG_MASK
|
||||||
#define ANALOG_MASK 0
|
#define ANALOG_MASK 0
|
||||||
|
|
|
||||||
36
temp.c
36
temp.c
|
|
@ -117,9 +117,6 @@ void temp_sensor_tick() {
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
uint16_t temp = 0;
|
uint16_t temp = 0;
|
||||||
#ifdef TEMP_THERMISTOR
|
|
||||||
uint8_t j;
|
|
||||||
#endif
|
|
||||||
//time to deal with this temp sensor
|
//time to deal with this temp sensor
|
||||||
switch(temp_sensors[i].temp_type) {
|
switch(temp_sensors[i].temp_type) {
|
||||||
#ifdef TEMP_MAX6675
|
#ifdef TEMP_MAX6675
|
||||||
|
|
@ -173,25 +170,26 @@ void temp_sensor_tick() {
|
||||||
|
|
||||||
#ifdef TEMP_THERMISTOR
|
#ifdef TEMP_THERMISTOR
|
||||||
case TT_THERMISTOR:
|
case TT_THERMISTOR:
|
||||||
|
do {
|
||||||
|
uint8_t j;
|
||||||
|
//Read current temperature
|
||||||
|
temp = analog_read(temp_sensors[i].temp_pin);
|
||||||
|
|
||||||
//Read current temperature
|
//Calculate real temperature based on lookup table
|
||||||
temp = analog_read(temp_sensors[i].temp_pin);
|
for (j = 1; j < NUMTEMPS; j++) {
|
||||||
|
if (pgm_read_word(&(temptable[j][0])) > temp) {
|
||||||
//Calculate real temperature based on lookup table
|
// multiply by 4 because internal temp is stored as 14.2 fixed point
|
||||||
for (j = 1; j < NUMTEMPS; j++) {
|
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])));
|
||||||
if (pgm_read_word(&(temptable[j][0])) > temp) {
|
break;
|
||||||
// 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;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//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;
|
break;
|
||||||
#endif /* TEMP_THERMISTOR */
|
#endif /* TEMP_THERMISTOR */
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue