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

twi sensor i am not getting the data

hi,

i am using nrf52832 devkit and max30208 temp. sensor. i am not getting updated data. iprefer this link for i2c address.(https://os.mbed.com/users/cyberjoey/code/OT07_I2C_GUI_Firmware//file/2e7483c7281b/main.cpp/

source code 

/** @file
* @defgroup tw_sensor_example main.c
* @{
* @ingroup nrf_twi_example
* @brief TWI Sensor Example main file.
*
* This file contains the source code for a sample application using TWI.
*
*/

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

#include "nrf_log.h"
#include "nrf_log_ctrl.h"
#include "nrf_log_default_backends.h"

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

/* Common addresses definition for temperature sensor. */
#define max30208_slave_add 0x50U // (0x50U >> 1)
#define max30208_config 0x01U
#define max30208_read_data 0x08U
#define max30208_temp_setup 0x14U
#define max30208_temp_setup_value 0xC1U

#define LM75B_REG_TEMP 0x00U
#define LM75B_REG_CONF 0x01U
#define LM75B_REG_THYST 0x02U
#define LM75B_REG_TOS 0x03U

#define OT07_FIFO_DATA 0x08U // read temp. data
#define OT07_ADC_SETUP 0x14 // OT07 Temp Seneor Setup (ADC_RES[7:6]) & Convert Temperature [0]
/* Mode for LM75B. */
#define NORMAL_MODE 0U

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

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

double Temp_sample;


static void temp_tx_setup(void)
{
m_xfer_done = false;
ret_code_t err_code;
uint8_t reg[2] = {max30208_temp_setup, max30208_temp_setup_value};
err_code = nrf_drv_twi_tx(&m_twi, max30208_slave_add, reg, sizeof(reg), false);
APP_ERROR_CHECK(err_code);
while (m_xfer_done == false);

}

static void temp_rx_setup(void)
{
ret_code_t err_code;
m_xfer_done = false;
char data[2];
uint16_t count;
uint8_t reg[1] = {max30208_read_data};
err_code = nrf_drv_twi_tx(&m_twi, max30208_slave_add, reg, 2, false);
APP_ERROR_CHECK(err_code);
while (m_xfer_done == false);

err_code = nrf_drv_twi_rx(&m_twi, max30208_read_data, data, 2);
APP_ERROR_CHECK(err_code);
NRF_LOG_INFO("Temperature:data %d ", count);

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

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

/* Writing to pointer byte. */
/* reg[0] = OT07_ADC_SETUP;
err_code = nrf_drv_twi_tx(&m_twi, LM75B_ADDR, reg, 1, false);
APP_ERROR_CHECK(err_code);
while (m_xfer_done == false);*/

/* reg[0] = OT07_FIFO_DATA;
m_xfer_done = false;
err_code = nrf_drv_twi_tx(&m_twi, LM75B_ADDR, reg, 1, 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(double temp)
{
//NRF_LOG_INFO("Temperature: %lf, int %d Celsius degrees.",temp, (int) temp);
}

/**
* @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(Temp_sample);
NRF_LOG_INFO("data sent event");
}
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_lm75b_config = {
.scl = ARDUINO_SCL_PIN,
.sda = ARDUINO_SDA_PIN,
.frequency = NRF_DRV_TWI_FREQ_400K,
.interrupt_priority = APP_IRQ_PRIORITY_HIGH,
.clear_bus_init = false
};

err_code = nrf_drv_twi_init(&m_twi, &twi_lm75b_config, twi_handler, NULL);
APP_ERROR_CHECK(err_code);

nrf_drv_twi_enable(&m_twi);
}

/**
* @brief Function for reading data from temperature sensor.
*/
static void read_sensor_data()
{
temp_tx_setup();


nrf_delay_ms(500);
m_xfer_done = false;
char data[2];
uint16_t count;
ret_code_t err_code;
uint8_t reg[1] = {max30208_read_data};
err_code = nrf_drv_twi_tx(&m_twi, max30208_slave_add, reg, 2, false);
APP_ERROR_CHECK(err_code);
while (m_xfer_done == false);

err_code = nrf_drv_twi_rx(&m_twi, max30208_read_data, data, 2);
APP_ERROR_CHECK(err_code);
//calculate temperture from data
count = (uint16_t)(data[0]*256 + data[1]);
if (count >= 32768)
{count = 65536 - count;}
Temp_sample = (double)(count * 0.005);
NRF_LOG_INFO("Temperature data: %d , %X ", ,Temp_sample);
}

/**
* @brief Function for main application entry.
*/
int main(void)
{
APP_ERROR_CHECK(NRF_LOG_INIT(NULL));
NRF_LOG_DEFAULT_BACKENDS_INIT();
NRF_LOG_INFO("\r\nTWI sensor example started.");

NRF_LOG_FLUSH();
twi_init();
max30208_setmode();

while (true)
{
nrf_delay_ms(500);

do
{
__WFE();
}while (m_xfer_done == false);

read_sensor_data();
NRF_LOG_FLUSH();
}
}

/** @} */

Parents Reply Children
Related