2926 views|18 replies

931

Posts

3

Resources
The OP
 

[CH579M-R1] + Help: Unable to drive LSM303DLH magnetic field sensor [Copy link]

 
 

I bought an LSM303DLH magnetic field sensor module online and planned to use the CH579M-R1 development board to drive it. Here is a photo of the module:

There are 8 pins on the module, 4 of which are used for I2C communication (including the positive and negative poles of the power supply), and the other 4 are ADDR (acceleration address selection), INT1, INT2, and DRY (magnetic field data ready for detection).

By analyzing the information provided by the merchant and the STM32 driver code, there is only I2C communication content, so I only connected the I2C SDA and DCK and 3.3V power supply, and the other pins were left vacant.

In this example, the initialization of LSM303DLH only operates three registers. The code is as follows:

*********************************************
* 函数名称:InitLSM303D(void)
* 函数功能:初始化LSM303D
* 入口参数:无
* 出口参数:无
* 备    注:根据需要请参考数据手册,进行修改
*********************************************/
void InitLSM303D(void)
{
    I2C_8bitSingle_Write(M_SlaveAddress,0x00,0x14);
    I2C_8bitSingle_Write(M_SlaveAddress,0x02,0x00);
    I2C_8bitSingle_Write(M_SlaveAddress,0x20,0x2F);   //测量范围,正负2g,16位模式0x27
}

The third line was originally written with 0x27, which means normal mode and 50Hz transmission rate. During the test, I changed it to 0x2F, which means the transmission rate was changed to 100Hz.

The data reading address of the magnetic field sensor in the module is different from that of the motion sensor. I checked the data sheet and it is correct. The code for reading data is as follows:

/*********************************************
* 函数名称:InitLSM303D(void)
* 函数功能:磁场数据和加速度数据读取
* 入口参数:无
* 出口参数:无
* 备    注:根据需要请参考数据手册,进行修改
*********************************************/
void LSM303D_Read(void)
{
	//磁场数据读取
    I2C_8bitAddrRead(M_SlaveAddress,0x03,6,(uint8_t *)BUF);//OUT_X_M
    M_x=(BUF[0] << 8) | BUF[1];         //合成16位数据
    M_y=(BUF[2] << 8) | BUF[3];         //合成16位数据
	M_z=(BUF[4] << 8) | BUF[5];         //合成16位数据
//    angle= atan2(M_y,M_x) * (180 / 3.14159265) + 180; //角度(度)angle in degrees
//    DATA_printf(TX_DATA,angle);         //转换角度轴数据到数组
//    Send_data('M');                     //发送数据,M:磁场0-360
    DATA_printf(TX_DATA,M_x);
	LCD_write_ASCII(0,2,1,TX_DATA);
    DATA_printf(TX_DATA,M_y);
	LCD_write_ASCII(0,3,1,TX_DATA);
    DATA_printf(TX_DATA,M_z);
	LCD_write_ASCII(0,4,1,TX_DATA);

//    LCD_write_value(0,2,5,0,0,M_x);
//    LCD_write_value(0,3,5,0,0,M_y);
//    LCD_write_value(0,4,5,0,0,M_z);
	
    //加速度数据读取
    BUF[0]=I2C_8bitSingle_Read(A_SlaveAddress,0x28);//OUT_X_A
    BUF[1]=I2C_8bitSingle_Read(A_SlaveAddress,0x29);//OUT_X_A

    BUF[2]=I2C_8bitSingle_Read(A_SlaveAddress,0x2A);//OUT_Y_A
    BUF[3]=I2C_8bitSingle_Read(A_SlaveAddress,0x2B);//OUT_Y_A

    BUF[4]=I2C_8bitSingle_Read(A_SlaveAddress,0x2C);//OUT_Z_A
    BUF[5]=I2C_8bitSingle_Read(A_SlaveAddress,0x2D);//OUT_Z_A
	  	  	
    A_x=(BUF[1] << 8) | BUF[0];         //合成16位数据
    A_y=(BUF[3] << 8) | BUF[2];         //合成16位数据
    A_z=(BUF[5] << 8) | BUF[4];         //合成16位数据
    A_x/=16.383;                        //mg
    A_y/=16.383;                        //mg
    A_z/=16.383;                        //mg
//    DATA_printf(TX_DATA,A_x);           //转换X轴数据到数组
//    Send_data('x');		   	            //发送数据,mg为单位
//    DATA_printf(TX_DATA,A_y);           //转换X轴数据到数组
//    Send_data('y');			            //发送数据,mg为单位
//    DATA_printf(TX_DATA,A_z);           //转换X轴数据到数组
//    Send_data('z');	                    //发送数据,mg为单位
//    USART1_SendData(0X0D);              //换行
//    USART1_SendData(0X0A);              //回车	
    DATA_printf(TX_DATA,A_x);
    LCD_write_ASCII(40,2,1,TX_DATA);
    DATA_printf(TX_DATA,A_y);
    LCD_write_ASCII(40,3,1,TX_DATA);
    DATA_printf(TX_DATA,A_z);
    LCD_write_ASCII(40,4,1,TX_DATA);

//    LCD_write_value(40,2,5,0,0,A_x);
//    LCD_write_value(40,3,5,0,0,A_y);
//    LCD_write_value(40,4,5,0,0,A_z);
	
    Delayms(1);						
}

The original code sends the read data through the serial port, and I modified it to display it on the LCD screen.

After the code is compiled, it is downloaded to the development board for testing. The values of the three axes of magnetic field and acceleration are all the same, 514 and 31 respectively. The orientation of the module will not change no matter how it is moved:

Compile directly using the example provided by the merchant, prompting that the library file is missing:

I downloaded the test post of the shrimp version from the forum, but it also prompted that the file was missing and could not be compiled:

I spent two days carefully reading the data sheet, but still couldn't find a solution to the problem. I also analyzed the code provided by the merchant and found that many register addresses in the modules were defined, but I couldn't find where these definitions were referenced:

#ifndef __LSM303D_H
#define __LSM303D_H


//磁场内部寄存器***********************************
/* Magnetometer registers磁力计寄存器 */
#define CRA_REG_M		0x00	/* Configuration register A */
#define CRB_REG_M		0x01	/* Configuration register B */
#define MR_REG_M		0x02	/* Mode register */
/* resume state index恢复状态索引 */
#define RES_CRA_REG_M		0	/* Configuration register A */
#define RES_CRB_REG_M		1	/* Configuration register B */
#define RES_MR_REG_M		2	/* Mode register */
/* Output register start address输出寄存器起始地址 */
#define OUT_X_M			    0x03
/* Magnetic Sensor Operation Mode磁传感器操作模块 */
#define NORMAL_MODE     	0x00
#define POS_BIAS         	0x01
#define NEG_BIAS         	0x02
#define CC_MODE          	0x00
#define SC_MODE			    0x01
#define SLEEP_MODE		    0x03
 //加速度内部寄存器***********************************
#define AXISDATA_REG	  	    0x28
#define WHOAMI_LSM303DLH_ACC	0x32	/*	Expctd content for WAI	*/
/*	CONTROL REGISTERS	*/
#define WHO_AM_I		0x0F	/*	WhoAmI register		*/
#define CTRL_REG1		0x20	/*				*/
#define CTRL_REG2		0x21	/*				*/
#define CTRL_REG3		0x22	/*				*/
#define CTRL_REG4		0x23	/*				*/
#define	CTRL_REG5		0x24	/*				*/

#define	INT_CFG1		0x30	/*	interrupt 1 config	*/
#define	INT_SRC1		0x31	/*	interrupt 1 source	*/
#define	INT_THS1		0x32	/*	interrupt 1 threshold	*/
#define	INT_DUR1		0x33	/*	interrupt 1 duration	*/

#define	INT_CFG2		0x34	/*	interrupt 2 config	*/
#define	INT_SRC2		0x35	/*	interrupt 2 source	*/
#define	INT_THS2		0x36	/*	interrupt 2 threshold	*/
#define	INT_DUR2		0x37	/*	interrupt 2 duration	*/

#define	M_SlaveAddress    0x3C
#define	A_SlaveAddress    0x30


void InitLSM303D(void);         //传感器初始化
void LSM303D_Read(void);        //读取传感器数据


#endif

In the above definition, only two I2C addresses are actually used, and the others are not referenced anywhere.

I searched for similar products on Taobao and found that the information they provided was exactly the same, so they should all come from the same source.

My question now is:

1. How to connect the ADDR, INT1, INT2, and DRY pins on the module (I only need to read the magnetic field sensor data for now, and have not yet involved the motion sensor data)?

2. Is the above module initialization code still missing, causing the module to not work normally?

3. Are there any prerequisites before reading data?

Supplementary content (2020-10-16 13:07): With your enthusiastic help, the data can basically be read out. The key is to heat the module.
This post is from Domestic Chip Exchange

Latest reply

Qinheng CH579M-R1 Development Board Review Summary post: https://bbs.eeworld.com.cn/thread-1140005-1-1.html   Details Published on 2020-10-19 10:41
 
 

9720

Posts

24

Resources
2
 

After the module is powered off and then back on, read the registers with non-zero default values to see if they are the same as those in the data sheet.

This post is from Domestic Chip Exchange

Comments

After testing, the value of register 20h before and after initialization is 2. The initial value of the register should be 7, and the initialization value is 39 (0x27), but the two values I read are both 2. Maybe there is a problem with the reading code, but this I2C code reads DS1307 normally without any error.  Details Published on 2020-10-10 12:16
Personal signature虾扯蛋,蛋扯虾,虾扯蛋扯虾
 
 
 

1w

Posts

25

Resources
3
 

Remember that the LSM303 is in low power mode by default after power-on. You must set the control register first so that the sensor will work.

This post is from Domestic Chip Exchange

Comments

It is already set to normal mode.  Details Published on 2020-10-10 11:50
 
 
 

931

Posts

3

Resources
4
 
dcexpert posted on 2020-10-10 11:49 Remember that the LSM303 is in low power mode by default after power on. You must set the control register first so that the sensor will work.

It is already set to normal mode.

This post is from Domestic Chip Exchange
 
 
 

1w

Posts

25

Resources
5
 

First read the contents of the WHO_AM_I register to see if it is correct.

This post is from Domestic Chip Exchange
 
 
 

931

Posts

3

Resources
6
 
littleshrimp posted on 2020-10-10 11:09 After the module is powered off and then powered on again, read the registers with non-zero default values to see if they are the same as the data in the data sheet?

After testing, the value of register 20h before and after initialization is 2. The initial value of the register should be 7, and the initialization value is 39 (0x27), but the two values I read are both 2. Maybe there is a problem with the reading code, but this I2C code reads DS1307 normally without any error.

This post is from Domestic Chip Exchange

Comments

Check again if it is a 7-bit address or 8-bit address problem. If it doesn't work, change a fake address (write one at random) to see if the read data changes. If it still doesn't work, try another MCU. First, eliminate the hardware problem of this sensor board.  Details Published on 2020-10-10 13:32
 
 
 

9720

Posts

24

Resources
7
 
hujj posted on 2020-10-10 12:16 After testing, the value of register 20h before and after initialization is 2. The initial value of the register should be 7, and the initialized value is 39 (0x27), but I read...

Let's see if it's a 7-bit address or an 8-bit address problem.

If it doesn't work, change a fake address (write one at random) and see if the read data changes

If it doesn't work, try another microcontroller to eliminate the hardware problem of this sensor board.

This post is from Domestic Chip Exchange

Comments

OK, I will test a few more registers and use a logic analyzer to capture the timing of I2C communication to see if there is still a problem here.  Details Published on 2020-10-10 15:17
Personal signature虾扯蛋,蛋扯虾,虾扯蛋扯虾
 
 
 

931

Posts

3

Resources
8
 
littleshrimp posted on 2020-10-10 13:32 Check if it is a 7-bit address or an 8-bit address. If it doesn't work, change a fake address (write one at random) and see if the data read has any...

OK, I'll test a few more registers.

Then use a logic analyzer to capture the timing of I2C communication to see if there is still a problem here.

This post is from Domestic Chip Exchange
 
 
 

7462

Posts

2

Resources
9
 

The old std library is used. It seems that st is no longer supported. You can find the previous version yourself. The yellow exclamation mark means it cannot be found. Please check whether the file path is correct first.

This post is from Domestic Chip Exchange

Comments

Thanks for your advice! The downloaded program cannot be compiled successfully due to missing files. There may be problems with the I2C read and write in the transplanted code. The read data are all the same. Further analysis and testing are being carried out.  Details Published on 2020-10-12 08:09
Personal signature

默认摸鱼,再摸鱼。2022、9、28

 
 
 

931

Posts

3

Resources
10
 
freebsder posted on 2020-10-11 21:33 The old std library used, st doesn't seem to be supported anymore, you can find the previous version yourself. The yellow exclamation mark means it can't be found, check if the file path is correct first. ...

Thanks for the advice!

The downloaded program cannot be compiled successfully due to missing files. There may be problems with the I2C reading and writing in the transplanted code. The data read are all the same and are under further analysis and testing.

This post is from Domestic Chip Exchange
 
 
 

931

Posts

3

Resources
11
 

After several days of debugging, I finally completed the I2C communication with LSM303DLH and successfully performed read and write operations on LSM303DLH. It seems that the DS1307 calendar module has a stronger fault tolerance for I2C communication. My original code can read and write data for the DS1307 calendar module, but it can't communicate with the LSM303DLH module. After carefully comparing the timing captured by the logic analyzer with the requirements in the data sheet and slowly adjusting, I finally completed the I2C communication. The following figure shows the test process:

This is the data read out:

However, the data returned by the sensor module is incorrect and does not change with the rotation of the sensor position. It seems that the LSM303DLH module has not been configured correctly. However, from the timing diagram captured by the logic analyzer, the configuration data of the three registers should have been sent to the sensor module, but I don’t know why the sensor cannot work properly. According to the example found, the initialization configuration of the module is:

I2C_8bitSingle_Write(M_SlaveAddress,0x00,0x14); //0x14|0x10
I2C_8bitSingle_Write(M_SlaveAddress,0x02,0x00);
I2C_8bitSingle_Write(M_SlaveAddress,0x20,0x27); //Measurement range, positive and negative 2g, 16-bit mode 0x27

The timing diagram captured by the logic analyzer is as follows:

The above timing diagram is sending data 0x14 to register 0x00 and data 0x00 to register 0x02, and the following timing diagram is sending data 0x27 to register 0x20. From the timing diagram, the sent address and data are correct, and the module also receives the data normally (with Ack response).

The following is the timing diagram for receiving data:

Here is an animation of the testing process:

This post is from Domestic Chip Exchange
 
 
 

931

Posts

3

Resources
12
 
This post was last edited by hujj on 2020-10-14 21:08

I don't know if it's because the DuPont line is not in good contact or for other reasons, but today I could read the magnetic field data once in a while. When I turned the module, the data would change, but it was short-lived and I couldn't read the correct data anymore.

This post is from Domestic Chip Exchange
 
 
 

7462

Posts

2

Resources
13
 

Come on, come on, it's quite detailed.

This post is from Domestic Chip Exchange

Comments

Thanks for the encouragement, I'm still exploring.  Details Published on 2020-10-15 09:04
Personal signature

默认摸鱼,再摸鱼。2022、9、28

 
 
 

1942

Posts

2

Resources
14
 

The magnetic field sensor should be calibrated. If you cannot read the data and there is no problem with the timing, try shortening the communication line. This kind of communication problem requires constant debugging.

If you feel that you cannot communicate with the magnetic sensor (but can communicate with the accelerometer), you can try to blow the magnetic sensor again with a hot air gun. A colleague has encountered this problem before.

This post is from Domestic Chip Exchange

Comments

Thank you very much! I really blew the module with a hair dryer and I can read the data again. No wonder I can occasionally read the data when I press the module with my hand. I thought it was caused by poor contact.  Details Published on 2020-10-15 09:29
 
 
 

931

Posts

3

Resources
15
 
freebsder posted on 2020-10-14 21:03 Come on, come on, it's quite detailed

Thanks for the encouragement, I'm still exploring.

This post is from Domestic Chip Exchange
 
 
 

931

Posts

3

Resources
16
 
w494143467 Published on 2020-10-14 22:28 The magnetic field sensor should need to be calibrated. If you can't read the data and there is no problem with the timing, you can try to shorten the communication line. This kind of communication problem...

Thank you very much! I really blew the module with a hair dryer and I can read the data again. No wonder I can occasionally read the data when I press the module with my hand. I thought it was caused by poor contact.

This post is from Domestic Chip Exchange

Comments

This is just experience. The colleague was stuck for two days and looked for various solutions. Finally, the supervisor said, "Just brag about it," and it worked. My colleague was so excited. Haha, you can organize this post and post it as an experience post. If it is well written, I can feature it!  Details Published on 2020-10-15 09:49
 
 
 

1942

Posts

2

Resources
17
 
hujj posted on 2020-10-15 09:29 Thank you very much! I really blew the module with a hair dryer and I can read the data again. No wonder I sometimes press the module with my hand...

This is just my experience. My colleague was stuck for two days and looked for various solutions. Finally, the supervisor said, "Blow it!" and it worked. My colleague was so excited, haha.

You can organize your post and post it as an experience post. If it is well written, I can add it to the featured posts!

This post is from Domestic Chip Exchange

Comments

Thank you for the encouragement! I haven't been able to read the data stably yet. I will definitely summarize the experience and lessons after it is completely successful.  Details Published on 2020-10-15 10:04
 
 
 

931

Posts

3

Resources
18
 
w494143467 Published on 2020-10-15 09:49 This is just my experience. My colleague was stuck for two days and found various solutions. Finally, the supervisor said, "Blow it, and it worked." My colleague was excited...

Thank you for the encouragement! I haven't been able to read the data stably yet. I will definitely summarize the experience and lessons after it is completely successful.

This post is from Domestic Chip Exchange
 
 
 

1w

Posts

203

Resources
19
 

Qinheng CH579M-R1 Development Board Review

Summary post: https://bbs.eeworld.com.cn/thread-1140005-1-1.html

This post is from Domestic Chip Exchange
Add and join groups EEWorld service account EEWorld subscription account Automotive development circle
Personal signature

玩板看这里:

https://bbs.eeworld.com.cn/elecplay.html

EEWorld测评频道众多好板等你来玩,还可以来频道许愿树许愿说说你想要玩的板子,我们都在努力为大家实现!

 
 
 

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