Microcontroller 如何计算数据的Xor

Microcontroller 如何计算数据的Xor,microcontroller,stm32,checksum,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 c

我正在尝试为一些命令实现校验和,以便使用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)

如果我能很好地理解你。这意味着每个字节有7位数据,1位奇偶校验,比如如果你想发送“1”,即0x31(0b00110001),那么即使奇偶校验+7位数据也会将其转换为0xB1(0x10110001),现在你必须将这个值异或到下一个要发送的字节。假设你想发送100个字节,然后你必须有一个临时寄存器,它会对每个字节进行异或运算,比如temp\u byte=^byte\u 1;temp_byte=^byte_2,依此类推。在发送第100个字节(即实际数据的最后一个字节)后,还必须发送Temp_字节。
PC将对所有101个字节进行异或检查,如果结果为0,则认为数据接收良好。

首先,什么是0x02 xor 0xFD?当然不是零…0x02=0b00000010 0xFD=0b11111101,看到模式了吗?在上面的情况下,命令0x02后面应该跟校验和,它是0xFD。但在数据表中提到,命令或数据+校验和必须为0。我不明白@old_timer这个命令不是数据,所以他们可以从中摆脱出来。命令及其补码的异或不等于0。大多数情况下,忽略这一部分,文档并不总是正确的,要习惯它…注意所有的流程图,它们什么时候指定校验和?