I'm not using a thermo-click, but the adafruit break out board.
It has connections for D0, CS and CLK i have connected it as follows:
DO -> RC4(SDI1)
CS -> RE0
CLK -> RC3(SCK)
The code i've built up from examples obtained plucking out the parts i considered correct to build up the code.
I'm not getting any response, no values and no discernible read/comms with the MAX?
i.e. the code should pick up errors, open-cct, short-cct etc.
but i get nothing even with no k-type connected.
I may well have a faulty/damaged break out board, but i have no confidence the SPI is setup correctly in the first place.
it seems such a simple thing, but beyond me?
if i can get advice, confirmation the SPI is setup correctly, then i can go from there.
code i'm using as follows.
Code: Select all
/*PIC18F45K22, internal osc 8MHz, PLL enabled
CONFIG1H : $300001 : 0x0038
CONFIG2L : $300002 : 0x001F
CONFIG2H : $300003 : 0x003C
CONFIG3H : $300005 : 0x00BF
CONFIG4L : $300006 : 0x0081
CONFIG5L : $300008 : 0x000F
CONFIG5H : $300009 : 0x00C0
CONFIG6L : $30000A : 0x000F
CONFIG6H : $30000B : 0x00E0
CONFIG7L : $30000C : 0x000F
CONFIG7H : $30000D : 0x0040
*/
// Lcd module connections
sbit LCD_RS at LATB4_bit;
sbit LCD_EN at LATB5_bit;
sbit LCD_D4 at LATB0_bit;
sbit LCD_D5 at LATB1_bit;
sbit LCD_D6 at LATB2_bit;
sbit LCD_D7 at LATB3_bit;
sbit LCD_RS_Direction at TRISB4_bit;
sbit LCD_EN_Direction at TRISB5_bit;
sbit LCD_D4_Direction at TRISB0_bit;
sbit LCD_D5_Direction at TRISB1_bit;
sbit LCD_D6_Direction at TRISB2_bit;
sbit LCD_D7_Direction at TRISB3_bit;
// End Lcd module connections
sbit THERMO_CS at LATE0_bit;//chip-select
sbit THERMO_CS_Dir at TRISE0_bit;
int tmp2, intTemp, remTemp;
float temperature, tempInternal;
char s_temp[15], s_tempInternal[15];
unsigned short temp_byte[4] = {0, 0, 0, 0};
void MAX31855_Read() {
THERMO_CS = 0;
Delay_ms(350);
temp_byte[0] = SPI_Read(0);
temp_byte[1] = SPI_Read(0);
temp_byte[2] = SPI_Read(0);
temp_byte[3] = SPI_Read(0);
THERMO_CS = 1;
}
void Compute_Temp_Value() {
tmp2 = temp_byte[0];
tmp2 = tmp2 << 8;
tmp2 = tmp2 | temp_byte[1];
remTemp = tmp2 >> 2;
remTemp = remTemp & 0x03;// Decimal part of temperature value
temperature = remTemp * 0.25;
intTemp = tmp2 >> 4;// Intiger part of temperature value
temperature += intTemp;// Temperature value
FloatToStr(temperature, s_temp);
}
// NOTE FOR NOW DO NOT HANDLE NEGATIVE VALUES
void Compute_InternalTemp_Value() {
//temp_byte[2] = 0b00011001;
//temp_byte[3] = 0b00000000;
tmp2 = temp_byte[2];
tmp2 = tmp2 << 8;
tmp2 = tmp2 | temp_byte[3];
remTemp = tmp2 >> 4;
tempInternal = remTemp * 0.0625;
FloatToStr(tempInternal, s_tempInternal);
}
void main() {
THERMO_CS_Dir = 0;
THERMO_CS = 1;
TRISC5_bit = 0;// output for SDO1
TRISC4_bit = 1;// input for SDI1
TRISC3_bit = 0;// output for SCK1
Lcd_Init();
Lcd_Cmd(_LCD_CLEAR); // Clear display
Lcd_Cmd(_LCD_CURSOR_OFF);
Delay_ms(100);
//SPI1_Init();
SPI1_Init_Advanced(
_SPI_MASTER_OSC_DIV16,
_SPI_DATA_SAMPLE_MIDDLE,
_SPI_CLK_IDLE_LOW,
_SPI_LOW_2_HIGH
); // Initialize SPI communication*/
SPI_Set_Active(&SPI1_Read, &SPI1_Write);// Sets the SPI1 module active
Delay_ms(100);
while (1) {
Delay_ms(100);
MAX31855_Read();
///*
if((temp_byte[1] & 0x01) == 0x01){// Fault detection
Lcd_Out(1,1,"ERROR");
if((temp_byte[3] & 0x01) == 0x01){// Open circuit fault?
Lcd_Out(2,1,"Open circuit");
Delay_ms(1000);
}
//
if((temp_byte[3] & 0x02) == 0x02){// Short to GND fault?
Lcd_Out(2,1,"Short to GND");
Delay_ms(1000);
}
//
if((temp_byte[3] & 0x04) == 0x04){// Short to Vcc fault?
Lcd_Out(2,1,"Short to Vcc");
Delay_ms(1000);
}
}
else{
Compute_Temp_Value();
Lcd_Out(1,1,"TempTC: ");
Lcd_Out(1,8,s_temp);
Compute_InternalTemp_Value();
Lcd_Out(2,1,"TempInt:");
Lcd_Out(2,9,s_tempInternal);
}
//*/
Delay_ms(500);
}
}