AVR thermocouple interfacing problems using MAX6675

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

Hi everyone!

I need help with interfacing MAX6675 using SPI on ATMEGA16 - in GCC!!!
I have got K-type thermocouple (inside soldering pen) and i would like to get it's temperature in Celsius.
I was browsing the net for some tutorials or examples but nothing worked so i crated this topic.

I found some libraries too, but most of them was for arduino or they were too complicated.

I have learned basics about SPI and I2C, it's difficult to create library like this for me.

I'm newbie using MCUs.
Thank you all for helping me.

Last Edited: Mon. Jun 11, 2012 - 06:53 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

You do not need a library. There are code examples of SPI use in the Mega16 spec sheet. SPI is so fast that you generally don't even need interrupts. And, this is really not a "gcc problem"; its pretty much the same for all c compilers.

Jim

Jim Wagner Oregon Research Electronics, Consulting Div. Tangent, OR, USA http://www.orelectronics.net

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

Thank you Jim, I will try to find it out.

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

That one is specially easy. No registers to write to. Just read.

SO pin connects to the AVR MISO pin
SCK pin connects to the AVR SCK pin
/CS pin connects to the /SS pin

The one non-intuitive thing you have to do with the master (the AVR) is that IT has to "transmit" some byte for every byte it receives. So, you write anything to the data register, start it, wait for it to finish, then read the data register. Makes no difference what you transmit because it does not go anywhere.

You also have to manage the /SS pin in software. Note the section /SS Pin Functionality for details.

Jim

Jim Wagner Oregon Research Electronics, Consulting Div. Tangent, OR, USA http://www.orelectronics.net

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

Hi, I have read atmega16 and max6675 datasheets and atmel spi guide too. I made the following code: http://pastebin.com/aKk58z36

While termocouple is connected, program cant pass trought sending spi so the display shows only max6675 test.

When i diconnect termocouple from max6675 or whole max6675 and restart the program it says that termocouple is not connected. Haven't you any advice what can be wrong?

Thank you very much!
Luboš Fukan

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

Luboš, put up a schematic. The OP will be moved to the GE forum, o you ought to edit your title and put thermocouple in it too.

You ought to code without the "magic number" thing. Better to do, for example:

#define MAX6675_CS  0

void max6675_select() {
    cbi(PORTB, 4);
    PORTA &= ~( 1<< MAX6675_CS ); //led diode indication instead of debugger :((
}

Much better for getting what code doing ! You're not initializing SPI completely,get AVR151 from Atmel page. Move the spi_init() code out of the read() function and make it have own function ( why do init() every time read() called ? :wink: ) .

1) Studio 4.18 build 716 (SP3)
2) WinAvr 20100110
3) PN, all on Doze XP... For Now
A) Avr Dragon ver. 1
B) Avr MKII ISP, 2009 model
C) MKII JTAGICE ver. 1

Last Edited: Sat. Jun 23, 2012 - 12:11 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

#define CSLO() PORTC &= ~0x01
#define CSHI() PORTC |=  0x01

#define C1LO() PORTC &= ~0x02
#define C1HI() PORTC |=  0x02

unsigned int raw; //raw bits from max6675 (1/16th degrees)
float fqdeg,fqdegsm;
//----------------------------
void readmax6675(void){
//read max6675 thermocouple preamp     takes 220ms!
unsigned int dh,dl;
unsigned int qdeg; //quarter degrees

  INTR_OFF();
  SPCR=0x5B;    //0x5b =0x53 | 0x08 slow spi, clk pol flipped for max6675
  CSLO();       //conv halt, output bit 0
  spiout8(0);   //send dummy byte
  dh=readspi(); //read hi byte 
  spiout8(0);   //send dummy byte
  dl=readspi(); //read lo byte
  CSHI();       //conv resume
  SPCR=0x50;    //fastest spi back on for needle  
  INTR_ON();
  
  raw=dh*256 + dl; //raw thermocouple reading 1/16th deg C
  qdeg=raw >> 2;  //12 bit unsigned integer number (quarter degrees C)
  fqdeg=qdeg;
  fqdegsm += 0.1*(fqdeg-fqdegsm);
  degC=fqdegsm*0.25;  
  degF=degC*1.8 + 32.0;  
}

Imagecraft compiler user

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

Thank you guys i will try it all tomorrow, now i must learn to school.

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

indianajones11 wrote:

* ( that letter key on my keyboard not working )
*PI completely,

Is that a new american virus (eating kbd S'es ??)

You're the 2'nd i have seen today.

/Bingo

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

Bingo, for me I got lucky since it started working again. Probably a stuck key .

1) Studio 4.18 build 716 (SP3)
2) WinAvr 20100110
3) PN, all on Doze XP... For Now
A) Avr Dragon ver. 1
B) Avr MKII ISP, 2009 model
C) MKII JTAGICE ver. 1

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

So it took me a while to realize that the mcu is broken. I soleved it another way, i have used amplifier for the thermocouple output, i calibrated it and i read its value using adc (1°C=10mV) and woho it works. So thank you all. spi works great with another mcu. :D

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

Very good job, way to stick with it ! :D

1) Studio 4.18 build 716 (SP3)
2) WinAvr 20100110
3) PN, all on Doze XP... For Now
A) Avr Dragon ver. 1
B) Avr MKII ISP, 2009 model
C) MKII JTAGICE ver. 1

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

Where is the code sir ?, i am not able find codes;

sainisagar

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

sagarsaini wrote:

Where is the code sir ?, i am not able find codes;

Which code?

"Experience is what enables you to recognise a mistake the second time you make it."

"Good judgement comes from experience.  Experience comes from bad judgement."

"Wisdom is always wont to arrive late, and to be a little approximate on first possession."

"When you hear hoofbeats, think horses, not unicorns."

"Fast.  Cheap.  Good.  Pick two."

"We see a lot of arses on handlebars around here." - [J Ekdahl]

 

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

And after 7 years which sir??

John Samperi

Ampertronics Pty. Ltd.

www.ampertronics.com.au

* Electronic Design * Custom Products * Contract Assembly