Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

[ARM] need help : unable to read eeprom using i2c

Status
Not open for further replies.

sai shankar

Newbie level 2
Joined
Apr 11, 2013
Messages
2
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Location
hyderabad
Activity points
1,318
Hi I am trying to read and write a byte to eeprom using i2c in lpc2138.... here is my code.... untill repeated start and then the read condition it is going well after that i am storing the data to a buffer but its not working... please go through the code and let me know if i am doing any thing working....

#include<lpc213x.h>
#define EEPROM 0xA0
//--- LPC21XX I2C flags --------------------

#define I2C_FLAG_AA (1<<2)
#define I2C_FLAG_SI (1<<3)
#define I2C_FLAG_STO (1<<4)
#define I2C_FLAG_STA (1<<5)
#define I2C_FLAG_I2EN (1<<6)

void init_EEPROM(void);
void MT_EEPROM(void);
void MR_EEPROM(void);
static void i2c_write(int);
void i2c_stop(void);
int i2c_ctrl(unsigned int);
void check_I2C_STATUS(unsigned char);

void lcdcmd(unsigned char);
void lcddata(unsigned char);
void delay(unsigned int);
void lcdinit(void);
void display(unsigned char *str);
unsigned char d;

int main(void)
{
IO1DIR=0x07FF0000;
lcdinit();
display("EEPROM START");
delay(1000);
init_EEPROM();
MT_EEPROM();
init_EEPROM();
MR_EEPROM();
}


static void i2c_write(int byte)
{
I2C0DAT= byte;
delay(500);
check_I2C_STATUS(I2C0STAT);
I2C0CONCLR=I2C_FLAG_SI; // clearing SI bit
while(!(I2C0CONSET & I2C_FLAG_SI));
}

void i2c_stop()
{
I2C0CONCLR = I2C_FLAG_SI;
I2C0CONSET |= I2C_FLAG_AA | I2C_FLAG_STO;
}

void init_EEPROM(void)
{
PINSEL0=0x00000050; /*scl=0 p0.5 and sda = p0.7*/
delay(1);
//I2C0SCLH=0xC8;
//I2C0SCLL=0xC8;
I2C0SCLH=0x0C;
I2C0SCLL=0x0C;
delay(1);
I2C0CONCLR=0xFF;
I2C0CONSET=0x40; //I2EN
I2C0CONSET |= I2C_FLAG_I2EN; //enable I2C
//while(I2C0STAT != 0x08);
}

int i2c_ctrl(unsigned int addr)
{
int chk;
I2C0CONCLR=0xFF;
I2C0CONSET |= I2C_FLAG_I2EN | I2C_FLAG_STA;
while(!(I2C0CONSET & I2C_FLAG_SI));
I2C0DAT=addr;
check_I2C_STATUS(I2C0STAT);
I2C0CONCLR = I2C_FLAG_STA | I2C_FLAG_SI;
if(addr & 1)
chk = 0x40;
else
chk = 0x18;
while(!(I2C0CONSET & I2C_FLAG_SI));
if(I2C0STAT != chk)
{
i2c_stop();
display("ERROR");
}
return 0;
}
void MT_EEPROM()
{
i2c_ctrl(0xA0);
i2c_write((0x7F>>8)&0x7F);
i2c_write(0xFF&0xFF);
i2c_write(0x31);
i2c_stop();
}

void MR_EEPROM(void)
{
unsigned char d;
i2c_ctrl(0xA0);
i2c_write((0x00>>8)&0x7F);
i2c_write(0x00&0xFF);

i2c_ctrl(0xA1);
check_I2C_STATUS(I2C0STAT);

d=(unsigned char)I2C0DAT;
delay(500);
check_I2C_STATUS(I2C0STAT);
i2c_stop();
lcdcmd(0x01);
lcddata(d);
delay(500);
}

void check_I2C_STATUS(unsigned char s)
{
lcdcmd(0x80);
lcdcmd(0x01);
switch(s)
{
case 0x08: display("Start 08");
delay(2000);
break;
case 0x10:display("Rep Start ");
delay(2000);
break;
case 0x18: display("slave+W add 18");
delay(2000);
break;
case 0x20: display("Start 20 SLA+W NA");
delay(2000);
break;
case 0x28: display("data 28");
delay(2000);
break;
case 0x30: display("data 30 NA");
delay(2000);
break;
case 0x40: display("SLA+R ack 40");
delay(2000);
break;
case 0x48: display("SLA+R Nack 48");
delay(2000);
break;
case 0x50: display("data ack 50");
delay(2000);
break;
case 0x58: display("data nack 58");
delay(2000);
break;
default : display("Wrong Status");
delay(2000);
}
}
void delay(unsigned int v)
{
int i,j;
for(i=0;i<v;i++)
for(j=0;j<1000;j++);
}
void display(unsigned char *str)
{
while(*str)
{
lcddata(*str);
str++;
}
}
void lcddata(unsigned char c)
{
IO1CLR=0x07F80000;//p0.3 to p0.10
IO1SET=c<<19;
IO1SET=0x00010000;//rs=1 at p0.0
IO1CLR=0x00020000;//rw=0 at p0.1
IO1SET=0x00040000;//en=1 at p0.2
delay(100);
IO1CLR=0x00040000;//en=0 at p0.2
}
void lcdcmd(unsigned char c)
{
IO1CLR=0x07F80000;;//p0.16 to p0.23
IO1SET=c<<19;
IO1CLR=0x00010000;//rs=0 at p1.16
IO1CLR=0x00020000;//rw=0 at p1.17
IO1SET=0x00040000;//en=1 at p1.18
delay(100);
IO1CLR=0x00040000;//en=0 at p0.18
}

void lcdinit()
{
lcdcmd(0x38);
lcdcmd(0x01);
lcdcmd(0x0e);
lcdcmd(0x06);
}


thanks in advance
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top