View previous topic :: View next topic |
Author |
Message |
pilar
Joined: 30 Jan 2008 Posts: 197
|
I can to calculate the Checksum using the atoi_b16? |
Posted: Mon May 03, 2010 3:55 pm |
|
|
Hi, I have 12 string stored in a memory eeprom and I need to calculate the CheckSUM of this string to TX, I am using the atoi_b16 but I can not get the correct value, and here is the String of memory eeprom
38
31
39
35
35
43
30
45
32
30
34
35
Here is my code: Code: |
int16 CheckSUM;
CheckSUM = 0;
for (i=0; i<12; i+=2)
{
CheckSUM += atoi_b16 (read_eeprom(i));
}
CheckSUM = 0xFF - CheckSUM;
|
|
|
|
ckielstra
Joined: 18 Mar 2004 Posts: 3680 Location: The Netherlands
|
|
Posted: Mon May 03, 2010 4:51 pm |
|
|
read_eeprom returns an int8, not an int16 so you will have to increase the addresses by 1, not 2.
I don't know atoi_b16 but most likely you can do without it.
The string data you have listed, are these decimal or hexadecimal values? |
|
|
pilar
Joined: 30 Jan 2008 Posts: 197
|
|
Posted: Mon May 03, 2010 5:32 pm |
|
|
Quote: | The string data you have listed, are these decimal or hexadecimal values? |
They are decimal |
|
|
ckielstra
Joined: 18 Mar 2004 Posts: 3680 Location: The Netherlands
|
|
Posted: Tue May 04, 2010 2:56 am |
|
|
Have you fixed the address increasing by 1 instead of 2?
If it still doesn't work you'll have to provide more data.
For the fastest response post a complete working test program. Including input data and the expected CRC value. |
|
|
Wayne_
Joined: 10 Oct 2007 Posts: 681
|
|
Posted: Tue May 04, 2010 5:46 am |
|
|
You have several problems, some are code issues and others are your understanding of the problem.
First as pointed out read_eeprom returns an 8 bit value from the address given, it does not return the address of the string or even the string itself.
Secondly, atoi or atoi16 requires a null terminated string, which takes us back to your first problem.
Next is the data itself, I assume from your description that it is a decimal STRING stored in eeprom so the actual data for the first sting is:-
"38" = (hex) 0x33, 0x38, 0x00 (null terminated)
Next, do you need the actual values for your checksum or can you use the ascii values e.g
"38" instead of using 38 use (dec)51 + (dec)56 = 107 ?
This would mean you don't need to convert the value.
To convert the value is realy easy though. 2 options, read the string and use atoi or read each char and convert yourself.
Next is are all the numbers 2 digits ?
To simplify the problem, why are you storing them as ascii ?
And lastly, your routine maybe flawed, you are generating a 16 bit value and then subtracting that from 0xFF, why ? This may not give you the result you think it is. |
|
|
pilar
Joined: 30 Jan 2008 Posts: 197
|
|
Posted: Tue May 04, 2010 7:46 am |
|
|
Quote: | Next is the data itself, I assume from your description that it is a decimal STRING stored in eeprom so the actual data for the first sting is:-
"38" = (hex) 0x33, 0x38, 0x00 (null terminated)
|
Quote: | And lastly, your routine maybe flawed, you are generating a 16 bit value and then subtracting that from 0xFF, why ? This may not give you the result you think it is. |
hi Wayne_,
I made a mistake with the definition of the format (they are hexadecimal string), with these remarks I have more clear the situation and how to proceed with that.
I am are generating a 16 bit value and then subtracting that from 0xFFFF because in this specific case the value of Checksum should be 16 bits, maybe not the right way to get it but that is how I need it.
Thank you.. |
|
|
|