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

Writing Setup Structure to Data EEPROM

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



Joined: 14 Dec 2009
Posts: 33
Location: Wales

View user's profile Send private message

Writing Setup Structure to Data EEPROM
PostPosted: Wed Jan 20, 2010 10:04 am     Reply with quote

I am trying to get some code to work to write and read a data
structure from the PIC data eeprom. I've snipped lots out
but the bones are below.

It obviously isn't working but does anyone know what the
problem is ? I need to copy string data to a structure but
the structure doesn't like copy via the char * pointer.

Thanks in advance
Code:

#include    <18F4620.h>
#device    PASS_STRINGS=IN_RAM
#device   *= 16;
#include    <stdio.h>
#include    <string.h>
#include    <stdarg.h>
#include   <stdlibm.h>

const   char   CHANNEL[]={"PID Channel 1 - PP Control"};

struct   PID   {   
                        char             ident[sizeof(CHANNEL)];
         float   Kp, Ki, Kd      ;   //
         float   rate         ;   // Ramp rate in KPa/min
         float   rsp         ;   // Ramping SP MPa.
         float   tsp         ;   // Target SP in MPa.
         float    mv         ;   // Read as zero at startup
         float    pb         ;   // Proportional band in KPa.
         float   mverr      ;   // MV Error - not stored.
         BOOL fwd         ;   // Forward or reverse PID
         BOOL   s_ok         ;
         }    trx,    *ptrx         ;   

// declaration of structure and pointer to an instance of this

void   read_eeprom_string(char *array, int8 address, int8 max_size)
{
int8   i   =   0;
*array   =   0;

while(i<max_size)   
{   *array=read_eeprom(address+i);
   if (*array==0) { i=max_size; }
   else   {   array++;
      *array=0;   }   i++;
   }
}

//*******************************************
//   Function: write_eeprom_string
//*******************************************

void   write_eeprom_string(char *array, int8 address, int8 max_size)
{
int8   i=0;
while(i < max_size)   {   write_eeprom(address+i, *array);
                  if (*array==0) { i=max_size; }
                  array++; i++;
            }
}

//    Start of main ..

void   main()
{
trx.Kp   =   10;
trx.Ki          =   20;
trx.Kd   =   35;

ptrx   =   &trx;
EOB   =   sizeof(trx)+2;

write_eeprom_string((char *) ptrx,  0,  EOB);
read_eeprom_string( (char * )ptrx,  0,  EOB);

//   etc.
//   fprintf("\r\n Ki=%f Kp=%f Kd=%f, trx.Kp, trx.Ki, trx.Kd);

}
Ttelmah
Guest







PostPosted: Wed Jan 20, 2010 10:15 am     Reply with quote

Won't work reliably.
The problem is that the 'string' functions will terminate, when they see a '0' byte (end of string marker in C), in the array....

Make the functions generic, so something like:
Code:

//EPROM save/retrieve routines.
void EEPROM_GET(int *ptr,int num,int addr) {
   int count;
   for (count=0;count<num;count++)
   {
      ptr[count]=READ_EEPROM(addr+count);
   }
}

void EEPROM_PUT(int *ptr,int num,int addr) {
   int count;
   for (count=0;count<num;count++)
   {
      WRITE_EEPROM(addr+count,ptr[count]);
   }
}


//Then in your case:

EEPROM_PUT(&trx,sizeof(STRUCT PID),0);

//and to read

EEPROM_GET(&trx,sizeof(STRUCT PID),0);

You also have another problem, since you may transfer two bytes _beyond_ the array (if a 0 marker is not met), which depending on luck, may/will be another variable, resulting in this being corrupted.

Best Wishes
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