2131 views|0 replies

17

Posts

1

Resources
The OP
 

ST32F103x Read the sensor value on X-NUCLEO-IKS01A3 via I2C [Copy link]

 This post was last edited by zdzd23 on 2019-10-6 23:52

ST32F103x 通过I2C读取X-NUCLEO-IKS01A3上传感器的值.rar (3.81 MB, downloads: 29)



ST32F103x 通过I2C读取X-NUCLEO-IKS01A3上传感器的值.rar (3.81 MB, downloads: 29)

First, the wiring: 3.3V, GND, SCL, SDA Predefined code:
/*Chip address*/
#define LSM6DSO_ADRESS 0XD6
#define LIS2MDL_ADRESS 0X3C
#define LIS2DW12_ADRESS 0X32
#define LPS22HH_ADRESS 0XBA
#define HTS221_ADRESS 0XBE
#define STTS751_ADRESS 0X94

Read and write functions:

u8 IC_ReadOneByte(u16 IC_ID,u16 addr)
{
u8 temp=0;
u8 ret_suc =1;
I2C_Start();
I2C_Send_Byte(IC_ID&0xfe);//Send device address
ret_suc=I2C_Wait_Ack();
if(ret_suc == 1) {return -1;}
I2C_Send_Byte(addr);//Send register address
ret_suc=I2C_Wait_Ack();
if(ret_suc == 1) {return -1;}
I2C_Start();
I2C_Send_Byte(IC_ID|0x01);//Send device address
ret_suc=I2C_Wait_Ack();
if(ret_suc == 1) {return -1;}
temp=I2C_Read_Byte(0); // 0 represents NACK
//I2C_NAck();
I2C_Stop();

return temp;
}

**************************************************************************

u8 IC_WriteOneByte(u16 IC_ID,u16 addr,u8 value)
{
u8 ret_suc =1;
I2C_Start();
I2C_Send_Byte(IC_ID&0xfe);//Send device address
ret_suc=I2C_Wait_Ack();
if(ret_suc == 1) {return -1;}
I2C_Send_Byte(addr);
//Single byte is the low bit of data address
ret_suc=I2C_Wait_Ack();
if(ret_suc == 1) {return -1;}

I2C_Send_Byte(value);
ret_suc=I2C_Wait_Ack();
if(ret_suc == 1) {return -1;}
I2C_Stop();

delay_ms(10);
return 0;
}
Be sure to pay attention to the specific signal timing here. If the timing is wrong, I will waste several days here.

Chip initialization:

/**********************LPS22HH initialization****************************************/
rig=0xff ;
rig=IC_ReadOneByte(LPS22HH_ADRESS,0X10);
printf("The value of LPS22HH register CONG_REG:%d\r\n",rig);
rig=rig&0x8f;
rig=rig|0X10;
do
{
if(0==IC_WriteOneByte(LPS22HH_ADRESS,0X10,rig)){j=7;} ;
delay_ms(10);
reg=IC_ReadOneByte(LPS22HH_ADRESS,0X10);
//j++;
}
while((j<6)&&(reg!=rig));
reg=IC_ReadOneByte(LPS22HH_ADRESS,0X10);
printf("The value of the register after writing:%d\r\n",reg);

delay_ms(10);
/**********************LPS22HH initialization end****************************************/
//*******************Initial setting of LSM6DSO register *****************************

j=0;
do
{
if(0==IC_WriteOneByte(LSM6DSO_ADRESS,0X10,0x10)){j=8;} ;
}
while(j!=8);
j=0;
do
{
if(0==IC_WriteOneByte(LSM6DSO_ADRESS,0X11,0x12)){j=8;} ;
}
while(j!=8);
//*******************Initial setting of LSM6DSO registersend ******************************
//*******************Initial setting of HTS221 registers *****************************

j=0;
do
{
if(0==IC_WriteOneByte(HTS221_ADRESS,0X10,0x09)){j=8;} ;
}
while(j!=8);
j=0;
do
{
if(0==IC_WriteOneByte(HTS221_ADRESS,0X20,0x81)){j=8;} ;
}
while(j!=8);
//*******************Initial setting of HTS221 registersend ******************************
while(1)
{
/**********************stt751 read****************************************/

Attention should be paid to the specific functions of the register bits here. Some chip modes must be selected, otherwise they will not enter the working state.

Reading and printing program: /**********************LSM6DSO read****************************************/
printf("/******************Five,LSM6DSO**********************/\r\n");
//Read Who I am
x=y=z=0 ;
value=IC_ReadOneByte(LSM6DSO_ADRESS,0X0f);
printf("I am LSM6DSO, ID is: %d\r\n",value);
delay_ms(10);
//Read the value of acceleration
//Read X
value_H=IC_ReadOneByte(LSM6DSO_ADRESS,0X23);
value_L=IC_ReadOneByte(LSM6DSO_ADRESS,0X22);
x=(value_H)<<8|value_L;
delay_ms(10);
//Read Y
value_H=IC_ReadOneByte(LSM6DSO_ADRESS,0X25);
value_L=IC_ReadOneByte(LSM6DSO_ADRESS,0X24);
y=(value_H)<<8|value_L;
delay_ms(10);
//Read Z
value_H=IC_ReadOneByte(LSM6DSO_ADRESS,0X27);
value_L=IC_ReadOneByte(LSM6DSO_ADRESS,0X26);
z=(value_H)<<8|value_L;
printf("The read value of LSM6DSO acceleration sensor is: X:%d Y:%d Z:%d\r\n",x,y,z);


//Read the value of gyroscope
x=y=z=0 ;
//Read X
value_H=IC_ReadOneByte(LSM6DSO_ADRESS,0X29);
value_L=IC_ReadOneByte(LSM6DSO_ADRESS,0X28);
x=(value_H)<<8|value_L;
delay_ms(10);
//Read Y
value_H=IC_ReadOneByte(LSM6DSO_ADRESS,0X2b);
value_L=IC_ReadOneByte(LSM6DSO_ADRESS,0X2a);
y=(value_H)<<8|value_L;
delay_ms(10);
//Read Z
value_H=IC_ReadOneByte(LSM6DSO_ADRESS,0X2d);
value_L=IC_ReadOneByte(LSM6DSO_ADRESS,0X2c);
z=(value_H)<<8|value_L;
printf("The value of the gyroscope of LSM6DSO is: X:%d Y:%d Z:%d\r\n",x,y,z);
//End of reading the value of the gyroscope
printf("/***********************************************/\r\n");
value=0;
delay_ms(100);
/**********************LSM6DSO readEND****************************************/
/**************************HTS221 read****************************************/
printf("/*******************Six**************************/\r\n");
delay_ms(100);
x=y=value=0 ;
value=IC_ReadOneByte(HTS221_ADRESS,0X0F);
printf("I am HTS221, my ID is: %d\r\n",value);
//Read
temperaturevalue_H=IC_ReadOneByte(HTS221_ADRESS,0X2B);
value_L=IC_ReadOneByte(HTS221_ADRESS,0X2A);
x=(value_H)<<8|value_L;
delay_ms(10);
//Read humidityvalue_H
=IC_ReadOneByte(HTS221_ADRESS,0X29);
value_L=IC_ReadOneByte(HTS221_ADDRESS,0X28);
y=(value_H)<<8|value_L;
delay_ms(10);
printf("The temperature of HTS221 is %d, the humidity is %d\r\n",x,y);
delay_ms(10);

printf("/*******************************************/\r\n");
delay_ms(100);
/**************************HTS221 readEND********************************************/

The effect is as follows:

Video sharing address: https://training.eeworld.com.cn/course/5351/learn?preview=1#lesson/22237

 

Just looking around
Find a datasheet?

EEWorld Datasheet Technical Support

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