ds1302+lcd1602程序.docx
《ds1302+lcd1602程序.docx》由会员分享,可在线阅读,更多相关《ds1302+lcd1602程序.docx(12页珍藏版)》请在冰豆网上搜索。
ds1302+lcd1602程序
#include
#include
#defineuintunsignedint
#defineucharunsignedchar
/*****************************
1602液晶显示位定义
*****************************/
#definelcd_dataP0
sbitrs=P1^7;
sbitrw=P1^6;
sbiten=P2^4;
voidwrite_1602_cmd(uint);
voidwrite_1602_dat(uint);
voiddelay1ms(uint);
voidlcd1602_init();
ucharnyx[16]="2011-00-00MON";
ucharsfm[13]="00:
00:
00";
/******************************
ds1302的位定义
******************************/
sbitrst=P2^2;
sbitsclk=P1^4;
sbitio=P1^5;
voidwrite_ds1302_byte(uint);
voidlcd_busy_wait();
voidwrite_ds1302(uint,uint);
uintread_ds1302(uint);
voidinit_time();
voidread_time();
voidxieshijian2(uintadd,uintshu);
voidxieshijian1(uintadd,uintshu);
voidxiexingqi(uintadd,uintshu);
voidtime_bcd(uintx);
uinttime_wadd[]={0x8c,0x88,0x86,0x8a,0x84,0x82,0x80};
uinttime_radd[]={0x8d,0x89,0x87,0x8b,0x85,0x83,0x81};
uinttime_init[7]={11,0,0,1,0,0,0};
uinttime_shuzhi[7]={0,0,0,0,0,0,0};
ucharxingqi[7][3]={{"MON"},{"TUS"},{"WEN"},{"THU"},{"FRI"},{"STA"},{"SUN"}};
//按键定义
sbitkey1=P1^0;
sbitkey2=P1^1;
sbitkey3=P1^2;
//标志位定义
voidkeyscan();
uintgn_flag;
//程序开始
voidmain()
{
uintnum;
lcd1602_init();
init_time();
write_1602_cmd(0x80);
for(num=0;num<16;num++)
{
write_1602_dat(nyx[num]);
}
write_1602_cmd(0x80+0x40);
for(num=0;num<13;num++)
{
write_1602_dat(sfm[num]);
}
while
(1)
{
keyscan();
if(gn_flag==0)
{
read_time();
xieshijian2(10,time_shuzhi[6]);
xieshijian2(7,time_shuzhi[5]);
xieshijian2(4,time_shuzhi[4]);
xiexingqi(12,time_shuzhi[3]);
xieshijian1(9,time_shuzhi[2]);
xieshijian1(6,time_shuzhi[1]);
xieshijian1(3,time_shuzhi[0]);
}
}
}
/************************
1602读写时序操作
************************/
voidwrite_1602_cmd(uintcmd)
{
lcd_busy_wait();
en=0;
rs=0;
rw=0;
lcd_data=cmd;
_nop_();
en=1;
_nop_();
en=0;
}
voidwrite_1602_dat(uintdat)
{
lcd_busy_wait();
en=0;
rs=1;
rw=0;
lcd_data=dat;
_nop_();
en=1;
_nop_();
en=0;
}
voidlcd1602_init()
{
write_1602_cmd(0x38);
delay1ms(4);
write_1602_cmd(0x0c);
write_1602_cmd(0x06);
write_1602_cmd(0x01);
lcd_data=0xff;
delay1ms(4);
}
voidlcd_busy_wait()
{
rs=0;
rw=1;
en=1;
lcd_data=0xff;
while(lcd_data&0x80);
en=0;
}
voiddelay1ms(uintx)
{
uinti,j;
for(i=x;i>0;i--)
for(j=110;j>0;j--);
}
/*****************************
ds1302的时序操作
*****************************/
voidwrite_ds1302_byte(uintdate)
{
uinti;
for(i=0;i<8;i++)
{
sclk=0;
io=date&0x01;
date>>=1;
sclk=1;
}
}
voidwrite_ds1302(uintcmd,uintdate)
{
rst=0;
_nop_();
sclk=0;
_nop_();
rst=1;
_nop_();
write_ds1302_byte(cmd);
write_ds1302_byte(date);
rst=0;
_nop_();
io=1;
sclk=1;
}
uintread_ds1302(uintadd)
{
uintshu,i;
rst=0;
_nop_();
sclk=0;
_nop_();
rst=1;
_nop_();
write_ds1302_byte(add);
for(i=0;i<8;i++)
{
shu>>=1;
sclk=0;
if(io)shu|=0x80;
sclk=1;
}
rst=0;
_nop_();
io=1;
sclk=1;
returnshu;
}
voidinit_time()
{
uinti,a,b;
for(i=0;i<7;i++)
{
a=time_init[i]/10;
b=time_init[i]%10;
time_init[i]=a*16+b;
}
write_ds1302(0x8e,0x00);
for(i=0;i<7;i++)
{
write_ds1302(time_wadd[i],time_init[i]);
}
write_ds1302(0x8e,0x80);
}
voidread_time()
{
uinti;
for(i=0;i<7;i++)
{
time_shuzhi[i]=read_ds1302(time_radd[i]);
}
}
voidxieshijian2(uintadd,uintshu)
{
uinta,b;
a=shu/16;
b=shu%16;
write_1602_cmd(0x80+0x40+add);
write_1602_dat(0x30+a);
write_1602_dat(0x30+b);
}
voidxieshijian1(uintadd,uintshu)
{
uinta,b;
a=shu/16;
b=shu%16;
write_1602_cmd(0x80+add);
write_1602_dat(0x30+a);
write_1602_dat(0x30+b);
}
voidxiexingqi(uintadd,uintshu)
{
uinti;
write_1602_cmd(0x80+add);
for(i=0;i<3;i++)
{
write_1602_dat(xingqi[shu-1][i]);
}
}
voidtime_bcd(uintx)
{
uinta,b;
a=time_shuzhi[x]/16;
b=time_shuzhi[x]%16;
if(b==0x0a)
{
a++;
b=0;
}
time_shuzhi[x]=a*16+b;
}
/********************************
按键设置时间
********************************/
voidkeyscan()
{
uinti;
if(key1==0)
{
delay1ms(5);
if(key1==0)
{
while(!
key1);
gn_flag++;
if(gn_flag==8)
{
gn_flag=0;
write_1602_cmd(0x0c);
write_ds1302(0x8e,0x00);
for(i=0;i<7;i++)
{
write_ds1302(time_wadd[i],time_shuzhi[i]);
}
write_ds1302(0x8e,0x10);
}
switch(gn_flag)
{
case1:
write_1602_cmd(0x80+0x40+11);
write_1602_cmd(0x0f);break;
case2:
write_1602_cmd(0x80+0x40+8);break;
case3:
write_1602_cmd(0x80+0x40+5);break;
case4:
write_1602_cmd(0x80+14);break;
case5:
write_1602_cmd(0x80+10);break;
case6:
write_1602_cmd(0x80+7);break;
case7:
write_1602_cmd(0x80+4);break;
default:
break;
}
}
}
if(key2==0)
{
delay1ms(5);
if(key2==0)
{
while(!
key2);
switch(gn_flag)
{
case1:
time_shuzhi[6]++;
time_bcd(6);
if(time_shuzhi[6]==0x59)time_shuzhi[6]=0;
xieshijian2(10,time_shuzhi[6]);
write_1602_cmd(0x80+0x40+11);
break;
case2:
time_shuzhi[5]++;
time_bcd(5);
if(time_shuzhi[5]==0x59)time_shuzhi[5]=0;
xieshijian2(7,time_shuzhi[5]);
write_1602_cmd(0x80+0x40+8);
break;
case3:
time_shuzhi[4]++;
time_bcd(4);
if(time_shuzhi[4]==0x12)time_shuzhi[4]=0;
xieshijian2(4,time_shuzhi[4]);
write_1602_cmd(0x80+0x40+5);
break;
case4:
time_shuzhi[3]++;
time_bcd(3);
if(time_shuzhi[3]==0x8)time_shuzhi[3]=1;
xiexingqi(12,time_shuzhi[3]);
write_1602_cmd(0x80+14);
break;
case5:
time_shuzhi[2]++;
time_bcd
(2);
if(time_shuzhi[2]==0x30)time_shuzhi[2]=0;
xieshijian1(9,time_shuzhi[2]);
write_1602_cmd(0x80+10);
break;
case6:
time_shuzhi[1]++;
time_bcd
(1);
if(time_shuzhi[1]==0x13)time_shuzhi[1]=1;
xieshijian1(6,time_shuzhi[1]);
write_1602_cmd(0x80+7);
break;
case7:
time_shuzhi[0]++;
time_bcd(0);
if(time_shuzhi[6]==0x50)time_shuzhi[6]=0;
xieshijian1(3,time_shuzhi[0]);
write_1602_cmd(0x80+4);
break;
default:
break;
}
}
}
}