Hai all,
Good Day! I am using CRC16. I am new to CRC16. In this CRC initial value is 0xFFFF and polynomial is 0x8005 .
And i referred this page and i used the code. But not getting what the value is expected. i checked with online crc calculator but not getting the result.
http://www.nongnu.org/avr-libc/user-manual/group__util__crc.html#ga95371c87f25b0a2497d9cba13190847f
http://www.lammertbies.nl/comm/info/crc-calculation.html
And my code is:
/***************************************************** This program was produced by the CodeWizardAVR V2.05.3 Standard Automatic Program Generator © Copyright 1998-2011 Pavel Haiduc, HP InfoTech s.r.l. http://www.hpinfotech.com Project : Version : Date : 4/7/2015 Author : Dev6 Company : sass Comments: Chip type : ATmega128 Program type : Application AVR Core Clock frequency: 14.745600 MHz Memory model : Small External RAM size : 0 Data Stack size : 1024 *****************************************************/ #include <mega128.h> #include <string.h> #ifndef RXB8 #define RXB8 1 #endif #ifndef TXB8 #define TXB8 0 #endif #ifndef UPE #define UPE 2 #endif #ifndef DOR #define DOR 3 #endif #ifndef FE #define FE 4 #endif #ifndef UDRE #define UDRE 5 #endif #ifndef RXC #define RXC 7 #endif #define FRAMING_ERROR (1<<FE) #define PARITY_ERROR (1<<UPE) #define DATA_OVERRUN (1<<DOR) #define DATA_REGISTER_EMPTY (1<<UDRE) #define RX_COMPLETE (1<<RXC) // USART0 Receiver buffer #define RX_BUFFER_SIZE0 256 char rx_buffer0[RX_BUFFER_SIZE0]; #if RX_BUFFER_SIZE0 <= 256 unsigned char rx_wr_index0,rx_rd_index0,rx_counter0; #else unsigned int rx_wr_index0,rx_rd_index0,rx_counter0; #endif // This flag is set on USART0 Receiver buffer overflow bit rx_buffer_overflow0; // USART0 Receiver interrupt service routine interrupt [USART0_RXC] void usart0_rx_isr(void) { char status,data; status=UCSR0A; data=UDR0; if ((status & (FRAMING_ERROR | PARITY_ERROR | DATA_OVERRUN))==0) { rx_buffer0[rx_wr_index0++]=data; #if RX_BUFFER_SIZE0 == 256 // special case for receiver buffer size=256 if (++rx_counter0 == 0) rx_buffer_overflow0=1; #else if (rx_wr_index0 == RX_BUFFER_SIZE0) rx_wr_index0=0; if (++rx_counter0 == RX_BUFFER_SIZE0) { rx_counter0=0; rx_buffer_overflow0=1; } #endif } } #ifndef _DEBUG_TERMINAL_IO_ // Get a character from the USART0 Receiver buffer #define _ALTERNATE_GETCHAR_ #pragma used+ char getchar(void) { char data; while (rx_counter0==0); data=rx_buffer0[rx_rd_index0++]; #if RX_BUFFER_SIZE0 != 256 if (rx_rd_index0 == RX_BUFFER_SIZE0) rx_rd_index0=0; #endif #asm("cli") --rx_counter0; #asm("sei") return data; } #pragma used- #endif // Standard Input/Output functions #include <stdio.h> // Declare your global variables here unsigned int crc16_update(unsigned int crc, unsigned char a) { int i; crc ^= a; for (i = 0; i < 8; ++i) { if (crc & 1) crc = (crc >> 1) ^ 0x8005; else crc = (crc >> 1); } return crc; } void main(void) { // Declare your local variables here unsigned char arr[] = "123456789"; unsigned int i =0; unsigned int crc = 0xffff; // Input/Output Ports initialization // Port A initialization // Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In // State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T PORTA=0x00; DDRA=0x00; // Port B initialization // Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In // State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T PORTB=0x00; DDRB=0x00; // Port C initialization // Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In // State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T PORTC=0x00; DDRC=0x00; // Port D initialization // Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In // State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T PORTD=0x00; DDRD=0x00; // Port E initialization // Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In // State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T PORTE=0x00; DDRE=0x00; // Port F initialization // Func7=In Func6=Out Func5=Out Func4=Out Func3=Out Func2=Out Func1=Out Func0=In // State7=T State6=0 State5=0 State4=0 State3=0 State2=0 State1=0 State0=T PORTF=0x00; DDRF=0x7E; // Port G initialization // Func4=In Func3=In Func2=In Func1=In Func0=In // State4=T State3=T State2=T State1=T State0=T PORTG=0x00; DDRG=0x00; // USART0 initialization // Communication Parameters: 8 Data, 1 Stop, No Parity // USART0 Receiver: On // USART0 Transmitter: On // USART0 Mode: Asynchronous // USART0 Baud Rate: 9600 UCSR0A=0x00; UCSR0B=0x98; UCSR0C=0x06; UBRR0H=0x00; UBRR0L=0x5F; // Global enable interrupts #asm("sei") puts("\nstart"); for(i =0; i<strlen(arr); ++i) { crc = crc16_update(crc, arr[i]); // printf("\ncrc1 = %d", crc); } printf("\ncrc = 0x%x", crc); puts("\nend"); while (1) { } }
And output i am getting is
start
crc = 0x3d7b
end
Expected output is
0xBB3D
I dont know where the problem is. Can you please suggest me where the problem is?