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 support@ccsinfo.com

Help reading Serial EEPROM

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



Joined: 03 Jan 2004
Posts: 2

View user's profile Send private message

Help reading Serial EEPROM
PostPosted: Sat Jan 03, 2004 9:06 pm     Reply with quote

Wow, first time to this group and already I'm looking for help with code.

My problem is that I can write a Floating point number to a serial EEPROM but cannot read back any reliable data that makes sense.

The hardware is part of a datalogger I'm working on and the serial EEPROM has been tested using inteter number and works fine- I believe that I can write a floating point number to the EEPROM but cannot read the hex values I expect to see.

I've distilled the code and I would be most appreciative if anyone can offer me inisghts to my problem.

Thanks, Al


PCM Version 2.693 (yes it is old!)


#include <16F877.H>

#fuses XT, NOPROTECT, NOPUT, NOWDT, NOBROWNOUT, NOLVP, NOCPD, NOWRT

/* EXTERNAL EEPROM ASSIGNMENTS */

#define EEPROM_ADDRESS long int
#define EEPROM_BYTE_SIZE 8192
#define EEPROM_SCL PIN_C3
#define EEPROM_SDA PIN_C4

#define hi(x) (*(&x+1))

#use delay ( clock=4000000 ) // clock 4 Mhz

#use rs232(baud=9600,xmit=PIN_C6,rcv=PIN_C7,parity=N)
#use i2c ( master, scl=EEPROM_SCL, sda=EEPROM_SDA )

void init_ext_eeprom ( void );
void write_ext_eeprom ( long int lngAddress, byte intData );
byte read_ext_eeprom ( long int lngAddress );

void WRITE_FLOAT_EXT_EEPROM(long int n,float fTempVal);

static float fTempVal;
static long int iTempVal;


void main ( void )
{
long int j,k;
float data;
init_ext_eeprom(); /* set SDA and SCL to float */

data=100.20; /* float data to start recording */
printf("\n\r");
for (k=0; k<3;k++)
{
printf("\n\rdata= %3.2f",data);
WRITE_FLOAT_EXT_EEPROM(k,data);
data=data+.2;
}

for (j=0; j<12;j++)
{
iTempVal=read_ext_eeprom(j);
}
}

/* EEPROM Routines */
void init_ext_eeprom ( void )
{
output_float ( EEPROM_SCL );
output_float ( EEPROM_SDA );
}

void write_ext_eeprom ( long int lngAddress, byte intData )

{
byte intDataR;
i2c_start();
i2c_write ( 0xa0 ); /* set up for writing address and data, chip address 000 */
i2c_write ( hi ( lngAddress ) ); /* write high address */
i2c_write ( lngAddress ); /* write low address */
i2c_write ( intData ); /* write data */
i2c_stop();
delay_ms ( 11 );
printf("\n\r\t\tWr %X",intData); /* print hex Write Data */
}



byte read_ext_eeprom ( long int lngAddress )
{
byte intData;

i2c_start();
i2c_write ( 0xa0 ); /* set up for writing address, chip address 000 */
i2c_write ( hi ( lngAddress ) ); /* write high address */
i2c_write ( lngAddress ); /* write low address */
i2c_start();
i2c_write ( 0xa1 ); /* set up for reading data, chip address 000 */
intData = i2c_read ( 0 ); /* read data */
i2c_stop();
delay_ms ( 11 );
printf("\n\r\t\tRd %X",intData); /* print hex Read Data */
return ( intData );
}


void WRITE_FLOAT_EXT_EEPROM(long int n,float fTempVal)

{
int i;

for (i=0;i<4;i++)
{
write_ext_eeprom(i+n,*(&fTempVal+i));
delay_ms(2);
}
}



Output Data on serial port:



data= 100.19
Wr 85
Wr 48
Wr 66
Wr 66

data= 100.39
Wr 85
Wr 48
Wr CC
Wr CC

data= 100.59
Wr 85
Wr 49
Wr 33
Wr 32

This is the serial data read - bears litlle resemblance to the written data

Rd 85
Rd 85
Rd 85 <- data 100.59?
Rd 49
Rd 33 <-
Rd 32
Rd 85
Rd 85
Rd 4B
Rd 38
Rd 4F
mcafzap



Joined: 07 Sep 2003
Posts: 46
Location: Manchester, UK

View user's profile Send private message

Re: Help reading Serial EEPROM
PostPosted: Sat Jan 03, 2004 9:22 pm     Reply with quote

data=100.20; /* float data to start recording */
printf("\n\r");
for (k=0; k<3;k++)
{
printf("\n\rdata= %3.2f",data);
WRITE_FLOAT_EXT_EEPROM(k,data);
data=data+.2;
}

for (j=0; j<12;j++)
{
iTempVal=read_ext_eeprom(j);
}



I may be mistaken, but you seem to be incrementing your address within the EEPROM by one instead of 4 (since each float requires 4 bytes).

Steve
Steve H.
Guest







PostPosted: Sat Jan 03, 2004 10:34 pm     Reply with quote

Check out these FAQ's...

http://www.ccsinfo.com/faq/?21

http://www.ccsinfo.com/faq/?24

Steve H.
Guest








PostPosted: Sun Jan 04, 2004 11:34 pm     Reply with quote

Thank you very much Steve, I thought I had taken the 4 byte conversion into account but not so.

The program works and just in time- it's -23.4 degrees C outside and now I can start recording my temperature sensors.

Thanks again and Seasons Greetins.

Al
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