Hi all,
Good Day! I have two problems.
Problem 1: I have problem in strlen calculation. I am trying to calculate CRC16 for a FRAME with initial value of CRC = 0xFFFF. In this code if i initialized unsigned char array as global then i am getting correct strlen value. after sending a frame i am getting strlen value as 62. it is giving the value 62 = 4 + 58(frame length). In this if i initialized unsigned char as local then i am getting strlen is 10. I am allocating only 4 byte but how i am getting 10 as strlen?
Frame Format:
*0,0,0,0,1,6,1,2,3,4,5,6,2,1,1,1,B,2,2,1,0.00,00.0,0.00,0#
Problem 2: How to compare crc0 value with crc1 value. crc0 is unsigned int and crc1 is unsigned char array. in the below code
if(crc0 == crc1) Is this comparison is correct or not.
Even i tried with pointers.
can you please give me a guidance to get a solution for these two problems.
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/10/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 <stdio.h> #include <string.h> #include <stdlib.h> #include <delay.h> unsigned char rx_rcv=0, rcv_comp=0; unsigned char uartin_data[256]; unsigned char temp_uartin[26]; unsigned int uartin_length=0; #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(data == '*') { rcv_comp = 0; rx_wr_index0 = 0; rx_rcv = 1; uartin_length = 0; } if(data == '#') { rcv_comp = 1; } 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 unsigned int crc16_update_little_endian(unsigned int crc, unsigned char arr[]) { // Least significant bit first (little-endian) // x^16+x^15+x^2+1 = 1010 0000 0000 0001 (1) = 0xA001 (CRC 16 POLYNOMIAL REVERSED) unsigned int i,j; for(j =0; j<26/*strlen(arr)*/; ++j) { crc ^= arr[j]; for (i = 0; i < 8; ++i) // Assuming 8 bits per byte { if (crc & 0x0001) // if rightmost (most significant) bit is set { crc = (crc >> 1) ^ 0xA001; } else { crc = (crc >> 1); } } //printf("\nj: %d, \tcrc: %X", j,crc); } return crc; } void uartin_function(void) { unsigned int i=0,j=0,k=0; unsigned int crc0 = 0xFFFF; //0x0000 unsigned char crc1[4] = {'0','0','0','0'}; //unsigned char *pcrc = "0000"; //pcrc = malloc(4); //printf("\nstrlen_crc1: %d, \tstrlen_a: %d", strlen(crc1),strlen(a)); // printf("\nstrlen_crc1: %d", strlen(crc1)); //uartin_length = rx_wr_index0; if(rx_rcv == 1) { if(rcv_comp == 1) { strcpy(uartin_data,rx_buffer0); puts(uartin_data); for(i=1; i<32;) { temp_uartin[j] = uartin_data[i]; //printf("\ntemp_uartin[%d]: %c", j,temp_uartin[j]); i+=2; ++j; } for(i=41; i<57;) { temp_uartin[j] = uartin_data[i]; if((i==41)||(i==44)||(i==47)||(i==49)||(i==51)||(i==56)) { i+=2; } else { ++i; } } crc0 = crc16_update_little_endian(crc0, temp_uartin); //printf("\ncrc0 = %X", crc0); for(i=33; i<40;) { crc1[k] = uartin_data[i]; //*(pcrc) = uartin_data[i]; //printf("\ncrc1[%d]: %c", k,crc1[k]); i+=2; ++k; //++pcrc; } puts(crc1); //pcrc=crc1; //puts(pcrc); if(crc0 == crc1) { puts("\nTRUE"); puts(crc1); } else { puts("\nFALSE"); puts(crc1); } } rcv_comp = 0; } rx_rcv = 0; } // end of uartin_function void main(void) { // Declare your local variables here // 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=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 PORTF=0x00; DDRF=0x00; // 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"); while (1) { uartin_function(); delay_ms(2000); } }