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 program file problem with 8051

Status
Not open for further replies.

anboli

Full Member level 2
Full Member level 2
Joined
Mar 9, 2012
Messages
144
Helped
4
Reputation
8
Reaction score
4
Trophy points
1,298
Activity points
2,513
i have the problem with I2C program, while interfacing with ds1307 RTC.

can anyone upload the I2C header or program for the 8051 controller?
:cry:


here is my source code for I2C.

void I2CInit()
{
SDA = 1;
SCL = 1;
}

void I2CStart(){

SDA=1;
SCL=1;
_nop_(); //No operation
_nop_();
SDA=0;
SCL=0;
// SCL = 1;
// _nop_(); //No operation
// _nop_();
// SDA = 0;
// SCL = 0;
}
//===============================================================================

void I2CRestart(){
SCL = 0;
SDA = 1;
delay(4);
SCL = 1;
SDA = 0;
}
//===============================================================================

void I2CStop(){
SDA=0;
SCL=1;
_nop_();
_nop_();
SDA=1;
SCL=0;
// SCL = 0;
// SDA = 0;
// LCD_delay(4);
// SCL = 1;
// SDA = 1;
}

void I2CAck(){

SDA = 0;
SCL = 1;
_nop_();
_nop_();
SCL = 0;
SDA = 1;
}
//===============================================================================
void I2CNak(){
SDA = 1;
SCL = 1;
SCL = 0;
}
 

Message is unavailable.
 
  • Like
Reactions: anboli

    anboli

    Points: 2
    Helpful Answer Positive Rating
thanks for the response sir.

sir, i had taken the code from that website.

i had worked with the program, its quite running, but what was the problem occurs in the program,

its not reading the data when we specifying particular address.

more than that its working when we just copy and paste the same program.




//Program to interface Serial EEPROM AT24C02 with 8051 microcontroller (AT89C51)
#include<reg51.h>
#include<intrins.h> //For using [_nop_()]


sbit sda=P2^0;
sbit scl=P2^1;

sbit led=P2^3;
bit ack;
sbit led1=P2^2;
sfr lcd_data_pin=0x80;//p2 port
sfr output=0xA0;//p0 port

sbit rs=P3^6;
sbit rw=P3^5;
sbit en=P3^7;

unsigned char reead,write,write2,i,j,k;
unsigned int temp;
void delay(unsigned int count)
{
int i,j;
for(i=0;i<count;i++)
for(j=0;j<1275;j++);
}
void lcd_command(unsigned char comm)
{
lcd_data_pin=comm;
en=1;
rs=0;
rw=0;
delay(1);
en=0;
}
void lcd_data(unsigned char disp)
{
lcd_data_pin=disp;
en=1;
rs=1;
rw=0;
delay(1);
en=0;
}
lcd_dataa(unsigned char *disp)
{
int x;
for(x=0;disp[x]!=0;x++)
{
lcd_data(disp[x]);
}
}
void lcd_ini()
{
lcd_command(0x38); // for using 8-bit 2 row LCD
delay(5);
lcd_command(0x0F); // for display on, cursor blinking
delay(5);
lcd_command(0x80);
delay(5);
}

void aknowledge() //acknowledge condition
{
scl=1;
_nop_();
_nop_();
scl=0;
}
void start() //start condition
{
sda=1;
scl=1;
_nop_(); //No operation
_nop_();
sda=0;
scl=0;
}
void stop() //stop condition
{
sda=0;
scl=1;
_nop_();
_nop_();
sda=1;
scl=0;
}
void send_byte(unsigned char value) //send byte serially
{
unsigned int i;
unsigned char send;
send=value;
for(i=0;i<8;i++)
{
sda=send/128; //extracting MSB
send=send<<1; //shiftng left
scl=1;
_nop_();
_nop_();
scl=0;
}
ack=sda; //reading acknowledge
sda=0;
}
unsigned char read_byte() //reading from EEPROM serially
{
unsigned int i;
sda=1;
reead=0;
for(i=0;i<8;i++)
{
reead=reead<<1;
scl=1;
_nop_();
_nop_();
if(sda==1)
reead++;
scl=0;
}
sda=0;
return reead; //Returns 8 bit data here
}
void save() //save in EEPROM
{
start();
send_byte(0xA0); //device address
aknowledge();
send_byte(0x00); //word address
aknowledge();
send_byte(5); //send data
aknowledge();
send_byte(65);
aknowledge();
send_byte(75);
aknowledge();
stop();
if(ack==0)
{
led1=1;
delay(100);
led1=0;

lcd_command(0x86);
lcd_data('5');

lcd_command(0x87);
lcd_data('A');
delay(100);
}
else
{led1=1;
aknowledge();
lcd_dataa("No write");
delay(100);
}

////*********************************************////
// //
//*********************************************//
// send_byte(0x01); //word address
// aknowledge();
// send_byte(6); //send data
// aknowledge();
// send_byte(66);
// aknowledge();
// //stop();
// if(ack==0)
// {
// led1=1;
// delay(100);
// led1=0;
// delay(100);
// lcd_command(0x89);
// lcd_data('6');
// lcd_command(0x8A);
// lcd_data('A');
// }
// else
// { led1=1;
// aknowledge();
// lcd_dataa("2");
// delay(100);
// }
//
//
// send_byte(0x02); //word address
// aknowledge();
// send_byte(7); //send data
// aknowledge();
// send_byte(67);
// aknowledge();
// stop();
// if(ack==0)
// {
// led1=1;
// delay(100);
// led1=0;
// delay(100);
// //lcd_command(0x01);
// lcd_command(0x8c);
// lcd_data('7');
// lcd_command(0x8d);
// lcd_data('A');
// }
// else
// {led1=1;
// aknowledge();
// lcd_dataa("3");
// delay(100);
// }
//}
}


void Read()
{
start();
send_byte(0xA0);
aknowledge();
send_byte(0x00);
aknowledge();
start();
send_byte(0xA1); //device address
aknowledge();
i=read_byte();
aknowledge();
j=read_byte();
aknowledge();
k=read_byte();
aknowledge();
stop();
if(i==5)
{
led=0;
delay(100);
led=1;
delay(100);
write=i+48;
lcd_command(0xC6);
lcd_data(write);
delay(50);
}
else
{
led=1;
lcd_command(0x01);
lcd_dataa("No Read of i");
delay(100);
}
if(k==75)
{
lcd_command(0xC7);
lcd_data(k);
delay(50);
}
else
{lcd_command(0x01);
lcd_dataa("No Read of j");
delay(100);
}
if(j==65)
{
lcd_command(0xC7);
lcd_data(j);
delay(50);
}
else
{lcd_command(0x01);
lcd_dataa("No Read of j");
delay(100);
}
//aknowledge();
i=0;
j=0;
//*****************************second read*****************
// start();
// send_byte(0xA0);
// aknowledge();
// send_byte(0x00);
// aknowledge();
// start();
// send_byte(0xA1); //device address
// aknowledge();
// i=read_byte();
// aknowledge();
// j=read_byte();
// aknowledge();
// stop();
// if(i==5)
// {
// led=0;
// delay(100);
// led=1;
// delay(100);
// write=i+48;
// //lcd_command(0x01);
// lcd_command(0xC9);
// lcd_data(write);
//
// delay(200);
// }
// else
// {lcd_command(0xC9);
// lcd_dataa("1");
// delay(100);
// led=1;
// }
// if(j==65)
// {
// lcd_command(0xCa);
// lcd_data(j);
// delay(200);
// }
// else
// {lcd_dataa("1");
// delay(100);
// led=1;
// }
//// aknowledge();
//

}

void main()
{

lcd_ini();
lcd_dataa("Sent :");
lcd_command(0xC0);
lcd_dataa("Read :");
temp=0;
save();
while(1)
{

Read();
}
}
 
Last edited:

try this code

Code:
#include<intrins.h>
#include<reg51.h>
sbit rs=P0^1;
sbit rw=P0^2;
sbit en=P0^3;
sbit en1=P2^7;
sbit sda=P1^0;
sbit scl=P1^1;
void i2c_start();
void lcd_init1();
void i2c_stop();
void data_init();
void display_routine(unsigned char);
void slave_address(unsigned char);
void data_write(unsigned char);
unsigned char data_read();
void ack();
void ack1();
void m_ack();
void m_nack();
void lcd_init();
void data_mode1(unsigned char );
void data_mode(unsigned char);
void command_mode(unsigned char);
void busy();
unsigned char ch1[][5]={"sun","mon","tue","wed","thur","fri","sat"};
unsigned char ch[7],x;
int main()
{
unsigned char i=0,b=0,c=0,z=0;
P2=0x00;
rs=0;
rw=0;
en=0;
en1=1;
scl=0;
sda=0;
i2c_start();
slave_address(0xd0);
ack();
data_write(0x00);
ack();
data_write(0x00);
ack();
data_write(0x08);
ack();
data_write(0x07);
ack();
data_write(0x4);
ack();
data_write(0x6);
ack();
data_write(0x6);
ack();
data_write(0x12);

i2c_stop();

lcd_init();
//while(1)
//{
command_mode(0x83);
i2c_start();
slave_address(0xd0);
ack();
data_write(0x00);
ack();
i2c_start();
slave_address(0xd1);
ack();		  
for(i=0;i<7;i++)
{
ch[i]=data_read();
if(i==7)
{
m_nack();
}
else{
m_ack();
}
}
i2c_stop();
display_routine(ch[2]);
data_mode(':');
display_routine(ch[1]);
data_mode(':');
display_routine(ch[0]);
command_mode(0xc0);
data_mode1(ch[3]);
command_mode(0xc7);
display_routine(ch[4]);
data_mode('-');
display_routine(ch[5]);
data_mode('-');
display_routine(ch[6]);
//}
return 0;
}

void data_write(unsigned char a)
{
unsigned char i=0,x=0;
for(i=0;i<8;i++)
{
if(a&0x80)
{
sda=1;
}
else
{
sda=0;
}
scl=1;
_nop_();
_nop_();
scl=0;
a=a<<1;
}
}

unsigned char data_read()
{
unsigned char a=0,b=0,c=0,i;
sda=1;
for(i=0;i<8;i++)
{
a<<=1;
if(sda)
{
a=a|0x01;
}
scl=1;
_nop_();
_nop_();
//_nop_();
scl=0;
}
return a;
}
void slave_address(unsigned char a)
{
data_write(a);
}
void ack()
{
sda=0;
scl=1;
_nop_();
_nop_();
//_nop_();
scl=0;
if(sda==0)
{
;
}
else
{
;
}
}

void m_ack()
{
sda=0;
scl=1;
_nop_();
_nop_();
//_nop_();
scl=0;
}
void m_nack()
{
sda=1;
scl=1;
_nop_();
_nop_();
//_nop_();
scl=0;
}
void i2c_start()
{
sda=1;
scl=1;
_nop_();
_nop_();
//_nop_();
sda=0;
scl=0;
}

void i2c_stop()
{
sda=0;
scl=1;
_nop_();
_nop_();
sda=1;
scl=0;
}

void command_mode(unsigned char a)
{
//delay();
busy();
P2=a;
rs=0;
rw=0;
en=1;
_nop_();
_nop_();
en=0;
}

void data_mode(unsigned char a)
{
busy();
P2=a;
data_init();
}

void data_mode1(unsigned char p)
{
unsigned char j=0;
busy();
p=p-1;
while(ch1[p][j]!='\0')
{
P2=ch1[p][j];
data_init();
j++;
}
}
void data_init()
{
rs=1;
rw=0;
en=1;
_nop_();
_nop_();
en=0;
}

void lcd_init()
{
command_mode(0x01);
command_mode(0x38);
command_mode(0x06);	
command_mode(0x0c);
command_mode(0x80);
}

void busy()
{
en1=1;
rs=0;
rw=1;
while(en1)
{
en=0;
_nop_();
_nop_();
en=1;
}
}
void display_routine(unsigned char ch)
{
unsigned char b=0,c=0;
b=ch&0x0f;
b=b|0x30;
c=ch>>4;
c=c|0x30;
data_mode(c);
data_mode(b);
}

hope this helps
 

In this code I'm trying to determine what the:
Code:
sbit en1=P2^7;
is referring to. An LED state, indicator? Used in place of an int value maybe?

Code:
#include<intrins.h>
#include<reg51.h>
sbit rs=P0^1;
sbit rw=P0^2;
sbit en=P0^3;
sbit en1=P2^7;
sbit sda=P1^0;
sbit scl=P1^1;
void i2c_start();
void lcd_init1();
void i2c_stop();
void data_init();
void display_routine(unsigned char);
void slave_address(unsigned char);
void data_write(unsigned char);
unsigned char data_read();
void ack();
void ack1();
void m_ack();
void m_nack();
void lcd_init();
void data_mode1(unsigned char );
void data_mode(unsigned char);
void command_mode(unsigned char);
void busy();
unsigned char ch1[][5]={"sun","mon","tue","wed","thur","fri","sat"};
unsigned char ch[7],x;
int main()
{
unsigned char i=0,b=0,c=0,z=0;
P2=0x00;
rs=0;
rw=0;
en=0;
en1=1;
scl=0;
sda=0;
i2c_start();
slave_address(0xd0);
ack();
data_write(0x00);
ack();
data_write(0x00);
ack();
data_write(0x08);
ack();
data_write(0x07);
ack();
data_write(0x4);
ack();
data_write(0x6);
ack();
data_write(0x6);
ack();
data_write(0x12);

i2c_stop();

lcd_init();
//while(1)
//{
command_mode(0x83);
i2c_start();
slave_address(0xd0);
ack();
data_write(0x00);
ack();
i2c_start();
slave_address(0xd1);
ack();		  
for(i=0;i<7;i++)
{
ch[i]=data_read();
if(i==7)
{
m_nack();
}
else{
m_ack();
}
}
i2c_stop();
display_routine(ch[2]);
data_mode(':');
display_routine(ch[1]);
data_mode(':');
display_routine(ch[0]);
command_mode(0xc0);
data_mode1(ch[3]);
command_mode(0xc7);
display_routine(ch[4]);
data_mode('-');
display_routine(ch[5]);
data_mode('-');
display_routine(ch[6]);
//}
return 0;
}

void data_write(unsigned char a)
{
unsigned char i=0,x=0;
for(i=0;i<8;i++)
{
if(a&0x80)
{
sda=1;
}
else
{
sda=0;
}
scl=1;
_nop_();
_nop_();
scl=0;
a=a<<1;
}
}

unsigned char data_read()
{
unsigned char a=0,b=0,c=0,i;
sda=1;
for(i=0;i<8;i++)
{
a<<=1;
if(sda)
{
a=a|0x01;
}
scl=1;
_nop_();
_nop_();
//_nop_();
scl=0;
}
return a;
}
void slave_address(unsigned char a)
{
data_write(a);
}
void ack()
{
sda=0;
scl=1;
_nop_();
_nop_();
//_nop_();
scl=0;
if(sda==0)
{
;
}
else
{
;
}
}

void m_ack()
{
sda=0;
scl=1;
_nop_();
_nop_();
//_nop_();
scl=0;
}
void m_nack()
{
sda=1;
scl=1;
_nop_();
_nop_();
//_nop_();
scl=0;
}
void i2c_start()
{
sda=1;
scl=1;
_nop_();
_nop_();
//_nop_();
sda=0;
scl=0;
}

void i2c_stop()
{
sda=0;
scl=1;
_nop_();
_nop_();
sda=1;
scl=0;
}

void command_mode(unsigned char a)
{
//delay();
busy();
P2=a;
rs=0;
rw=0;
en=1;
_nop_();
_nop_();
en=0;
}

void data_mode(unsigned char a)
{
busy();
P2=a;
data_init();
}

void data_mode1(unsigned char p)
{
unsigned char j=0;
busy();
p=p-1;
while(ch1[p][j]!='\0')
{
P2=ch1[p][j];
data_init();
j++;
}
}
void data_init()
{
rs=1;
rw=0;
en=1;
_nop_();
_nop_();
en=0;
}

void lcd_init()
{
command_mode(0x01);
command_mode(0x38);
command_mode(0x06);	
command_mode(0x0c);
command_mode(0x80);
}

void busy()
{
en1=1;
rs=0;
rw=1;
while(en1)
{
en=0;
_nop_();
_nop_();
en=1;
}
}
void display_routine(unsigned char ch)
{
unsigned char b=0,c=0;
b=ch&0x0f;
b=b|0x30;
c=ch>>4;
c=c|0x30;
data_mode(c);
data_mode(b);
}

Compiles perfect, but no display.
Any ideas?
 
Last edited:

In this code I'm trying to determine ...
The question sounds pretty usesless (as well as the old post preceeding it) I think.

Any usefull code would refer to a specific known hardware.
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top