PDA

View Full Version : Having trouble with SERIN command and MAX6675



emerson
- 10th December 2010, 04:13
Hello again. I'm trying to have a 16f630 receive serial data from a MAX6675. I read the SERIN section in the help topic but I still can't understand how to set it up. here are the datasheets for the chips and my code as well:

http://www.datasheetarchive.com/pdf-datasheets/Datasheets-301/8892.html

http://pdf1.alldatasheet.com/datasheet-pdf/view/87840/MICROCHIP/16F630.html




INCLUDE "modedefs.bas"

'Setup MAX6675 - Thermocouple
MAXData var porta.5 ' MAXpin7
MAXcs var porta.4 ' MAXpin6
MAXClock var porta.2 ' MAXpin5

MAXResult var word
THC var word ' degrees Celsius (* power of 10)
THF var word 'stores degrees F

Main:

low MAXcs ' select chip
serin maxdata, N2400,maxclock, [MAXResult\16] ' get data
high MAXcs ' start new data apture

THC= (MAXResult/32) ' Deg. C
THF= (((THC*18)/10)+32) 'Conversion to Deg. F

lcdout $fe, 1
lcdout "Temperature: ", dec THF, "F"] 'display temp. on LCD

pause 20 'give time between read cycles for MAX6675 to work right

goto Main

mackrackit
- 10th December 2010, 04:43
Wrong type of serial. This shows how to read it
http://www.picbasic.co.uk/forum/showthread.php?t=10759&p=71178#post71178
And if you want degrees F add this bit
TempF = ABS(TempC) */ 461
TempF = TempF + 32

emerson
- 10th December 2010, 04:58
Wrong type of serial. This shows how to read it
http://www.picbasic.co.uk/forum/showthread.php?t=10759&p=71178#post71178
And if you want degrees F add this bit
TempF = ABS(TempC) */ 461
TempF = TempF + 32

I tried the code and when I compile I get a "Invalid RAM location specified". How can I fix this?

Also, all I see on the LCD is a line of black boxes across the top row. Is this a timing problem?
The same circuit setup works with my other lcd program so I know it's not a wiring error.

I still need to change the ports...whoops

mackrackit
- 10th December 2010, 05:04
Normally black boxes are a contrast problem, but is the LCD cod is the same as on another project...

Post your whole current code and config setup so we can have a look.

emerson
- 10th December 2010, 05:21
Normally black boxes are a contrast problem, but is the LCD cod is the same as on another project...

Post your whole current code and config setup so we can have a look.

This is what I have so far:


INCLUDE "modedefs.bas"

define OSC 4

'Alias pins - MAX6675 temp sensor
MXSCLK var PORTA.5 'Clock
MXCS var PORTA.2 'Chip Select
MXSO Var PORTA.4 'Serial out

'Allocate MAX6875 Variables
MXTemp var word 'raw data from 6675/type K sensor
TempC var word 'converted to degrees C


'-----------Read and display temperature from MAX6675-----------
MXCS = 0 'Chip select low
shiftin MXSO, MXSCLK, 0, [MXTemp\16] 'read the data to MXTemp
MXCS = 1 'Chip select high
TempC = MXtemp >> 5 'right shift the data 5 places to get degrees C (Read the Data sheet)
LCDOUT $fe,2,"Temp = ", DEC TempC, "degrees C " ' Spit it out on an LCD

The contrast on the LCD is alright I have it adjusted properly. What does "Invalid RAM location specified" mean?

mackrackit
- 10th December 2010, 05:34
What does "Invalid RAM location specified" mean?

In this case it means you are compiling a LCDOUT command without having LCD defines.

emerson
- 10th December 2010, 13:50
In this case it means you are compiling a LCDOUT command without having LCD defines.

Alright, I put my LCD defines into the code and i get a readout of 32 degrees F (the temp probe on my multimeter reads the room is 70 degrees F), but the temperature didn't change when I put a lighter to the temp probe. I added a main loop to cycle through the program but that didn't help. Is it something with the SHIFTIN command or could I have fried the surface mount chip when soldering leads to it?
And thank you for your help. I've been working on this for weeks with no turnout.

Here is my current code:


INCLUDE "modedefs.bas"

define OSC 4

'Define LCD registers and bits
DEFINE LCD_DREG PORTC 'Define PIC port used for LCD Data lines
DEFINE LCD_DBIT 0 'LCD Data Starting Bit = Bit 0 or 4
DEFINE LCD_EREG PORTC 'Define PIC port used for E line of LCD
DEFINE LCD_EBIT 5 'Define Port pin used for E connection
DEFINE LCD_RSREG PORTC 'Define PIC port used for RS line of LCD
DEFINE LCD_RSBIT 4 'Define Port pin used for RS connection
DEFINE LCD_BITS 4 'Define the 4 bit communication mode to LCD
DEFINE LCD_LINES 2 'Define using a 2 line LCD
DEFINE LCD_COMMANDUS 2000 'Define delay time between sending LCD commands
DEFINE LCD_DATAUS 50 'Define delay time between data sent.

Pause 500 ' Wait for LCD to startup

'Alias pins - MAX6675 temp sensor
MXSCLK var PORTA.5 'Clock MAX pin 5
MXCS var PORTA.2 'Chip Select MAX pin 6
MXSO Var PORTA.4 'Serial out MAX pin 7

'Allocate MAX6875 Variables
MXTemp var word 'raw data from 6675/type K sensor
TempC var word 'converted to degrees C
TempF var word


'-----------Read and display temperature from MAX6675-----------

Main:

MXCS = 0 'Chip select low
shiftin MXSO, MXSCLK, 0, [MXTemp\16] 'read the data to MXTemp
MXCS = 1 'Chip select high
TempC = MXtemp >> 5 'right shift the data 5 places to get degrees C (Read the Data sheet)
TempF = ABS(TempC) */ 461
TempF = TempF + 32
lcdout $fe, 1
LCDOUT $fe,2," Temperature "
lcdout $fe, $c0, " Faren=", DEC Tempf, " Cel=", dec tempc ' Spit it out on an LCD
pause 500

goto main

Bruce
- 10th December 2010, 15:34
You want to read each bit on the falling edge of the clock so use mode 2. And you only need to shift MXtemp >> 3. Not 5.

emerson
- 10th December 2010, 15:50
You want to read each bit on the falling edge of the clock so use mode 2. And you only need to shift MXtemp >> 3. Not 5.

I changed both but there's still no response? On the datasheet it says the MAX6675 serial clock frequency is 4.3MHz, and I'm running my pic at 4MHz, will this effect data communication?

Bruce
- 10th December 2010, 16:02
If you have an O-scope check the data out pin. That will at least let you know if it's responding.

emerson
- 10th December 2010, 16:59
If you have an O-scope check the data out pin. That will at least let you know if it's responding.

the O-scope test checked out alright

emerson
- 10th December 2010, 17:39
I just disabled the comparators in the chip but it still won't. I even replaced the thermocouple and the MAX76675 but i get the same response. I have no idea what could be wrong with this. Any ideas?

Bruce
- 10th December 2010, 17:40
Have you tried sending the raw value received 'without modifying it' to your LCD while heating or cooling the part?

Something like: lcdout $fe, $c0, BIN16 MXtemp

Does it change when heated?

Edit: Since you're using the internal osc on this one, place Define OSCCAL_1K 1 in the top section. Not sure it that will fix it, but it will for sure affect timing.

emerson
- 10th December 2010, 17:51
Have you tried sending the raw value received 'without modifying it' to your LCD while heating or cooling the part?

Something like: lcdout $fe, $c0, BIN16 MXtemp

Does it change when heated?

Edit: Since you're using the internal osc on this one, place Define OSCCAL_1K 1 in the top section. Not sure it that will fix it, but it will for sure affect timing.

I just tried it and there is no change

Bruce
- 10th December 2010, 17:56
What's the 16-bit value shown?

emerson
- 10th December 2010, 18:01
What's the 16-bit value shown?

0000 0000 0000 0000

zeros across the board with no change to heat

mackrackit
- 10th December 2010, 18:11
Is the thermocouple a K type?
The code given in the link I gave works.
Check the hardware. Bad connections or backwards connection on the thermocouple or wrong type will cause problems.

Bruce
- 10th December 2010, 18:12
Did you look at the data output pin on the MAX chip with your O-scope to see if it's changing the value sent?

If you send 16 clocks, and the data line stays at ground, I would think there's a problem with the MAX IC. If you see transitions on the data line, there's something going on with the PIC side.

emerson
- 10th December 2010, 18:24
Is the thermocouple a K type?
The code given in the link I gave works.
Check the hardware. Bad connections or backwards connection on the thermocouple or wrong type will cause problems.

Both the thermocouples I have are K type and are connected properly. The ground lead on the thermocouple is tied to ground as well as the negative input on the MAX6675 as demonstrated in the datasheet. I did a continuity test on all wires coming from the chip and they're all good.

emerson
- 10th December 2010, 19:31
is the code you gave specifically for the 16f630?

mackrackit
- 10th December 2010, 19:38
No. The code shows how to use the MAX chip.

emerson
- 10th December 2010, 20:14
i found in this post (http://www.picbasic.co.uk/forum/showthread.php?t=10759&p=71178#post71178) that you stated you need a temperature
reference for every time the wire material from the thermocouple changes.


Anyplace the wire material changes you need a reference temperature and calcs. A couple exceptions here and there though.

I had copper wire soldered to the terminals on my thermocouple. I disconnected the copper wires but it didn't help

mackrackit
- 10th December 2010, 22:03
Do you have pin #6 (CS) on the MAX6675 pulled low? I use a 10K to the zero rail on mine.

aratti
- 11th December 2010, 11:23
MAX6675 need more than 200 millisecs for the temperature convertion. You are polling too fast so you don't get any response. Place a delay of 250 msecs to allow convertion.

edit:My note was reffered to code seen in post#5. I just see that you added delay 500 in your code in post #7, so my answer is obsolete.
My new suggestion are :

add INCLUDE "modedefs.bas" on top of your code
and modify your SHIFTIN command adding:

shiftin MXSO, MXSCLK, MSBPRE, [MXTemp\16] 'read the data to MXTemp

and see if it works.



Al.

flotulopex
- 15th April 2017, 20:59
Hi,

It's my first time using a MAX6675 k-type thermocouple converter and got it to work thanks to (99%) of the info in this thread :).

AAMOF, I had to modify one info found above and it is about bit shifting.

First, my working code (and hopefully correct) is:
' ====== FUSES ================================================== ==================================
' PIC 16F690
@ __Config _FCMEN_OFF &_IESO_OFF &_CPD_OFF &_WDT_OFF &_INTRC_OSC_NOCLKOUT &_BOR_OFF &_CP_OFF &_PWRTE_OFF &_MCLRE_OFF

' ====== REGISTERS ================================================== ==============================
' 76543210
OPTION_REG = %10000000 ' PORT A&B Pull-Ups disabled (see WPUA & WPUB)
OSCCON = %01100000 ' Internal RC set to 4Mhz - not to be used with XTal
ANSEL = %00000000 ' Analog inputs Channels Bit<0>=0 to Bit<7>=7
ANSELH = %00000000 ' Analog inputs Channels Bit<0>=8 to Bit<3>=11
ADCON0 = %00000000 ' A/D Module OFF
CM1CON0 = %00000000 ' Comparator1 Module is OFF
CM2CON0 = %00000000 ' Comparator2 Module is OFF
INTCON = %00000000 ' INTerrupts CONtrol
PORTA = %00000000 ' Ports High/Low (0 to 5)
TRISA = %00000000 ' Set Input/Output (0 to 5)
PORTB = %00100000 ' Ports High/Low (4 to 7) B5=MAX6675-CS
TRISB = %00010000 ' Set Input/Output (4 to 7) B4=MAX6675-SDO
PORTC = %00000000 ' Ports High/Low (0 to 7)
TRISC = %00000000 ' Set Input/Output (0 to 7)

' ====== DEFINES ================================================== ================================
DEFINE OSC 4

DEFINE SHIFT_PAUSE 250 'for MAX6675 conversion time

DEFINE LCD_DREG PORTC 'LCD data port
DEFINE LCD_DBIT 0 'LCD data starting bit 0 or 4 on µC
DEFINE LCD_RSREG PORTC 'LCD Register Select port
DEFINE LCD_RSBIT 4 'LCD Register Select bit
DEFINE LCD_EREG PORTC 'LCD Enable port
DEFINE LCD_EBIT 5 'LCD Enable bit
DEFINE LCD_BITS 4 'LCD bus size 4 or 8
DEFINE LCD_LINES 2 'Line number on LCD

' ====== VARIABLES ================================================== ==============================
MAX6675_SDO var PORTB.4
MAX6675_CS var PORTB.5
MAX6675_SCK var PORTB.6

RawTemp var word
RawTemp = 0
Temp var byte
Temp = 0
TempUnit var byte
TempUnit = 0
TempDec var byte
TempDec = 0

' ====== PROGRAM ================================================== ================================

PAUSE 500 ' time for LCD to settle

MAX6675_READ:

MAX6675_CS = 0
shiftin MAX6675_SDO,MAX6675_SCK,0,[RawTemp\16] 'mode is MSBPRE
MAX6675_CS = 1

Temp = RawTemp >> 2

TempUnit = Temp / 10 'get temperature Units for display
TempDec = Temp MOD 10 'get temperature Decimals for display
lcdout $FE, $80,"BIN ", BIn16 Rawtemp
lcdout $FE, $C0,"Tmp ", DEC TempUnit,".",dec TempDec," C"

PAUSE 1000

GOTO MAX6675_READ
END

According to the MAX6675 datasheet: "Read the 16 output bits on the falling edge of the clock. The first bit, D15, is a dummy sign bit and is always zero. Bits D14–D3 contain the converted temperature in the order of MSB to LSB".

If I get it well, when reading the termocouple, I get 16 bits and I need to "keep" bits 14 to 3.
8391

So, to get the (what I hope is correct) right value, why do I have to shift by "2" instead of what has been said above (shift by 3) or even shift by 1 since bit 14 is the second bit to start with?

mackrackit
- 16th April 2017, 00:35
Unless you are trying to get the fractional part of the reading 10 bits is all you need.

flotulopex
- 16th April 2017, 06:39
Unless you are trying to get the fractional part of the reading 10 bits is all you need.
I don't get it, Dave...

richard
- 16th April 2017, 09:10
why do I have to shift by "2" instead of what has been said above (shift by 3) or even shift by 1 since bit 14 is the second bit to start with?

the data can be read in msb first by shiftin but it is then translated to a normal binary representation so bit 14 is read as bit 14.

shifting by 2 is incorrect , as dave indicated
Temp = RawTemp >> 5
will get the high 10 bits of the 12bit reading ,resulting in the integer component of the reading

or

Temp = RawTemp >> 3
int_part=temp/4
fract_part=(temp//4)*25

lcdout $FE, $C0,"Tmp ", #int_part,".",dec2 fract_part," C"

flotulopex
- 16th April 2017, 12:41
This is confusing to me.

My code gives me almost exactly what my home thermometer shows:
8394

This code
Temp = RawTemp >> 3
int_part=temp/4
fract_part=(temp//4)*25
lcdout $FE, $C0,"Tmp ", #int_part,".",dec2 fract_part," C" display this:
8395
Where is the truth?

richard
- 16th April 2017, 13:26
for a start your temp var is a declared as byte , how can 12 bit reading fit in that ?

flotulopex
- 16th April 2017, 13:40
I corrected my code just after I sent it - couldn't edit the post anymore, should have mentionned it, sorry :o

flotulopex
- 16th April 2017, 13:43
Anyway, I understand the calculation to get the temperature value BUT why do I have around 5°C diffrence between the thermocouple and my home thermometer?

Must a thermocouple be kind of "adjusted" (correct the result in the code) or something like that?

flotulopex
- 16th April 2017, 13:51
My actual code is:
' ====== FUSES ================================================== ==================================
' PIC 16F690
@ __Config _FCMEN_OFF &_IESO_OFF &_CPD_OFF &_WDT_OFF &_INTRC_OSC_NOCLKOUT &_BOR_OFF &_CP_OFF &_PWRTE_OFF &_MCLRE_OFF

' ====== DEFINES ================================================== ================================
DEFINE OSC 4

DEFINE SHIFT_PAUSE 250 'for MAX6675 conversion time

DEFINE LCD_DREG PORTC 'LCD data port
DEFINE LCD_DBIT 0 'LCD data starting bit 0 or 4 on µC
DEFINE LCD_RSREG PORTC 'LCD Register Select port
DEFINE LCD_RSBIT 4 'LCD Register Select bit
DEFINE LCD_EREG PORTC 'LCD Enable port
DEFINE LCD_EBIT 5 'LCD Enable bit
DEFINE LCD_BITS 4 'LCD bus size 4 or 8
DEFINE LCD_LINES 2 'Number lines on LCD

' ====== REGISTERS ================================================== ==============================
' 76543210
OPTION_REG = %10000000 ' PORT A&B Pull-Ups disabled (look WPUA & WPUB)
'OSCCON = %01100000 ' Internal RC set to 4Mhz - not to be used with XTal
ANSEL = %00000000 ' Analog inputs Channels 0 to 7
ANSELH = %00000000 ' Analog inputs Channels 8 to 11
ADCON0 = %00000000 ' A/D Module is OFF
CM1CON0 = %00000000 ' Comparator1 Module is OFF
CM2CON0 = %00000000 ' Comparator2 Module is OFF
INTCON = %00000000 ' INTerrupts CONtrol / RA2 is ON
PORTA = %00000000 ' Ports High/Low (0 to 5)
TRISA = %00000000 ' Set Input/Output (0 to 5)
PORTB = %00000000 ' Ports High/Low (4 to 7)
TRISB = %00000000 ' Set Input/Output (4 to 7)
PORTC = %00000000 ' Ports High/Low (0 to 7)
TRISC = %00000000 ' Set Input/Output (0 to 7)

' ====== VARIABLES ================================================== ==============================
MAX6675_SDO var PORTB.4
MAX6675_CS var PORTB.5
MAX6675_SCK var PORTB.6

Temperature var word
Temperature = 0

' ====== PROGRAM ================================================== ================================

MAX6675_READ:

MAX6675_CS = 0
shiftin MAX6675_SDO,MAX6675_SCK,0,[Temperature\16]
MAX6675_CS = 1

Temperature = Temperature >> 3

lcdout $FE,2,DEC Temperature/4,",",DEC (Temperature//4)*25," C"

PAUSE 1000

Goto MAX6675_READ:

END

richard
- 16th April 2017, 13:56
if you have cheap ebay sourced k type probes a reading of 5 -+ is typical , they seem more accurate [precise anyway] at higher temps >200c
but there is still a fair spread of variation in the readings

how to interpret reading

flotulopex
- 16th April 2017, 14:05
Thanks Richard,

With your calculation example you gave me above, I could understand how to read my result.

Do you know if thermocouples are "qualified" according to their precision?

PS: yup, mine has made the travel from China :wink:

richard
- 16th April 2017, 14:26
Do you know if thermocouples are "qualified" according to their precision?

not cheapies on ebay .

a more likely source of some probes with actual specs would be rs components ,farnel/element14 ,digikey or similar
but they will cost more.

I have never had the need for real absolute accuracy and just add or subtract a suitable offset to compensate for error,
so far it works acceptably for my 3d printer [precision being more important]

mackrackit
- 16th April 2017, 16:01
And I will add... Take extra care with your cold junctions. The 6675 does a good job compensating if the junction is close and in the same environment as it is. As do not have the cold junction on the outside of an enclosure and the 6675 on the inside. And noise that can be picked up from breadboard connections can be trouble.

flotulopex
- 16th April 2017, 17:39
...As do not have the cold junction on the outside of an enclosure and the 6675 on the inside.

How do you mean this?

mackrackit
- 16th April 2017, 18:05
The MAX6675 has cold junction compensation built in, but only works if the cold junction is close to the MAX6675.
This article explains cold junction compensation in detail. https://www.maximintegrated.com/en/app-notes/index.mvp/id/4026

flotulopex
- 16th April 2017, 19:48
Nice and very interresting.

Thanks a lot :wink: