Using PIC32MX Clicker to read MAX31856 Thermocoupe chip

General discussion on mikroC PRO for PIC32.
Post Reply
Author
Message
yassirreebob
Posts: 6
Joined: 06 Feb 2011 17:47

Using PIC32MX Clicker to read MAX31856 Thermocoupe chip

#1 Post by yassirreebob » 16 Mar 2019 01:04

I was beating my head against the wall trying to get the PIC32MX clicker to talk to the Adafruit Max31856 thermocouple board. I took a half hour walk in the woods, and when I got back it took me 10 minutes to fix the problems. Never underestimate the effect that a brief change of scenery can have on your concentration.

Here's the code. I had the TX output from the clicker connected to an FTDI module.

// TCIO PIC32 MAX31856

#define CR 0x0D //CR carriage return
#define LF 0x0A //LF line feed(new line)

char s_temp[16];
char text[5];
short TC_Data;
char SPI_Val;
char take,buffer;
char tempLTHB, tempLTMB, tempLTLB ;
unsigned int tempF, tempC;

sbit THERMO_CS at LATG9_bit;
sbit THERMO_CS_Direction at TRISG9_bit;
//&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
void Initialize_Max31856 ()
{
THERMO_CS = 0;
SPI2_Write(0x80);
SPI2_Write(0x80); //Auto conversion
THERMO_CS = 1;

THERMO_CS = 0;
SPI2_Write(0x00);
take = SPI2_Read(buffer);
THERMO_CS = 1;
ByteToHex(take, s_temp); //
UART5_Write_Text(" 0h ");
UART5_Write_Text(s_temp); //

THERMO_CS = 0;
SPI2_Write(0x81);
SPI2_Write(0x02); // J-type TC
THERMO_CS = 1;

THERMO_CS = 0;
SPI2_Write(0x01);
take = SPI2_Read(buffer);
THERMO_CS = 1;
ByteToHex(take, s_temp); //
UART5_Write_Text(" 01h ");
UART5_Write_Text(s_temp); //
UART5_Write(13);
}
//&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
void MAX31856_Read()
{
THERMO_CS = 0;
SPI2_Write(0x0C);
tempLTHB = SPI2_Read(0); // Register 0Ch
tempLTMB = SPI2_Read(0); // Register 0Dh
tempLTLB = SPI2_Read(0); // Register 0Eh
THERMO_CS = 1;

UART5_Write_Text("Temp C ");
tempC = tempLTHB <<8 | tempLTMB;
tempC = tempC >>4;

IntToStr(tempC, text);
Ltrim(text);
strcat(text, " ");
UART5_Write_Text(text);

// Convert to Fahrenheit
UART5_Write_Text("Temp F ");
tempF = TempC;
tempF = tempF *9;
tempF = tempF /5;
tempF = tempF + 32;
//tempF = (tempF * 9.0/5.0)+ 32;
IntToStr(tempF, text);
Ltrim(text);
strcat(text, "\r\n");
UART5_Write_Text(text);
}
//$$*******************************************************************************
void main()
{
AD1CON1 = 00000000; //
AD1CON2 = 00000000; //

// AD1PCFG = 0xFFFF;
// JTAGEN_bit = 0;

//TRISA = 0xFC; //
TRISB = 0x02CF;
TRISC = 0x80;
TRISD = 0x006;
TRISE = 0xFF;
TRISF = 0x0C;

TRISGbits.TRISG6 = 0; //MOSO
TRISGbits.TRISG7 = 1; //MISO
TRISGbits.TRISG8 = 0; //SCLK

// PLLEN_bit = 1; // Enable PLL
// Delay_ms(150);

CM1CON = 0; // Turn off comparators
CM2CON = 0;

THERMO_CS_Direction = 0;
THERMO_CS = 1;

//$$**************************************************************//**************************************************************
UART5_INIT(9600);

SPI2_Init_Advanced(_SPI_MASTER, _SPI_8_BIT, 64,_SPI_SS_DISABLE, _SPI_DATA_SAMPLE_MIDDLE, _SPI_CLK_IDLE_HIGH, _SPI_ACTIVE_2_IDLE);
SPI_Set_Active(SPI2_Read, SPI2_Write); // Sets the SPI2 module active

Initialize_Max31856();
//$$**************************************************************
while(1)
{
Delay_ms(1200);
MAX31856_Read();
}
}

User avatar
stefan.filipovic
mikroElektronika team
Posts: 1135
Joined: 18 Dec 2018 10:30

Re: Using PIC32MX Clicker to read MAX31856 Thermocoupe chip

#2 Post by stefan.filipovic » 18 Mar 2019 11:14

Hi,

I'm glad that you have managed to solve this.

Thank you for sharing your code with us.

Kind regards,
Stefan Filipović

Post Reply

Return to “mikroC PRO for PIC32 General”