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

EEPROM 24C512 R/W Problem, Need Help

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



Joined: 21 Apr 2007
Posts: 44

View user's profile Send private message

EEPROM 24C512 R/W Problem, Need Help
PostPosted: Sat Jul 07, 2007 3:59 am     Reply with quote

I am trying to develop the driver for 24C512 EEPROM but so far I am having problems.
My desire is to write and read from the address 1.
Need help...
Below is the code:

Code:
#include <18F452.h>
#include "24C512.c"
#fuses HS,NOWDT,NOPROTECT,NOLVP
#use delay(clock=20000000)

void main()
{
   int8 n;
   EEPROM_ADDRESS address;
   
   SET_TRIS_A(0);
   SET_TRIS_C(0);
   SET_TRIS_D(0);
   
   PORTA=0xFF;
   PORTB=0xFF;
   PORTC=0xFF;
   PORTD=0xFF;
   delay_ms(500); //let the system to stabilize
   address=1;
   PORTC=0;
   PORTD=0;   
   delay_ms(500);
   while (TRUE)
   {
      for (n=1; n<100;n++)
      {
         write_ext_eeprom(address, n);
         PORTC=n;
         delay_ms(200);
         PORTD=read_ext_eeprom(address);
         delay_ms(200);
      }//end for
   }//end while
}


========24C512.C=================

Code:
#use delay(clock=20000000)
#ifndef EEPROM_SDA

#define EEPROM_SDA  PIN_B1
#define EEPROM_SCL  PIN_B0

#endif

#define EEPROM_ADDRESS long int
#define EEPROM_SIZE   65535

void start_ext_eeprom()
{
   output_low(EEPROM_SCL);
   output_high(EEPROM_SDA);
   output_high(EEPROM_SCL);
   delay_ms(1);
   output_low(EEPROM_SDA);
   delay_ms(5);
}

void stop_ext_eeprom()
{
   output_low(EEPROM_SCL);
   output_low(EEPROM_SDA);
   output_high(EEPROM_SCL);
   delay_ms(1);
   output_high(EEPROM_SDA);
   delay_ms(5);
}

void clock_data(Byte data)
{
   int8 ctr;

   for(ctr=0; ctr<8; ctr++)
   {
      output_bit(EEPROM_SDA, shift_left(&data,1,0));
      output_high(EEPROM_SCL);
      delay_ms(1); // let the pin stabilize
      output_low(EEPROM_SCL);
      delay_ms(1); //let the pin stabilize
   }
   delay_ms(5); // delay for EEPROM write cycle
}


void write_ext_eeprom(long int address, BYTE data)
{
   start_ext_eeprom();
   clock_data(0xa0);  // Device address & R/w bit is low
   while (input(EEPROM_SDA));
   clock_data(address>>8);
   clock_data(address);
   while (input(EEPROM_SDA));   
   clock_data(data);
   while (input(EEPROM_SDA));   
   stop_ext_eeprom();
}

BYTE read_ext_eeprom(long int address)
{
   int8 ctr;
   BYTE data;
   
   start_ext_eeprom();
   clock_data(0xa0);  // Device address & R/w bit is High
   while (input(EEPROM_SDA));
   clock_data(address>>8);
   clock_data(address);
   while (input(EEPROM_SDA)); 
   start_ext_eeprom(); //Send start again
   clock_data(0xa1);  // Device address & R/w bit is low to READ 
   while (input(EEPROM_SDA));   
   //===========read the data===================
   for(ctr=0; ctr<8; ctr++)
   {
      shift_left(&data,1,input(EEPROM_SDA));
      output_high(EEPROM_SCL);
      delay_ms(1); // let the pin stabilize.
      output_low(EEPROM_SCL);
      delay_ms(1); // let the pin stabilize.
   }   
   stop_ext_eeprom();
   return(data);
}
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Sat Jul 07, 2007 10:49 am     Reply with quote

You're not using the CCS driver.

Use the CCS driver. Here's the file location:
Quote:
c:\Program Files\PICC\Drivers\24512.c
AdamkT1



Joined: 21 Apr 2007
Posts: 44

View user's profile Send private message

PostPosted: Sat Jul 07, 2007 6:04 pm     Reply with quote

I have tried the driver also but that too does not seem to work. The program just hangs in the first write statement of the driver after the start statement.
Besides as a learner, I wish to do it myself.
Can some body highlight the mistake in the code?
The only idea left with me, is that may be, the pin SDA is not configured as an input that is why the value read is always a Zero.

Thanks for the reply. Rolling Eyes
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Sat Jul 07, 2007 6:38 pm     Reply with quote

If the CCS driver doesn't work, then there is something wrong with your
hardware. That's why I suggested that you try it first. Now you should
look closely at your connections, etc. Do you have pull-up resistors
on the SDA and SCL lines ?
AdamkT1



Joined: 21 Apr 2007
Posts: 44

View user's profile Send private message

PostPosted: Sat Jul 07, 2007 6:43 pm     Reply with quote

No pullups.
Can you provide me any hardware requirements.

Thanks again.
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Sat Jul 07, 2007 6:45 pm     Reply with quote

Add a pull-up resistor to +5v on each of SDA and SCL. Use 4.7K ohm
resistors.
AdamkT1



Joined: 21 Apr 2007
Posts: 44

View user's profile Send private message

PostPosted: Sat Jul 07, 2007 6:48 pm     Reply with quote

Ok.
I would need a little time to make that happen as I am not very good at hardware.
Appreciate the prompt responses. Thank you.
AdamkT1



Joined: 21 Apr 2007
Posts: 44

View user's profile Send private message

PostPosted: Sun Jul 08, 2007 1:26 am     Reply with quote

Its working fine now.
Thanks for the support.
I wish I could send the proteus file for new comers, as I see that this topic has been discussed so many times.

Thanks again
Very Happy
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