您的位置 首页 新品

STC89C52与AT24C02按字节写(C51)

#includereg52.h>#includeintrins.h>#defineucharunsignedcharuchari,a;ucharcodetable[]={0xC0,0xF9

#include

#include
#define uchar unsigned char
uchar i,a;
uchar code table[]={0xC0,0xF9,0xA4,0xB0,0x99,0x92,0x82,0xF8};
sbit wp=P1^0;
sbit scl=P1^1;
sbit sda=P1^2;
sbit led=P2^0;

void delay()
{
_nop_();
_nop_();
_nop_();
}

void de1s() //差错 0us
{
unsigned char a,b,c;
for(c=167;c>0;c–)
for(b=171;b>0;b–)
for(a=16;a>0;a–);
_nop_(); //if Keil,require use intrins.h
}

void d10ms() //差错 0us
{
unsigned char a,b,c;
for(c=1;c>0;c–)
for(b=38;b>0;b–)
for(a=130;a>0;a–);
}

void write(uchar t)
{
for(i=0;i<8;i++)
{
sda=(bit)(t&0x80); //等价于RLC
scl=1;
scl=0;
t<<=1;
}
sda=1;
scl=1;
delay();
while(sda!=0);
scl=0;
}

void start()
{
sda=1;
scl=1;
delay();
sda=0;
delay();
scl=0;
}

void stop()
{
sda=0;
delay();
scl=1;
delay();
sda=1;
scl=0;
}

void wriic()
{
uchar j;
start();
write(0xae);
write(0x00);
for(j=0;j<8;j++)
{
write(table[j]);
}
stop();
}

uchar read()
{
uchar n,rbyte=0;
for(n=0;n<8;n++)
{
sda=1;
scl=1;
rbyte<<=1;
rbyte|=(uchar)(sda);
scl=0;
}
sda=0;
delay();
scl=1;
delay();
scl=0;
return(rbyte);
}

void reiic()
{
start();
write(0xae);
write(0x00);
start();
write(0xaf);
for(i=0;i<8;i++)
{
P0=read();
de1s();
}
stop();
}

void main()
{
wp=0;
led=0;
SP=0x50;
wriic();
d10ms();
reiic();
while(1);
}

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

为您推荐

联系我们

联系我们

在线咨询: QQ交谈

邮箱: kf@86ic.com

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

微信扫一扫关注我们

返回顶部