当前位置:网站首页>[robomaster] a board receives jy-me01 angle sensor data -- Modbus Protocol & CRC software verification
[robomaster] a board receives jy-me01 angle sensor data -- Modbus Protocol & CRC software verification
2022-07-29 08:06:00 【Bug can't be finished】
Angle sensor to be used in new car JY-ME01 It uses modbus Protocol for data transmission , Need to use A The board receives the data and parses the angle data , Record it for future use .
1、cubemax Configure the underlying
1.1 Clock and project configuration
Basic configuration method , It's all universal .



1.2 A serial port dma To configure
1.2.1 Open serial port , This page defaults 
1.2.2 Open serial port interrupt , Just tick it 
1.2.3 Turn on dma
Add one RX Of dma, altogether 3 Step 

After configuration, it can be consistent with the above figure .( Main confirmation The two data widths in the lower right corner are Byte)
2、 Serial port to receive data code
file :main.c
unsigned char RX_BUFFER[20]; // Define global variables to receive arrays
// After serial port initialization
__HAL_UART_ENABLE_IT(&huart8,UART_IT_IDLE);// To interrupt
HAL_UART_Receive_DMA(&huart8,RX_BUFFER,9);// Turn on dma Received receive array , length 9Byte
file :stm32f4xx_it.c
UART8_IRQHandler - Serial interrupt function
uint16_t temp;
if(__HAL_UART_GET_FLAG(&huart8,UART_FLAG_IDLE)==SET){
__HAL_UART_CLEAR_IDLEFLAG(&huart8); // Clear flag bits , Prepare to receive data next time
HAL_UART_DMAStop(&huart8); // stop it dma transmission
temp=9-hdma_uart8_rx.Instance->NDTR; /* Read ndtr In the register dma Number of data bytes sent , Then work out distance 9 How many more bytes are needed */
HAL_UART_Transmit_DMA(&huart8,RX_BUFFER,temp); // Send the remaining bytes to the receive array
}
HAL_UART_Receive_DMA(&huart8,RX_BUFFER,9); // restart dma
3.CRC check
principle
Because it is applied CRC Check algorithm , So don't study the principle too deeply .
Take a rough look at the calculation principle :
1、 preset 1 individual 16 Bit register is hexadecimal FFFF( whole 1), This register is CRC register .
2、 Take the first one. 8 Bit binary data ( That is, the first byte of the communication information frame ) And 16 Bit CRC The lower eight bits of the register are different or , Store the results in CRC register .
3、 hold CRC The contents of the register are shifted one bit to the right ( Low down ) use 0 Fill the top , And detect the shift out position after the right shift .
4、 If the shift out bit is zero , Then repeat step three ( One more right ); If you move out of the position, it's 1,CRC Registers and polynomials A001 To engage in exclusive or .
5、 Repeat step 3 and 4, Until you move right 8 Time , So the whole 8 All bit data is processed .
6、 Repeat step 2 and 5, Process the next byte of communication information frame .
7、 After calculating all bytes of the communication information frame according to the above steps , Got 16 position CRC The height of the register 、 Low byte swapping .
8、 final CRC The contents of the register are :CRC Check code .
a key :stm32 In common use CRC Verification method
1. Hardware CRC
Save computing resources , however F4 Turn on CRC Parameters cannot be configured after , It's very metaphysical , Temporarily abandon
2. Real time computing CRC check
Occupy FLASH Small , But the calculation speed is a little slower
3. Look-up table method CRC check
Look up table calculation speed , But occupation FLASH(A Board memory is large, do whatever you like ), So choose this method .
Code implementation
from CRC The verification principle is known ,CRC One feature of the check is that each eight bit binary byte corresponds to CRC Same check code . So for a byte, you can directly look up the table to get the corresponding verification result value .
The verification result corresponds to the array
It is recommended to put it in the file separately established by the user function
const unsigned char TabH[] = {
//CRC High byte value table
0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0,
0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41,
0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0,
0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40,
0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1,
0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41,
0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1,
0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41,
0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0,
0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40,
0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1,
0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40,
0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0,
0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40,
0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0,
0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40,
0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0,
0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41,
0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0,
0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41,
0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0,
0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40,
0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1,
0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41,
0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0,
0x80, 0x41, 0x00, 0xC1, 0x81, 0x40
};
const unsigned char TabL[] = {
//CRC Low byte value table
0x00, 0xC0, 0xC1, 0x01, 0xC3, 0x03, 0x02, 0xC2, 0xC6, 0x06,
0x07, 0xC7, 0x05, 0xC5, 0xC4, 0x04, 0xCC, 0x0C, 0x0D, 0xCD,
0x0F, 0xCF, 0xCE, 0x0E, 0x0A, 0xCA, 0xCB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 0x5C,
0x5D, 0x9D, 0x5F, 0x9F, 0x9E, 0x5E, 0x5A, 0x9A, 0x9B, 0x5B,
0x99, 0x59, 0x58, 0x98, 0x88, 0x48, 0x49, 0x89, 0x4B, 0x8B,
0x8A, 0x4A, 0x4E, 0x8E, 0x8F, 0x4F, 0x8D, 0x4D, 0x4C, 0x8C,
0x44, 0x84, 0x85, 0x45, 0x87, 0x47, 0x46, 0x86, 0x82, 0x42,
0x43, 0x83, 0x41, 0x81, 0x80, 0x40
} ;
float ANGLE; // Define the angle variable
uint16_t GetModbusCRC16_Tab(uint8_t *data, uint32_t len)//Modbus-CRC check ----- Look-up table method
{
unsigned int index;
unsigned char crcH = 0xFF; // high CRC byte
unsigned char crcL = 0xFF; // low CRC byte
while (len--) {
// Calculate the specified length of CRC
index = crcL ^ *data++;
crcL = crcH ^ TabH[ index];
crcH = TabL[ index];
}
return crcL<<8|crcH;// High and low displacement
}
Recommend one that can be used for inspection CRC Whether the result is the right website :http://www.ip33.com/crc.html
Serial port interrupt function is added CRC The process of calculation , Calculate the angle and output
Original calculation formula :Angle = 90063 / 262144 * 360 = 123.682708
crc_val=GetModbusCRC16_Tab(RX_BUFFER,7);
if(crc_val==((RX_BUFFER[7]<<8)|RX_BUFFER[8])){
ANGLE=(float)((RX_BUFFER[3]<<24)|(RX_BUFFER[4]<<16)|(RX_BUFFER[5]<<8)|RX_BUFFER[6]);
ANGLE=ANGLE/0x40000*360;
}
The result of changing the interrupt function .
Running results 
边栏推荐
- CDM - code division multiplexing (easy to understand)
- [beauty of software engineering - column notes] 29 | automated testing: how to kill bugs in the cradle?
- The database uses PSQL and JDBC to connect remotely and disconnect automatically from time to time
- Amaze UI icon query
- [cryoelectron microscope] relion4.0 pipeline command summary (self use)
- Alibaba political commissar system - Chapter 4: political commissars are built on companies
- "Swiss Army Knife" -nc in network tools
- Effective learning of medical image segmentation annotation based on noise pseudo tags and adversarial learning
- [note] the art of research (understand the importance of the problem)
- Operator overloading
猜你喜欢

CentOS deploy PostgreSQL 13

Arduinoide + stm32link burning debugging
![[paper reading | cryoelectron microscope] interpretation of the new subtomogram averaging method in relion 4.0](/img/8d/03e63d651b713f547b090e6e740b06.png)
[paper reading | cryoelectron microscope] interpretation of the new subtomogram averaging method in relion 4.0

Do you want to meet all the needs of customers

STM32 serial port garbled
![[cryoelectron microscope | paper reading] a feature guided, focused 3D signal permutation method for subtogram averaging](/img/50/594dfc9affbcca97166d475fe09ad3.png)
[cryoelectron microscope | paper reading] a feature guided, focused 3D signal permutation method for subtogram averaging

佳木斯市场监管局开展防疫防虫害专题食品安全网络培训

Tle5012b+stm32f103c8t6 (bluepill) reading angle data

The smallest positive number that a subset of an array cannot accumulate

@Use of jsonserialize annotation
随机推荐
Phased learning about the entry-level application of SQL Server statements - necessary for job hunting (I)
Unity beginner 2 - tile making and world interaction (2D)
In an SQL file, a test table and data are defined above, and you can select* from the test table below
[skill accumulation] presentation practical skill accumulation, common sentence patterns
Alibaba political commissar system - Chapter 1: political commissars are built on companies
[beauty of software engineering - column notes] 30 | make good use of source code management tools to make your collaboration more efficient
阿里巴巴政委体系-第三章、阿里政委与文化对接
JVM garbage collection mechanism (GC)
[flask introduction series] installation and configuration of flask Sqlalchemy
Very practical shell and shellcheck
Dynamic Thresholds Buffer Management in a Shared Buffer Packet Switch论文总结
[note] the art of research (understand the importance of the problem)
Postman interface test | JS script blocking sleep and non blocking sleep
Day 014 二维数组练习
(视频+图文)机器学习入门系列-第5章 机器学习实践
网络安全之安全基线
Resize2fs: bad magic number in super block
Internet worm
The software package is set to - > Yum source
[beauty of software engineering - column notes] 28 | what is the core competitiveness of software engineers? (next)