【CH579M-R1】+Drive LSM303DLH Magnetic Field Sensor
[Copy link]
This post was last edited by hujj on 2020-10-16 18:57
My homemade smart car needs to use an electronic compass for orientation, so I bought an LSM303DLH magnetic field sensor module online and tried to drive it with the CH579M-R1 development board. The front photo of the module is as follows:
This is the reverse of the module:
This is a three-axis magnetic field + three-axis acceleration sensor module, which communicates with the host device via the I2C protocol. There are 8 pads on the module, 4 of which are GND, SDA, SCL and Vcc pins, and the other 4 are motion sensor interrupt and control pins. I currently only use the I2C communication and power pins.
The information provided by the merchant was extremely limited. The sample code could not be compiled due to the lack of relevant library files. There was also little complete code found online. Therefore, I could only test it slowly based on the sample provided by the merchant. It took me more than a week to do this, and I stepped on many pitfalls. I was ready to give up several times, but I seemed to see a glimmer of hope. After posting for help, with everyone's enthusiastic help, I finally succeeded in driving it. The following is my debugging process.
The first is I2C communication. In my test environment, there is already a DS1307 calendar module, which also communicates using I2C. Based on this, I drive the LSM303DLH. According to the code for reading and writing the module in the example, the value read out of the relevant register is a fixed value, which does not change no matter how the module is rotated (see the figure below).
After working on it for a few days with no clue, I had to post for help. With the suggestion of littleshrimp moderator, I tried to compare the values of the registers before and after initialization and read the empty addresses. I found that the values read from the registers were all 2, and the value returned for the empty address was also 2. Obviously, there was a problem with the I2C communication.
To this end, I carefully checked the timing requirements for I2C communication in the data sheet, and then gradually adjusted the timing captured by the logic analyzer. The following figure shows the hand-drawn relevant timing requirements:
After repeated adjustments to the code, the timing diagram captured by the logic analyzer is already close to the standard value (see the figure below):
Despite this, the module does not respond to I2C communication, and the returned values are all zero:
Analyzing the captured timing diagram, we can also see that the module has no response (see the figure below):
Because in my test environment, I2C communication is shared with the DIN and CLK of the LCD display, and there was no problem when driving the DS1307 calendar module, and it can be driven normally, so this solution was also used when testing the LSM303DLH module. But after several days of unsuccessful testing, I had to consider using a separate pin for I2C communication. For this reason, I also replaced multiple pins for separate testing, but the results were all unsuccessful. Just when I was about to give up the test and consider buying another module, suddenly data appeared on the display (see the figure below), and I was ecstatic.
Just when I was preparing for further testing, the screen was all zero again, and I still couldn't read the data. I tested it repeatedly and tossed it for two days. During this time, I sometimes pressed the module with my hand, and occasionally I could read the data. I guessed that it might be caused by the poor connection of the DuPont line, so I replaced it with a new DuPont line and tested it with a multimeter. However, it didn't work as expected. The module still couldn't operate normally. I was at a loss and was ready to give up. Moderator w494143467 suggested that I try to heat the module with a hair dryer. I did it doubtfully, and a miracle happened. Data appeared on the screen, proving that the I2C communication was normal. In this way, when I couldn't read the data, I would blow a few breaths to heat the module first, and then use a hair dryer if it didn't work. I don't know if the manufacturer knows this problem with the chip. It is said that the temperature in my area is not low now, and the indoor temperature is above 20 degrees, which should not cause the chip to work abnormally.
After the I2C communication is normal, the next step is how to configure the registers. The example provided by the merchant only configures three registers. According to this configuration, the data of the motion sensor cannot be read out. To configure the registers well, you must first have a detailed understanding of the registers in the chip. For this purpose, I translated some of the chip register information into Chinese and organized it into an EXCEL table for display. The following figure shows some of the data:
The LSM303DLH chip uses two I2C addresses, of which the magnetic field sensor I2C address is 0x3C and the motion sensor I2C address is 0x30. Their register addresses do not overlap, but it is unknown why they occupy two I2C addresses.
From the above figure, we can see that the magnetic field sensor uses 13 registers, with addresses from 00H to 0CH. Only the first three registers can be set, and the others are read-only. According to the code in the example, I only set the 00H and 02H registers.
There are 22 registers used by the motion sensor in the data sheet, see the figure below:
Because I did not enable the interrupt function, I only set three of them. In the example, I only set the mode register with address 20H, but the data set was 0x27, that is, the data rate was 50Hz. I changed it to 0x2F (data rate 100Hz) before I could read the three-axis data of the motion sensor. I added the data filtering function and self-test function, and since then, the configuration of the module has been basically completed.
After the module is basically working properly, the data read fluctuates greatly, and calibration is required at this time. This work went smoothly. I just put the entire test device on the mouse pad and changed the angle several times, and the data became normal. The calibration should be completed. As it was late at night, the shooting effect was poor. I will make up the final test animation tomorrow.
The following are the data EXCEL compressed files and project compressed files I organized :
LSM303DLHC.rar
(402.42 KB, downloads: 19)
MyTest.rar
(875.35 KB, downloads: 12)
|