I recently took over a project that required me to read RM3100 data from an STC microcontroller. I checked on Taobao and found that it was not cheap.
This thing itself is not very difficult, the problem is that there is no reference material, just the official website.
The program idea is very simple, adjust SPI communication and adjust initialization settings.
Looking at the data sheet, these registers can all be set, but fortunately, not too many.
There is no need to set them all up, just set the ones you need. I mainly set the continuous measurement and conversion cycle.
/**
* @brief Sets cycle count and updates gain and max_data_rate values
* @param[in] desire value PNI recomends values between 30 and 400 Hz
* @return 1 if successful, 0 otherwise.
*/
void setCycleCount ( u16 value )
{
char to_reg[6];
if (value > 400)
value=400;
else if (value < 30)
value=30;
to_reg[0] = value>>8;
to_reg[1] = value;
to_reg[2] = to_reg[0];
to_reg[3] = to_reg[1];
to_reg[4] = to_reg[0];
to_reg[5] = to_reg[1];
SPIWriteBytes(M3D_3100_CCX,6,to_reg);
}
/**
* @brief Sets data rate in Continuous Measurement Mode.
* Fails if desire datarate is higher than the max data rate recommended by PNI.
* @param[in] Data rate configuration BYTE
* @return 1 if successful, 0 otherwise.
*/
void setCMMdatarate ( char conf )
{
char *ptr;
float temp = 1000;
switch (conf){
case CMM_UPDATERATE_600: temp=600;
break;
case CMM_UPDATERATE_300: temp=300;
break;
case CMM_UPDATERATE_150: temp=150;
break;
case CMM_UPDATERATE_75 : temp=75;
break;
case CMM_UPDATERATE_37 : temp=37;
break;
case CMM_UPDATERATE_18 : temp=18;
break;
case CMM_UPDATERATE_9 : temp=9;
break;
case CMM_UPDATERATE_4_5: temp=4.5;
break;
case CMM_UPDATERATE_2_3: temp=2.3;
break;
case CMM_UPDATERATE_1_2: temp=1.2;
break;
case CMM_UPDATERATE_0_6: temp=0.6;
break;
case CMM_UPDATERATE_0_3: temp=0.3;
break;
case CMM_UPDATERATE_0_15: temp=0.15;
break;
case CMM_UPDATERATE_0_075: temp=0.075;
break;
}
ptr = &conf;
SPIWriteBytes(M3D_3100_TMRC,1,ptr);
}
After setting, the data is read
/**********************/
/**** ¶ÁÈ¡ Êý¾Ý ******/
/**********************/
void MagGetData(void)
{
int i = 0;
unsigned char temp[9]={0};
static long Mag_Data[3]={0};
static char state = 0; //0 :cmd 1:wait get data
static char cNoDataCnt = 0;
if (CheckMagDataReady())
{
//´«¸ÐÆ÷×¼±¸ºÃ´¦ÀíÊý¾Ý
state = 0;
//¶ÁÈ¡ 1. 2. 3. 4. 5. 6. 7. 8. 9.
SPIReadBytes(0xA4,9,temp);
//
Mag_Data[0]=temp[0]<<16 | temp[1]<<8 | temp[2]; //ned×ø±êϵ--Ç°ÌáÊÇËùÓк¸½Ó¶¼ÊDZê¼Çλº¸½Óµ½N
Mag_Data[1]=temp[3]<<16 | temp[4]<<8 | temp[5];
Mag_Data[2]=temp[6]<<16 | temp[7]<<8 | temp[8];
for(i=0;i<3;i++)
{
if(Mag_Data[i]&0x00800000)
Mag_Data[i]|=0xff000000;
}
temp[0]=0x70;
SPIWrite(0,0x70);
}
else
{
//´«¸ÐÆ÷»¹Ã»×¼±¸ºÃ´¦ÀíÊý¾Ý
cNoDataCnt+=5;
if (cNoDataCnt>=20)
{
cNoDataCnt=0;
SPIWrite(0,0x70);
}
}
//Êý¾ÝתÒƵ½½á¹¹Ìå
rm3100.rx = Mag_Data[0];
rm3100.ry = Mag_Data[1];
rm3100.rz = Mag_Data[2];
//´®¿ The most beautiful thing ÈýÖá²ÎÊý
printf("´Å³¡Ç¿¶Èrn");
printf("XÖá:%ldrn",rm3100.rx);
printf("YÖá:%ldrn",rm3100.ry);
printf("ZÖá:%ldrn",rm3100.rz);
}
After completion, the currently measured magnetic field strength can be printed out through the serial port
Previous article:STC89C516 drives DS12C887 clock module
Next article:STC15 microcontroller reads and writes large capacity SD card (IO port simulates SPI mode)
- Popular Resources
- Popular amplifiers
- Principles and Applications of Single Chip Microcomputers 3rd Edition (Zhang Yigang)
- Microcontroller Principles and Application Project Tutorial - Based on STC15W4K32S4 Series Microcontroller
- MC9S12XS MCU Principle and Embedded System Development
- MCU Principles and Interface Technology C51 Programming (Edited by Zhang Yigang)
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- Innolux's intelligent steer-by-wire solution makes cars smarter and safer
- 8051 MCU - Parity Check
- How to efficiently balance the sensitivity of tactile sensing interfaces
- What should I do if the servo motor shakes? What causes the servo motor to shake quickly?
- 【Brushless Motor】Analysis of three-phase BLDC motor and sharing of two popular development boards
- Midea Industrial Technology's subsidiaries Clou Electronics and Hekang New Energy jointly appeared at the Munich Battery Energy Storage Exhibition and Solar Energy Exhibition
- Guoxin Sichen | Application of ferroelectric memory PB85RS2MC in power battery management, with a capacity of 2M
- Analysis of common faults of frequency converter
- In a head-on competition with Qualcomm, what kind of cockpit products has Intel come up with?
- Dalian Rongke's all-vanadium liquid flow battery energy storage equipment industrialization project has entered the sprint stage before production
- Allegro MicroSystems Introduces Advanced Magnetic and Inductive Position Sensing Solutions at Electronica 2024
- Car key in the left hand, liveness detection radar in the right hand, UWB is imperative for cars!
- After a decade of rapid development, domestic CIS has entered the market
- Aegis Dagger Battery + Thor EM-i Super Hybrid, Geely New Energy has thrown out two "king bombs"
- A brief discussion on functional safety - fault, error, and failure
- In the smart car 2.0 cycle, these core industry chains are facing major opportunities!
- The United States and Japan are developing new batteries. CATL faces challenges? How should China's new energy battery industry respond?
- Murata launches high-precision 6-axis inertial sensor for automobiles
- Ford patents pre-charge alarm to help save costs and respond to emergencies
- New real-time microcontroller system from Texas Instruments enables smarter processing in automotive and industrial applications