This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

I am running MCP9808 on NRF52 EVB, i have this following program which is not giving the correct values on terminal display

I have this program running on KEIL 5, what could be the problem with the code and Do you guys have any sample code for mcp9808? Note- I am using the wired connection not a Bluetooth app to sense the temperature Please Help Thank you!

#include <stdio.h>
#include "boards.h"
#include "app_util_platform.h"
#include "app_error.h"
#include "nrf_drv_twi.h"
#include "nrf_delay.h"



//#define NRF_LOG_MODULE_NAME "APP"
#include "nrf_log.h"
#include "nrf_log_ctrl.h"

/* TWI instance ID. */
 #define TWI_INSTANCE_ID     0
#define TWI_ADDRESSES      127

/* Common addresses definition for temperature sensor. */
//#define MCP_9808_ADDR          (0x18)

//#define MCP_9808_REG_TEMP      0x00U
//#define MCP_9808_REG_CONF      0x01U
//#define MCP_9808_REG_THYST     0x02U
//#define MCP_9808_REG_TOS       0x03U
#define SCL_PIN             28
#define SDA_PIN             29

#define MCP_9808_ADDR        (0x18)

//#define MCP_9808_REG_RFU     0x00U
#define MCP_9808_REG_CONF  0x01
//#define MCP_9808_REG_TUPPER  0x02U
//#define MCP_9808_REG_TLOWER  0x03U
//#define MCP_9808_REG_TCRIT   0x04U
//#define MCP_9808_REG_TEMP      0x05U
//#define MCP_9808_REG_MID       0x06U3
//#define MCP_9808_REG_DID       0x07U
//#define MCP_9808_REG_RESR    0x08U
/* Mode for MCP_9808. */
#define NORMAL_MODE 0U
//#define MCP9808_REG_UPPER_TEMP         0x02
//#define MCP9808_REG_LOWER_TEMP         0x03
//#define MCP9808_REG_CRIT_TEMP          0x04
#define MCP9808_REG_AMBIENT_TEMP         0x05
//#define MCP9808_REG_MANUF_ID           0x06
//#define MCP9808_REG_DEVICE_ID          0x07


/* Indicates if operation on TWI has ended. */
static volatile bool m_xfer_done = false;

/* TWI instance. */
static const nrf_drv_twi_t m_twi_mcp9808 = NRF_DRV_TWI_INSTANCE(TWI_INSTANCE_ID);

/* Buffer for samples read from temperature sensor. */
static uint16_t m_sample;

/**
 * @brief Function for setting active mode on MMA7660 accelerometer.
 */
void MCP_9808_set_mode(void)
{
   ret_code_t err_code;

    /* Writing to MCP_9808_REG_CONF "0" set temperature sensor in NORMAL mode. */
			uint8_t reg[2] = {MCP_9808_REG_CONF, NORMAL_MODE};
			err_code = nrf_drv_twi_tx(&m_twi_mcp9808, MCP_9808_ADDR, reg, sizeof(reg), false);
			
			APP_ERROR_CHECK(err_code);
			while (m_xfer_done == false);

			/* Writing to pointer byte. */
			reg[0] = MCP9808_REG_AMBIENT_TEMP;
			m_xfer_done = false;
			err_code = nrf_drv_twi_tx(&m_twi_mcp9808, MCP_9808_ADDR, reg, sizeof(reg), false);
			APP_ERROR_CHECK(err_code);
			while (m_xfer_done == false);
}



/**
 * @brief Function for handling data from temperature sensor.
 *
 * @param[in] temp          Temperature in Celsius degrees read from sensor.
 */
__STATIC_INLINE void data_handler(uint32_t temp)
{
	union
{
float floatVal;
uint32_t bytes[4];
} floatConverter;


    floatConverter.bytes[0]=0x02;
    floatConverter.bytes[1]=0x03;
    floatConverter.bytes[2]=0x04;
    floatConverter.bytes[3]=0x05;

float eepromFloat = floatConverter.floatVal;
 // NRF_LOG_INFO("Temperature: %x  degree Celsius.\r\n", floatConverter.bytes[3]);
	  NRF_LOG_INFO("Temperature: %d  degree Celsius.\r\n",temp);
		//NRF_LOG_INFO("TWI device detected at address 0x%x.\r\n", address);
}

/**
 * @brief TWI events handler.
 */
void twi_handler(nrf_drv_twi_evt_t const * p_event, void * p_context)
{
    switch (p_event->type)
    {
        case NRF_DRV_TWI_EVT_DONE:
            if (p_event->xfer_desc.type == NRF_DRV_TWI_XFER_RX)
            {
                data_handler(m_sample);
            }
            m_xfer_done = true;
            break;
        default:
            break;
    }
}

/**
 * @brief UART initialization.
 */
void twi_init (void)
{
    ret_code_t err_code;

    const nrf_drv_twi_config_t twi_mcp9808_config = {
			 .scl              	 = SCL_PIN,
       .sda                = SDA_PIN,
       .frequency          = NRF_TWI_FREQ_100K,
       .interrupt_priority = APP_IRQ_PRIORITY_HIGH,
       .clear_bus_init     = false
    };

    err_code = nrf_drv_twi_init(&m_twi_mcp9808, &twi_mcp9808_config, twi_handler, NULL);
    APP_ERROR_CHECK(err_code);

    nrf_drv_twi_enable(&m_twi_mcp9808);
}

/**
 * @brief Function for reading data from temperature sensor.
 */
static void read_sensor_data()
{
	 ret_code_t err_code;
    m_xfer_done = false;

    /* Read 1 byte from the specified address - skip 3 bits dedicated for fractional part of temperature. */
  // ret_code_t  
	err_code = nrf_drv_twi_rx(&m_twi_mcp9808, MCP_9808_ADDR, (uint8_t*)&m_sample, sizeof(m_sample));
    
	
	APP_ERROR_CHECK(err_code);
}

/**
 * @brief Function for main application entry.
 */
int main(void)
{
    APP_ERROR_CHECK(NRF_LOG_INIT(NULL));

    NRF_LOG_INFO("\r\nTWI sensor reading\r\n");
    NRF_LOG_FLUSH();
    twi_init();
    MCP_9808_set_mode();
	
    while (true)
    {
        nrf_delay_ms(1000);
    
        do
        {
            __WFE();
        }while (m_xfer_done == false);

        read_sensor_data();
        NRF_LOG_FLUSH();
    }
		
}



/** @} */
Parents
  • This depends on the device, but typically this is done by writing two bytes to the device. First the register address, and then the value of the register. This is shown in the TWI sensor example in the SDK:

    #define CONF_REG_ADDR 0xXX
    #define CONF_REG_VAL  0b00001000
    
    void LM75B_set_mode(void)
    {
        ret_code_t err_code;
    
        uint8_t reg[2] = {CONF_REG_ADDR, CONF_REG_VAL};
        err_code = nrf_drv_twi_tx(&m_twi, MCP_9808_ADDR, reg, sizeof(reg), false);
        APP_ERROR_CHECK(err_code);
        while (m_xfer_done == false);
    }
    
Reply
  • This depends on the device, but typically this is done by writing two bytes to the device. First the register address, and then the value of the register. This is shown in the TWI sensor example in the SDK:

    #define CONF_REG_ADDR 0xXX
    #define CONF_REG_VAL  0b00001000
    
    void LM75B_set_mode(void)
    {
        ret_code_t err_code;
    
        uint8_t reg[2] = {CONF_REG_ADDR, CONF_REG_VAL};
        err_code = nrf_drv_twi_tx(&m_twi, MCP_9808_ADDR, reg, sizeof(reg), false);
        APP_ERROR_CHECK(err_code);
        while (m_xfer_done == false);
    }
    
Children
No Data
Related