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

EEPROM problem

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



Joined: 26 Feb 2012
Posts: 23

View user's profile Send private message Visit poster's website

EEPROM problem
PostPosted: Sat Mar 03, 2012 5:33 am     Reply with quote

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: 9162
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Sat Mar 03, 2012 6:16 am     Reply with quote

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

View user's profile Send private message Visit poster's website

PostPosted: Sat Mar 03, 2012 7:07 am     Reply with quote

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

View user's profile Send private message Visit poster's website

PostPosted: Sun Mar 04, 2012 10:46 am     Reply with quote

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..
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