新聞中心

EEPW首頁 > 嵌入式系統(tǒng) > 設(shè)計(jì)應(yīng)用 > 通過I2C芯片EEPRON24C02控制P1口

通過I2C芯片EEPRON24C02控制P1口

作者: 時(shí)間:2016-11-30 來源:網(wǎng)絡(luò) 收藏
通過芯片AT24C02控制P1口。程序:

#include
sbit sda=P2^1;
sbit scl=P2^0;
void delay1()
{
;;;
}

本文引用地址:http://m.butianyuan.cn/article/201611/323748.htm

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);
}



關(guān)鍵詞: I2C芯片EEPRON24C0

評(píng)論


技術(shù)專區(qū)

關(guān)閉