通過I2C芯片EEPRON24C02控制P1口
#include
sbit sda=P2^1;
sbit scl=P2^0;
void delay1()
{
;;;
}
void delay()
{
int x,y;
for(x=0;x<5;x++)
for(y=0;y<120;y++);
}
void start()
{sda=1;
delay1();
scl=1;
delay1();
sda=0;
delay1();
}
void stop()
{
sda=0;
delay1();
scl=1;
delay1();
sda=1;
delay1();
}
void respons()
{
sda=0;
delay1();
scl=1;
delay1();
scl=0;
delay1();
}
void init()
{
sda=1;
scl=1;
}
void write_byte(unsigned char date)
{
unsigned char temp,i;
temp=date;
scl=0;
delay1();
for(i=0;i<8;i++)
{
temp=temp<<1;
sda=CY;
delay1();
scl=1;
delay1();
scl=0;
delay1();
}
sda=1;
delay1();
}
unsigned char read_byte()
{
unsigned char i,j,k;
scl=0;
delay1();
sda=1;
delay1();
for(i=0;i<8;i++)
{
scl=1;
delay1();
j=sda;
k=(k<<1)|j;
scl=0;
delay1();
}
return k;
}
void write_add(unsigned char addres,unsigned char date)
{
start();
write_byte(0xae);
respons();
write_byte(addres);
respons();
write_byte(date);
respons();
stop();
}
unsigned char read_add(unsigned char address)
{unsigned char date;
start();
write_byte(0xae);
respons();
write_byte(address);
respons();
start();
write_byte(0xaf);
respons();
date=read_byte();
stop();
return date;
}
void main()
{
init();
write_add(3,0x55);
delay();
P1=read_add(3);
while(1);
}
評論