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 CCS Technical Support

Reading and Writing Strings to/from EEPROM

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



Joined: 14 Jun 2011
Posts: 24
Location: St. Louis

View user's profile Send private message Send e-mail

Reading and Writing Strings to/from EEPROM
PostPosted: Sat Jul 23, 2011 2:59 pm     Reply with quote

I am trying to write a string to EEPROM and read it back to compare with an input string from the keyboard. I can initialize the EEPROM and read the data but I cannot read the data into a string variable to make use of. Any help?
Code:

#include <16F690.h>
#include <string.h>
#use delay (internal=4mhz)
#fuses INTRC_IO, NOMCLR, NOCPD
#use rs232 (baud = 9600, xmit = PIN_C4,rcv = PIN_C5, ENABLE=PIN_C1, RETURN=PIN_C2, ERRORS, TIMEOUT=5000)
/#rom 0x2100={78,65,77,69,32,48,48,58,48,55,58,101,48,58,100,54,58,101,48,58,52,50,13}

// Global Variables
int8 *i, c;
char BTPAIR_MEM[23];

void main()
{
   BTPAIR_MEM = "00:11:22:33:44:55:66";
   printf("\n\r");
   printf("\n\rMemory Read: ");
   for(i = 0; i < 23; i++)
    {
    printf("%c" read_eeprom(i));
    c = read_eeprom(i);
    BTPAIR_MEM[i]=c;
    }   

//   delay_ms(1000);
   printf("\n\r");
   printf("BTPAIR_MEM(string): %s\n\r", BTPAIR_MEM);
}

Memory read is correct when it printf to the screen but when I try to move the values to BTPAIR_MEM and printf - I get garbage out.
Ttelmah



Joined: 11 Mar 2010
Posts: 19513

View user's profile Send private message

PostPosted: Sun Jul 24, 2011 2:41 am     Reply with quote

A 'string' in C, is just a sequence of bytes, _but with one critical extra_. The 'null' terminator. You are not adding a terminator character, so your sequence is unterminated, and the print will show garbage, till it happens to hit a null somewhere in memory..... You also need one extra character of storage for the terminator. So BTPAIR_MEM needs 24 characters to hold a 23 character 'string', and after the loop, you need to add the terminator before trying to print it.

Best Wishes
ckielstra



Joined: 18 Mar 2004
Posts: 3680
Location: The Netherlands

View user's profile Send private message

PostPosted: Sun Jul 24, 2011 7:05 am     Reply with quote

In the start of your program you declared:
Code:
char BTPAIR_MEM[23];
This declares BTPAIR_MEM as a pointer to a reserved memory space for 23 characters.

Then you have:
Code:
   BTPAIR_MEM = "00:11:22:33:44:55:66";
This line is a bug. Though technically correct, it is a bug because it is not doing what you want it to do. Unlike some newer languages, in C the string is not a built in type so you can not do a copy by using the '=' operator. The compiler sees "blabla" as a pointer to a character array.
What happens now is that the original BTPAIR_MEM pointer is overwritten with a new address for the number string. This number string is somewhere in ROM and the original pointer to 23 bytes of free RAM is lost.
Any BTPAIR_MEM operations you do after this will try to use the ROM address to do something in RAM and you will corrupt data at unpredictable addresses.

The correct method for initializing the array would have been to copy the data, for example:
Code:
strcpy(BTPAIR_MEM, "00:11:22:33:44:55:66");
schmez



Joined: 14 Jun 2011
Posts: 24
Location: St. Louis

View user's profile Send private message Send e-mail

Thank you
PostPosted: Sun Jul 24, 2011 8:26 pm     Reply with quote

Thank you all for the helpful comments. Strings are a funny thing - it has been 15+ years since I have done programming and things are slowly coming back to me.
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