Having trouble with SERIN command and MAX6675
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-...-301/8892.html
http://pdf1.alldatasheet.com/datashe...IP/16F630.html
Code:
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
1 Attachment(s)
it's working... but bit-shifting not clear to me :-(
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:
Code:
' ====== 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.
Attachment 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?
Re: it's working... but bit-shifting not clear to me :-(
Unless you are trying to get the fractional part of the reading 10 bits is all you need.
Re: it's working... but bit-shifting not clear to me :-(
Quote:
Originally Posted by
mackrackit
Unless you are trying to get the fractional part of the reading 10 bits is all you need.
I don't get it, Dave...
Re: Having trouble with SERIN command and MAX6675
Quote:
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"
2 Attachment(s)
Re: Having trouble with SERIN command and MAX6675
This is confusing to me.
My code gives me almost exactly what my home thermometer shows:
Attachment 8394
This code
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:
Attachment 8395
Where is the truth?
Re: Having trouble with SERIN command and MAX6675
for a start your temp var is a declared as byte , how can 12 bit reading fit in that ?
Re: Having trouble with SERIN command and MAX6675
I corrected my code just after I sent it - couldn't edit the post anymore, should have mentionned it, sorry :o
Re: Having trouble with SERIN command and MAX6675
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?
Re: Having trouble with SERIN command and MAX6675
My actual code is:
Code:
' ====== 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
1 Attachment(s)
Re: Having trouble with SERIN command and MAX6675
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
Re: Having trouble with SERIN command and MAX6675
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:
Re: Having trouble with SERIN command and MAX6675
Quote:
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]
Re: Having trouble with SERIN command and MAX6675
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.
Re: Having trouble with SERIN command and MAX6675
Quote:
Originally Posted by
mackrackit
...As do not have the cold junction on the outside of an enclosure and the 6675 on the inside.
How do you mean this?
Re: Having trouble with SERIN command and MAX6675
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/a...ex.mvp/id/4026
Re: Having trouble with SERIN command and MAX6675
Nice and very interresting.
Thanks a lot :wink: