Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

I2c with serial eeprom...

Status
Not open for further replies.

koolguy

Active Member
Hi,

I am using atmlu020 EEPROM with AD0 - AD2 to GND
and testing whether the send data is matched from reading data at same addr by switching LED On Pin...

so, here is the code don't know why the if statement looping cont and led is blinking??


Code:
#include <htc.h>
__CONFIG(LVP_OFF & BOREN_OFF & PWRTE_ON & WDTE_OFF & FOSC_HS);
#define _XTAL_FREQ 20000000
 
#define SDATA RC4
#define SCLK RC3
void I2C_init(),I2C_start(void),I2C_write(char x), I2cSTOP(void);	
void i2c_Wait(void);
unsigned char i2c_Read(unsigned char ack);

void main(void){
 
	TRISC3=1; //direction to input have be changed
	TRISC4=1;
	unsigned char ch,c;
TRISC5=0;
TRISD2=0;
__delay_ms(200);
RD2=1;

c=0xff;
while(1){ 

   I2C_init();
__delay_ms(50);
	I2C_start();
	I2C_write(0b10100000);//command 1010 and 000 and 0 for write
	I2C_write(0X10);// addr at location 
	I2C_write(0XFF);//data
   I2cSTOP();


 I2C_init();
__delay_ms(50);
	I2C_start();
	I2C_write(0b10100001);//command 1010 and 000 and 0 for read
I2C_write(0X10);// addr
	ch= i2c_Read(0); //read data
   I2cSTOP();
if(ch&&0X0F){ // comparing data with ch

RD2=0; 
__delay_ms(400);
RD2=1; 
__delay_ms(800);
}
else{

RD2=0; 
}
ch=0;
}

}
 
 
 
 
void I2C_init(void)
	{
  SSPCON = 0x38;      // set I2C master mode
 SSPCON2 = 0x00;
SSPADD = 0x0C;  //400KHZ  20MHz xtal
SSPSTAT|=0X80;
 PSPIF=0;      // clear SSPIF interrupt flag
 BCLIF=0;      // clear bus collision flag
}
 
 
 
void I2C_start(void)
{
    i2c_Wait();
    SEN=1;
}
 
 void I2C_write(char x){
	i2c_Wait();	
SSPBUF=x;
 
 
 
		}
 
void i2c_Wait(void){
    while((SSPCON2 & 0X1F || (SSPSTAT & 0X04)));
}
 
void I2cSTOP(void)
{
    i2c_Wait();
    PEN=1;
}

unsigned char i2c_Read(unsigned char ack)
{
    // Read data from slave
    // ack should be 1 if there is going to be more data read
    // ack should be 0 if this is the last byte of data read
    unsigned char i2cReadData;

    i2c_Wait();
    RCEN=1;
    i2c_Wait();
    i2cReadData = SSPBUF;
    i2c_Wait();
    if ( ack ) ACKDT=0;	        // Ack
    else       ACKDT=1;	        // NAck
    ACKEN=1;                    // send acknowledge sequence

    return( i2cReadData );
}
 
You know the basic structure of a microcontroller program:
C:
void main(void)
{
    /* Initializations etc. that are executed once on startup */
    /* .... */

    /* Infinite loop that keeps repeating and keeps the microcontroller "going" */
    while(1){
        /* Whatever you put in here keeps repeating */
    }
}
You have the if-statement inside the while(1) loop.. therefore the code keeps repeating.
 
Last edited:
Ok, i have edited ...
but LED glow than off...

Code:
void main(void){ 
	TRISC3=1; //direction to input have be changed
	TRISC4=1;
	unsigned char ch,c;
TRISC5=0;
TRISD2=0;
__delay_ms(200);
RD2=1;
c=0xff;
   I2C_init();
__delay_ms(50);
	I2C_start();
	I2C_write(0b10100000);//esc default addr
	I2C_write(0X10);//esc default addr
	I2C_write(0XFF);//esc default addr
   I2cSTOP();
 I2C_init();
__delay_ms(50);
	I2C_start();
	I2C_write(0b10100001);//esc default addr
I2C_write(0X10);//esc default addr
	ch= i2c_Read(0);
   I2cSTOP();
while(1){
if(ch&&0XFF){
RD2=0; 
__delay_ms(400);
RD2=1; 
__delay_ms(800);
}
if(!ch&&0XFF){
RD2=0; }
ch=0;


}
}
 
What is it supposed to do?

Of course the LED turns off when you turn it off with this piece of code at the end of your while(1) loop:

C:
while(1) {
    if(!ch&&0XFF){
        RD2=0;
    }
    ch=0;
}

if you set ch=0, then the first if-check fails and LED is turned off by the second if-statement.. or that is what I assume RD2=0; does.
 
Why don't you test the data simply like this

C:
while(1) {

    // write data "0xFF"

    // read data back to variable ch

    if(ch == 0xFF){
        RD2=1;  // LED ON
    } else {
        RD2=0;  // LED OFF
    }
}

Actually your first code is very close to that.. I just removed couple of unnecessary lines that made the LED blink.

And instead of using 0xFF as test data, I would use 0x55.. which is a lot better because it has both zeros and ones.
 
Last edited:
Ritesh!! Can you please try and indent your code so we can read it

Like this

C:
#include <htc.h>
__CONFIG(LVP_OFF & BOREN_OFF & PWRTE_ON & WDTE_OFF & FOSC_HS);
#define _XTAL_FREQ 20000000
 
#define SDATA RC4
#define SCLK RC3
void I2C_init(),I2C_start(void),I2C_write(char x), I2cSTOP(void);	
void i2c_Wait(void);
unsigned char i2c_Read(unsigned char ack);
 
void main(void)
	{
	TRISC3=1; //direction to input have be changed
	TRISC4=1;
	unsigned char ch,c;
	TRISC5=0;
	TRISD2=0;
	__delay_ms(200);
	RD2=1;

	c=0xff;
	while(1)
		{ 

		I2C_init();
		__delay_ms(50);
		I2C_start();
		I2C_write(0b10100000);//command 1010 and 000 and 0 for write
		I2C_write(0X10);// addr at location 
		I2C_write(0XFF);//data
		I2cSTOP();
 
		I2C_init();
		__delay_ms(50);
		I2C_start();
		I2C_write(0b10100001);//command 1010 and 000 and 0 for read
		I2C_write(0X10);// addr
		ch= i2c_Read(0); //read data
		I2cSTOP();
		if(ch&&0X0F)
			{ // comparing data with ch
			RD2=0; 
			__delay_ms(400);
			RD2=1; 
			__delay_ms(800);
			}
		else
			{
			RD2=0; 
			}
		ch=0;
		}
 
	}
 
void I2C_init(void)
	{
	SSPCON = 0x38;	// set I2C master mode
	SSPCON2 = 0x00;
	SSPADD = 0x0C;	//400KHZ  20MHz xtal
	SSPSTAT|=0X80;
	PSPIF=0;		// clear SSPIF interrupt flag
	BCLIF=0;		// clear bus collision flag
	}
 
 
 
void I2C_start(void)
	{
    i2c_Wait();
    SEN=1;
	}
 
void I2C_write(char x)
 	{
	i2c_Wait();	
	SSPBUF=x;
	}
 
void i2c_Wait(void)
	{
    while((SSPCON2 & 0X1F || (SSPSTAT & 0X04)));
	}
 
void I2cSTOP(void)
	{
    i2c_Wait();
    PEN=1;
	}
 
unsigned char i2c_Read(unsigned char ack)
	{
    // Read data from slave
    // ack should be 1 if there is going to be more data read
    // ack should be 0 if this is the last byte of data read
    unsigned char i2cReadData;
 
    i2c_Wait();
    RCEN=1;
    i2c_Wait();
    i2cReadData = SSPBUF;
    i2c_Wait();
    if ( ack ) ACKDT=0;	        // Ack
    else       ACKDT=1;	        // NAck
    ACKEN=1;                    // send acknowledge sequence
 
    return( i2cReadData );
}
 
Hi,
It is working fine but i think write and read operation is changing bit position working in 0Xff but not in other condition

Code:
void main(void){ 
	TRISC3=1; //direction to input have be changed
	TRISC4=1;
	unsigned char ch,c;
TRISC5=0;
TRISD2=0;
__delay_ms(200);
RD2=1;
__delay_ms(400);
RD2=1;

   I2C_init();
__delay_ms(50);
	I2C_start();
	I2C_write(0b10100000);// ad0 to ad3 gnd  write operation as per manual 
	I2C_write(0X10);//addr eeprom
	I2C_write(0xff);//data work in 0xff but not in other like 0x10 etc 
   I2cSTOP();
 I2C_init();
__delay_ms(50);
	I2C_start();
	I2C_write(0b10100001);//ad0 to ad3 gnd  read operation as per manual 
I2C_write(0X10);//addr
	ch= i2c_Read(0);//data
   I2cSTOP();
while(1){
  if(ch == 0xff){
        RD2=1;  // LED ON
__delay_ms(400);
        RD2=0;  // LED ON
__delay_ms(400);
    } 


else {
        RD2=0;  // LED OFF
    }
}
}
 
use:

CODE=C

instead of CODE and you'll get color coded code.
 
Last edited:
working in 0Xff but not in other condition

When you change the test data, did you also remember to change the if-statement to test for this new data?

C:
// If you change this data..
I2C_write(0x55);//data work in 0xff but not in other like 0x10 etc 
 

// You also need to change this test..
if(ch == 0x55)
 
how to do this color full

First make the tabs correct with your text editor... Then when you wrap your code in "code tags" ( # ) you then insert "=C" within the
Code:
 [/COLOR]brackets after the word code.  like this..

[COLOR="#0000CD"] [CODE=C] [/COLOR] then the rest..
 
I am using atmlu020 EEPROM

I could not find that eeprom.. is it one of the Microchips I2C eeproms? The link goes to google search results and there is no chip named atmlu020.

EDIT: Ok, it must be one of the Atmel AT24Cxxx eeproms (atml=atmel.. d'oh!). Am I right?
 
Last edited:
When you change the test data, did you also remember to change the if-statement to test for this new data?

Yes i have changed don't you think while reading the MSB and LSB changed there position something like that..?


C:
void main(void){ 
	TRISC3=1; //direction to input have be changed
	TRISC4=1;
	unsigned char ch,c;
TRISC5=0;
TRISD2=0;
__delay_ms(200);
RD2=1;
__delay_ms(400);
RD2=1;
 
   I2C_init();
__delay_ms(50);
	I2C_start();
	I2C_write(0b10100000);// ad0 to ad3 gnd  write operation as per manual 
	I2C_write(0X10);//addr eeprom
	I2C_write(0xff);//data work in 0xff but not in other like 0x10 etc 
   I2cSTOP();
 I2C_init();
__delay_ms(50);
	I2C_start();
	I2C_write(0b10100001);//ad0 to ad3 gnd  read operation as per manual 
I2C_write(0X10);//addr
	ch= i2c_Read(0);//data
   I2cSTOP();
while(1){
  if(ch == 0xff){
        RD2=1;  // LED ON
__delay_ms(400);
        RD2=0;  // LED ON
__delay_ms(400);
    } 
 
 
else {
        RD2=0;  // LED OFF
    }
}
}

Microchips I2C eeproms
Yes it is from Microchip......no idea about serial number something different is written on it..!!
 
Just like I suspected.. Ritesh, read my post #12.

When you change the data you write in, you also have to change the if-statement to reflect that change.

EDIT: sorry, misread the latest code. It looks ok.. but anyway..
 
Then what should be done i mean is there any problem in code??
and i found this chip for voice recording its cool aPR33A1/aPR33A2/aPR33A3
 
Then what should be done i mean is there any problem in code??

If the memory read/write fails, then the problem might be in code or in hardware. Check the hardware and check from the EEPROM datasheet the details how to communicate with the chip. I2C devices are all a little bit different.. there is almost always something "unexpected" that you have to do..
 
Status
Not open for further replies.

New Articles From Microcontroller Tips

Back
Top