View previous topic :: View next topic |
Author |
Message |
tkidder
Joined: 19 Feb 2005 Posts: 11 Location: new york usa
|
storing strings in eeprom |
Posted: Thu Mar 08, 2007 7:30 pm |
|
|
I have a barcode reader application that gets ascii data , up to 20 characters
that are all numeric, "0012345678905" for example. I need to store these codes in internal eeprom memory, 18f452. The only way i know is to split the barcode into two strings, then convert to long integers,
x=atoi32(string1);
y=atoi32(string2);
//then save
WRITE_LongEEPROM(20,string1);
WRITE_LongEEPROM(30,string2);
To retrieve the data I need to convert each part back
and reassamble them into the origional string.
One of the problems is that the atoi32 function strips the leading zeros.
any help would be greatly appreciated. |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Thu Mar 08, 2007 10:12 pm |
|
|
You could save it as packed BCD (two digits per byte), and also save a
byte the tells how many digits are in the number.
How many of these 20-digit numbers do you need to save ? |
|
|
rnielsen
Joined: 23 Sep 2003 Posts: 852 Location: Utah
|
|
Posted: Thu Mar 08, 2007 10:16 pm |
|
|
Depending on the amount of data that you need to store and the size of eeprom you could simply store the original ascii data to the eeprom. The number 0(zero) would be stored as 0x30, 9 would be 0x39 and so forth. This way you could take what you get from the serial port and go right to the eeprom without any need for conversions.
Ronald |
|
|
tkidder
Joined: 19 Feb 2005 Posts: 11 Location: new york usa
|
storing strins in eeprom |
Posted: Fri Mar 09, 2007 7:10 am |
|
|
I am using, get_string(barcode); to get the data from the scanner.
into char code[20];
are there any examples of how to store this string in eeprom? I assume i would need to store each character in consecutive byte locations.Would i need to convert to hex? I would then need to retrieve the bytes and assemble into a string. There should be no problem with storage since only one code is being saved.
Sorry for my ignorance on this subject. |
|
|
rnielsen
Joined: 23 Sep 2003 Posts: 852 Location: Utah
|
|
Posted: Fri Mar 09, 2007 10:17 am |
|
|
Code: | store_data()
{
int i;
for(i = 0; i < 19; i++)
{
write_eeprom(i, code[i]);
}
}
read_data()
{
int i;
for(i = 0; i < 19; i++)
{
read_code[i] = read_eeprom(i);
}
read_code[19] = 0;// null terminator
}
|
This should take your data and store it to and read from the onboard eeprom. It is already in HEX so you don't need to do any converting to it.
Ronald
EDIT: I just realized that your variable code[] only has 20 places defined. The code used the 21st place to store the null character. The code has been edited. |
|
|
|