Commits

Martin Scharrer committed dad668a

Most Significant Bit First Version.

Comments (0)

Files changed (1)

 #define CRC16_POLY      0x1021
 #define CRC16_POLYREV   0x8408
 
+//#undef  CRC16_POLY
+//#undef  CRC16_POLYREV
+
+//#define CRC16_POLY      0x000D
+//#define CRC16_POLYREV   0xB000
+
+
 inline void crc16_init(uint16_t* crc)
 {
     *crc = CRC16_INIT;
 }
 
+// Most Significant Bit first
 void crc16_update(uint16_t* crc, uint8_t* data, size_t length)
 {
     int i;
+    uint16_t byte;
     while (length-- > 0)
     {
+        byte = (uint16_t)*data++ << 8;
         for (i=0; i<8; i++)
         {
-            
+            if ( (*crc & 0x8000) ^ (byte & 0x8000) )
+            {
+                *crc = (*crc << 1) ^ CRC16_POLY;
+            }
+            else
+            {
+                *crc <<= 1;
+            }
+            byte <<= 1;
         }
     }
 }
 
 int main(int argc, char* argv[])
 {
-    uint8_t  stdtext[9] = "123456789";
+    uint8_t  stdtext[10] = "123456789";
     uint8_t* buffer;
     size_t   length;
     uint16_t crc;
     printf("CRC: %04X\n", crc);
     return 0;
 }
+