Reading data from MAX6675

Go To Last Post
10 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I am using MAX 6675 with Atmega6490, i can't read data from the thermocouple,
the part of code which is used for reading data from MAX6675 is given

uint16_t MAX6675ReadTemp(void)
{
  uint16_t tempRead = 0;
  uint8_t valRead = 0;
  SPDR = 0x00;
  // pull SS Line low;
  SPI_PORT &= ~(1<<SS_BIT);

 
   // wait for transmissions complete
  while(!(SPSR & (1<<SPIF)));
  valRead = SPDR;
  SPDR = 0x00;

  tempRead = valRead;
  tempRead <<= 8; //shift 8 places to the left.

  // wait for transmission complete
  while(!(SPSR & (1<<SPIF))); // do nothing

  valRead = SPDR;
  tempRead = valRead;

  //tempRead |= valRead;

  // pull SS Line high
  SPI_PORT |= (1<<SS_BIT);


  return tempRead;
}
Last Edited: Sat. May 4, 2013 - 06:32 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

You have pulled Slave Select low AFTER initiating the first transfer. You need to do so BEFORE initiating the transfer.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Oh, okay. I just changed it but it made no difference to the result so there's something else wrong too. :(
I've connected an LCD to check the output of Max6675, but the change in temperature doesn't affect the value on LCD so I'm thinking that maybe data is not being read from the Max6675.

 // pull SS Line low;
  SPI_PORT &= ~(1<<SS_BIT);
  
  // start SPI data transfer
  SPDR = 0x00;
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Can you write to the lcd without reading the tc?

#define CSLO() PORTC &= ~0x40 //was 0x01 last rev
#define CSHI() PORTC |=  0x40
unsigned int raw; //raw bits from 6675 (1/4 degrees)
unsigned char dh,dl;
float fqdeg,fqdegsm;
//---------------------
void readmax6675(void){
//read max6675 thermocouple preamp     takes 220ms!  called every 250ms
//bit d2 lo means tc open
unsigned int qdeg; //quarter degrees

  INTR_OFF();
  CSLO();       //conv halt, output bit 0
  asm("nop");   //56ns
  asm("nop");   //56ns
  spiout8(0);   //send dummy byte, generates 8 clks
  dh=readspi(); //read hi byte 
  spiout8(0);   //send dummy byte, generates 8 clks
  dl=readspi(); //read lo byte
  CSHI();       //conv resume
  INTR_ON();
  
  raw=(((unsigned int)dh) << 8) + dl; //raw thermocouple reading 1/4 deg C x4? x8?
  tcopenerr=(raw & 0x0004) != 0; //check tc open err bit d2
  if(!tcopenerr){
    qdeg=raw >> 3;   //12 bit unsigned integer number (4096 quarter degC->1024 degC)  bg 12/18/12
    if(usetcomp){
      if(qdeg < 3200){
        tcorrfac=0.001*qdeg - 32; //dec 18 12 correction fac 8 deg at 0, goes to 0 at 800 deg c
      }else{
        tcorrfac=0.0;
      }	
      fqdeg=qdeg + tcorrfac; //add correction factor
    }else{	
      fqdeg=qdeg; //use raw 6675 reading (too hot at lo temps by 20 degF or so)
    }	 
    fqdegsm = fqdegsm + 0.5*(fqdeg-fqdegsm); //was .125 9/9/08
    tcdegC=fqdegsm * 0.25;      //cvt qdeg to degC  
    tcdegF=tcdegC * 1.8 + 32.0; //cvt degC to degF
  }	 
}

Imagecraft compiler user

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Yes, i can write to LCD without reading temperature.
Can you please explain this part of code to me, here 'temp' is the value of temperature returned from the code posted above. I got this code bit from the web (as mine wasn't working).

 temp &= 0x0003;
        memset(tempString,0,16);
        itoa(temp*25, tempString, 10);
        if(tempString[0] == '0');
          tempString[1] = '0';
        lcd_data_str(tempString);

        lcd_data_str(" ");
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

When you AND two bits together, you get a 1 if both are 1, otherwise you get a 0. So if I and temp with 0x0003, I am throwing away the top 14 bits and keeping the bottom two bits. Then the 2 b its that remain are converted to a string and sent to the lcd. I bet it reads 0.

Imagecraft compiler user

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

New modified code is posted as a new topic as i wasn't allowed to post it here. It is still not working.
Here is the link to that topic

https://www.avrfreaks.net/index.php?name=PNphpBB2&file=viewtopic&p=1062591#1062591

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I bet you cant post c code that has a percent in a printf statement. Just put a space after the percent sign.

Imagecraft compiler user

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Okay i have removed that printf statement. I think there is some issue with the clock signal. How do i send clock signal to MAX6675? Currently in my proteus design i can't see any clock pulse being transferred to MAX6675. I have connected SCK of MAX with SCK of Atmega 6490.
Here is the code for initializing MAX

void MAX6675Init(void)
{
SPI_DDR |= (1<<SS_BIT)|(1<<SCK_BIT);
SPI_DDR &= ~(1<<MOSI_BIT);
// enable SPI in Master Mode with SCK = CK/128
SPCR = (1<<SPE)|(1<<MSTR)|(1<<CPHA)|(1<<SPR0)|(1<<SPR1);
}
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Our code gives same value on LCD, even if we change the temperature, it doesn't change the value on LCD. I always get same value from MAX. :(