在 Python 中计算 CRC8-ATM CRC
8 位 CRC8-ATM 多项式用于许多嵌入式应用,包括 Trinamic UART 控制的步进电机驱动器如 TMC2209:
$$\text{CRC} = x^8 + x^2 + x^1 + x^0$$以下代码提供了如何在 Python 中计算此类 CRC 的示例:
crc8_atm.py
def compute_crc8_atm(datagram, initial_value=0):
crc = initial_value
# 遍历数据中的字节
for byte in datagram:
# 遍历字节中的位
for _ in range(0, 8):
if (crc >> 7) ^ (byte & 0x01):
crc = ((crc << 1) ^ 0x07) & 0xFF
else:
crc = (crc << 1) & 0xFF
# 移到下一位
byte = byte >> 1
return crc此代码已在 TMC2209 上经过现场验证。
Check out similar posts by category:
Algorithms, Embedded, MicroPython, Python
If this post helped you, please consider buying me a coffee or donating via PayPal to support research & publishing of new posts on TechOverflow