Knud Dalgaard / 310-TMC3-TestHW

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers crc16.cpp Source File

crc16.cpp

00001 
00002 
00003 //----------------------------------------
00004 // Includes
00005 //----------------------------------------
00006 #include "mbed.h"
00007 #include "crc16.h"
00008 
00009 
00010 //----------------------------------------
00011 // Prototype declaration
00012 //----------------------------------------
00013 
00014 
00015 //----------------------------------------
00016 // global Variables
00017 //----------------------------------------
00018 
00019 
00020 //----------------------------------------
00021 // Function
00022 //----------------------------------------
00023 
00024 //---------------------------------------------------------------------
00025 //  Function Name:  calcCRC16
00026 //  Description:    calculate CRC16 from preview CRC16 value and new data byte
00027 //
00028 //  Inputs:                 - uint8 *ptrData:   first data byte
00029 //                                  - uint16 numbBytes: Number of data bytes to process
00030 //                                  - uint16 *ptrCRC16: return value
00031 //
00032 //  Returns:        None
00033 //
00034 //  Last Change:    05.04.2013
00035 //-----------------------------------------------------------------------
00036 void calcCRC16(char *ptrData, int numbBytes, unsigned short *ptrCRC16)
00037 {
00038     unsigned short i;
00039     unsigned short i_bytes;
00040     unsigned short CRC16;
00041     char data;
00042     
00043     CRC16 = CRC16_STARTVALUE;
00044 
00045     for(i_bytes=0; i_bytes < numbBytes; i_bytes++)
00046     {
00047         data = *ptrData++;  // get new data, inc pointer
00048 
00049         for(i=0;i<8;i++)
00050         {
00051             if( ( (CRC16 ^ data) & 0x01) == 0x01 )
00052             {
00053                 CRC16 >>= 1;
00054                 CRC16 ^= CRC16_POLYNOME;
00055             }//if
00056             else
00057             {
00058                 CRC16 >>= 1;
00059             }//else
00060             
00061             data >>= 1;     // shift data 1 bit right, dividing it by 2
00062         }//for
00063 
00064     }//for i_bytes
00065 
00066     *ptrCRC16 = CRC16; //return CRC16
00067 }