jignesh doshi
Full Member level 2
- Joined
- Aug 11, 2011
- Messages
- 131
- Helped
- 2
- Reputation
- 4
- Reaction score
- 2
- Trophy points
- 1,298
- Activity points
- 2,112
any one has read routine for I2C communication?
I tried to read but i can't success.
My code is as shown in below code section.
I tried to read but i can't success.
My code is as shown in below code section.
Code:
void main()
{
lcd_ini();
lcd_command(0x01);
delay(5);
while(1)
{
start_eprom();
write_eprom(0xA0);
aknowledge();
write_eprom(0x00);
aknowledge();
write_eprom(0xba);
aknowledge();
stop_eprom();
start_eprom();
write_eprom(0xA0);
aknowledge();
write_eprom(0x00);
aknowledge();
start_eprom();
write_eprom(0xA1);
aknowledge();
eprom = read_eprom();
no_aknowledge();
stop_eprom();
lcd();
}
}
void write_eprom(unsigned char value)
{
unsigned int i;
unsigned char send;
send=value;
for(i=0;i<8;i++)
{
eprom_data=send/128;
send=send<<1;
eprom_clk=1;
_nop_();
_nop_();
eprom_clk=0;
}
}
unsigned char read_eprom(void)
{
int i;
unsigned char x;
eprom_data=1;
x=0;
i=0x80;
do
{
eprom_clk=0;
I2C_delay();
eprom_clk=1;
I2C_delay();
if (eprom_data)
x=(x)|i;
i>>=1;
}while(i!=0);
return x;
}
void start_eprom()
{
eprom_data=1;
eprom_clk=1;
_nop_(); //No operation
_nop_();
eprom_data=0;
eprom_clk=0;
}
void stop_eprom()
{
eprom_data=0;
eprom_clk=1;
_nop_();
_nop_();
eprom_data=1;
eprom_clk=0;
}
void aknowledge()
{
eprom_data = 0;
eprom_clk=1;
_nop_();
_nop_();
eprom_clk=0;
eprom_data = 1;
}
void no_aknowledge()
{
eprom_data = 1;
eprom_clk=1;
_nop_();
_nop_();
eprom_clk=0;
eprom_data = 1;
}
void I2C_delay()
{
unsigned char i;
for(i=0; i<0x0f; i++);
}