Skip to content

Commit

Permalink
CRC8 check update
Browse files Browse the repository at this point in the history
  • Loading branch information
xzf89718 committed Nov 14, 2022
1 parent 1539fa6 commit a8377b0
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 6 deletions.
6 changes: 3 additions & 3 deletions AHT20_sensor_control/AHT20_sensor_wrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -195,9 +195,9 @@ def CheckCRC(self):
"""
# Get all 6 bytes data and 1 bytes CRC code
frame_for_CRC8_check = 0x00
for i in range(0, 7):
frame_for_CRC8_check << 8 | self.all_data[i]
isCRCOK = AHT20_crc8_check(frame_for_CRC8_check)
# for i in range(0, 7):
# frame_for_CRC8_check << 8 | self.all_data[i]
isCRCOK = AHT20_crc8_check(self.all_data)
if (isCRCOK):
return AHT20Status.AHT20_CRCOK
else:
Expand Down
49 changes: 46 additions & 3 deletions AHT20_sensor_control/crc8_helper.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,49 @@
# A total of 6 * 8 bits data need to check
N_DATA = 6
# 1 * 8 bits CRC
N_CRC = 1
# Initial value. Equal to bit negation the first data (status of AHT20)
INIT = 0xFF
# Useful value to help calculate
LAST_8_bit = 0xFF

def AHT20_crc8_check(int_data):

# Devide number retrieve from CRC-8 MAXIM G(x) = x8 + x5 + x4 + 1
CRC_DEVIDE_NUMBER = 0x131

# Data and CRC taken from AHT20, use this for testing?
TEST_DATA = [[28, 184, 245, 165, 156, 208, 163], [28, 185, 16, 149, 156, 83, 112], [
28, 184, 249, 85, 156, 114, 213], [28, 185, 9, 53, 156, 54, 45], [28, 185, 70, 117, 156, 189, 33]
, [28, 185, 64, 165, 156, 61, 209]]


def AHT20_crc8_check(all_data_int, init_value=0xFF):
"""
The input data shoule be:
Status Humidity0 Humidity1 Humidity2|Temperature0 Temperature1 Temperature2 CRCCode
In python's int64.
"""
# Preprocess all the data and CRCCode from AHT20
DATA_FROM_AHT20 = 0x00
# Preprocessing the first data (status)
DATA_FROM_AHT20 = DATA_FROM_AHT20 | all_data_int[0]
print(bin(DATA_FROM_AHT20))
DATA_FROM_AHT20 = DATA_FROM_AHT20 ^ init_value
print(bin(DATA_FROM_AHT20))
for i_data in range(1, N_DATA + N_CRC):
DATA_FROM_AHT20 = DATA_FROM_AHT20 << 8 | all_data_int[i_data]
print(bin(DATA_FROM_AHT20))
mod_value = DATA_FROM_AHT20 % CRC_DEVIDE_NUMBER
print(mod_value)
if (mod_value == 0):
return True
else:
return False


def AHT20_crc8_calculate(all_data_int):
pass

def AHT20_crc_calculate(int_data):
pass
if __name__ == "__main__":
for data in TEST_DATA:
AHT20_crc8_check(data, INIT)

0 comments on commit a8377b0

Please sign in to comment.