Forum Menu




 


Log in Problems?
New User? Sign Up!
AVR Freaks Forum Index

Post new topic   Reply to topic
View previous topic Printable version Log in to check your private messages View next topic
Author Message
fukanlubos
PostPosted: Jun 07, 2012 - 10:11 PM
Newbie


Joined: Jun 07, 2012
Posts: 5


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 by fukanlubos on Jun 11, 2012 - 07:53 AM; edited 1 time in total
 
 View user's profile Send private message  
Reply with quote Back to top
ka7ehk
PostPosted: Jun 07, 2012 - 10:31 PM
10k+ Postman


Joined: Nov 22, 2002
Posts: 12049
Location: Tangent, OR, USA

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

"The only thing standing between us and victory is defeat" P.G.Wodhouse in Wooster & Jeeves series
 
 View user's profile Send private message  
Reply with quote Back to top
fukanlubos
PostPosted: Jun 07, 2012 - 10:47 PM
Newbie


Joined: Jun 07, 2012
Posts: 5


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

_________________
Luboš Fukan
AVR newbie, Czech Republic
 
 View user's profile Send private message  
Reply with quote Back to top
ka7ehk
PostPosted: Jun 07, 2012 - 11:41 PM
10k+ Postman


Joined: Nov 22, 2002
Posts: 12049
Location: Tangent, OR, USA

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

"The only thing standing between us and victory is defeat" P.G.Wodhouse in Wooster & Jeeves series
 
 View user's profile Send private message  
Reply with quote Back to top
fukanlubos
PostPosted: Jun 08, 2012 - 04:44 PM
Newbie


Joined: Jun 07, 2012
Posts: 5


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
 
 View user's profile Send private message  
Reply with quote Back to top
indianajones11
PostPosted: Jun 10, 2012 - 10:18 PM
Raving lunatic


Joined: Nov 28, 2004
Posts: 3552
Location: San Diego, Ca

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:
Code:
#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 by indianajones11 on Jun 23, 2012 - 01:11 PM; edited 1 time in total
 
 View user's profile Send private message  
Reply with quote Back to top
bobgardner
PostPosted: Jun 10, 2012 - 11:31 PM
10k+ Postman


Joined: Sep 04, 2002
Posts: 21261
Location: Orlando Florida

Code:


#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
 
 View user's profile Send private message Send e-mail Visit poster's website 
Reply with quote Back to top
fukanlubos
PostPosted: Jun 11, 2012 - 02:07 PM
Newbie


Joined: Jun 07, 2012
Posts: 5


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

_________________
Luboš Fukan
AVR newbie, Czech Republic
 
 View user's profile Send private message  
Reply with quote Back to top
Bingo600
PostPosted: Jun 12, 2012 - 09:12 PM
Raving lunatic


Joined: Apr 25, 2004
Posts: 3809
Location: Denmark

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
 
 View user's profile Send private message  
Reply with quote Back to top
indianajones11
PostPosted: Jun 23, 2012 - 01:13 PM
Raving lunatic


Joined: Nov 28, 2004
Posts: 3552
Location: San Diego, Ca

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
 
 View user's profile Send private message  
Reply with quote Back to top
fukanlubos
PostPosted: Sep 09, 2012 - 05:13 PM
Newbie


Joined: Jun 07, 2012
Posts: 5


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. Very Happy

_________________
Luboš Fukan
AVR newbie, Czech Republic
 
 View user's profile Send private message  
Reply with quote Back to top
indianajones11
PostPosted: Sep 10, 2012 - 05:51 AM
Raving lunatic


Joined: Nov 28, 2004
Posts: 3552
Location: San Diego, Ca

Very good job, way to stick with it ! Very Happy

_________________
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
 
 View user's profile Send private message  
Reply with quote Back to top
Display posts from previous:     
Jump to:  
All times are GMT + 1 Hour
Post new topic   Reply to topic
View previous topic Printable version Log in to check your private messages View next topic
Powered by PNphpBB2 © 2003-2006 The PNphpBB Group
Credits