chiark / gitweb /
Added MAX6675 support. (Thanks to gregfrost)
authorErik van der Zalm <erik@vdzalm.eu>
Sun, 5 Feb 2012 12:05:07 +0000 (13:05 +0100)
committerErik van der Zalm <erik@vdzalm.eu>
Sun, 5 Feb 2012 12:05:07 +0000 (13:05 +0100)
Needs some work to remove the blocking in read max6675.

Marlin/Configuration.h
Marlin/pins.h
Marlin/temperature.cpp

index 6b5e19a3d380f9bcab6c5239356e52aaf037efd9..49721cc193d4387049f61044b7eae4bbade2b376 100644 (file)
@@ -55,6 +55,8 @@
 #define HEATER_0_USES_AD595
 //#define HEATER_1_USES_AD595
 //#define HEATER_2_USES_AD595
+//#define HEATER_0_USES_MAX6675
 
 // Select one of these only to define how the bed temp is read.
 //#define THERMISTORBED 1
index 02f26150a1dbdc7341f51daffc597f6d6b62d93d..9ff7dd94a7aa7ba8b1350398c99f3a3b87784c99 100644 (file)
 // SPI for Max6675 Thermocouple 
 
 #ifndef SDSUPPORT
-// these pins are defined in the SD library if building with SD support  #define SCK_PIN          52
-  #define MISO_PIN         50
-  #define MOSI_PIN         51
+// these pins are defined in the SD library if building with SD support  
+  #define MAX_SCK_PIN          52
+  #define MAX_MISO_PIN         50
+  #define MAX_MOSI_PIN         51
   #define MAX6675_SS       53
 #else
   #define MAX6675_SS       49
 #endif
 
-
 #endif
+
 /****************************************************************************************
 * Duemilanove w/ ATMega328P pin assignment
 *
index 371c6e589b6f63d9c9954507258a9c0f9f30ba99..0be5f2607df05b16615bba02813ebf210c601ed9 100644 (file)
@@ -283,6 +283,12 @@ int temp2analog(int celsius, uint8_t e) {
       SERIAL_ERRORLNPGM(" - Invalid extruder number!");
       kill();
   }
+  #ifdef HEATER_0_USES_MAX6675
+    if (e == 0)
+    {
+      return celsius * 4;
+    }
+  #endif
   if(heater_ttbl_map[e] != 0)
   {
     int raw = 0;
@@ -352,7 +358,14 @@ float analog2temp(int raw, uint8_t e) {
       SERIAL_ERROR((int)e);
       SERIAL_ERRORLNPGM(" - Invalid extruder number !");
       kill();
-  }
+  } 
+  #ifdef HEATER_0_USES_MAX6675
+    if (e == 0)
+    {
+      return 0.25 * raw;
+    }
+  #endif
+
   if(heater_ttbl_map[e] != 0)
   {
     float celsius = 0;
@@ -446,6 +459,22 @@ void tp_init()
     SET_OUTPUT(FAN_PIN);
   #endif  
 
+  #ifdef HEATER_0_USES_MAX6675
+    #ifndef SDSUPPORT
+      SET_OUTPUT(MAX_SCK_PIN);
+      WRITE(MAX_SCK_PIN,0);
+    
+      SET_OUTPUT(MAX_MOSI_PIN);
+      WRITE(MAX_MOSI_PIN,1);
+    
+      SET_INPUT(MAX_MISO_PIN);
+      WRITE(MAX_MISO_PIN,1);
+    #endif
+    
+    SET_OUTPUT(MAX6675_SS);
+    WRITE(MAX6675_SS,1);
+  #endif
+
   // Set analog inputs
   ADCSRA = 1<<ADEN | 1<<ADSC | 1<<ADIF | 0x07;
   DIDR0 = 0;
@@ -595,6 +624,62 @@ void bed_max_temp_error(void) {
   SERIAL_ERRORLNPGM("Temperature heated bed switched off. MAXTEMP triggered !!");
 }
 
+#define HEAT_INTERVAL 250
+#ifdef HEATER_0_USES_MAX6675
+long max6675_previous_millis = -HEAT_INTERVAL;
+int max6675_temp = 2000;
+
+int read_max6675()
+{
+  if (millis() - max6675_previous_millis < HEAT_INTERVAL) 
+    return max6675_temp;
+  
+  max6675_previous_millis = millis();
+  max6675_temp = 0;
+    
+  #ifdef       PRR
+    PRR &= ~(1<<PRSPI);
+  #elif defined PRR0
+    PRR0 &= ~(1<<PRSPI);
+  #endif
+  
+  SPCR = (1<<MSTR) | (1<<SPE) | (1<<SPR0);
+  
+  // enable TT_MAX6675
+  WRITE(MAX6675_SS, 0);
+  
+  // ensure 100ns delay - a bit extra is fine
+  delay(1);
+  
+  // read MSB
+  SPDR = 0;
+  for (;(SPSR & (1<<SPIF)) == 0;);
+  max6675_temp = SPDR;
+  max6675_temp <<= 8;
+  
+  // read LSB
+  SPDR = 0;
+  for (;(SPSR & (1<<SPIF)) == 0;);
+  max6675_temp |= SPDR;
+  
+  // disable TT_MAX6675
+  WRITE(MAX6675_SS, 1);
+
+  if (max6675_temp & 4) 
+  {
+    // thermocouple open
+    max6675_temp = 2000;
+  }
+  else 
+  {
+    max6675_temp = max6675_temp >> 3;
+  }
+
+  return max6675_temp;
+}
+#endif
+
+
 // Timer 0 is shared with millies
 ISR(TIMER0_COMPB_vect)
 {
@@ -653,6 +738,9 @@ ISR(TIMER0_COMPB_vect)
       #if (TEMP_0_PIN > -1)
         raw_temp_0_value += ADC;
       #endif
+      #ifdef HEATER_0_USES_MAX6675 // TODO remove the blocking
+        raw_temp_0_value = read_max6675();
+      #endif
       temp_state = 2;
       break;
     case 2: // Prepare TEMP_BED
@@ -732,7 +820,7 @@ ISR(TIMER0_COMPB_vect)
     #endif
 
 #if EXTRUDERS > 1    
-    #ifdef HEATER_1_USES_AD595
+    #ifdef HEATER_1_USES_AD595 || defined HEATER_0_USES_MAX6675
       current_raw[1] = raw_temp_1_value;
     #else
       current_raw[1] = 16383 - raw_temp_1_value;