您的位置 首页 系统

51单片机IO口模仿驱动AT24c02

includereg51h>includeintrinsh>voidsomenop(){uchari=5;while(i–){_nop_();}}8位扫描显示,共阳数码管ucharshu

#include

#include

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

//8位扫描显现,共阳数码管
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地址
//单片机写数据会占用一些时刻,所以延时一瞬间较好

}
}

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 个位
P2=0x08;
delaym();

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

P0=shum[min%10]&0x7f;//加小数点
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地址读出秒数据
min=read_address(100);   //从AT24C02的100地址读出分数据

while(1)
{
disp();
}
}

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

为您推荐

联系我们

联系我们

在线咨询: QQ交谈

邮箱: kf@86ic.com

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

微信扫一扫关注我们

返回顶部