为什么我程序下载后,1602液晶显示的不是我输进去的数值,而且关掉
电源再开还有存储功能,还从刚才断电的地方继续走。
如图所示:
程序如下:
#include<reg52.h>
#include"LCD1602.h"
#define uchar unsigned char
#define uint unsigned int
sbit TSCLK=P1^0;
sbit
tiO=P1^1;
sbit TEN=P1^2;
uchar Year,Month,Day,Hour,Min,Sec;
void delayms(uint z) //延时函数ms
{
uint x,y;
for(x=z;x>0;x--)
for(y=114;y>0;y--);
}
void Write_1302date(uchar com,uchar dat) //DS1302写数据
{
uchar i;
TEN=0;
TSCLK=0;
TEN=1;
for(i=0;i<8;i--)
{
TSCLK=0;
TIO=com&0x01;
TSCLK=1;
com>>=1;
}
for(i=0;i<8;i--)
{
TSCLK=0;
TIO=dat&0x01;
TSCLK=1;
dat>>=1;
}
}
uchar Read_1302date(uchar com) //DS1302读数据
{
uchar i,dat;
TEN=0;
TSCLK=0;
TEN=1;
for(i=0;i<8;i++)
{
TSCLK=0;
TIO=com&0x01;
TSCLK=1;
com>>=1;
}
for(i=0;i<8;i++)
{
TSCLK=0;
dat>>=1;
if(TIO) dat|=0x80; //如果TIO为高电平写1 否则写0
TSCLK=1;
}
return dat;
}
uchar date_BCD(uchar dat) //数据转BCD
{
uchar dat1,dat2;
dat1=dat/10;
dat2=dat%10;
dat2=dat2+dat1*16;
return dat2;
}
uchar BCD_date(uchar dat) //BCD转数据
{
uchar dat1,dat2;
dat1=dat/16;
dat2=dat%16;
dat2=dat2+dat1*10;
return dat2;
}
void main()
{
init();
Write_1302date(0x8e,0);//关闭写保护
Write_1302date(0x80,date_BCD(30)); //秒
Write_1302date(0x82,date_BCD(15)); //分
Write_1302date(0x84,date_BCD(5)); //时
Write_1302date(0x86,date_BCD(30)); //日
Write_1302date(0x88,date_BCD(9)); //月
Write_1302date(0x8c,date_BCD(17));;//年
Write_1302date(0x8e,0x80);//开启写保护
while(1)
{
Write_1302date(0x8e,0);//关闭写保护
Sec=BCD_date(Read_1302date(0x81));
Min=BCD_date(Read_1302date(0x83));
Hour=BCD_date(Read_1302date(0x85));
Day=BCD_date(Read_1302date(0x87));
Month=BCD_date(Read_1302date(0x89));
Year=BCD_date(Read_1302date(0x8d));
Write_1302date(0x8e,0x80);//开启写保护
ONE_Write_sfm(7,Year);
ONE_Write_sfm(10,Month);
ONE_Write_sfm(13,Day);
TWO_Write_sfm(6,Hour);
TWO_Write_sfm(9,Min);
TWO_Write_sfm(12,Sec);
}
}
1602.h程序如下:
#define uchar unsigned char
#define uint unsigned int
sbit lcden=P3^4; //使能端
sbit lcdrs=P3^5; //数据命令选择端
sbit lcdrw=P3^6; //读写控制端
sbit dula=P2^6;
sbit wela=P2^7;
uchar code table3[]="Date:20 - - ";
uchar code table4[]="Time: - - ";
void delay(uint z) //延时函数ms
{
uint x,y;
for(x=z;x>0;x--)
for(y=114;y>0;y--);
}
void Write_1602com(uchar com) //1602写命令
{
lcdrs=0;
P0=com;
delay(5);
lcden=1;
delay(5);
lcden=0;
}
void Write_1602date(uchar dat)
{
lcdrs=1;
P0=dat;
delay(5);
lcden=1;
delay(5);
lcden=0;
}
void ONE_Write_sfm(uchar add,uchar date)
{
uchar shi,ge;
shi=date/10;
ge=date%10;
Write_1602com(0x80+add);
Write_1602date(0x30+shi);
Write_1602date(0x30+ge);
}
void TWO_Write_sfm(uchar add,uchar date)
{
uchar shi,ge;
shi=date/10;
ge=date%10;
Write_1602com(0x80+0x40+add);
Write_1602date(0x30+shi);
Write_1602date(0x30+ge);
}
void init()
{
uchar num;
dula=0;
wela=0;
lcden=0;
lcdrw=0;
Write_1602com(0x38);
Write_1602com(0x01);
Write_1602com(0x0c);
Write_1602com(0x06);
Write_1602com(0x80);
for(num=0;num<15;num++)
{
Write_1602date(table3[num]);
delay(5);
}
Write_1602com(0x80+0x40);
for(num=0;num<15;num++)
{
Write_1602date(table4[num]);
delay(5);
}
}
-
一周热门 更多>