Simple Software Validates the Hardware CRC and Detects Errors in the Serial Bit Stream of 1-Wire® Thermal Devices
Abstract: All 1-Wire thermal devices contain a unique idenTIficaTIon code in read-only memory (ROM). This idenTIficaTIon code is used as a unique network address on a 1-Wire bus. Additionally the scratchpad memory in some thermal devices also has a CRC (cyclic redundancy check) byte to validate 1-Wire communication. This application note and the accompanying software provide an easy way to verify successful communication among 1-Wire devices or to detect errors in the serial data stream. The methodology calculates the DOW (Dallas One Wire) CRC value in software and validates that value against the CRC value from the CRC hardware implementation in each device. The DS1822 Econo 1-Wire Digital Thermometer serves as the example device.
A CRC (cyclic redundancy check) is the most effective scheme for error detection in a serial data stream. CRC lets the user validate successful communication among the thermal devices. . A CRC also has minimal hardware requirements. The DOW (Dallas One Wire) CRC is used in the Maxim 1-Wire thermal products, e.g. the DS1821, DS1822, DS18B20. The polynomial that represents this DOW CRC is:
Polynomial = X8 + X5 + X4 + 1
For details on the CRC algorithm, see application note 27, "Understanding and Using Cyclic Redundancy Checks with Maxim iButton® Products." The CRC can be most easily understood by considering the function as it would actually be built in hardware, usually represented as a shift register arrangement with feedback. The hardware arrangement in the DS1822 is shown in Figure 1.
Figure 1. The CRC hardware representation.
Each DS1822 contains a unique 8-byte identification code stored in ROM. The least significant byte of the ROM code contains the DS1822's 1-Wire family code: 22h. The next 6 bytes contain a unique serial number. The most significant byte is a CRC byte that is calculated from the first 7 bytes of the ROM code.
The first 8 bytes of the DS1822 scratchpad contain the values for the high- and low-temperature thresholds and the Configuration register. These bytes also contain the temperature value read by the DS1822 and reserved registers. Byte 9 is the CRC value calculated from the first 8 bytes.
The CRC algorithm used for calculating the CRC byte for both the scratchpad and the ROM is the same, as explained in application note 27. The Shift register in Figure 1 starts with an initial condition of all 0s in its bit values. One bit starting with the LSB is shifted in through the input data. After all the bits are shifted in, the result represents the calculated CRC value for that particular data. In total, 56 bits (7 bytes) are shifted in for the ROM code and 64 bits (8 bytes) for the scratchpad.
The DS1822 contains CRC hardware that will provide a CRC value either for the ROM code or for the scratchpad. This information is then transmitted on the 1-Wire bus. The received data can be corrupted by the communication interface. The packet of received data (8 bytes for the ROM code and 9 bytes for the scratchpad) can be verified as correct by using a software-calculated CRC.
A Microsoft® Excel spreadsheet (using Microsoft Excel 2003) is designed to calculate this CRC value for the DS1822. A snapshot is shown below in Figure 2.
Figure 2. A snapshot of Excel CRC calculator.
The user enters the values in the last 7 bytes of the ROM code or the first 8 bytes of the scratchpad. By pressing the Calculate CRC button, the corresponding CRC values are calculated; these values can be verified against the value calculated in the hardware.
The VBA code used to develop this Excel spreadsheet follows.
Private Sub ROMCRC_Click() Dim InHex, OutBinStr As String Dim OutBinArr(1 To 56) As Integer Dim OutDec, i, CRC(1 To 8), CRCTemp As Integer InHex = Range("ROMByte1").Value & Range("ROMByte2").Value & Range("ROMByte3").Value & Range("ROMByte4").Value & Range("ROMByte5").Value & Range("ROMByte6").Value & Range("ROMByte7").Value OutBinStr = HexToBin(InHex) ' Convert string to array, LSB = OutBinArr(1) For i = 1 To 56 OutBinArr(57 - i) = Mid$(OutBinStr, i, 1) ' Split(OutBinStr) Next i 'Initialize CRC For i = 1 To 8 CRC(i) = 0 Next i 'Calculate CRC For i = 1 To 56 CRCTemp = CRC(1) Xor OutBinArr(i) CRC(1) = CRC(2) CRC(2) = CRC(3) CRC(3) = CRC(4) Xor CRCTemp CRC(4) = CRC(5) Xor CRCTemp CRC(5) = CRC(6) CRC(6) = CRC(7) CRC(7) = CRC(8) CRC(8) = CRCTemp Next i DecCRC = BinToDec(CRC) Range("ROMCRCValue").Value = DecCRC End Sub Private Sub ScratchCRC_Click() Dim InHex, OutBinStr As String Dim OutBinArr(1 To 64) As Integer Dim OutDec, i, CRC(1 To 8), CRCTemp As Integer InHex = Range("HexByte1").Value & Range("HexByte2").Value & Range("HexByte3").Value & Range("HexByte4").Value & Range("HexByte5").Value & Range("HexByte6").Value & Range("HexByte7").Value & Range("HexByte8").Value OutBinStr = HexToBin(InHex) ' Convert string to array, LSB = OutBinArr(1) For i = 1 To 64 OutBinArr(65 - i) = Mid$(OutBinStr, i, 1) ' Split(OutBinStr) Next i 'Initialize CRC For i = 1 To 8 CRC(i) = 0 Next i 'Calculate CRC For i = 1 To 64 CRCTemp = CRC(1) Xor OutBinArr(i) CRC(1) = CRC(2) CRC(2) = CRC(3) CRC(3) = CRC(4) Xor CRCTemp CRC(4) = CRC(5) Xor CRCTemp CRC(5) = CRC(6) CRC(6) = CRC(7) CRC(7) = CRC(8) CRC(8) = CRCTemp Next i DecCRC = BinToDec(CRC) Range("DecCRCValue").Value = DecCRC End Sub Private Function HexToBin(hstr) 'convert hex string to binary string cnvarr = Array("0000", "0001", "0010", "0011", _ "0100", "0101", "0110", "0111", "1000", _ "1001", "1010", "1011", "1100", "1101", _ "1110", "1111") bstr = "" For i = 1 To Len(hstr) hdgt = Mid(hstr, i, 1) cix = CInt("&H" & hdgt) bstr = bstr & cnvarr(cix) Next HexToBin = bstr End Function Function BinToDec(bstr) 'convert 8 bit Binary number to Decimal Dim j, Out As Integer Out = 0 For j = 1 To 8 Out = Out + bstr(j) * 2 ^ (j - 1) Next j BinToDec = Out End FunctionConclusion
CRC values make it easy to ensure that the communication among 1-Wire devices is successful. By using a software-based CRC in tandem with a 1-Wire device's hardware CRC, the 1-wire communication can be validated.
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)