Application of accumulation and verification algorithm in embedded system~

Link: https://blog.csdn.net/m0_37697335

Preface

Because the outside world will always have more or less interference to the circuit, for digital signals, it is likely that the transmitted data will appear to be very different.

For many occasions where data needs to be transmitted, especially some data may affect the actions of some hardware (such as some embedded devices, robots, etc.), wrong data may bring some hidden risks, which is scary to think about.

Since I am in the embedded-related field, I usually play with single-chip microcomputers. Of course, the performance of single-chip microcomputers varies greatly. However, many of the performances can only be said to be barely enough, after all, cost considerations.

So today's verification algorithm is relatively simple, but effective, especially some 性能一般的硬件.

Said today 累加和校验算法,又名CheckSum算法protagonist: . As for the source, I won’t be careful here.

Implementation of Accumulation and Check Algorithm

sender:

Accumulate the required data to get a data sum, and negate the sum to get our check value. Then send the data to be sent to the receiver together with this check value.

receiver:

Accumulate the received data (including checksum), and then add 1. If you get 0, then there is no transmission error in the data.

Note that the types used by the sender and the receiver to save the accumulation result must be the same, otherwise the overflow cannot be achieved by adding 1 and thus cannot get 0, and the verification will be invalid.

Let's give an example:

Sender: To send 0xA8, 0x50, we use unsigned char (8 bits) to save the accumulated sum, which is 0xF8 (0b11111000), and the checksum is 0x07 (0b00000111) by negating it. Then send these three data out.

Recipient: If the reception is correct, the cumulative sum of the three data is (0b11111111), at this time, add 1, and the result obtained is 0 (the actual result should be 0b100000000, but because it is saved using unsigned char (8 bits) Cumulative sum, so the high bits are truncated, leaving only the low eight bits of 0).

From the above example, we can know that the purpose of the algorithm is to add the cumulative sum and the check value to get a binary result where every bit is 1. This result is obviously very easy to handle, and this algorithm is also very easy to implement. Simple, the code example in C language is given below.

Sender: The following is the code for how to get the check value, and the result is the check value we want.

U8 TX_CheckSum(U8 *buf, U8 len) //buf为数组,len为数组长度
{ 
    U8 i, ret = 0;
 
    for (i = 0; i < len; i++)
    {
        ret += *(buf++);
    }
    ret = ~ret;
    return ret;
}

Receiver: The input contains the check value sent by the sender. If the value returned by the function is 0, the data is correct.

U8 RX_CheckSum(U8 *buf, U8 len) //buf为数组,len为数组长度
{ 
    U8 i, ret = 0;
 
    for (i = 0; i < len; i++)
    {
        ret += *(buf++);
    }
    ret = ret;
    return ret+1;
}

1. It is said that many software engineers envy hardware engineers

2. Several communication interfaces commonly used in microcontrollers, such as I2C, SPI, UART, etc.

3. The results of the January rankings of programming languages ​​are released, and we have five important findings

4.5 yuan changed to 70, hey, the chip is out of stock again

5. How does the RISC-V processor design the instruction set? What's so special

6. Macro definitions commonly used by embedded engineers

Disclaimer: This article is reproduced online, and the copyright belongs to the original author. If you are involved in the copyright of the work, please contact us, we will confirm the copyright based on the copyright certification materials you provide and pay the author's remuneration or delete the content.

Guess you like

Origin blog.csdn.net/DP29syM41zyGndVF/article/details/114266987