|
|
View previous topic :: View next topic |
Author |
Message |
Can
Joined: 26 Feb 2012 Posts: 23
|
EEPROM problem |
Posted: Sat Mar 03, 2012 5:33 am |
|
|
Hey there,
I have a setup with one 18F2550, one DS1307 RTC, and one 24LC512. I am trying to get some data written on some address in EEProm through USB via a terminal program.
When I try to read the data of DS1307 I have no problem, the data displays as it should. For the i2c part I arranged the ex_extee program and 24512 driver. the resulting part looks like that :
Code: |
do {
printf(usb_cdc_putc,"\r\nRead or Write: ");
cmd=rp_getc(); //rp_getc() is defined as usb_cdc_getc()
cmd=toupper(cmd);
rp_putc(cmd);
} while ( (cmd!='R') && (cmd!='W') );
printf(usb_cdc_putc,"\n\rLocation: ");
#if sizeof(EEPROM_ADDRESS)==1
address = gethex(); //gethex function is arranged in the corresponding c file
#else
#if EEPROM_SIZE>0xfff
address = gethex();
#else
address = gethex1();
#endif
address = (address<<8)+gethex();
#endif
if(cmd=='R')
printf(usb_cdc_putc,"\r\nValue: %X\r\n",READ_EXT_EEPROM( address ) );
if(cmd=='W') {
printf(usb_cdc_putc,"\r\nNew value: ");
value = gethex();
printf(usb_cdc_putc,"\n\r");
WRITE_EXT_EEPROM( address, value );
}
|
Because the input function gethex is not defined for the USB i arranged the c file as this:
Code: | BYTE gethex1() {
char digit;
digit = usb_cdc_getc();
usb_cdc_putc(digit);
if(digit<='9')
return(digit-'0');
else
return((toupper(digit)-'A')+10);
}
BYTE gethex() {
unsigned int8 lo,hi;
hi = gethex1();
lo = gethex1();
if(lo==0xdd)
return(hi);
else
return( hi*16+lo );
} |
When i tried to write data, the PIC seemed like it is stuck (because i2c indicator leds i put kept lighting). I made some minor adjustments to the 24512 driver to see where I got stuck and it seems like it gets stuck in the while loop in the following code:
Code: |
void write_ext_eeprom(long int address, BYTE data)
{
short int status;
i2c_start();
i2c_write(0xa0);
i2c_write(address>>8);
i2c_write(address);
i2c_write(data);
i2c_stop();
i2c_start();
status=i2c_write(0xa0);
printf(usb_cdc_putc,"0xa0\r\n");
while(status==1)
{
i2c_start();
status=i2c_write(0xa0);
printf(usb_cdc_putc, "I am in the while");
}
i2c_stop();
}
|
I tried removing the while loop but data does not seem to be written correctly. When i read from the address, I just read FF..
Little help? |
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9225 Location: Greensville,Ontario
|
|
Posted: Sat Mar 03, 2012 6:16 am |
|
|
1) are you sure the EEPROM base address is 0xA0 ?
2) correct I2C bus pullups ?
3) I2CSTOP() or I2Cstop(0) needed for that device ?
4)Whenever debugging new code you should always display the address,data,variables,etc. Send them to a PC terminal program or to a local LCD unit on the PIC. Guessing, staring at code, changing a line or two won't SHOW you what's going on. You need this realtime feedback to make an informed choice as to how to correct the error.
5) there are small I2C bus testing programs 'here and there' to locate any devices on the bus.Heck, you could have a defective EEPROM... |
|
|
Can
Joined: 26 Feb 2012 Posts: 23
|
|
Posted: Sat Mar 03, 2012 7:07 am |
|
|
Hello temtronic,
According to the datasheet the data should start with 1010 then continue with address bits (which all the selectors are pulled down to ground) and then finalize with 1 or 0 according to the read or write operation respectively. I suppose my pull-ups are correct because I can read from the DS1307 chip from the same i2c line without a problem. I derived the program from the driver and example program supplied by ccs. So actually i don't know what is the difference between i2c_stop() and i2c_stop(0).
I found one i2c test program from the forum, which i was using for testing the RTC. I can not see the EEPROM using that program, but i think that it has something to do with the eeprom sending an ack or not. I tried 2 different EEPROMs but no luck, either both of them are defective or i am doing something wrong. I bet it is the second one :( |
|
|
Can
Joined: 26 Feb 2012 Posts: 23
|
|
Posted: Sun Mar 04, 2012 10:46 am |
|
|
Well I figured out the problem after a lot of code trying and everything. It seems like the LEDs I used for indicating the activity of SDA and SCL line were causing it. Interestingly some LEDs cause problems but some don't.. I changed the LEDs and I can communicate with the EEPROM without any problems.. |
|
|
|
|
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
|