本文包含原理图、PCB、源代码、封装库、中英文PDF等资源
您需要 登录 才可以下载或查看,没有账号?注册会员
×
#include<reg52.h>
#define uchar unsigned char
#define uint unsigned int
uchar code table1[]={"2009-00-00 sat"} ;
uchar code table2[]={ " 20:00:00"};
sbit lcdrs=P3^0;
sbit lcdrw=P3^1;
sbit lcden=P3^2;
sbit ACC0=ACC^0;
sbit ACC7=ACC^7;
sbit dsio=P2^5; //
sbit dsclk=P2^4; //
sbit dsrst=P2^6; //
sbit set=P1^0; // 时间设置
sbit up=P1^1; // 上
sbit down=P1^2; // 下
void write_sfm(uchar add,uchar shu);
uchar shi,fen,miao,ri,yue,nian;
void keyscan();
void delay(uchar z) //延时
{
uchar x,y;
for(x=200;x>0;x--)
for(y=z;y>0;y--);
}
/***********往DS1302写入一字节***************/
void inputbyte(uchar add)
{
uchar i;
ACC=add;
for(i=8;i>0;i--)
{
dsio=ACC0;
dsclk=1;
dsclk=0;
ACC=ACC>>1;
}
}
/**********ds1302输出一字节****************/
uchar outputbyte(void)
{
uchar i;
for(i=8;i>0;i--)
{
ACC=ACC>>1;
ACC7=dsio;
dsclk=1;
dsclk=0;
}
return ACC;
}
/*********往ds1302写入数据*****************/
void write_ds(uchar add,uchar ucda)
{
dsrst=0;
dsclk=0;
dsrst=1;
inputbyte(add);
inputbyte(ucda);
dsclk=1;
dsrst=0;
}
/*********从DS1302读出数据*****************/
uchar read_ds(uchar add)
{
uchar ucda;
dsrst=0;
dsclk=0;
dsrst=1;
inputbyte(add);
ucda=outputbyte();
dsclk=1;
dsrst=0;
return (ucda);
}
/*********往1602液晶写入指令*****************/
void write_com(uchar com)
{
lcdrs=0;
lcdrw=0;
delay(1);
P0=com;
delay(1);
lcden=1;
delay(1);
lcden=0;
}
/********往1602液晶写入数据******************/
void write_date(uchar date1)
{
lcdrs=1;
lcdrw=0;
delay(1);
P0=date1;
delay(1);
lcden=1;
delay(1);
lcden=0;
}
/**********设置DS1302****************/
void write_setds(uchar addr,uchar date)
{
write_ds(0x8e,0x00);
write_ds(addr,date);
write_ds(0x8e,0x80);
}
/*************初始化*************/
void init()
{
uchar i;
lcden=0;
write_com(0x38);
write_com(0x0c);
write_com(0x06);
write_com(0x01);
write_com(0x80+0x00);
for(i=0;i<14;i++)
{
write_date(table1[i]);
delay(2);
}
write_com(0x80+0x40);
for(i=0;i<10;i++)
{
write_date(table2[i]);
delay(2);
}
}
/************往1602写入日期**************/
void write_nyr(uchar add,uchar date)
{
uchar shi,ge ;
shi=date/10;
ge=date%10;
write_com(0x80+0x00+add);
write_date(0x30+shi);
write_date(0x30+ge);
}
/*********显示函数*****************/
void display()
{
uchar d;
d=read_ds(0x81);
miao=(d/16)*10+(d%16);
write_sfm(8,miao);
d=read_ds(0x83);
fen=(d/16)*10+(d%16);
write_sfm(5,fen);
d=read_ds(0x85);
shi=(d/16)*10+(d%16);
write_sfm(2,shi);
d=read_ds(0x87);
ri=(d/16)*10+(d%16);
write_nyr(8,ri);
d=read_ds(0x89);
yue=(d/16)*10+(d%16);
write_nyr(5,yue);
d=read_ds(0x8d);
nian=(d/16)*10+(d%16);
write_nyr(2,nian);
}
/*********往1602写入时间*****************/
void write_sfm(uchar add,uchar date)
{
uchar shi,ge;
shi=date/10;
ge=date%10;
write_com(0x80+0x40+add);
write_date(0x30+shi);
write_date(0x30+ge);
}
void main()
{
init();
write_setds(0x80,0x00); //设置秒
write_setds(0x82,0x10); //设置分
write_setds(0x84,0x10); //时
write_setds(0x86,0x08) ; // 日
write_setds(0x88,0x09); // 月
while(1)
{
display();
keyscan();
}
}
void keyscan()
{
uchar aa;
if(set==0)
{
delay(3);
if(set==0)
{
while(!set);
aa++;
write_setds(0x80,128|((miao/10)*16+miao%10)) ;
display();
if(aa==1)
{
write_com(0x80+0x40+8);
write_com(0x0f);
write_ds(0x8e,0x00);
display();
}
if(aa==2)
write_com(0x80+0x40+4);
if(aa==3)
write_com(0x80+0x40+2);
if(aa==4)
write_com(0x80+7);
if(aa==5)
write_com(0x80+5);
if(aa==6)
write_com(0x80+2);
if(aa==7)
{
aa=0;
write_com(0x0c);
write_com(0x80+miao);
write_ds(0x8e,0x80);
}
}
}
if(aa!=0)
{
if(up==0)
{
delay(3) ;
if(up==0)
{
while(!up);
if(aa==1)
{
miao++;
}
}
}
}
}
哪里还需要变动一下, |