TELECOMMAND MANAGER V1

Dependencies:   mbed SLCD mbed-rtos

crc.h

Committer:
shreeshas95
Date:
2015-04-15
Revision:
0:b5b370873460
Child:
1:df31097c8442

File content as of revision 0:b5b370873460:

#define WIDTH 16
#define TOPBIT (1 << 15) 
#define POLYNOMIAL 0x1021

class CRC{
private:
    typedef unsigned short int crctype; 
public:
    // no need of intializer
    crctype crcGenerate(const char message[], int nBytes){
        crctype remainder = 0xffff;
        int byte;
        char bit;
        
        for( byte = 0 ; byte < nBytes ; byte++ ){
            /*
            Bring the data byte by byte
            each time only one byte is brought
            0 xor x = x
            */
            remainder = remainder ^ ( message[byte] << (WIDTH-8) );
            
            for( bit = 8 ; bit > 0 ; bit--){
                /*
                for each bit, xor the remainder with polynomial
                if the MSB is 1
                */
                if(remainder & TOPBIT){
                    remainder = (remainder << 1) ^ POLYNOMIAL;
                    /*
                    each time the remainder is xor-ed with polynomial, the MSB is made zero
                    hence the first digit of the remainder is ignored in the loop
                    */
                }
                else{
                    remainder = (remainder << 1);
                }
            }
        }
        
        return remainder;
    }
};