KX022-1020(罗姆-加速度传感器)示例

概述

        做个笔录,最近项目使用了此款gsensor,实现了简单示例

一、环境

        硬件平台(RTL8762DK)
        IDE:keil5.29

1)、原理图
  

二、代码:

1、kx022.h

#include "rtl876x_i2c_wristband.h"
#include "os_timer.h"
#include "os_sched.h"
#include "stdio.h"
#define		KX022_7BITI2C_ADDRESS 	0x1E
#define		KX022_CHIP_ID			0x0F
#define		KX022_XOUT_L			0x06	
#define		KX022_CNTL1				0x18
#define		KX022_CNTL2				0x3B
#define		KX022_CNTL3				0x1A
#define  	KX022_ODCNTL		    0x1B
#define		KX022_CTRL_REG1			0x19
#define  	KX022_TILT_TIMER		0x22
#define  	KX022_WUFC				0x23

#define  	KX022_INS2				0x13
#define  	KX022_INT_REL			0x17
#define  	KX022_INC1				0x1C
#define  	KX022_INC2				0x1D
#define  	KX022_INC4				0x1F
#define  	KX022_ATH				0x30

2、kx022.c

void KX022_1020_Delay_ms(uint16_t delay_time)
{
	os_delay(delay_time);
}

uint8_t KX022_1020_I2C_Write(uint8_t reg, uint8_t dat)
{
	uint8_t I2C_WriteBuf[2] = {0x0, 0x0};
    I2C_WriteBuf[0] = reg;
    I2C_WriteBuf[1] = dat;
    uint32_t time_out = SYSTEM_IIC_TIMEROUT;
    while ((I2C_GetFlagState(HRS_I2C_BUS, I2C_FLAG_TFE) == RESET) && (--time_out != 0));
	time_out = SYSTEM_IIC_TIMEROUT;
    while ((I2C_GetFlagState(HRS_I2C_BUS, I2C_FLAG_MST_ACTIVITY) == SET) && (--time_out != 0));
    I2C_SetSlaveAddress(HRS_I2C_BUS, KX022_7BITI2C_ADDRESS);
    I2C_MasterWrite(HRS_I2C_BUS, I2C_WriteBuf, 2);
	
	if (time_out == 0) {
		RtkWristbandSys.flag_field.i2c_bus_lock = true;
	}
	return 0;
}

uint8_t KX022_1020_I2C_Read(uint8_t reg, uint8_t *buf, uint8_t len)
{
	uint8_t tmp_buffer[1] = {0};
	uint32_t time_out = SYSTEM_IIC_TIMEROUT;
	
	tmp_buffer[0] = reg;
    while ((I2C_GetFlagState(HRS_I2C_BUS, I2C_FLAG_TFE) == RESET) && (--time_out != 0));
	time_out = SYSTEM_IIC_TIMEROUT;
    while ((I2C_GetFlagState(HRS_I2C_BUS, I2C_FLAG_MST_ACTIVITY) == SET) && (--time_out != 0));
	
	I2C_SetSlaveAddress(HRS_I2C_BUS, KX022_7BITI2C_ADDRESS);
		
    I2C_Status ret = I2C_RepeatRead(HRS_I2C_BUS, tmp_buffer, 1, buf, len);
	
	if (time_out == 0) {
		RtkWristbandSys.flag_field.i2c_bus_lock = true;
	}
	
	if (ret != I2C_Success) {
		return 1;
	}
	return 0;
}

uint8_t KX022_1020_Write_Reg(uint8_t regAddr, uint8_t data)
{
	return KX022_1020_I2C_Write(regAddr, data);
}

uint8_t KX022_1020_Read_Reg(uint8_t regAddr, uint8_t *buf)
{
	return KX022_1020_I2C_Read(regAddr, buf, 1);
}

uint8_t KX022_1020_MultiRead_Reg(uint8_t regAddr, uint8_t *buf, uint8_t len)
{	
	return KX022_1020_I2C_Read(regAddr, buf, len);
}

void * m_kx022_gsensor_timer = NULL;
#define 	ACCELERATION	16384				//In terms of acceleration

void kx022_getData(int16_t *outData)
{
	uint8_t  data_reg[6] = {0};
			
	/* Read register data */
	KX022_1020_MultiRead_Reg(KX022_XOUT_L, data_reg, 6);
	
	/* Get high 16bits data */
	outData[0] = (int16_t)(data_reg[0] | data_reg[1] << 8);	
	outData[1] = (int16_t)(data_reg[2] | data_reg[3] << 8);			
	outData[2] = (int16_t)(data_reg[4] | data_reg[5] << 8); 
}


static void kx022_timerout_handler(void *pxTimer)
{	
	#if 0
	T_IO_MSG sensor_msg = {.type = HUB_MSG_GSENSOR, .subtype = GSENSOR_MSG_DEBUG,};
    send_msg_to_hub_task(&sensor_msg, __LINE__);
	#else
	
	float accX;
	float accY;
	float accZ;
	int16_t acc_data[3] = {0};
	
	kx022_getData(acc_data);
	
	DBG_LOG("*2* acc_x:%d, acc_y:%d, acc_z:%d", acc_data[0], acc_data[1], acc_data[2]);	

	accX = (float)acc_data[0]/ACCELERATION;	
	accY = (float)acc_data[1]/ACCELERATION;	
	accZ = (float)acc_data[2]/ACCELERATION;	
		
	DBG_LOG("*3* ax:%f, ay:%f, az:%f", accX, accY, accZ);	
	
    #endif
}

static void kx022_wake_up_init(void)
{
	uint8_t int_rel;
	DBG_LOG("kx02x wake_up_init init");
    KX022_1020_Write_Reg(KX022_CNTL1,0x00);   //CNTL1 0x18 standby
    KX022_1020_Delay_ms(30);          // delay 30ms
    KX022_1020_Write_Reg(KX022_CNTL3,0x06); //CNTL3 0x1A 0x06  motion detect 50HZ ODR
    KX022_1020_Write_Reg(KX022_INC2,0x7f);  //INC2 0x1D  0x7F enable 6
    KX022_1020_Write_Reg(KX022_WUFC,0x02);  //WUFC 0x23 0x02  wakeup delay time
    KX022_1020_Write_Reg(KX022_ATH,0x02);   //ATH  0x30 0x02  wakeup 
    KX022_1020_Write_Reg(KX022_INC1,0x30);  //INC1 0x1C 0x30  enable INT1 pin
    KX022_1020_Write_Reg(KX022_INC4,0x02);  //INC4 0x1F 0x02  wakeup INT1 pin
    KX022_1020_Read_Reg(KX022_INT_REL, &int_rel);  //INT_REL 0x17 
    KX022_1020_Write_Reg(KX022_CNTL1,0xc2); //CNTL1 0x18 0xc2, ,+/-2g, enable wakeup 
}

static void kx022_wake_up_check(void)
{
	uint8_t ins2_value, int_rel;
	
	KX022_1020_Read_Reg(KX022_INS2, &ins2_value);  //INS2(0x13),bit1 1:0
    DBG_LOG("ins2_value = %d", ins2_value);

    if(ins2_value & 0x02) {
		DBG_LOG("ins2_value = %d", ins2_value);
	}
    
    KX022_1020_Read_Reg(KX022_INT_REL, &int_rel);  //INT_REL 0x17 
}

static void kx022_sleep(void)
{
	
}

static void kx022_gsensor_init(void)
{
	uint8_t KX022_ID = 0;
	KX022_1020_Read_Reg(KX022_CHIP_ID, &KX022_ID);
	
	if (KX022_ID != 0x14) {
		DBG_LOG("* gsensor kx022_chip_Fail: 0x%x", KX022_ID);	
	} 
	
	DBG_LOG("* gsensor kx022_chip_id: 0x%x", KX022_ID);	

	DBG_LOG("testData:%f, %.2f, %.2f, %.2f", 1.222, -0.121, 0.001123, 100.0011333);


	#if 0
	KX022_1020_Write_Reg(KX022_CNTL1, 0x00);
	KX022_1020_Delay_ms(30);
	KX022_1020_Write_Reg(KX022_ODCNTL, 0x03);
	KX022_1020_Write_Reg(KX022_CNTL1, 0xc0);
	KX022_1020_Delay_ms(50);	
	#else
	KX022_1020_Write_Reg(KX022_CNTL1, 0x00);
	KX022_1020_Delay_ms(30);
	KX022_1020_Write_Reg(KX022_CNTL1, 0x41);//2g
	KX022_1020_Delay_ms(5);
	KX022_1020_Write_Reg(KX022_ODCNTL, 0x02);
	KX022_1020_Delay_ms(5);
	KX022_1020_Write_Reg(KX022_CNTL3, 0xD8);
	KX022_1020_Delay_ms(5);
	KX022_1020_Write_Reg(KX022_TILT_TIMER, 0x01);
	KX022_1020_Delay_ms(5);
	KX022_1020_Write_Reg(KX022_CNTL1, 0xc1);
	KX022_1020_Delay_ms(30);	
	#endif
}

static void kx022_gsensor_timer_create(void)
{
	if (m_kx022_gsensor_timer == NULL) {
		os_timer_create(&m_kx022_gsensor_timer, "m_kx022_gsensor_timer", 1, 500, true, kx022_timerout_handler);
	}
	os_timer_start(&m_kx022_gsensor_timer);
}

三、运行结果:

四、总结

        希望能帮助到有用的人。

评论 8
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Ch_champion

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值