Go Down

Topic: Need Help Using Thermocouple with MAX6675 (Read 445 times) previous topic - next topic

mojomoney

I'm a newbie and I'm trying to use the MAX6675 with a K type TC, both of which I purchases from sparkfun, but I've got no clue where to begin to write the code to read the SPI from the MAX6675.  I read through the SPI EEPROM tutorial and that a bit over my head.  Has anyone else accomplished this, and if so, may I see the code that you used for this or for any other similar SPI read only sensor?  Please?  

kg4wsv

Here is some code that reads a DS1722 temperature sensor, which is an SPI device.  Not sure if the Dallas and Maxim protocols are similar, but it's an SPI example, anyway.

-j
Code: [Select]
#define DATAOUT 11 //MOSI
#define DATAIN  12 //MISO
#define SPICLOCK  13 //sck
#define SLAVESELECT 10 //ss
#define DS1722_POWER 9

#define DS1722_SELECT HIGH
#define DS1722_DESELECT LOW

#define DS1722_CONFIG_BYTE 0xEE
#define CONFIG_REG_READ 0x00
#define CONFIG_REG_WRITE 0x80
#define TEMP_ADDR_HI 0x02
#define TEMP_ADDR_LOW 0x01

byte clr;
byte temperature[2];

char spi_transfer(volatile char data)
{
 SPDR = data;                    // Start the transmission
 while (!(SPSR & (1<<SPIF)))     // Wait the end of the transmission
 {
 };
 return SPDR;                    // return the received byte
}

void setup()
{
 byte n;
 byte config = 0xAB;
 Serial.begin(9600);

 temperature[0]=0x12;
 temperature[1]=0x34;

 pinMode(DATAOUT, OUTPUT);
 pinMode(DATAIN, INPUT);
 pinMode(SPICLOCK, OUTPUT);
 pinMode(SLAVESELECT, OUTPUT);
 pinMode(DS1722_POWER, OUTPUT);
 digitalWrite(DS1722_POWER, HIGH); //disable device
 delay(250);
 
 digitalWrite(SLAVESELECT, DS1722_DESELECT); //disable device
 // set up SPI control register
 SPCR = (1<<SPE)|(1<<MSTR)|(1<<CPOL)|(1<<CPHA);
 clr=SPSR;
 clr=SPDR;
 delay(10);

 // read config byte
 digitalWrite(SLAVESELECT, DS1722_SELECT);
 spi_transfer(CONFIG_REG_READ);
 config = spi_transfer(0xFF);
 digitalWrite(SLAVESELECT, DS1722_DESELECT);
 delay(100);

 Serial.print(config, HEX);
 Serial.print('\n', BYTE);

 // write config byte to the configuration register
 digitalWrite(SLAVESELECT, DS1722_SELECT);
 spi_transfer(CONFIG_REG_WRITE);
 spi_transfer(DS1722_CONFIG_BYTE);
 digitalWrite(SLAVESELECT, DS1722_DESELECT);
 delay(100);

 // read config byte
 digitalWrite(SLAVESELECT, DS1722_SELECT);
 spi_transfer(CONFIG_REG_READ);
 config = spi_transfer(0xFF);
 digitalWrite(SLAVESELECT, DS1722_DESELECT);
 delay(100);

 Serial.print(config, HEX);
 Serial.print('\n', BYTE);

 Serial.print('h', BYTE);
 Serial.print('i', BYTE);
 Serial.print('\n', BYTE);//debug
 delay(1000);
}

void loop()
{
//  float c, f;
 // write config byte to the configuration register
 digitalWrite(SLAVESELECT, DS1722_SELECT);
 spi_transfer(CONFIG_REG_WRITE);
 spi_transfer(DS1722_CONFIG_BYTE);
 digitalWrite(SLAVESELECT, DS1722_DESELECT);

 delay(1400);

 digitalWrite(SLAVESELECT,DS1722_SELECT);
 spi_transfer(TEMP_ADDR_HI);
 temperature[0] = spi_transfer(0x00);
 digitalWrite(SLAVESELECT, DS1722_DESELECT); //release chip, signal end transfer
 delay(25); // just because....
 digitalWrite(SLAVESELECT,DS1722_SELECT);
 spi_transfer(TEMP_ADDR_LOW);
 temperature[1] = spi_transfer(0x00);
 digitalWrite(SLAVESELECT, DS1722_DESELECT); //release chip, signal end transfer

 Serial.print(temperature[0] * 9 / 5 + 32, DEC);
 Serial.print(' ', BYTE);
 Serial.print(temperature[0], DEC);
 if(temperature[1] & 0x80)
 {
   Serial.print( '.', BYTE);
   Serial.print( '5', BYTE);
 }
 Serial.print('\r', BYTE);
 Serial.print('\n', BYTE);

 delay(2000);
}


Go Up