Embedded system Fun Blog
























































Find out all the best information, libraries and circuit about the latest Embedded systems.
Showing posts with label MAX6675. Show all posts
Showing posts with label MAX6675. Show all posts

Sunday, 6 January 2013

Arduino code for MAX6675

.from: https://github.com/codebendercc/arduino-files/blob/master/extra-libraries/max6675/max6675.cpp

X================================X max6675.h X================================X

// this library is public domain. enjoy!
// www.ladyada.net/learn/sensors/thermocouple

#if ARDUINO >= 100
 #include "Arduino.h"
#else
 #include "WProgram.h"
#endif

class MAX6675 {
 public:
  MAX6675(int8_t SCLK, int8_t CS, int8_t MISO);

  double readCelsius(void);
  double readFahrenheit(void);
  // For compatibility with older versions:
  double readFarenheit(void) { return readFahrenheit(); }
 private:
  int8_t sclk, miso, cs;
  uint8_t spiread(void);
};

X================================X max6675.cpp X================================X

// this library is public domain. enjoy!
// www.ladyada.net/learn/sensors/thermocouple

#include <avr pgmspace.h="pgmspace.h">
#include <util delay.h="delay.h">
#include <stdlib .h=".h">
#include "max6675.h"

MAX6675::MAX6675(int8_t SCLK, int8_t CS, int8_t MISO) {
  sclk = SCLK;
  cs = CS;
  miso = MISO;

  //define pin modes
  pinMode(cs, OUTPUT);
  pinMode(sclk, OUTPUT);
  pinMode(miso, INPUT);

  digitalWrite(cs, HIGH);
}
double MAX6675::readCelsius(void) {

  uint16_t v;

  digitalWrite(cs, LOW);
  _delay_ms(1);

  v = spiread();
  v &lt;&lt;= 8;
  v |= spiread();

  digitalWrite(cs, HIGH);

  if (v &amp; 0x4) {
    // uh oh, no thermocouple attached!
    return NAN;
    //return -100;
  }

  v &gt;&gt;= 3;

  return v*0.25;
}

double MAX6675::readFahrenheit(void) {
  return readCelsius() * 9.0/5.0 + 32;
}

byte MAX6675::spiread(void) {
  int i;
  byte d = 0;

  for (i=7; i&gt;=0; i--)
  {
    digitalWrite(sclk, LOW);
    _delay_ms(1);
    if (digitalRead(miso)) {
      //set the bit to 0 no matter what
      d |= (1 &lt;&lt; i);
    }

    digitalWrite(sclk, HIGH);
    _delay_ms(1);
  }

  return d;
}

X================================X X================================X


</stdlib></util></avr>