新聞中心

EEPW首頁 > 嵌入式系統(tǒng) > 設(shè)計(jì)應(yīng)用 > 51單片機(jī)IO口模擬驅(qū)動(dòng)AT24c02

51單片機(jī)IO口模擬驅(qū)動(dòng)AT24c02

作者: 時(shí)間:2016-11-23 來源:網(wǎng)絡(luò) 收藏
#include

#include

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

void somenop()
{
uchar i=5;
while(i--)
{
_nop_();
}
}

//8位掃描顯示,共陽數(shù)碼管
uchar shum[]={
0xC0,0xF9,0xA4,0xB0,0x99,0x92,0x82,0xF8,0x80,0x90};
void delaym()
{
uchar i;
for(i=0;i<0xff;i++);

}

void init();
void star();
void stop();
void ack();
void write_byte(uchar date);
uchar read_byte();
sbit scl=P1^2;
sbit sda=P1^3;
uchar sec,min=30,tt;

void init()
{
sda=1;
somenop();
scl=1;
somenop();

}

void star()
{
sda=1;
somenop();
scl=1;
somenop();
sda=0;
somenop();
}
void stop()
{
sda=0;
somenop();
scl=1;
somenop();
sda=1;
somenop();
}
void ack()
{
scl=1;
somenop();

scl=0;
somenop();
}
void write_byte(uchar date)
{
uchar i,temp;
temp=date;
for(i=0;i<8;i++)
{
scl=0;
somenop();

if((temp&0x80)==0x80)
{sda=1;}
else
{ sda=0;}
temp=temp<<1;
somenop();
scl=1;
somenop();
scl=0;
somenop();
}
scl=0;
somenop();
sda=1;
somenop();
}
uchar read_byte()
{
uchar i,temp1,j;
scl=0;
somenop();
sda=1;
somenop();
for(i=0;i<8;i++)
{
scl=1;
somenop();
j=sda;
temp1=(temp1<<1)|j;
scl=0;
somenop();
}
return temp1;
}


void write_address(uchar address,uchar date)
{
star();
write_byte(0xa0);
ack();
write_byte(address);
ack();
write_byte(date);
ack();
stop();
}
uchar read_address(uchar address)
{
uchar redata;
star();
write_byte(0xa0);
ack();
write_byte(address);
ack();
star();
write_byte(0xa1);
ack();
redata=read_byte();
stop();
return redata;
}


void timer0() interrupt 1
{
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
tt++;
if(tt==20)
{
tt=0;
sec++;
if(sec==60)
{sec=0;min++;

if(min==60)
{ min=0;}
write_address(100,min); delaym();
}
write_address(10,sec); delaym(); //把秒寫入AT24C02的10地址
//單片機(jī)寫數(shù)據(jù)會(huì)占用一些時(shí)間,所以延時(shí)一會(huì)兒較好

}
}

void int0(void) interrupt 0
{
EA=0;
min++;
if(min==60)
{
min=0;
}
write_address(100,min); //把分寫入AT24C02的100地址
delaym();
EA=1;
}


void disp()
{
P0=shum[sec%10];//sec 個(gè)位
P2=0x08;
delaym();

P0=shum[sec/10];//sec 十位
P2=0x04;
delaym();

P0=shum[min%10]&0x7f;//加小數(shù)點(diǎn)
P2=0x02;
delaym();

P0=shum[min/10];
P2=0x01;
delaym();
}

void main()
{
TMOD=0x01;
ET0=1;
EX0=1;
IT0=1;
EA=1;
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
TR0=1;
init();
sec=read_address(10);//從AT24C02的10地址讀出秒數(shù)據(jù)
min=read_address(100);  ?。瘡模粒裕玻矗茫埃驳模保埃暗刂纷x出分?jǐn)?shù)據(jù)

while(1)
{
disp();
}
}




評論


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

關(guān)閉