如何计算数据的Xor

时间:2018-04-15 21:06:03

标签: microcontroller stm32 checksum

我正在尝试实现某些命令的校验和,以便使用uart与内部ROM通信。例如:commad代码0x02 + 0xFD将给出设备的id。如何计算0xFD?

From the data sheet:
All communications from the programming tool (PC) to the device are verified by:
1. checksum: received blocks of data bytes are XORed. A byte containing the computed
XOR of all previous bytes is added to the end of each communication (checksum byte).
By XORing all received bytes, data + checksum, the result at the end of the packet
must be 0x00
2. for each command the host sends a byte and its complement (XOR = 0x00)
3. UART: parity check active (even parity)

1 个答案:

答案 0 :(得分:0)

如果我理解你的话。那意味着每个字节有7位数据,1位奇偶校验,就像你想要发送' 1'这是0x31(0b00110001),然后偶校验+7数据位将其更改为0xB1(0x10110001),现在您必须将此值xor到您要发送的下一个字节。让我们说你要发送100字节,然后你有使用临时寄存器来保持每个字节的xor,如temp_Byte = ^ Byte_1; temp_byte = ^ Byte_2等等。发送第100个字节(这是实际数据的最后一个字节)后,您还必须发送Temp_byte。 PC将通过xor<所有101个字节进行检查,如果结果为0,则认为数据收到的很好。