473,385 Members | 1,748 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

calculation of 32 bit CRC(Cyclic Redundancy Code)in C

please help me!!
i have written a program to calculate 32 bit CRC(used for ETHERNET)..but its not working and giving correct output..
please let me know my mistakes..

output should be :E7 B0 51 93
Expand|Select|Wrap|Line Numbers
  1. /**************  program starts here *************************/
  2. typedef unsigned long ul;
  3. typedef unsigned char uchar;
  4.  
  5. uchar buffer_bytes[64] = {0x00,0x14,0x85,0x31,0x24,0x10,0x00,0x15,0x00,0x15,0x9f,0x07,0x08,0x06,0x00,0x01,0x08,0x00,0x06,0x04,0x00,0x02,0x00,0x15,0x00,0x15,0x9f,0x07,0xc0,0xa8,0x00,0x02,0x00,0x14,0x85,0x31,0x24,0x10,0xc0,0xa8,0x00,0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
  6. ul     remainder ,temp;        //to get 32 bit number
  7. unsigned short weld,joint;
  8.  
  9. unsigned char i,count = 0 ,str_length =64,  *ptr,j;
  10.  
  11.  
  12. #define MSB 0x80000000;
  13. #define POLY 0x04C11DB7    
  14. #define printf printf_fast_f
  15.  
  16. void init_uart(void)
  17. {
  18.     ES = 0 ;                    //Disable serial interrupt.
  19.     SCON = 0x50 ;                //mode 0,REN enabled
  20.     BDRCON = 0x0E ;                //
  21.  
  22.     PCON &= 0x7F ;
  23.     BRL = 224 ;
  24.     BDRCON |= 0x10 ;    
  25. }
  26.  
  27. void putchar(uchar send_this)
  28. {
  29.     uart_send_char(send_this);
  30. }
  31.  
  32. void main(void)
  33. {
  34.     init_uart();
  35.  
  36.     ptr = &buffer_bytes[0];
  37.  
  38.     //----------------------------------------------------------------------------------------------------------/
  39.     //            get 32 bit number out of the whole string                                                         /
  40.     //----------------------------------------------------------------------------------------------------------/
  41.     weld = (unsigned short)*ptr;                                                                                                   
  42.     ptr++;                                                                                                                              
  43.     weld = ((weld << 8) & 0xff00) +  (unsigned short)*ptr;        //16 bit number higher                                            
  44.     ptr++;
  45.     joint =  (unsigned short)*ptr;
  46.     ptr ++;
  47.  
  48.     joint = ((joint << 8) &0xff00) + (unsigned short) *ptr    ;    //16 bit number lower
  49.     ptr ++;                                                        //pointer points the next byte after 4th byte
  50.     remainder =  weld;
  51.     remainder = (remainder << 16) & 0xffff0000  ;        
  52.     remainder = remainder + joint;                                    //32 bit number
  53.  
  54.  
  55.     //remainder = remainder ^ (ul) 0xFFFFFFFF;
  56. //----------------------------------------------------------------------------------------------------
  57.  
  58.     //str_length = str_length  - 4;
  59.  
  60.     for(j = str_length; j>0 ; j--)                    //str_length = no. of bytes in string
  61.     {
  62.         if((remainder & (ul)0xFF000000)==0x00000000)
  63.         {
  64.             remainder = remainder << 8;
  65.             remainder = (remainder & (ul)0xFFFFFF00) | (ul)*ptr;
  66.         }
  67.         else
  68.         {
  69.             for(i=0; i<8 ; i++)
  70.             {
  71.             printf(" %lx\r\n",remainder);
  72.                 if((remainder & (ul)0x80000000) == (ul)0x80000000)        //if MSB is high then shift left by 1, get next bit 0
  73.                 {                                                        
  74.                     remainder = remainder << 1;
  75.  
  76.                     if((*ptr & 0x80) == 0x80)
  77.                     {
  78.                         remainder |= (ul)0x00000001;
  79.                     }
  80.                     else 
  81.                     {
  82.                         remainder |= (ul)0x00000000;
  83.                     }
  84.                     *ptr = *ptr << 1;                            //XOR remainder with POLY
  85.                     printf("msg %lx\r\n",remainder);
  86.  
  87.                     remainder = remainder ^ (ul)0x04C11DB7;
  88.                     printf("XOR abov msg wiD POLY\r\n");
  89.                 }
  90.                 else
  91.                 {
  92.  
  93.                 //    while((remainder & (ul)0x80000000) == (ul)0x00000000)
  94.                 //    {
  95.                         remainder = remainder << 1;
  96.                         if((*ptr & 0x80) == 0x80)
  97.                         {
  98.                             remainder |= (ul)0x00000001;
  99.                         }
  100.                         else
  101.                         { 
  102.                             remainder |= (ul)0x00000000;
  103.                         }
  104.                         *ptr = *ptr << 1;
  105.                 //        i++;
  106.                 //        if (i >= 8)break;
  107.                     //}
  108.  
  109.                 }
  110.             }
  111.             ptr ++;
  112.             //printf("ptr = %2x\r\n",*ptr);
  113.         }
  114.     }
Jan 11 '10 #1
0 2071

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Paul Mendez | last post by:
I really need your assistance. I tried what you gave me and it did not work and I am thinking that the formatting that showed up when u saw my posting might have confused you. So I made sure to...
3
by: Iain Miller | last post by:
Can anybody help me with some Access 2000 code? I don't do a lot of coding in Access & so every time I come back to do something I pretty much have to relearn the syntax from scratch so this is...
5
by: Susanne Strege | last post by:
Hello... I'm wondering if it is possible to perform a CRC or Checksum on the data contained in a simple Linked List that uses pointers to nodes???
3
by: Babak | last post by:
Hello everyone, I'm working on some Finite Elements(FE) codes in C and now I encountered some problems in assembly stage. The main idea is that a large number of 3 by 3 elemental stiffness...
4
by: Marek | last post by:
Hi, I'm developin windows forms app in c#. I would like to use Ansi C algoritm in one of method of my app. Can I do that?
9
by: CptDondo | last post by:
I'm working with some legacy C code. Apparently the author didn't know or care about the difference between int, int16_t, unsigned int, and so on. He does a lot of bitwise |, &, etc on signed...
1
by: marcyb | last post by:
I need this to charge sales tax on if oklahoma resident is checked <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"...
0
by: cmdolcet69 | last post by:
0x07 Description: The slave will respond with a "Potentiometer Value Reply" upon reception of a valid "Potentiometer Value Request" packet. Potentiometer Reply Packet Format Byte...
2
sreekandank
by: sreekandank | last post by:
Dear friends, I want to implement CRC in C...I have implemented in my ideas for several times. I didn't get the correct output .I hav only less idea to implement that in C.. So I want the C...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.