STC15W reads RM3100 data via SPI

Publisher:吉州古玩斋Latest update time:2022-08-09 Source: csdnKeywords:STC15W Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

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

Keywords:STC15W Reference address:STC15W reads RM3100 data via SPI

Previous article:STC89C516 drives DS12C887 clock module
Next article:STC15 microcontroller reads and writes large capacity SD card (IO port simulates SPI mode)

Latest Microcontroller Articles
  • Download from the Internet--ARM Getting Started Notes
    A brief introduction: From today on, the ARM notebook of the rookie is open, and it can be regarded as a place to store these notes. Why publish it? Maybe you are interested in it. In fact, the reason for these notes is ...
  • Learn ARM development(22)
    Turning off and on interrupts Interrupts are an efficient dialogue mechanism, but sometimes you don't want to interrupt the program while it is running. For example, when you are printing something, the program suddenly interrupts and another ...
  • Learn ARM development(21)
    First, declare the task pointer, because it will be used later. Task pointer volatile TASK_TCB* volatile g_pCurrentTask = NULL;volatile TASK_TCB* vol ...
  • Learn ARM development(20)
    With the previous Tick interrupt, the basic task switching conditions are ready. However, this "easterly" is also difficult to understand. Only through continuous practice can we understand it. ...
  • Learn ARM development(19)
    After many days of hard work, I finally got the interrupt working. But in order to allow RTOS to use timer interrupts, what kind of interrupts can be implemented in S3C44B0? There are two methods in S3C44B0. ...
  • Learn ARM development(14)
  • Learn ARM development(15)
  • Learn ARM development(16)
  • Learn ARM development(17)
Change More Related Popular Components
Guess you like

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号