analog mask calculated automagically

This commit is contained in:
Stephan Walter 2011-02-20 20:55:22 +01:00 committed by Michael Moon
parent 31634c6a8f
commit eecf3af9f1
8 changed files with 52 additions and 88 deletions

View File

@ -1,48 +1,21 @@
#include "analog.h"
#include "temp.h"
#include <avr/interrupt.h>
#ifndef ANALOG_MASK
#warning ANALOG_MASK not defined - analog subsystem disabled
#define ANALOG_MASK 0
#endif
/* OR-combined mask of all channels */
#undef DEFINE_TEMP_SENSOR
#define DEFINE_TEMP_SENSOR(name, type, pin) | (((type == TT_THERMISTOR) || (type == TT_AD595)) ? 1 << (pin) : 0)
static const uint8_t analog_mask = 0
#include "config.h"
;
#undef DEFINE_TEMP_SENSOR
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
#else
// ANALOG_MASK == 0
#define ANALOG_START 0
#define ANALOG_START_MASK 1
#endif
volatile uint16_t adc_result[8] __attribute__ ((__section__ (".bss")));
static uint8_t adc_counter;
static volatile uint16_t adc_result[8] __attribute__ ((__section__ (".bss")));
void analog_init() {
#if ANALOG_MASK > 0
if (analog_mask > 0) {
#ifdef PRR
PRR &= ~MASK(PRADC);
#elif defined PRR0
@ -55,36 +28,32 @@ void analog_init() {
ADCSRA = MASK(ADEN) | MASK(ADPS2) | MASK(ADPS1) | MASK(ADPS0);
adc_counter = 0;
adc_running_mask = 1;
AIO0_DDR &= ~(ANALOG_MASK);
DIDR0 = (ANALOG_MASK) & 0x3F;
AIO0_DDR &= ~analog_mask;
DIDR0 = analog_mask & 0x3F;
// now we start the first conversion and leave the rest to the interrupt
ADCSRA |= MASK(ADIE) | MASK(ADSC);
#endif /* ANALOG_MASK > 0 */
} /* analog_mask > 0 */
}
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
if (analog_mask > 0) {
adc_result[ADMUX & 0x0F] = ADC;
// find next channel
do {
adc_counter++;
adc_running_mask <<= 1;
if (adc_counter == 8) {
adc_counter = ANALOG_START;
adc_running_mask = ANALOG_START_MASK;
}
} while ((adc_running_mask & (ANALOG_MASK)) == 0);
adc_counter &= 0x07;
} while ((analog_mask & (1 << adc_counter)) == 0);
// start next conversion
ADMUX = (adc_counter) | REFERENCE;
ADCSRA |= MASK(ADSC);
}
}
uint16_t analog_read(uint8_t channel) {
#if ANALOG_MASK > 0
if (analog_mask > 0) {
uint16_t r;
uint8_t sreg;
@ -100,7 +69,7 @@ uint16_t analog_read(uint8_t channel) {
SREG = sreg;
return r;
#else
} else {
return 0;
#endif
}
}

View File

@ -212,9 +212,6 @@ undefine if you don't want to use them
// #define TEMP_PT100
#define TEMP_INTERCOM
// ANALOG_MASK is a bitmask of all analog channels used- bitwise-or them all together
#define ANALOG_MASK 0
/***************************************************************************\
* *
* Define your temperature sensors here *

View File

@ -207,9 +207,6 @@ undefine if you don't want to use them
// #define TEMP_PT100
// #define TEMP_INTERCOM
// ANALOG_MASK is a bitmask of all analog channels used- bitwise-or them all together
#define ANALOG_MASK MASK(AIO5_PIN)
/***************************************************************************\
* *
* Define your temperature sensors here *

View File

@ -228,9 +228,6 @@ undefine if you don't want to use them
// #define TEMP_PT100
// #define TEMP_INTERCOM
// ANALOG_MASK is a bitmask of all analog channels used- bitwise-or them all together
#define ANALOG_MASK MASK(AIO0_PIN)
/***************************************************************************\
* *
* Define your temperature sensors here *

View File

@ -217,9 +217,6 @@ undefine if you don't want to use them
// #define TEMP_PT100
// #define TEMP_INTERCOM
// ANALOG_MASK is a bitmask of all analog channels used- bitwise-or them all together
#define ANALOG_MASK MASK(AIO1_PIN) | MASK(AIO2_PIN)
/***************************************************************************\
* *
* Define your temperature sensors here *

View File

@ -144,7 +144,8 @@ void init(void) {
// set up default feedrate
current_position.F = startpoint.F = next_target.target.F = SEARCH_FEEDRATE_Z;
// start up analog read interrupt loop, if anything uses analog as determined by ANALOG_MASK in your config.h
// start up analog read interrupt loop,
// if any of the temp sensors in your config.h use analog interface
analog_init();
// set up temperature inputs

25
temp.c
View File

@ -15,24 +15,15 @@
#include "intercom.h"
#endif
typedef enum {
TT_THERMISTOR,
TT_MAX6675,
TT_AD595,
TT_PT100,
TT_INTERCOM,
TT_DUMMY,
} temp_types;
typedef enum {
PRESENT,
TCOPEN
} temp_flags_enum;
typedef struct {
uint8_t temp_type;
temp_type_t temp_type;
uint8_t temp_pin;
uint8_t heater_index;
heater_t heater;
} temp_sensor_definition_t;
#undef DEFINE_TEMP_SENSOR
@ -95,6 +86,9 @@ void temp_init() {
update_send_cmd(0);
break;
#endif
default: /* prevent compiler warning */
break;
}
}
}
@ -214,7 +208,7 @@ void temp_sensor_tick() {
#ifdef TEMP_AD595
case TT_AD595:
temp = analog_read(temp_pin);
temp = analog_read(temp_sensors[i].temp_pin);
// convert
// >>8 instead of >>10 because internal temp is stored as 14.2 fixed point
@ -255,6 +249,9 @@ void temp_sensor_tick() {
break;
#endif /* TEMP_DUMMY */
default: /* prevent compiler warning */
break;
}
temp_sensors_runtime[i].last_read_temp = temp;
@ -266,8 +263,8 @@ void temp_sensor_tick() {
temp_sensors_runtime[i].temp_residency = 0;
}
if (temp_sensors[i].heater_index < NUM_HEATERS) {
heater_tick(temp_sensors[i].heater_index, i, temp_sensors_runtime[i].last_read_temp, temp_sensors_runtime[i].target_temp);
if (temp_sensors[i].heater < NUM_HEATERS) {
heater_tick(temp_sensors[i].heater, i, temp_sensors_runtime[i].last_read_temp, temp_sensors_runtime[i].target_temp);
}
}
}

11
temp.h
View File

@ -21,6 +21,15 @@ typedef enum {
} temp_sensor_t;
#undef DEFINE_TEMP_SENSOR
typedef enum {
TT_THERMISTOR,
TT_MAX6675,
TT_AD595,
TT_PT100,
TT_INTERCOM,
TT_DUMMY,
} temp_type_t;
#define temp_tick temp_sensor_tick
void temp_init(void);
@ -34,4 +43,4 @@ uint16_t temp_get(temp_sensor_t index);
void temp_print(temp_sensor_t index);
#endif /* _TIMER_H */
#endif /* _TEMP_H */