STC89C52与AT24C02按字节写(C51)
#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);
}
STC89C52AT24C02按字节 相关文章:
- Windows CE 进程、线程和内存管理(11-09)
- RedHatLinux新手入门教程(5)(11-12)
- uClinux介绍(11-09)
- openwebmailV1.60安装教学(11-12)
- Linux嵌入式系统开发平台选型探讨(11-09)
- Windows CE 进程、线程和内存管理(二)(11-09)