-------------------------------------------------- From: "Isaac Marino Bavaresco" Sent: Friday, September 17, 2010 1:06 PM To: "Microcontroller discussion list - Public." Subject: Re: [EE] CRC for Maxim temp sensor DS18B20 > Em 17/9/2010 03:49, Ruben J=F6nsson escreveu: >>> Here is some example data from the sensor: >>> 0x82 0x01 0x4B 0x46 0x7F 0xFF 0x0E 0x10 >>> CRC calculated by sensor (sent with data as 9th byte): 0x70 >>> CRC calculated by spreadsheet: 0x92 >>> >> Here are my C routines to calculate and check the CRC for 1-wire devices= : >> >> byte OW_CRC(byte *p,int numBytes) >> // Return the CRC value for numBytes number of bytes in an array >> // Note that if the CRC is included as the last byte the result >> // should be 0 if the CRC is good. >> { >> int byteIndex; >> int bitCount; >> byte CRC=3D0; >> byte nextByte; >> bool b; >> for(byteIndex=3D0;byteIndex> nextByte=3Dp[byteIndex]; >> for(bitCount=3D0;bitCount<8;bitCount++){ >> b=3D(CRC ^ nextByte)&1; >> CRC>>=3D1; >> if (b){ >> CRC^=3D0x8c; >> } >> nextByte>>=3D1; >> } >> } >> return CRC; >> } >> >> bool OW_IsIDValid(OW_ID *pID) >> // Return true if the one wire ID has a good CRC. 8th byte is CRC. >> { >> return (OW_CRC((byte*)pID,8)=3D=3D0); >> } >> >> if (OW_CRC(scratchpad,9)=3D=3D0){ // Valid CRC for nine bytes as read f= rom >> // scratchpad where the 9th byte is the=20 >> CRC. >> >> /Ruben >> > > > For bit-banged serial communications (as 1-Wire), I personally prefer to > do the CRC calculations one bit at a time as they arrive, it is much=20 > faster. > > The code that does the real calculation takes just a couple of lines and > you don't need any loops, pointer dereferences, etc. That sounds like a good way of doing things - with this project speed is no= t=20 really an issue though (one reading every 15 mins, PIC sleeps in between) s= o=20 I'll probably leave it as is, plus there is not much time left to get it=20 finished. =20 --=20 http://www.piclist.com PIC/SX FAQ & list archive View/change your membership options at http://mailman.mit.edu/mailman/listinfo/piclist .