Beware that this post is related to an SDK in maintenance mode
More Info: Consider nRF Connect SDK for new designs
This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

sensing temperature with a thermocouple / thermocouple

hello, testing the spi examples of SDK16.0.0 I was trying to read a type k thermocouple with a max31856 on the nRF52840dk board, I made the connections of ss, sck, mosi, miso. The problem is that I don't get thermocouple values, is there another way to read this type of sendor with the nRF52840dk board?

Parents
  • Hi

    Have you made sure to configure the SPI in the nRF52840 with the right mode and frequency to work with your sensor?

    Have you tried to probe the SPI lines with a scope or logic analyzer to verify that the communication looks OK?

    Best regards
    Torbjørn

  • I have another doubt, to read a type K thermocouple with a max31856 through SPI, which example could be useful to read the data sent by the sensor?

  • This is the datasheet link datasheets.maximintegrated.com/.../MAX31856.pdf, the examples I used to work on the Raspberry always existed the SPI command to write and read, this is one of the examples to use as a guide github.com/.../max31856.c, www.maximintegrated.com/.../ tb_tab2 and www.raspberrypi.org/.../viewtopic.php, the latter use it as a base to modify it with data from the other two codes. What is common in these codes is that there is a command to write and read data from the MAX31856 records through SPI and in the documentation of nRF52840DK I have not found it. So my question is, if the command or function exists in this model?

  • The MAX31856 neither knows nor cares what the SPI master is.

    All the MAX31856 sees is the SPI lines - SDI, SDO, SCK, and CS.

    Similarly, the nRF52 neither knows nor cares what the SPI slave is: it just drives the SPI lines - MISO, MOSI, SCK, CS in accordance with your code's instructions.

    So you just need to ensure that the nRF52 is driving those lines as shown in the MAX datasheet

    See the figures on page 16 of the MAX datasheet.

  • I elaborated this example, at the moment it is done only to obtain the varibals stored in the MAX31856, but, when I was going to elaborate the other calculations to take out the temperature the data of the MAX did not arrive anymore. Do you have any advice that could be wrong? , I already checked the circuit connections and they are well connected.

    #include "nrfx_spim.h"
    #include "app_util_platform.h"
    #include "nrf_gpio.h"
    #include "nrf_delay.h"
    #include "boards.h"
    #include "app_error.h"
    #include <string.h>
    #include "nrf_log.h"
    #include "nrf_log_ctrl.h"
    #include "nrf_log_default_backends.h"
    
    #define NRFX_SPIM_SCK_PIN  3
    #define NRFX_SPIM_MOSI_PIN 4
    #define NRFX_SPIM_MISO_PIN 28
    #define NRFX_SPIM_SS_PIN   29
    #define NRFX_SPIM_DCX_PIN  30
    
    #define Cold_Junction_Resolution  0.015625
    #define TC_Resolution             0.0078125  
    
    #define MAX31856_WRITE            0x80
    #define MAX31856_READ             0x00
    
    #define Average_1_Bit             0x0F
    #define Average_2                 0x10
    #define Average_4                 0x20
    #define Average_8                 0x30
    #define Average_16                0x40
    
    #define No_Fault_Mask             0x02
    #define Interrupt_Mode            0x04
    
    #define OC_Fault_Disable_Bit      0xCF
    #define OC_Fault_Enable_1         0x10
    #define OC_Fault_Enable_2         0x20
    #define OC_Fault_Enable_3         0x30
    
    #define TC_TypeB_Bit              0xF0
    #define TC_TypeE                  0x01
    #define TC_TypeJ                  0x02
    #define TC_TypeK                  0x03
    #define TC_TypeN                  0x04
    #define TC_TypeR                  0x05
    #define TC_TypeS                  0x06
    #define TC_TypeT                  0x07
    #define VM_Gain8                  0x80
    #define VM_Gain16                 0xC0
    
    #define MAX31856_cr0_REG          0x00
    #define MAX31856_cr0_1SHOT        0x40
    #define Stop_Conversion_Bit       0x3F
    #define MAX31856_cr0_50HZ         0x01
    #define MAX31856_CHIP_SELECT      0
    #define MAX31856_cr0_FAULTCLR     0x02
    #define MAX31856_CJTO             0x09
    #define MAX31856_CJTH             0x0A
    #define MAX31856_CJTL             0x0B
    #define MAX31856_LTCBH            0x0C
    #define MAX31856_LTCBM            0x0D
    #define MAX31856_LTCBL            0x0E
    
    #define SPI_INSTANCE  3                                           /**< SPI instance index. */
    static const nrfx_spim_t spi = NRFX_SPIM_INSTANCE(SPI_INSTANCE);  /**< SPI instance. */
    static volatile bool spi_xfer_done;  /**< Flag used to indicate that SPI instance completed the transfer. */
    static uint8_t       m_tx_buf[4];  /**< TX buffer. */
    static uint8_t       m_rx_buf[5];  /**< RX buffer. */
    static const uint8_t m_length = sizeof(m_tx_buf);
    
    uint8_t Max31856_cr0, Max31856_cr1;
    int temp, offset;
    float cold_junction_temperature, ctemp;
    
    void spim_event_handler(nrfx_spim_evt_t const * p_event, void * p_context)
    {
        spi_xfer_done = true;
        NRF_LOG_INFO("Transfer completed.");
    
        if (m_tx_buf[3] == 11){
            printf("RX1 = %d\n",m_rx_buf[0]);
            printf("RX1 = %d\n",m_rx_buf[1]);
            printf("RX1 = %d\n",m_rx_buf[2]);
            printf("RX1 = %d\n",m_rx_buf[3]);
            NRF_LOG_INFO(" Received:");
            NRF_LOG_HEXDUMP_INFO(m_rx_buf, strlen((const char *)m_rx_buf)); 
            
        }
        else if (m_tx_buf[3] == 14){
            printf("RX2 = %d\n",m_rx_buf[0]);
            printf("RX2 = %d\n",m_rx_buf[1]);
            printf("RX2 = %d\n",m_rx_buf[2]);
            printf("RX2 = %d\n",m_rx_buf[3]);
            NRF_LOG_INFO(" Received:");
            NRF_LOG_HEXDUMP_INFO(m_rx_buf, strlen((const char *)m_rx_buf));
        }
    }
    
    int confi(){
          spi_xfer_done = false;
          Max31856_cr0 = OC_Fault_Enable_1|MAX31856_cr0_50HZ|Interrupt_Mode;
          Max31856_cr1 = Average_1_Bit|TC_TypeK;
    
          m_tx_buf[0] = MAX31856_WRITE;
          m_tx_buf[1] = Max31856_cr0;
          m_tx_buf[2] = Max31856_cr1;
          m_tx_buf[3] = No_Fault_Mask;
          nrfx_spim_xfer_desc_t xfer_desc=NRFX_SPIM_XFER_TRX(m_tx_buf, m_length, m_rx_buf,m_length);
          APP_ERROR_CHECK(nrfx_spim_xfer_dcx(&spi, &xfer_desc, 0, 15));
    }
    
    int oneshoot(){
          spi_xfer_done = false;
          Max31856_cr0 = Stop_Conversion_Bit;
          m_tx_buf[0] = MAX31856_WRITE;
          m_tx_buf[1] = MAX31856_cr0_REG;
          m_tx_buf[2] = Max31856_cr0|MAX31856_cr0_1SHOT|MAX31856_cr0_FAULTCLR;
          nrfx_spim_xfer_desc_t xfer_desc=NRFX_SPIM_XFER_TRX(m_tx_buf, m_length, m_rx_buf,m_length);
          APP_ERROR_CHECK(nrfx_spim_xfer_dcx(&spi, &xfer_desc, 0, 15));
          nrf_delay_ms(250);
    }
    
    int CJT(){
          
          spi_xfer_done = false;
          m_tx_buf[0] =  MAX31856_READ;
          m_tx_buf[1] = MAX31856_READ|MAX31856_CJTO;
          m_tx_buf[2] = MAX31856_READ|MAX31856_CJTH;
          m_tx_buf[3] = MAX31856_READ|MAX31856_CJTL;
          nrfx_spim_xfer_desc_t xfer_desc=NRFX_SPIM_XFER_TRX(m_tx_buf, m_length, m_rx_buf, m_length);
          APP_ERROR_CHECK(nrfx_spim_xfer_dcx(&spi, &xfer_desc, 0, 15));
          printf("CJTO = %d\n",m_rx_buf[1]);
          printf("CJTH = %d\n",m_rx_buf[2]);
          printf("CJTL = %d\n",m_rx_buf[3]);}
    
    int LTCB(){
          spi_xfer_done = false;
          m_tx_buf[0] = MAX31856_READ;
          m_tx_buf[1] = MAX31856_READ | MAX31856_LTCBH;
          m_tx_buf[2] = MAX31856_READ | MAX31856_LTCBM;
          m_tx_buf[3] = MAX31856_READ | MAX31856_LTCBL;
          nrfx_spim_xfer_desc_t xfer_desc=NRFX_SPIM_XFER_TRX(m_tx_buf, m_length, m_rx_buf,m_length);
          APP_ERROR_CHECK(nrfx_spim_xfer_dcx(&spi, &xfer_desc, 0, 15));
          printf("LTCH = %d\n",m_rx_buf[1]);
          printf("LTCM = %d\n",m_rx_buf[2]);
          printf("LTCL = %d\n",m_rx_buf[3]);
          return (spi_xfer_done); }
    
    int main(void){
        bsp_board_init(BSP_INIT_LEDS);
    
        APP_ERROR_CHECK(NRF_LOG_INIT(NULL));
        NRF_LOG_DEFAULT_BACKENDS_INIT();
    
        nrfx_spim_config_t spi_config = NRFX_SPIM_DEFAULT_CONFIG;
        spi_config.frequency      = NRF_SPIM_FREQ_4M;
        spi_config.ss_pin         = NRFX_SPIM_SS_PIN;
        spi_config.miso_pin       = NRFX_SPIM_MISO_PIN;
        spi_config.mosi_pin       = NRFX_SPIM_MOSI_PIN;
        spi_config.sck_pin        = NRFX_SPIM_SCK_PIN;
        spi_config.dcx_pin        = NRFX_SPIM_DCX_PIN;
        spi_config.mode           = NRF_SPIM_MODE_3;
        spi_config.use_hw_ss      = true;
        spi_config.ss_active_high = false;
        APP_ERROR_CHECK(nrfx_spim_init(&spi, &spi_config, spim_event_handler, NULL));
        NRF_LOG_INFO("NRFX SPIM example started.");
    
        while (1)
        {
            confi();
            oneshoot();
            CJT();
            LTCB();
    
            while (!spi_xfer_done){__WFE();}
    
            NRF_LOG_FLUSH();
    
            bsp_board_led_invert(BSP_BOARD_LED_0);
            nrf_delay_ms(2000);
        }
    }
    

  • Hi 

    Do you have a scope trace of the SPI signals between the two devices, so I can see what is going on over the bus?

    Best regards
    Torbjørn

  • Hello, I already solved the problem, I changed from the SPIM example to the one of SPI and with this I got a good reading of the data and with these I already managed to calculate the thermocouple temperature

Reply Children
Related