CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to support@ccsinfo.com

CRC16 CCITT

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
ldalcol



Joined: 11 Mar 2015
Posts: 3

View user's profile Send private message

CRC16 CCITT
PostPosted: Mon Mar 16, 2015 5:27 pm     Reply with quote

Hi, I'm a new in C program, but I have a problem with make correctly CRC16. Sorry if my problem is very soft, but like I said, I'm starting in C.
Code:

#include <ComSerial_crc.h>
#include <math.h>
#include <string.h>
#include <stdio.h>
#INCLUDE <stdlib.h>
#INCLUDE <input.c>

++++++++++++++++++++++++
Code from crc.c deleted: generate_16bit_crc()
Reason: Forum rule #10
10. Don't post the CCS example code or drivers
http://www.ccsinfo.com/forum/viewtopic.php?t=26245
- Forum Moderator
++++++++++++++++++++++++

Code:

void main()
{
//int x;
unsigned char x[7]; //assim o display funciona
int16 i, j,tam, chk, retchk;

set_tris_d(0b00000000);
output_d(0b11111111);

delay_ms(1000);
     
while(TRUE)
   {
    x[0] = getc(); //isdigit testa se é um numero
    x[1] = getc();
    chk = generate_16bit_crc(x,0x02,CRC_CCITT);
    puts(chk);
  }     
 

Tks.
Ttelmah



Joined: 11 Mar 2010
Posts: 19359

View user's profile Send private message

PostPosted: Tue Mar 17, 2015 1:47 am     Reply with quote

You don't need to post the library part. Just include it. You are breaching CCS copyright by posting the section from the library....

That said, the key problem is with what you are doing with the value returned.
The CRC function returns a number, not a string.

Instead of puts(chk) which will never work, use:

printf("%04LX\n\r",chk);

Which will format the number as four hex digits, or:

printf("%04Lu\n\r",chk);

Which will print the number in decimal (and add a line feed/carriage return).

So the demo code becomes:
Code:

#include <ComSerial_crc.h>
//You are not using anything in the other libraries - no point in including them
#include <crc.c> //Include the crc generator functions

void main()
{
   unsigned char x[7]; //assim o display funciona
   unsigned int16 chk; //checksum value

   //set_tris_d(0b00000000); //Pointless
   output_d(0b11111111);

   delay_ms(1000);
     
   while(TRUE)
   {
       x[0] = getc(); //isdigit testa se é um numero
       x[1] = getc();
       chk = generate_16bit_crc(x,0x02,CRC_CCITT); //generate CRC of two characters
       printf("%04Lu\n\r",chk); //and display.
   }     
}

Notice how much smaller the code becomes....
ldalcol



Joined: 11 Mar 2015
Posts: 3

View user's profile Send private message

PostPosted: Wed Mar 18, 2015 9:52 am     Reply with quote

Thank you, formatting work!
I apologize for posting information prohibited, it will not be repeated!
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group