AT24C02開機記憶
/*
程序功能:記憶開機的次數(shù),并顯示在數(shù)碼管上
*/
//************************************
#includereg52.h>
#define uint unsigned int
#define uchar unsigned char
uchar code num[]={0x80,0xed,0x42,0x48,0x2c,0x18,0x10,0xcc,0x00,0x08};
//****管腳定義****
sbit scl=P0^3;//艾方西的數(shù)據(jù)線
sbit sda=P0^4;//艾方西的時鐘線
sbit rck=P2^0;
sbit sck=P2^1;
sbit si=P2^2; //數(shù)碼管的串行數(shù)據(jù)輸入端
sbit key1=P1^0;
//====函數(shù)的聲明=====
void delayus();//微秒級延時函數(shù)
void delayms();//毫秒級延時函數(shù)
void start();//起始函數(shù)
void stop();//終止函數(shù)
void ack();//應(yīng)答函數(shù)
void noack();//非應(yīng)答函數(shù)
void write_byte(uchar date);//寫字節(jié)函數(shù)
uchar read_byte();//讀字節(jié)函數(shù)
void xie(uchar add,uchar date);//寫操作函數(shù)
uchar du(uchar add);//讀操作函數(shù)
void write_595(uchar date);//數(shù)碼管
void smg_595(uint j,uint i);
//************************************
void delayus()
{
; ; ;
}
/*void delayms(uint time)
{
uint i,j;
for(i=time;i>0;i--)
for(j=110;j>0;j--);
} */
void start()
{
sda=1;
delayus();
scl=1;//時鐘線在高電平期間,數(shù)據(jù)線由高到底的跳變表示起始
delayus();
sda=0;
delayus();
}
void stop()
{
sda=0;
delayus();
scl=1; //時鐘線在高電平期間,數(shù)據(jù)線由底到高的跳變表示終止
delayus();
sda=1;
delayus();
}
void ack()
{
uint i=0;
scl=1;
if((sda==1)(i250))i++;
scl=0;
delayus();
}
void noack()
{
sda=1;
delayus();
scl=0;
delayus();
scl=1;
delayus();
scl=0;
}
void write_byte(uchar date)
{
uint i;
scl=0;
for(i=0;i8;i++)
{
if(date0x80)
{
sda=1;
}
else
{
sda=0;
}
scl=1;
date=date1;
delayus();
scl=0;
sda=1;
delayus();
}
sda=1;
delayus();
}
uchar read_byte()
{
uint i;
uchar date=0x00;
sda=1;
delayus();
scl=0;
delayus();
for(i=0;i8;i++)
{
scl=1;
delayus();
date=date1;
if(sda)
{
date++;
}
scl=0;
delayus();
}
return date;
}
void xie(uchar add,uchar date)
{
start();
write_byte(0xa0);
ack();
write_byte(add);
ack();
write_byte(date);
ack();
stop();
}
uchar du(uchar add)
{
uchar k;
start();
write_byte(0xa0);
ack();
write_byte(add);
ack();
start();
write_byte(0xa1);
ack();
k=read_byte();
noack();
stop();
return k;
}
void chushihua()
{
sda=1;
scl=1;
}
void main()
{
uchar k=0;
k=du(7);
k=k%10;
smg_595(k,k);
k++;
xie(7,k);
while(1);
}
void smg_595(uint j,uint i)
{
write_595(num[i]);
write_595(num[j]);
rck=0;
rck=1;
}
void write_595(uchar date)
{
uint i;
for(i=0;i8;i++)
{
date>>=1;
sck=0;
si=CY;
sck=1;
}
}
評論