cancel
Showing results for 
Search instead for 
Did you mean: 

MAX31856 connection problem

IMaji
Associate II

Hello,

Have a Chinese version CJMCU-31856 with MAX31856 chip on board. Trying to establish connection, data transmit and read temperature from this chip with STM32F407VE using SPI2 port.

But no successful yet. I cannot read any data or transfer any data. Plus unhappily my logic analyzer stops work and I cannot figure out what is going with SPI2 port.

Here is the C and HEX.

Who has a experience working with it? Am I forgot something?

#include "stm32f4xx_hal.h"
 
#ifndef MAX31856_H
#define MAX31856_H
 
#define MAX31856_CR0_RREG           0x00    ///< Config 0 read register
#define MAX31856_CR0_WREG           0x80    ///< Config 0 write register
#define MAX31856_CR0_AUTOCONVERT   0x80    ///< Config 0 Auto convert flag
#define MAX31856_CR0_1SHOT         0x40    ///< Config 0 one shot convert flag
#define MAX31856_CR0_OCFAULT1      0x20    ///< Config 0 open circuit fault 1 flag
#define MAX31856_CR0_OCFAULT0      0x10    ///< Config 0 open circuit fault 0 flag
#define MAX31856_CR0_CJ            0x08    ///< Config 0 cold junction disable flag
#define MAX31856_CR0_FAULT         0x04    ///< Config 0 fault mode flag
#define MAX31856_CR0_FAULTCLR      0x02    ///< Config 0 fault clear flag
 
#define MAX31856_CR1_RREG           0x01    ///< Config 1 read register
#define MAX31856_CR1_WREG           0x81    ///< Config 1 write register
#define MAX31856_MASK_REG          0x02    ///< Fault Mask register
#define MAX31856_CJHF_REG          0x03    ///< Cold junction High temp fault register
#define MAX31856_CJLF_REG          0x04    ///< Cold junction Low temp fault register
#define MAX31856_LTHFTH_REG        0x05    ///< Linearized Temperature High Fault Threshold Register, MSB
#define MAX31856_LTHFTL_REG        0x06    ///< Linearized Temperature High Fault Threshold Register, LSB
#define MAX31856_LTLFTH_REG        0x07    ///< Linearized Temperature Low Fault Threshold Register, MSB
#define MAX31856_LTLFTL_REG        0x08    ///< Linearized Temperature Low Fault Threshold Register, LSB
#define MAX31856_CJTO_REG          0x09    ///< Cold-Junction Temperature Offset Register 
#define MAX31856_CJTH_REG          0x0A    ///< Cold-Junction Temperature Register, MSB
#define MAX31856_CJTL_REG          0x0B    ///< Cold-Junction Temperature Register, LSB
#define MAX31856_LTCBH_REG         0x0C    ///< Linearized TC Temperature, Byte 2 
#define MAX31856_LTCBM_REG         0x0D    ///< Linearized TC Temperature, Byte 1
#define MAX31856_LTCBL_REG         0x0E    ///< Linearized TC Temperature, Byte 0
#define MAX31856_SR_REG            0x0F    ///< Fault Status Register
 
#define ON 1
#define OFF 0
 
#define MAX31856_FAULT_CJRANGE     0x80    ///< Fault status Cold Junction Out-of-Range flag
#define MAX31856_FAULT_TCRANGE     0x40    ///< Fault status Thermocouple Out-of-Range flag
#define MAX31856_FAULT_CJHIGH      0x20    ///< Fault status Cold-Junction High Fault flag
#define MAX31856_FAULT_CJLOW       0x10    ///< Fault status Cold-Junction Low Fault flag
#define MAX31856_FAULT_TCHIGH      0x08    ///< Fault status Thermocouple Temperature High Fault flag
#define MAX31856_FAULT_TCLOW       0x04    ///< Fault status Thermocouple Temperature Low Fault flag
#define MAX31856_FAULT_OVUV        0x02    ///< Fault status Overvoltage or Undervoltage Input Fault flag
#define MAX31856_FAULT_OPEN        0x01    ///< Fault status Thermocouple Open-Circuit Fault flag
 
/** Noise filtering options enum. Use with setNoiseFilter() */
typedef enum {
MAX31856_NOISE_FILTER_50HZ,
MAX31856_NOISE_FILTER_60HZ
} max31856_noise_filter_t;
 
/** Multiple types of thermocouples supported */
typedef enum
{
  MAX31856_TCTYPE_B  = 0x00,//0b0000,
  MAX31856_TCTYPE_E  = 0x01,//0b0001,
  MAX31856_TCTYPE_J  = 0x02,//0b0010,
  MAX31856_TCTYPE_K  = 0x03,//0b0011,
  MAX31856_TCTYPE_N  = 0x04,//0b0100,
  MAX31856_TCTYPE_R  = 0x05,//0b0101,
  MAX31856_TCTYPE_S  = 0x06,//0b0110,
  MAX31856_TCTYPE_T  = 0x07,//0b0111,
  MAX31856_VMODE_G8  = 0x08,//0b1000,
  MAX31856_VMODE_G32 = 0x0C,//0b1100,
} max31856_thermocoupletype_t;
 
void write_max31856(uint8_t addr, uint8_t dt);
void read_max31856(uint8_t addr, uint8_t dt, uint8_t len);
float MAX31865_readTemp();
void set_thermocouple(uint8_t enable);
#endif
#include "MAX31856.h"
extern SPI_HandleTypeDef hspi2;
uint8_t TxBuffer[1] = {0};
uint8_t RxBuffer[1] = {0};
uint8_t data;
 
//------------------------------------------------
#define cs_reset() HAL_GPIO_WritePin(GPIOE, GPIO_PIN_15, GPIO_PIN_SET);
#define cs_set() HAL_GPIO_WritePin(GPIOE, GPIO_PIN_15, GPIO_PIN_RESET);
#define cs_strobe() cs_reset(); cs_set();
 
void write_max31856(uint8_t addr, uint8_t dt)
{
	cs_strobe();
	TxBuffer[0] = addr;
	HAL_SPI_Transmit(&hspi2, (uint8_t*) TxBuffer,1,5000);
	TxBuffer[0] = dt;
	HAL_SPI_Transmit(&hspi2, (uint8_t*) TxBuffer,1,5000);
	cs_reset();
}
void read_max31856(uint8_t addr, uint8_t dt, uint8_t len)
{
	cs_strobe();
	HAL_SPI_Receive(&hspi2,(uint8_t*)RxBuffer,1,5000);
	addr = RxBuffer[0];
	HAL_SPI_Receive(&hspi2,(uint8_t*)RxBuffer,1,5000);
	dt = RxBuffer[0];
	cs_reset();
}
 
void single_shot(void)
{
    uint8_t status;
 
    // Read config register
    read_max31856(MAX31856_CR0_RREG, status, 1);
 
    // Enable 1shot bit, and write back
    status |= MAX31856_CR0_1SHOT;
    write_max31856(MAX31856_CR0_WREG, status);
 
}
void autoConvert(uint8_t enable)
{
    uint8_t status;
    read_max31856(MAX31856_CR0_WREG, status, 1);
 
    if (enable)
    {
        status |= MAX31856_CR0_AUTOCONVERT;
    } else
    {
        status &= ~MAX31856_CR0_AUTOCONVERT;
    }
 
    write_max31856(MAX31856_CR0_WREG, status);
}
 
void set_thermocouple(uint8_t enable)
{
    uint8_t status;
    read_max31856(MAX31856_CR1_RREG , status, 1);
 
    if (enable)
    {
        write_max31856(MAX31856_CR1_WREG , MAX31856_TCTYPE_T);
    } else
    {
        write_max31856(MAX31856_CR1_WREG , MAX31856_TCTYPE_K);
    }
 
    //write_max31856(MAX31856_CR1_REG , MAX31856_TCTYPE_T);
}
 
float MAX31865_readTemp()
{
    // Activate autoconversation
    autoConvert(ON);
    HAL_Delay(10);
 
    // Perform a single conversion, and wait for the result
    single_shot();
    HAL_Delay(65);
 
    // Read data from max31856 data registers
    uint8_t buffer[2];
    read_max31856(MAX31856_LTCBL_REG,(uint8_t)buffer, 2);
 
    // Combine 2 bytes into 1 number, and shift 1 down to remove fault bit
    uint16_t data = buffer[0] << 8;
    data |= buffer[1];
    data >>= 1;
 
    // Calculate the temperature
    float temp = (float) data;
 
 
    return temp;
}

5 REPLIES 5
CŞAHİ.1
Associate

Could you tell me? Where did you find these codes?

CŞAHİ.1
Associate

Because you need to write a code into while(1) file in the main.c

Ökork.1
Associate II

If you solved the problem, can you share the program codes with me?

ozgurkorkmaz236@hotmail.com

hangi fonksiyonları yazmamız gerekiyor while döngüsünün içine?