Home
JAQForum Ver 24.01
Log In or Join  
Active Topics
Local Time 10:56 01 Aug 2025 Privacy Policy
Jump to

Notice. New forum software under development. It's going to miss a few functions and look a bit ugly for a while, but I'm working on it full time now as the old forum was too unstable. Couple days, all good. If you notice any issues, please contact me.

Forum Index : Microcontroller and PC projects : How to talk to a MAX6675 with a MM2....

     Page 1 of 2    
Author Message
Grogster

Admin Group

Joined: 31/12/2012
Location: New Zealand
Posts: 9610
Posted: 07:05am 17 Jun 2021
Copy link to clipboard 
Print this post

Hi all

I have some MAX6675 chips I want to put to use, but I am unsure how to talk to them.
They use a simple-ish sounding SPI type protocol, so my first thought was simply the built-in SPI protocol in the MM2.

However, these devices output 12-bit words, not 8/16/32-bit ones that the SPI channels are designed to deal with, so I think I would need some kind of bit-banging approach to talk to them, yes?

DATASHEET:

MAX6675 K-type thermocouple to SPI interface.pdf

Page 5 gives details on the serial interface, and it seems easy enough - pull the 6675's CS pin low as you would any other SPI device, feed it a clock signal to its clock input, and read the bits out.

The last bit(pun not intended!) is the bit I am stuck with.
How could I read 12 bits when the SPI channel protocol in the MM2 only allows you to have 8-bit, 16-bit or 32-bit words?

Twelve is not compatible with any of those.  

I am wondering if I could read 16-bits, and somehow disregard the extra 4 bits?

I know my way around UART at an expert level now, but am still something of an amateur with I2C or SPI, simply as I don't really use them that much.

Can anyone here suggest how I might go about reading the output from these devices?

Thanks for any replies to this thread.
Smoke makes things work. When the smoke gets out, it stops!
 
Grogster

Admin Group

Joined: 31/12/2012
Location: New Zealand
Posts: 9610
Posted: 07:14am 17 Jun 2021
Copy link to clipboard 
Print this post

Further to the above post, having read my way further into the datasheet, I THINK the output is actually a 16-bit word - see page 6.

It would seem I could not see the forest for the trees - my brain wondering how to read 12-bits, not realizing that there ARE actually 16-bits in the data output - 12 of those 16 are thermocouple data bits, the other bits - are other bits.    

Where is my brain? (RHETORICAL!!!!)    

EDIT: Now I have a 16-bit word, I need to be able to filter just bits 14 to 3 - the 12-bit data.  Assume the other bits are all zero.

Could I use one of those AND mask ideas for that?

6675 data byte AND 1000000000000111 kind of idea?

I'm not sure I have the mask idea correct either, but hopefully you can see what I mean - ignore bits 15, 2, 1 and 0 - just give me the value of the other 12 bits of the word.....
Edited 2021-06-17 17:21 by Grogster
Smoke makes things work. When the smoke gets out, it stops!
 
Volhout
Guru

Joined: 05/03/2018
Location: Netherlands
Posts: 5089
Posted: 07:24am 17 Jun 2021
Copy link to clipboard 
Print this post

Yes, it is 16 bit.

When you get the response you have to use bits 14...3 to get the 12 bit result.
As you suggested, mask it, and then >>3 (or divide by 8).

Configure the SPI port for "data valid at falling edge of clock". The max. SPI clock is 4.3MHz, so if you use 1MHz you will have plenty of margin.

Volhout...
Edited 2021-06-17 17:26 by Volhout
PicomiteVGA PETSCII ROBOTS
 
Grogster

Admin Group

Joined: 31/12/2012
Location: New Zealand
Posts: 9610
Posted: 07:28am 17 Jun 2021
Copy link to clipboard 
Print this post

OK, I acknowledge the 1MHz clock suggestion.

Now, how do I read JUST bits 14-3?
Generally speaking, bits 15, 2, 1 and 0 should be zero, so just reading the value of the 16-bit word would be fine, BUT if bits 2, 1 or 0 change, that will affect the value of the returned word, so I need to work out how to mask out those bits.

Will my mask idea above work do you think?

EDIT: We're crossing over editing!

Pseudo code:


Data = 6675 byte AND 1000000000000111 >>3


I will try it tomorrow, but I want to be reasonably sure I understand what I am doing before I do.

EDIT: WHY do I need to >>3 or divide by eight?  Don't we want the full 12-bit data?  I might be missing something VERY obvious here - I am no SPI expert in any interpretation of the word!
Edited 2021-06-17 17:33 by Grogster
Smoke makes things work. When the smoke gets out, it stops!
 
JohnS
Guru

Joined: 18/11/2011
Location: United Kingdom
Posts: 4044
Posted: 07:50am 17 Jun 2021
Copy link to clipboard 
Print this post

That AND will remove the bits you want.

But you could do it (using TheWord as it's not a byte!).

You'll need to prefix the numbers with something so MMBasic sees a binary number
(I'm away and can't recall how MMBasic does it, but let's guess 0b prefix):

E.g.
Data = TheWord AND 0b1000000000000111
Data = TheWord - Data

Or
Data = TheWord AND 0b0111111111111000

Then you have roughly what you want.

It's in the wrong place so you need to shift it right 3 bits as others have posted.

Data = Data >> 3

John
Edited 2021-06-17 17:51 by JohnS
 
Grogster

Admin Group

Joined: 31/12/2012
Location: New Zealand
Posts: 9610
Posted: 08:11am 17 Jun 2021
Copy link to clipboard 
Print this post

Thanks chums.

So, therefore:


T=THCOUPLE AND &b011111111111000


THCOUPLE being the 16-bit word read back from the 6675 chip.

???

It looks like I had my AND idea the wrong way around.
(what should have been zeros were ones and vise-versa)

Or, perhaps shortened to:


D=THCOUPLE AND &h7FF8 >>3

Edited 2021-06-17 18:12 by Grogster
Smoke makes things work. When the smoke gets out, it stops!
 
JohnS
Guru

Joined: 18/11/2011
Location: United Kingdom
Posts: 4044
Posted: 09:13am 17 Jun 2021
Copy link to clipboard 
Print this post

Yes, but I'd use some parens

D=(THCOUPLE AND &h7FF8) >>3

John
 
Grogster

Admin Group

Joined: 31/12/2012
Location: New Zealand
Posts: 9610
Posted: 04:32am 18 Jun 2021
Copy link to clipboard 
Print this post

Great, thanks.
Smoke makes things work. When the smoke gets out, it stops!
 
Turbo46

Guru

Joined: 24/12/2017
Location: Australia
Posts: 1642
Posted: 08:35pm 18 Jun 2021
Copy link to clipboard 
Print this post

It appears to me that if you only want the 12 bits of data and are going to ignore the umpteen higher bits in the integer then all you need to do is:

D = THCOUPLE >> 3

Bits 0 to 11 will hold the data you want.

Bill
Keep safe. Live long and prosper.
 
Grogster

Admin Group

Joined: 31/12/2012
Location: New Zealand
Posts: 9610
Posted: 09:24am 19 Jun 2021
Copy link to clipboard 
Print this post

OK, I will try that also - thanks.  
Smoke makes things work. When the smoke gets out, it stops!
 
Grogster

Admin Group

Joined: 31/12/2012
Location: New Zealand
Posts: 9610
Posted: 07:09am 24 Jul 2021
Copy link to clipboard 
Print this post

Further to this, I am getting a result, but it is ALWAYS the same number no matter what.  I have tried all the SPI modes, and although I get different values for different modes, the values for any given mode is always the same number.

I pick up the thermocouple in my hand, which should change the reading as my hand warms up the thermocouple, but that is not happening.

Anyone got any ideas of anything else I can try?





Smoke makes things work. When the smoke gets out, it stops!
 
phil99

Guru

Joined: 11/02/2018
Location: Australia
Posts: 2640
Posted: 07:53am 24 Jul 2021
Copy link to clipboard 
Print this post

As an experiment to see what the thermocouple is doing, print the whole 16 bits in binary then warm it and see which bits change.

Do
Pause 1000
BYTE = SPI(0)
Print BIN$(BYTE, 16), BIN$((BYTE >> 3),16)
loop

The data sheet says bit 15 is always 0 so:

D = BYTE >> 3

As shown in a pervious post should do the trick.

Also try swapping D+ and D- to see if the thermocouple polarity is wrong.
Edited 2021-07-24 18:26 by phil99
 
Volhout
Guru

Joined: 05/03/2018
Location: Netherlands
Posts: 5089
Posted: 05:55pm 24 Jul 2021
Copy link to clipboard 
Print this post

Hi Gogster,

If you get consistent &h84 out of the MAX6675 it is possible (I don't have one to prove it) that the T- terminal is not connected to ground. Of coarse it should be connected to the thermocouple, but also to GND. Bit 2 (the "4" in &h84) signals a open thermocouple when T- is not connected to ground.

Another thing to try: change the clock edge. The datasheet is not very clear about it. It gives data valid from rising edge. (tDO)

SPI open 100000,0,16


I wish I has a MAX6675, so I'd be better able to help you.

Regards,

Volhout

See page 5

MAX6675
Edited 2021-07-25 04:05 by Volhout
PicomiteVGA PETSCII ROBOTS
 
disco4now

Guru

Joined: 18/12/2014
Location: Australia
Posts: 1003
Posted: 12:07am 25 Jul 2021
Copy link to clipboard 
Print this post

In a previous discussion here geoff suggests SPI should be opened in mode 2 for this device. Not sure of the outcome but might help.

SPI OPEN 100000, 2, 16 ' speed is 100KHz, data size is 16 bits


Gerry
Latest F4 Latest H7 FotS
 
Grogster

Admin Group

Joined: 31/12/2012
Location: New Zealand
Posts: 9610
Posted: 04:16am 25 Jul 2021
Copy link to clipboard 
Print this post

Hi chums.

This is the result of phil99's code changes:





Does not matter if I touch the thermocouple or not, result is always the same.

This is the result of disco4now/Geoff's code:





I tried slowing down to 10kHz SPI speed, and the data is ALWAYS still the same, no matter what.  I will hunt out another thermocouple and try again.

EDIT: Removed thermocouple unit completely, and still get the same results.  Something not right here.  Either the thermocouple is crook, or the chip is or something.

EDIT: Put a brand new(different) thermocouple on it, still exactly the same output.  I wonder if the module is a fake.  I think I will order a few genuine chips in my next element14 order, and swap one of the chips.  SOIC8, so easy enough to remove.  I can already hear WhiteWizzard scolding me for this.....
Edited 2021-07-25 14:55 by Grogster
Smoke makes things work. When the smoke gets out, it stops!
 
TassyJim

Guru

Joined: 07/08/2011
Location: Australia
Posts: 6283
Posted: 04:50am 25 Jul 2021
Copy link to clipboard 
Print this post

The results form Phil's code looks like 14.75 degrees.
Could that be your current ambient?
If so, it might indicate that you have t+ and t- shorted.

Try that code again with the thermocouple removed but t- still connected to ground.

That should light up bit D2

Jim

Just saw your edit - you must be a mind reader.
Look for a short between the two input pins.
Edited 2021-07-25 14:51 by TassyJim
VK7JH
MMedit
 
Grogster

Admin Group

Joined: 31/12/2012
Location: New Zealand
Posts: 9610
Posted: 05:03am 25 Jul 2021
Copy link to clipboard 
Print this post

Hi Jim.

No, ambient is 22'C or so.

Result of disconnecting T+, but leaving T- connected: (T- is grounded)



Edited 2021-07-25 15:05 by Grogster
Smoke makes things work. When the smoke gets out, it stops!
 
TassyJim

Guru

Joined: 07/08/2011
Location: Australia
Posts: 6283
Posted: 05:12am 25 Jul 2021
Copy link to clipboard 
Print this post

OK,
Bit 2 has correctly gone high to indicate open circuit thermocouple.
Next try shorting T+ and T-
VK7JH
MMedit
 
Grogster

Admin Group

Joined: 31/12/2012
Location: New Zealand
Posts: 9610
Posted: 05:18am 25 Jul 2021
Copy link to clipboard 
Print this post

FOUND THE PROBLEM!!!!

I was NOT releasing the CS pin at the end of each reading, so I guess the chip was doing ONE sample, and then cos I never released the CS pin, it just kept sending the same data each time through the loop.  

Results I am getting now: (third column is degrees C)





...and the code I am now using:





Now that I have it working, I can now experiment some more.
Results are coming in now for both the original thermo, and the new one I tried.
Smoke makes things work. When the smoke gets out, it stops!
 
TassyJim

Guru

Joined: 07/08/2011
Location: Australia
Posts: 6283
Posted: 05:25am 25 Jul 2021
Copy link to clipboard 
Print this post

That's good news.
Something to store in the memory bank for later use...

Jim

I went looking through the data sheet again:
  Quote  Force CS low and apply a clock signal at SCK to read
the results at SO. Forcing CS low immediately stops
any conversion process. Initiate a new conversion
process by forcing CS high.

Edited 2021-07-25 15:29 by TassyJim
VK7JH
MMedit
 
     Page 1 of 2    
Print this page
The Back Shed's forum code is written, and hosted, in Australia.
© JAQ Software 2025