| cfernandez 
 
 
 Joined: 18 Oct 2003
 Posts: 145
 
 
 
			    
 
 | 
			
				| CRC16 problem with forum code |  
				|  Posted: Sat Jun 26, 2010 5:07 pm |   |  
				| 
 |  
				| Hi, 
 I need to use CRC16-1021 and I get the code from the forum library, I use a PIC18F8722 and this is my code:
 
  	  | Code: |  	  | int16 CRC16_1021( int16 lCRC, char *sBuffer, int iLen )
 {
 #if 0
 int8        iPtr;
 int8        iCRC[2];
 
 iCRC[ 0 ] = make8( lCRC, 1 );
 iCRC[ 1 ] = make8( lCRC, 0 );
 
 *(0x0FE9) = sBuffer;                // FSR0 = sBuffer;
 
 do
 {
 
 #ASM
 movf  0x0FEE,w              // 0x0FEE = POSTINC0
 xorwf iCRC[0],w             // (a^x):(b^y)
 movwf iPtr                  //
 andlw 0xf0                  // W = (a^x):0
 swapf iPtr,f                // Index = (b^y):(a^x)
 xorwf iPtr,f                // Index = (a^b^x^y):(a^x) = i2:i1
 
 
 // High byte
 movf  iPtr,W
 andlw 0xf0
 xorwf iCRC[1],W
 movwf iCRC[0]
 
 rlcf  iPtr,W                // use rlf for PIC16
 rlcf  iPtr,W                // use rlf for PIC16
 xorwf iCRC[0],f
 andlw 0xe0
 xorwf iCRC[0],f
 
 swapf iPtr,F
 xorwf iPtr,W
 movwf iCRC[1]
 #ENDASM
 
 } while ( --iLen );
 
 return make16( iCRC[ 0 ], iCRC[ 1 ] );
 
 #else
 
 int         iPtr;
 int16       lTmp;
 
 
 for ( iPtr = 0 ; iPtr < iLen ; iPtr++ )
 {
 lTmp  = ( make8( lCRC, 1 ) ^ sBuffer[ iPtr ] );
 
 lTmp ^= lTmp >> 4;
 
 lCRC = ( lCRC << 8 ) ^ ( lTmp << 12 ) ^ ( lTmp << 5) ^ lTmp;
 }
 
 return lCRC;
 
 #endif
 }
 
 | 
 I found a problem with this array:
 
  	  | Quote: |  	  | 0x03 0x02 0x00 0x9A 0x5B 0x10 0x00 0x00 0x00
 
 | 
 But when I use the C code the result is 0x105C, and when use the ASM code the result is 0x32AE.
 
 What is wrong?? For other array the result is the same in both code.
 
 Anyone can help me ?
 
 Can verify this using this link:
 http://www.lammertbies.nl/comm/info/crc-calculation.html
 
 Thanks
 |  |