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

Trouble writing floats to external EEPROM

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



Joined: 08 Jul 2005
Posts: 91

View user's profile Send private message

Trouble writing floats to external EEPROM
PostPosted: Thu Jun 08, 2006 10:29 am     Reply with quote

Hi all,

I'm trying to write a floating point # to external EERPOM (24FC515) and have read the online help regarding the WRITE_FLOAT_EXT_EEPROM function. I have an older version of PCM and do not have access to it. As such, I tried to recreate it using i2c commands. However, when printing the value to HT, I get .00000 and not 39.821099. I appreciate any help!

Code:

#include <16F877A.H>
#include <stdlib.h>
#include <math.h>

#define LED PIN_C0
#define eeprom_sda PIN_C4
#define eeprom_scl PIN_C3

#fuses HS,NOWDT,NOPROTECT,PUT,NOLVP
#use delay(clock=10000000)
#use rs232(baud=9600, xmit=PIN_D6, rcv=PIN_D7, stream=PC)
#use i2c (master, sda=eeprom_sda, scl=eeprom_scl)

void main()
{
   float data = 39.821099;
   float getData = 0.0;
   int addressHIGH=0, addressLOW=0;
   int i=0, j=0;

    while(1)
   {
      output_high(LED);

      if(addressHIGH<=255) // still have memory left to write to
      {
         i=0; j=0;

         // initialize external eeprom
         output_float(eeprom_scl);
         output_float(eeprom_sda);

         // write to eeprom
         i2c_start();            // inintializes i2c communication
         i2c_write(0xa0);         // 10100000
         i2c_write(addressHIGH);      // initializes HIGH address byte
         i2c_write(addressLOW);      // initializes LOW address byte
         for(i=0; i<4; i++)
         {
                i2c_write(*(((int*)&data)+i));   // writes floating value to address
         }
         delay_ms(5);
         i2c_stop();
         delay_ms(11);

         
         // read from eeprom
         i2c_start();
         i2c_write(0xa0);         // 10100000
         i2c_write(addressHIGH);      // initializes HIGH address byte
         i2c_write(addressLOW);      // initializes LOW address byte
         i2c_start();
         i2c_write(0xa1);         // 10100001
         for(j=0; j<4; j++)
         {
            (*((&getData)+i)) = i2c_read(0);
         }
         i2c_stop();


           

         fprintf(PC, "getData: %f", getData);
      }

   }
}
dbotkin



Joined: 08 Sep 2003
Posts: 197
Location: Omaha NE USA

View user's profile Send private message Send e-mail Visit poster's website

Re: Trouble writing floats to external EEPROM
PostPosted: Thu Jun 08, 2006 10:55 am     Reply with quote

weg22 wrote:
Hi all,
Code:

         for(i=0; i<4; i++)
         {
                i2c_write(*(((int*)&data)+i));   // writes floating value to address
         }


Looks a little odd, but I get confused by so many layers of parenthesis, casts and all that. Maybe try this:

Code:

   for (i = 0; i < 4; i++)
     i2c_write(*(&data + i) ) ;

Quote:

Code:

         for(j=0; j<4; j++)
         {
            (*((&getData)+i)) = i2c_read(0);
         }


I see one glaring problem there... no matter whether it's written to your external memory correctly or not, you're using j for the counter, but adding i to the data address. make them both j, or just re-use i.

Here's the info from the CCS help file:
Quote:
How do I write variables to EEPROM that are not a byte?


The following is an example of how to read and write a floating point number from/to EEPROM. The same concept may be used for structures, arrays or any other type.



n is an offset into the eeprom.

For floats you must increment it by 4.

For example, if the first float is at 0 the second one should be at 4 and the third at 8.



WRITE_FLOAT_EXT_EEPROM(long int n, float data) {

int i;

for (i = 0; i < 4; i++)

write_ext_eeprom(i + n, *(&data + i) ) ;

}



float READ_FLOAT_EXT_EEPROM(long int n) {

int i;

float data;

for (i = 0; i < 4; i++)

*(&data + i) = read_ext_eeprom(i + n);

return(data);

}


Hope this helps. I've written and read floats to and longs and int32s EEPROM quite a bit, and the method shown in teh help file works every time. The sample code they show is in the form of functions that accept an address, which isn't exactly how you're doing it, but the translation is pretty trivial.
weg22



Joined: 08 Jul 2005
Posts: 91

View user's profile Send private message

PostPosted: Thu Jun 08, 2006 10:57 am     Reply with quote

Duh!!! I didn't even see that sorry...I will try it shortly and let you know how it goes :-)
weg22



Joined: 08 Jul 2005
Posts: 91

View user's profile Send private message

PostPosted: Thu Jun 08, 2006 12:54 pm     Reply with quote

Okay, I made the changes suggested:

Code:

i2c_write(*(&data+i));  // writes floating value to address


and

Code:

(*(&getData+j)) = i2c_read(0);


and the output I get now is something other than zero, but still not correct:
getData: -63.999995

Anyone know what's going on?

Thanks,
-weg
weg22



Joined: 08 Jul 2005
Posts: 91

View user's profile Send private message

PostPosted: Thu Jun 08, 2006 5:44 pm     Reply with quote

I figured it out...thanks anyway!
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