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.

I2C protocol for 8051

Status
Not open for further replies.

pavan_85

Member level 1
Member level 1
Joined
Jul 3, 2008
Messages
33
Helped
1
Reputation
2
Reaction score
0
Trophy points
1,286
Location
hyderabad
Activity points
1,497
i2c protocol

Hi all,
I wanted to read/write data from serial EEPROM using I2C protocol.

My problem is that i am a little bit confused with the range of material i am getting on the net, each going in a different way.

I am totally confused as what to do to write data serially and then read it back when required.

Please help me out.

Regards,
pavan
 

8051 i2c

do you have a dedicated serial peripheral (TWI), or will you be bitbanging? Check your eeproms' datasheet. It will driagram the waveforms required for various methods of access.
 

sir i m very confused in using I2C protocol to interface 24c04 with 89s52 i m not able to display it on my lcd 16x2 which is connected on port 2.
 

Hi,

See this

**broken link removed**

**broken link removed**

May be this helps you.
 

can i have a simple code example working on kiel for 8051 interfacing with eeprom 24cxx series over i2c.. please.............................. !!! i want to do this its been very long please help
 

Visit NXP website for I2C-related specs and documents. They will tell you how to use I2C.

For reading/writing serial EEPROM (24Cxx series), you could use the I2C master controller if your MCU has it.
Or you could use the IO port and firmware to build an I2C master controller by your own. I think this is very
easy for an experienced fimrware programmer. Be aware of the input/output direction on SDA line, especially
for acknowledge phases.
 
if you are getting confused by the material in the web, then it is better to read data sheet of the eeprom AT24c04 to clarify your doubts. If you tell what is your doubt we can clarify it instead of telling it is confusing.

---------- Post added at 16:36 ---------- Previous post was at 15:24 ----------

#include <reg51.h>
#include <intrins.h>
#include <stdio.h>


//*****************************************

#define data_wt 0xa0 //EEPROM address to write the data
#define data_rd 0xa1 //EEPROM address to read the data

//*****************************************

//*****************************************


void lcd_cmd(unsigned char); //To write the commands to the LCD
void lcd_data(unsigned char); //To write the data to the LCD
void msdelay(unsigned int); //Delay for LCD
void start_bit(); //Condition for start bit to the I2C
void stop_bit(); //Condition for stop bit to the I2C
void slave_add(unsigned char); //To send bit by bit data to the slave
unsigned char data_read(); //To read the data from EEPROM and display on LCD

//*****************************************

sbit sda=P2^1; //SDA line
sbit scl=P2^0; //SCL line
sbit rs=P1^0; //LCD Register select
sbit en=P1^1; //LCD Enable

//*******************************************


//*******************************************

void main()
{
unsigned char lcd_cmds[]={0x28,0x0f,0x01,0x06,0x80}; //Commands for the 4 line LCD
unsigned char temp,ch,j=16;
unsigned char addr=0x10;
unsigned char data_to_wt[]="WELCOME"; //Data to store in EEPROM
unsigned int num=0,i=0;
unsigned char p1='1',p2='2',p3='3',k1,k2,k3;
unsigned char a[]="pwd correct",b[]="aces denied";

//*****************************************************

for(i=0x0;i<0x5;i++) //To give LCD commands
{
temp=lcd_cmds;
lcd_cmd(temp);
msdelay(2);
}

//*****************************************************
lcd_data('s');
lcd_cmd(0x80);
for(i=0;i<7;i++) //To give data to EEPROM
{
start_bit();
slave_add(data_wt);
slave_add(i);
slave_add(data_to_wt);
stop_bit();
msdelay(2);
msdelay(2);
}



msdelay(2);
//*****************************************************
msdelay(2);

for(i=0;i<7;i++) //To read data from EEPROM
{

start_bit();
slave_add(data_wt);
slave_add(i);
start_bit();
slave_add(data_rd);
ch=data_read();
stop_bit();
str=ch;
msdelay(2);
msdelay(2);
}


msdelay(2);

//******************************************************
for(i=0;i<7;i++) //To read data from EEPROM
{
lcd_data(str);
}

while(1);
}
else
{
lcd_cmd(0xc0);
for(i=0;i<=10;i++)
lcd_data(b);
}

msdelay(2);
//*****************************************************

}

//******************************************************

void lcd_cmd(unsigned char cmd) //LCD command
{
unsigned int cmd1;
cmd1=((cmd&0xf0)>>2);
P1=cmd1;
rs=0;
en=1;
msdelay(2);
en=0;
cmd1=((cmd&0x0f)<<2);
P1=cmd1;
rs=0;
en=1;
msdelay(2);
en=0;
msdelay(2);
}

//******************************************************

void lcd_data(unsigned char d1) //LCD data
{
unsigned int d2;
d2=((d1&0xf0)>>2);
P1=d2;
rs=1;
en=1;
msdelay(2);
en=0;
d2=((d1&0x0f)<<2);
P1=d2;
rs=1;
en=1;
msdelay(2);
en=0;
msdelay(2);
}

//*****************************************************

void start_bit() //Start bit condition
{
sda=1;
scl=1;
_nop_();
_nop_();
sda=0;
_nop_();
_nop_();
_nop_();
_nop_();
scl=0;
_nop_();
_nop_();
}

//*****************************************************
//*****************************************************
void stop_bit() //Stop bit condition
{
sda=0;
scl=1;
_nop_();
_nop_();
sda=1;
_nop_();
_nop_();
_nop_();
_nop_();
scl=0;
_nop_();
_nop_();
_nop_();
_nop_();
scl=1;
}

//******************************************************

void slave_add(unsigned char add)
{
unsigned char t,i;
t=add;

for(i=0;i<8;i++)
{
t=(t&0x80);
if(t!=0)
sda=1;
else
sda=0;
scl=1;
_nop_();
_nop_();
_nop_();
scl=0;
t=add<<1;
add=t;
}

sda=1;

scl=1;
_nop_();
_nop_();
_nop_();
scl=0;
msdelay(2);
}

//*******************************************************

unsigned char data_read()
{
unsigned char i,x=0;
unsigned char y;
y=0;
sda=1;

for(i=0;i<8;i++)
{
scl=0;
_nop_();
_nop_();
_nop_();
scl=1;

if(sda==1)
x=x|0x01;
y=x;
x=x<<1;
scl=0;
}

sda=1;

scl=1;
_nop_();
_nop_();
_nop_();
scl=0;
msdelay(2);
return(y);
}

//*******************************************************

void msdelay(unsigned int itime)
{
unsigned int i, j;
for(i=0; i<=itime; i++)
for(j=0; j<=1275; j++);
}
 
Hi,
I'm using software based I2C for CC2430 SOC( its 8051 based core from TI), I need Interface one I2c(slave) sensor to this controller.


SCL frequency for the sensor is 400kHz(this is max frequency in which the sensor works).

Since our CC2430 controller runs with 32MHz frequency , I'm not able to use the (I2C) code given in the RemoTi.

STATIC __near_func void hali2cWait( uint8 count )
{
while ( count-- );
}

STATIC _Bool hali2cRead( void )
{
// SCL low to let slave set SDA. SCL high for SDA
// valid and then get bit // I think Here the controller runs with 32MHz, I need to run this at freq b/w 200 to 400KHZ
hali2cClock( 0 );//making SCL pin as input
hali2cWait(1);
hali2cClock( 1 ); //making SCL pin as input
hali2cWait(1);

return OCM_SDA;
}



STATIC uint8 hali2cReceiveByte()
{
int8 i, rval = 0;

for (i=7; i>=0; --i) {
if (hali2cRead()) {
rval |= 1<<i;
}
}

return rval;
}

int8 HalI2CReceive(uint8 address, uint8 *buf, uint16 len)
{
hali2cReceive(address, buf, len);

return 0;
}

This code will run fine if the Sensor SCL of the clock is 32MHz(because cc2430 is running at this speed) but my sensor SCL is maximum 400kHz, so how can I make this run at 400kHz...

pls suggest me a solution to fix this problem.
 

I think u have to increase the delay in your instructions and check it on cro for the same..... Hope it will be much helpfull
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top