Using FRDM Board to output data and a clock for communications example.

Dependencies:   mbed

Revision:
10:90cc064f7082
Parent:
9:77ae456366f7
Child:
11:aa30552269b2
--- a/main.cpp	Wed Apr 22 10:49:08 2015 +0000
+++ b/main.cpp	Wed Apr 29 19:35:32 2015 +0000
@@ -7,7 +7,7 @@
 
 #define MAX 100 //set the max size of the character data storage array
 #define BYTE 128
-#define NIBBLE 16 //used to set binary position of msb
+#define NIBBLE 8 //used to set binary position of msb
 #define PREAMBLE 0x7E //preamble of 01111110
 #define POSTAMBLE 0x81 //postamble of 10000001
 //addressing 1st 4 bits indicate the network, 2nd 4 bits indicate the station id
@@ -139,12 +139,34 @@
 {
     //temp variable
     unsigned char temp;
+    //start at 2^25 / used to decrement through the bit places of the crc integer
+    int j = 16777216;
     
-    //shift crc over 5 spots
-    crc_bytes <<= 5;
+    //shift crc over 4 spots
+    crc_bytes <<= 4;
+    
+    //initialize temp variable to the 8 msb from crc_bytes, then XOR with CRC constant
+    temp = crc_bytes / j;
+    temp ^= CRC;
+    //decrement j
+    j--;
     
-    //return remainder
-    temp = crc_bytes % CRC;
+    //crc loop - shift in each bit position and xor with CRC constant
+    while(j>0)
+    {
+        //left shift current temp value
+        temp <<= 1;
+        //shift in new bit from crc bytes
+        temp += (crc_bytes / j) % 2;
+        //xor with crc constant
+        temp ^= CRC;
+        //decrement j
+        j--;        
+    }
+    
+    //mask out 4 msb bits and return just the 4 bit CRC remainder
+    temp = temp & 0x0F;
+    
     return temp;     
 }
 //crc = x^4+x+1