3041 views|0 replies

282

Posts

2

Resources
The OP
 

GD32E231 analog IIC driver LIS2DW12 [Copy link]

 

Continue playing with the accelerometer LIS2DW12.

The main purpose is to obtain the gravitational acceleration of the three axes.

Looking at the schematic diagram, this is connected to IIC2, but the interface using IIC1 can also be used, because IIC and IIC2 are connected to the same bus and can be used interchangeably:

In the above picture, you can see that the IIC address is 0x32


Registers. This chip has many registers.

The main registers used are as follows:

For some reason, two temperature registers are provided, one is 12-bit and the other is 8-bit. The two registers are as follows.

12bit:

The 4th bit of the temperature register is not used, so the read value needs to be divided by 16. See the following description 16LBS/℃. When the register value is all 0, it is 25 degrees Celsius, so the actual value is 25+register value/16/16.

8bit:

When the register value is all 0, it is 25℃, and when there is no 1LBS, it is 1℃, so the temperature value is 25 + register value .

Who_am_i Registers:

Control Register 1:

Status register:

XYZ output registers:



Code:

initialization:

	
	//初始化传感器LIS2DW12
	if(0 == Set_Reg_LIS2DW12(LIS2DW12_ADDR_W_1, LIS2DW12_CTRL1, 0x16))
	{
		send_str((unsigned char *)"IF_CTRL set successful\r\n");
	}
	else
	{
		send_str((unsigned char *)"IF_CTRL set fail\r\n");
	}

Register operation and serial port data transmission:

//LIS2DW12写
static int IIC_WrDat_LIS2DW12(uint8_t IIC_Add, uint8_t IIC_Reg, uint8_t IIC_Data)
{
	uint8_t ret_suc = 1;
	
	IIC_Start();
	IIC_Send_Byte(IIC_Add & 0xfe);
	ret_suc = IIC_Wait_Ack();
	if(ret_suc == 1)	{IIC_Stop();return -1;}
	
	IIC_Send_Byte(IIC_Reg);			//write data
	ret_suc = IIC_Wait_Ack();
	if(ret_suc == 1)	{IIC_Stop();return -1;}
	
	IIC_Send_Byte(IIC_Data);
	ret_suc = IIC_Wait_Ack();
	if(ret_suc == 1)	{IIC_Stop();return -1;}	
	
	IIC_Stop();
	
	return 0;
}


//LIS2DW12读
static int IIC_Read_LIS2DW12(uint8_t IIC_Add, uint8_t IIC_Reg)
{
	int ret;
	uint8_t ret_suc = 1;
	
	IIC_Start();
	IIC_Send_Byte(IIC_Add & 0xfe);
	ret_suc = IIC_Wait_Ack();
	if(ret_suc == 1)	{IIC_Stop();return -1;}
		
	IIC_Send_Byte(IIC_Reg);
	ret_suc = IIC_Wait_Ack();
	if(ret_suc == 1)	{IIC_Stop();return -1;}
		
	IIC_Start();
	IIC_Send_Byte(IIC_Add | 0x01);
	ret_suc = IIC_Wait_Ack();
	if(ret_suc == 1)	{IIC_Stop();return -1;}
	
    ret=IIC_Read_Byte(0);
	IIC_Stop();

	return ret;
}

//LIS2DW12读2个寄存器
static int IIC_Read2_LIS2DW12(uint8_t IIC_Add, uint8_t IIC_Reg)
{
	int ret;
	uint8_t ret_suc = 1;
	
	IIC_Start();
	IIC_Send_Byte(IIC_Add & 0xfe);
	ret_suc = IIC_Wait_Ack();
	if(ret_suc == 1)	{IIC_Stop();return -1;}
		
	IIC_Send_Byte(IIC_Reg);
	ret_suc = IIC_Wait_Ack();
	if(ret_suc == 1)	{IIC_Stop();return -1;}
		
	IIC_Start();
	IIC_Send_Byte(IIC_Add | 0x01);
	ret_suc = IIC_Wait_Ack();
	if(ret_suc == 1)	{IIC_Stop();return -1;}
	
    ret=IIC_Read_Byte(1);
	ret=(IIC_Read_Byte(0)<<8)|ret;
	
	IIC_Stop();

	return ret;
}

//设置寄存器
int Set_Reg_LIS2DW12(uint8_t add, uint8_t reg, uint8_t dat)
{
	int ret_succ = -1;
	uint8_t i = 20u;		//失败之后尝试的次数
	
	do{
		if(0 == IIC_WrDat_LIS2DW12(add, reg, dat))
		{
			ret_succ = 0;		//设置成功
			break;
		}
		else
		{
			i--;				//获取失败,再次尝试
		}
	}while(i>0u);
	
	return ret_succ;
}

//读取寄存器
int Get_Reg_LIS2DW12(uint8_t add, uint8_t reg)
{
	uint8_t i = 20u;
	int ret_get = -1;
	
	do{
		ret_get = IIC_Read_LIS2DW12(add, reg);
		if(-1 != ret_get)
		{
			break;				//获取数据成功
		}
		else
		{
			i--;				//获取失败,再次尝试
		}
	}while(i>0u);
	
	return ret_get;
}

//读取我是谁
int Get_WHO_AM_I_LIS2DW12(void)
{
	return Get_Reg_LIS2DW12(LIS2DW12_ADDR_W_1, LIS2DW12_REG_WHO_AM_I);	
}


//获取状态寄存器
int Get_Status_LIS2DW12(void)
{
	return Get_Reg_LIS2DW12(LIS2DW12_ADDR_W_1, LIS2DW12_CFG_STATUS);	
}

float Get_Temp_1_LIS2DW12(void)
{
	short temp_reg = (Get_Reg_LIS2DW12(LIS2DW12_ADDR_W_1, LIS2DW12_OUT_T_H)<<8)|Get_Reg_LIS2DW12(LIS2DW12_ADDR_W_1, LIS2DW12_OUT_T_L);
	float temp_cal = 0.0f;
	
	temp_cal = 25.0+temp_reg/16.0/16.0;
	
	return temp_cal;
}

signed char Get_Temp_2_LIS2DW12(void)
{
	return (25+(Get_Reg_LIS2DW12(LIS2DW12_ADDR_W_1, LIS2DW12_OUT_T)));
}

short Get_X_LIS2DW12(void)
{
	return ((Get_Reg_LIS2DW12(LIS2DW12_ADDR_W_1, LIS2DW12_OUT_X_H)<<8)|Get_Reg_LIS2DW12(LIS2DW12_ADDR_W_1, LIS2DW12_OUT_X_L));
}

short Get_Y_LIS2DW12(void)
{
	return ((Get_Reg_LIS2DW12(LIS2DW12_ADDR_W_1, LIS2DW12_OUT_Y_H)<<8)|Get_Reg_LIS2DW12(LIS2DW12_ADDR_W_1, LIS2DW12_OUT_Y_L));
}

short Get_Z_LIS2DW12(void)
{
	return ((Get_Reg_LIS2DW12(LIS2DW12_ADDR_W_1, LIS2DW12_OUT_Z_H)<<8)|Get_Reg_LIS2DW12(LIS2DW12_ADDR_W_1, LIS2DW12_OUT_Z_L));
}


//发送数据到串口
void Send_Uart_LIS2DW12(void)
{	
	char str[35] = {0};
	
	send_str((uint8_t *)"<-----------START--------->\r\n");		
	sprintf(str,"X:%d | Y:%d | Z:%d\r\n", Get_X_LIS2DW12(),Get_Y_LIS2DW12(),Get_Z_LIS2DW12());
	send_str((uint8_t *)str);	

	sprintf(str,"Temp:%.2f℃ | %d℃\r\n", Get_Temp_1_LIS2DW12(),Get_Temp_2_LIS2DW12());
	send_str((uint8_t *)str);

	sprintf(str,"Status:0x%X\r\n", Get_Status_LIS2DW12());
	send_str((uint8_t *)str);	
	
	sprintf(str,"Who_am_i:%X\r\n",Get_WHO_AM_I_LIS2DW12());			
	send_str((uint8_t *)str);			
	send_str((uint8_t *)"<------------END---------->\r\n\n");			
}




Display effect:

This post is from MEMS sensors
 
 

Just looking around
Find a datasheet?

EEWorld Datasheet Technical Support

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews

Room 1530, Zhongguancun MOOC Times Building, Block B, 18 Zhongguancun Street, Haidian District, Beijing 100190, China Tel:(010)82350740 Postcode:100190

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京B2-20211791 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号
快速回复 返回顶部 Return list