![](templates/subSilver/images/CCSLogo.jpg) |
![CCS C Software and Maintenance Offers](templates/subSilver/images/forumAd6.jpg) |
View previous topic :: View next topic |
Author |
Message |
ldalcol
Joined: 11 Mar 2015 Posts: 3
|
CRC16 CCITT |
Posted: Mon Mar 16, 2015 5:27 pm |
|
|
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. |
|
![](templates/subSilver/images/spacer.gif) |
Ttelmah
Joined: 11 Mar 2010 Posts: 19359
|
|
Posted: Tue Mar 17, 2015 1:47 am |
|
|
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.... |
|
![](templates/subSilver/images/spacer.gif) |
ldalcol
Joined: 11 Mar 2015 Posts: 3
|
|
Posted: Wed Mar 18, 2015 9:52 am |
|
|
Thank you, formatting work!
I apologize for posting information prohibited, it will not be repeated! |
|
![](templates/subSilver/images/spacer.gif) |
|
|
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
|