diff --git a/Makefile b/Makefile index 6235c20..722a44c 100644 --- a/Makefile +++ b/Makefile @@ -14,7 +14,7 @@ PROGRAM = mendel -SOURCES = $(PROGRAM).c serial.c dda.c gcode.c timer.c clock.c temp.c sermsg.c dda_queue.c watchdog.c debug.c sersendf.c heater.c +SOURCES = $(PROGRAM).c serial.c dda.c gcode.c timer.c clock.c temp.c sermsg.c dda_queue.c watchdog.c debug.c sersendf.c heater.c analog.c ############################################################################## # # diff --git a/analog.c b/analog.c index 0f1c62c..00d8aeb 100644 --- a/analog.c +++ b/analog.c @@ -12,7 +12,7 @@ uint8_t adc_running_mask, adc_counter; -volatile uint16_t adc_result[8] __attribute__ ((section ".bss")); +volatile uint16_t adc_result[8] __attribute__ ((__section__ (".bss"))); void analog_init() { #if ANALOG_MASK > 0 diff --git a/machine.h b/machine.h index 2651303..1bcec16 100644 --- a/machine.h +++ b/machine.h @@ -58,11 +58,20 @@ #define ACCELERATION_STEEPNESS 500000 #ifdef ACCELERATION_REPRAP -#ifdef ACCELERATION_RAMPING -#error Cant use ACCELERATION_REPRAP and ACCELERATION_RAMPING together. -#endif + #ifdef ACCELERATION_RAMPING + #error Cant use ACCELERATION_REPRAP and ACCELERATION_RAMPING together. + #endif #endif +// which temperature sensor are you using? +// #define TEMP_MAX6675 +#define TEMP_THERMISTOR +// #define TEMP_AD595 + +// if you selected thermistor or AD595, what pin is it on? +#define TEMP_PIN_CHANNEL AIO0_PIN +#define ANALOG_MASK MASK(TEMP_PIN_CHANNEL) + /* firmware build options */ @@ -91,8 +100,9 @@ firmware build options 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 +#ifndef ANALOG_MASK #define ANALOG_MASK 0 - +#endif // -------------------------------------------------------------------------- // you shouldn't need to edit anything below this line diff --git a/temp.c b/temp.c index 992b7e2..1e5f7d3 100644 --- a/temp.c +++ b/temp.c @@ -31,13 +31,53 @@ #include "debug.h" #include "heater.h" +#ifdef TEMP_MAX6675 +#endif + +#ifdef TEMP_THERMISTOR +#include "analog.h" + +#define NUMTEMPS 20 +uint16_t temptable[NUMTEMPS][2] PROGMEM = { + {1, 841}, + {54, 255}, + {107, 209}, + {160, 184}, + {213, 166}, + {266, 153}, + {319, 142}, + {372, 132}, + {425, 124}, + {478, 116}, + {531, 108}, + {584, 101}, + {637, 93}, + {690, 86}, + {743, 78}, + {796, 70}, + {849, 61}, + {902, 50}, + {955, 34}, + {1008, 3} +}; +#endif + +#ifdef TEMP_AD595 +#include "analog.h" +#endif + +#ifndef TEMP_MAX6675 + #ifndef TEMP_THERMISTOR + #ifndef TEMP_AD595 + #error none of TEMP_MAX6675, TEMP_THERMISTOR or TEMP_AD595 are defined! What type of temp sensor are you using? + #endif + #endif +#endif + uint16_t current_temp = 0; uint16_t target_temp = 0; uint8_t temp_flags = 0; -#define TEMP_FLAG_PRESENT 1 -#define TEMP_FLAG_TCOPEN 2 - uint8_t temp_residency = 0; #ifndef ABSDELTA @@ -47,6 +87,7 @@ uint8_t temp_residency = 0; uint16_t temp_read() { uint16_t temp; +#ifdef TEMP_MAX6675 SPCR = MASK(MSTR) | MASK(SPE) | MASK(SPR0); // enable MAX6675 @@ -82,6 +123,40 @@ uint16_t temp_read() { return current_temp; } } +#endif /* TEMP_MAX6675 */ + +#ifdef TEMP_THERMISTOR + uint8_t i; + + //Read current temperature + temp = analog_read(TEMP_PIN_CHANNEL); + + //Calculate real temperature based on lookup table + for (i = 1; i < NUMTEMPS; i++) { + if (pgm_read_word(&(temptable[i][0])) > temp) { + // multiply by 4 because internal temp is stored as 14.2 fixed point + temp = pgm_read_word(&(temptable[i][1])) + (pgm_read_word(&(temptable[i][0])) - temp) * 4 * (pgm_read_word(&(temptable[i-1][1])) - pgm_read_word(&(temptable[i][1]))) / (pgm_read_word(&(temptable[i][0])) - pgm_read_word(&(temptable[i-1][0]))); + break; + } + } + + //Clamp for overflows + if (i == NUMTEMPS) + temp = temptable[NUMTEMPS-1][1]; + + return temp; + +#endif /* TEMP_THERMISTOR */ + +#ifdef TEMP_AD595 + temp = analog_read(TEMP_PIN_CHANNEL); + + // convert + // >>8 instead of >>10 because internal temp is stored as 14.2 fixed point + temp = (temp * 500L) >> 8; + + return temp; +#endif /* TEMP_AD595 */ return 0; } diff --git a/temp.h b/temp.h index 9605f0e..fea61fe 100644 --- a/temp.h +++ b/temp.h @@ -3,9 +3,15 @@ #include +#include "machine.h" + // RepRap host software isn't exactly tolerant on what it ready back. #define REPRAP_HOST_COMPATIBILITY +#define TEMP_FLAG_PRESENT 1 +#define TEMP_FLAG_TCOPEN 2 + +#ifdef TEMP_MAX6675 typedef union { struct { uint8_t high; @@ -19,6 +25,14 @@ typedef union { uint16_t tristate :1; } interpret; } max6675_data_format; +#endif + +#ifdef TEMP_THERMISTOR +#include +#endif + +#ifdef TEMP_AD595 +#endif // setup temperature system void temp_init(void);