【Nordic】52832测试TWI(I2C)与LIS2DW12通信

PJM突然怀疑一个加速度传感器的问题,但fw是客户给的bin文件,没办法debug,只能自己用nordic的demo改一个,测测看。

 

先看了一下LIS2DW12.pdf的datasheet,把各个寄存器和可能用到的值记下来。

#ifndef LIS2DW12_CFG_H
#define LIS2DW12_CFG_H

#include 

/* REGISTER MAPING*/
#define OUT_T_L (0x0D)
#define OUT_T_H (0x0E)
#define WHO_AM_I (0x0F)
#define CTRL1 (0x20)
#define CTRL2 (0x21)
#define CTRL3 (0x22)
#define CTRL4_INT1_PAD_CTRL (23h)
#define CTRL5_INT2_PAD_CTRL (24h)
#define CTRL6 (0x25)
#define OUT_T (0x26)
#define STATUS (0x27)
#define OUT_X_L (0x28)
#define OUT_X_H (0x29)
#define OUT_Y_L (0x2A)
#define OUT_Y_H (0x2B)
#define OUT_Z_L (0x2C)
#define OUT_Z_H (0x2D)
#define FIFO_CTRL (0x2E)
#define FIFO_SAMPLES (0x2F)
#define TAP_THS_X (0x30)
#define TAP_THS_Y (0x31)
#define TAP_THS_Z (0x32)
#define INT_DUR (0x33)
#define WAKE_UP_THS (0x34)
#define WAKE_UP_DUR (0x35)
#define FREE_FALL (0x36)
#define STATUS_DUP (0x37)
#define WAKE_UP_SRC (0x38)
#define TAP_SRC (0x39)
#define SIXD_SRC (0x3A)
#define ALL_INT_SRC (0x3B)
#define X_OFS_USR (0x3C)
#define Y_OFS_USR (0x3D)
#define Z_OFS_USR (0x3E)
#define CTRL7 (0x3F)

#define CTRL1_VALUE (0x53)
#define CTRL2_VALUE (0x84)
#define CTRL6_VALUE (0x04)

typedef struct 
{
	uint16_t x_value;
	uint16_t y_value;
	uint16_t z_value;
} position_t;

#endif

在twi_sensor这个例程的基础上,把文件LIS2DW12_cfg.h添加到工程里,在main文件中。

接着根据写时序添加写寄存器的函数:

/**
 * @brief Function for LIS2DW12 to write one byte to register.
 * @para reg_addr 
 * @para data
 */
static void lis2dw12_write_onebyte(uint8_t reg_addr, uint8_t data)
{
	ret_code_t err_code;
	uint8_t reg[2] = {reg_addr, data};
	err_code = nrf_drv_twi_tx(&m_twi, LIS2DW12_SAD_WRITE, reg, sizeof(reg), false);
	APP_ERROR_CHECK(err_code);
	while (m_xfer_done == false);
}

根据读时序添加读寄存器的函数:

 

/**
 * @brief Function for LIS2DW12 to read one byte from register.
 * @para reg_addr 
 * @para *data
 */
static void lis2dw12_read_onebyte(uint8_t reg_addr, uint8_t *data)
{
	ret_code_t err_code;
	err_code = nrf_drv_twi_tx(&m_twi, LIS2DW12_SAD_WRITE, ®_addr, 1, true);
	APP_ERROR_CHECK(err_code);
	while (m_xfer_done == false);
	
	m_xfer_done = false;
	err_code = nrf_drv_twi_rx(&m_twi, LIS2DW12_SAD_READ, data, 1);
	APP_ERROR_CHECK(err_code);
	while (m_xfer_done == false);
}

 添加初始化的函数:

/**
 * @brief Function for initlise LIS2DW12.
 */
void LIS2DW12_initlise(void)
{
	lis2dw12_write_onebyte(CTRL1, CTRL1_VALUE);
	lis2dw12_write_onebyte(CTRL2, CTRL2_VALUE);
	lis2dw12_write_onebyte(CTRL6, CTRL6_VALUE);	
}

最后是读参数的函数:

/**
 * @brief Function to read x y z value.
 */
static position_t read_acc_value(void)
{
	ret_code_t err_code;
	position_t position;
	uint8_t tmp_low;
	uint8_t tmp_high; 
	lis2dw12_read_onebyte(OUT_X_L, &tmp_low);
	lis2dw12_read_onebyte(OUT_X_H, &tmp_high);
	position.x_value = (tmp_low & 0xFF) + (tmp_high<<6); 
	
	lis2dw12_read_onebyte(OUT_Y_L, &tmp_low);
	lis2dw12_read_onebyte(OUT_Y_H, &tmp_high);
	position.y_value = (tmp_low & 0xFF) + (tmp_high<<6);
	
	lis2dw12_read_onebyte(OUT_Z_L, &tmp_low);
	lis2dw12_read_onebyte(OUT_Z_H, &tmp_high);
	position.z_value = (tmp_low & 0xFF) + (tmp_high<<6); 	
	
	return position;
}

在main()中调用上面的函数就好啦:

/**
 * @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();
		LIS2DW12_initlise();
		position_t position;
    while (true)
    {
        nrf_delay_ms(500);
				position = read_acc_value();
        NRF_LOG_FLUSH();
    }
}

 

 

你可能感兴趣的:(蓝牙)