Email: Password: Remember Me | Create Account (Free)

Back to Subject List

Old thread has been locked -- no new posts accepted in this thread
???
11/03/08 14:49
Read: times


 
#159574 - I have added code-tags to Praveen's i2c code
Responding to: ???'s previous message
for other's benefit, I have added the code tags to Praveen's posting. It does appear to be written for a PIC rather than an 8051:


Praveen Kumar said:
hi..i have written a universal code for any eeprom with any microcontroller just change its write ID and READ ID..
if you are using 24c1024 than take upr_adr 0,1but if use lower than 24c1024 take upr_adr as zero.
ex-
if you are using 24c16 than always take upr_adr as 0,
WRITE_C--0xA0
READ_C--0xA1
connect A0,A1,A2 pin of eeprom to ground
here is code---
/*I2C ROUTINE.upr_addr is for AT24C1024.Its value shud be 1 for AT24C1024 else
it shud be 0.*/
#define		WRITE_C		0xA0 	//Command for write
#define		READ_C		0xA1	//Command for read
#define		SDA  PORTC.F4
#define		SCL  PORTC.F3
#define		SDA_Direction TRISC.F4
unsigned int WriteByteEEPROM(unsigned char upr_addr,unsigned int Address,unsigned char eep_data);
unsigned int ReadByteEEPROM(unsigned char upr_addr,unsigned int Address);
void	PollAck(unsigned char IDD);
void 		SendOneByteIIC(unsigned char d);
void 		StartBit(void);
void 		StopBit(void);
char 		SlaveAck(void);
void 		SlaveNoAck(void);
void 		IICClockDelay(void);
void 		Set_SDA_As_Output(void);
void 		Set_SDA_As_Input(void);
unsigned char 	ReadOneByteIIC(void);
void SendAck(void);


unsigned int WriteByteEEPROM(unsigned char upr_addr,unsigned int Address,unsigned char eep_data)
{
	unsigned char	ID;
  ID = WRITE_C | (upr_addr 7) | 0x0e;
	StartBit();			//Give start bit
	SendOneByteIIC(ID);		//Send the write command
	SlaveAck();			//Get the acknowledge from slave
	SendOneByteIIC((Address>>8) & 0x00ff);	//Send the address msb
	
	SlaveAck();			//Get the acknowledge from slave
	SendOneByteIIC(Address & 0x00ff);	//Send the address lsb
	
	SlaveAck();	        	//Get the acknowledge from slave
	
  SendOneByteIIC(eep_data);		//Send the Data
	SlaveAck();			//Get the acknowledge from slave
  StopBit();			//Give stop bit
  PollAck(ID);
return Address;
}

unsigned int ReadByteEEPROM(unsigned char upr_addr,unsigned int Address)
{ unsigned char	ID;
unsigned char eep_dataR;
	ID = WRITE_C | (upr_addr 7) | 0x0e;
	//Write Address
	StartBit();			//Give start bit
	SendOneByteIIC(ID);		//Send the write command
	SlaveAck();			//Get the acknowledge from slave
	
	SendOneByteIIC((Address>>8) & 0x00ff);	//Send the address msb
	SlaveAck();			//Get the acknowledge from slave
		
	SendOneByteIIC(Address & 0x00ff);	//Send the address lsb
	SlaveAck();			//Get the acknowledge from slave
	
	//Read a byte
	ID = READ_C | (upr_addr 7) | 0x0e;
	StartBit();			//Give start bit
	SendOneByteIIC(ID);		//Send the read command
	SlaveAck();			//Get the acknowledge from slave
  eep_dataR=ReadOneByteIIC();		//Read the byte
  SlaveNoAck();
  StopBit();			//Give stop bit
  return eep_dataR;
}

void	PollAck(unsigned char IDD)
{
	char	a;
	for(a=0;a a))
		{   Set_SDA_As_Input();
		    SDA = 1;
		}
		else
		{
		  SDA = 0;
  		Set_SDA_As_Output();
		}
		IICClockDelay();
		SCL	= 1;
		IICClockDelay();
	 }
}

void StartBit(void)
{
  SCL = 1;
	IICClockDelay();
	Set_SDA_As_Input();
	SDA	= 1;
	IICClockDelay();
	SDA	= 0;
	Set_SDA_As_Output();
	IICClockDelay();
	SCL = 0;
	IICClockDelay();
}

void StopBit(void)
{
	Set_SDA_As_Output();
	SDA	= 0;
	IICClockDelay();
	SCL	= 1;
	IICClockDelay();
	Set_SDA_As_Input();
	SDA = 1;
	IICClockDelay();
	SCL	= 0;
	IICClockDelay();
}

char SlaveAck(void)
{
  char	a;
  SCL	= 0;
	IICClockDelay();
	Set_SDA_As_Input();
	SDA	= 1;
	IICClockDelay();
	SCL = 1;
	IICClockDelay();
  a   = SDA;
	IICClockDelay();
	SCL = 0;
	IICClockDelay();
	return(a);
}

void SlaveNoAck(void)
{
	Set_SDA_As_Input();
	SDA	= 1;
	IICClockDelay();
	SCL	= 1;
	IICClockDelay();
	SCL = 0;
	IICClockDelay();
}
void SendAck(void)
{
	Set_SDA_As_Output();
	SDA	= 0;
	IICClockDelay();
	SCL	= 1;
	IICClockDelay();
	SCL = 0;
	IICClockDelay();
}


void IICClockDelay(void)
{
	char	aa;
	for(aa=0;aa a);
		}
		IICClockDelay();
		SCL = 0;
		IICClockDelay();
	}
	return(b);
}






List of 25 messages in thread
TopicAuthorDate
PROBLEM IN I2C WITH P89C668            01/01/70 00:00      
   Oscilloscope            01/01/70 00:00      
   Check your hardware I2C addressing            01/01/70 00:00      
   Re to David & Per            01/01/70 00:00      
      Reducing the bit-rate is irrelevant            01/01/70 00:00      
         you can, with VERY minor modifications ...            01/01/70 00:00      
   observations till 2 pm IST            01/01/70 00:00      
      check your hardware again            01/01/70 00:00      
         Re: david            01/01/70 00:00      
            A complete mystery            01/01/70 00:00      
   NEW DEVELOPEMENT            01/01/70 00:00      
      A good test setup            01/01/70 00:00      
         Re: A good test setup            01/01/70 00:00      
      Write your name to the DS1307 RAM            01/01/70 00:00      
         DS1307 max RAM is.....            01/01/70 00:00      
            My data sheet says RAM 0x08 to 0x3F            01/01/70 00:00      
               Sorry David            01/01/70 00:00      
                  Please post your code.            01/01/70 00:00      
                     not 12 individual writes            01/01/70 00:00      
                        Without the code this will go on forever            01/01/70 00:00      
            i2c code            01/01/70 00:00      
               Code?            01/01/70 00:00      
               WHY ON EARTH ...            01/01/70 00:00      
                  Mahesh has a problem with HIS code            01/01/70 00:00      
               I have added code-tags to Praveen's i2c code            01/01/70 00:00      

Back to Subject List