您的位置 首页 基础

经过I2C芯片EEPRON24C02操控P1口

通过芯片AT24C02控制P1口。程序:includereg52h>sbitsda=P2^1;sbitscl=P2^0;voiddelay1(){;;;}voiddelay(){intx,y;f

经过芯片AT24C02操控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,0×55);
delay();
P1=read_add(3);
while(1);
}

声明:本文内容来自网络转载或用户投稿,文章版权归原作者和原出处所有。文中观点,不代表本站立场。若有侵权请联系本站删除(kf@86ic.com)https://www.86ic.net/zhishi/jichu/318113.html

为您推荐

联系我们

联系我们

在线咨询: QQ交谈

邮箱: kf@86ic.com

关注微信
微信扫一扫关注我们

微信扫一扫关注我们

返回顶部