微波EDA网,见证研发工程师的成长!
首页 > 硬件设计 > 嵌入式设计 > AT89C2051+LCD1602+DS1302实时时钟设计(c51)

AT89C2051+LCD1602+DS1302实时时钟设计(c51)

时间:11-10 来源:互联网 点击:
#include

#include
#define uchar unsigned char
sbit rs=P3^0;//LCD1602控制脚
sbit rw=P3^1;
sbit e=P3^2;

sbit io=P3^4;//DS1302 PCI总线引脚
sbit rst=P3^5;
sbit sclk=P3^3;

uchar hour,minute,second,year,months,date,day;//显示时间寄存器
uchar whour,wminute,wsecond,wyear,wmonths,wdate,wday;//设制初始时间寄存器
uchar code table1[]="0123456789-:w";
uchar code table2[]="Date:20";
uchar code table3[]="Time:";

void delayms(uchar k)//1ms延时
{
uchar j,i;
for(i=0;ifor(j=0;j<120;j++);
}

void delay2us() //2us延时
{
_nop_();
_nop_();
}

void write1602_data(uchar dat)//1602写数据子程序
{
rs=1;
rw=0;
P1=dat;
delayms(2);
e=0;
e=1;
e=0;
}

void write1602_com(uchar com)//1602写命令子程序
//
{
rs=0;
rw=0;
P1=com;
delayms(5);
e=0;
e=1;
e=0;
}

void init1602()//1602初始化子程序
{
write1602_com(0x38);//lcd1602 16*2显示,5*7点阵,8bit数据接口
write1602_com(0x0f);//开显示,显示光标,光标闪烁
write1602_com(0x06);//当写一字符后地址指针加一,光标加一
write1602_com(0x01);//清屏
}

void disptop() //1602第一行显示子程序
{
uchar i;
write1602_com(0x80);
for(i=0;i<0x07;i++)
{
write1602_data(table2[i]);
delayms(1);
}
}

void dispbot() //1602第二行显示子程序
{
uchar i;
write1602_com(0xc0);
for(i=0;i<0x05;i++)
{
write1602_data(table3[i]);
delayms(1);
}
}

/*******************************************************/

void write1302byte(uchar dat)//1302写入一个字节子程序
{
uchar i;
sclk=0;
for(i=0;i<8;i++)
{
io=(bit)(dat&0x01);
delay2us();
sclk=1;
delay2us();
sclk=0;
dat=dat>>1;
}
}

uchar read1302byte() //1302读一个字节子程序
{
uchar i,dat;
for(i=0;i<8;i++)
{
dat>>=1;
if(io)
dat|=0x80;
delay2us();
sclk=1;
delay2us();
sclk=0;
delay2us();
}
return dat;
}

void writeset1302(uchar add,uchar dat)//1302读子女程序
{
rst=0;
delay2us();
rst=1;
write1302byte(add);
write1302byte(dat);
rst=0;
}

uchar readset1302(uchar add)//1302写子程序
{
uchar dat;
rst=0;
delay2us();
rst=1;
write1302byte(add);
dat=read1302byte();
rst=0;
return dat;
}

void init1302()//1302初始化子程序
{
uchar flag;
flag=readset1302(0x81);//判断1302是否有备用电池,若有则FLAG为0
if(flag&0x80)
{
writeset1302(0x8e,0x00);
writeset1302(0x80,((wsecond/10)<4|(wsecond%10)));
writeset1302(0x82,((wminute/10)<4|(wminute%10)));
writeset1302(0x84,((whour/10)<4|(whour%10)));
writeset1302(0x86,((wday/10)<4|(wday%10)));
writeset1302(0x88,((wmonths/10)<4|(wmonths%10)));
writeset1302(0x8a,((wdate/10)<4|(wdate%10)));
writeset1302(0x8c,((wyear/10)<4|(wyear%10)));
writeset1302(0x90,0xa5);
writeset1302(0x8e,0x80);
}
}

uchar readvalue(uchar value) //数据转换
{
uchar a;
a=((value&0x70)>>4)*10+(value&0x0f);
return a;
}

void read1302() //读取时间
{
uchar value;
value=readset1302(0x81);
second=readvalue(value);

value=readset1302(0x83);
minute=readvalue(value);

value=readset1302(0x85);
hour=readvalue(value);

value=readset1302(0x87);
day=readvalue(value);

value=readset1302(0x89);
months=readvalue(value);

value=readset1302(0x8b);
date=readvalue(value);

value=readset1302(0x8d);
year=((value&0xf0)>>4)*10+(value&0x0f);

}

void disptime() //时间显示
{
write1602_com(0x87);
write1602_data(table1[year/10]);
write1602_data(table1[year%10]);
write1602_data(table1[10]);
write1602_data(table1[months/10]);
write1602_data(table1[months%10]);
write1602_data(table1[10]);
write1602_data(table1[day/10]);
write1602_data(table1[day%10]);
write1602_com(0xc5);
write1602_data(table1[hour/10]);
write1602_data(table1[hour%10]);
write1602_data(table1[11]);
write1602_data(table1[minute/10]);
write1602_data(table1[minute%10]);
write1602_data(table1[11]);
write1602_data(table1[second/10]);
write1602_data(table1[second%10]);
write1602_com(0xce);
write1602_data(table1[12]);
write1602_data(table1[date]);
}

void inittime() //初始时间设置
{
whour=23;
wminute=59;
wsecond=20;
wyear=10;
wmonths=12;
wdate=3;
wday=1;
}

void main()
{
inittime();
init1602();
disptop();
dispbot();
init1302();
while(1)
{
read1302();
disptime();
}
}

Copyright © 2017-2020 微波EDA网 版权所有

网站地图

Top