关于用51控制ds1302和lcd1602显示时间的程序问题,主要问题是在于写入的数据小于8就能正常写入,但是数据大于8的时候就lcd1602就显示是一堆问号!本人入门菜鸟,求高手帮助,一下是程序:#include<reg51.h>
#include<intrins.h>
#define uchar unsigned char
#define uint unsigned int
unsigned char code read_addr[7] = {0x81, 0x83, 0x85, 0x87, 0x89, 0x8b, 0x8d};
unsigned char code write_addr[7] = {0x80, 0x82, 0x84, 0x86, 0x88, 0x8a, 0x8c};
unsigned char
time[7] = {0x00,0x00, 0x12, 0x01, 0x01, 0x02, 0x13};//设置时间秒分时日月星期年
uint nianshi,niange,yueshi,yuege,rvshi,rvge,shishi,shige,fenshi,fenge,miaoshi,miaoge;
uint miao,fen,shi,rv,yue,nian,timenian,timeyue,timerv,timeshi,timefen,timemiao;
sbit SCLK=P3^6;
sbit RST=P3^5;
sbit DSIO=P3^4;
sbit LCDE=P2^7;
sbit RS=P2^6;
sbit RW=P2^5;
sbit KEY1=P1^1;
sbit KEY2=P1^2;
sbit KEY0=P1^0;
uchar n,flag,num;
void delay(unsigned int z) //延时子函数1
{
unsigned char x,y;
for(x=z;x>0;x--)
{
for(y=20;y>0;y--);
}
}
void delay1(uint z) //延时子函数2
{
uchar x;
for(x=z;x>0;x--);
}
void write_com(unsigned char com)//1602液晶写入命令
{
RS=0;
RW=0;
LCDE=0;
P0=com;
delay(5);
LCDE=1;
delay(5);
LCDE=0;
}
void write_date(unsigned char date)//1602液晶写入数据
{
RS=1;
RW=0;
LCDE=0;
P0=date;
delay(5);
LCDE=1;
delay(5);
LCDE=0;
}
void write(unsigned char addr,unsigned char date) //ds1302写入数据
{
unsigned char i;
RST=0;
_nop_();
SCLK=0;
_nop_();
RST=1;
_nop_();
for(i=0;i<8;i++)
{
DSIO=addr&0x01;
addr=addr>>1;
SCLK=1;
_nop_();
SCLK=0;
_nop_();
}
for(i=0;i<8;i++)
{
DSIO=date;
date>>=1;
SCLK=1;
_nop_();
SCLK=0;
_nop_();
}
RST=0;
_nop_();
}
unsigned char read(unsigned char addr) //ds1302读出数据
{
unsigned char i,dat,dat1;
RST=0;
delay1(2);
SCLK=0;
delay1(2);
RST=1;
delay1(2);
for(i=0;i<8;i++)
{
DSIO=addr&0x01;
addr>>=1;
SCLK=1;
delay1(2);
SCLK=0;
delay1(2);
}
delay1(2);
for(i=0;i<8;i++)
{
dat1=DSIO;
dat=(dat>>1)|(dat1<<7);
SCLK=1;
delay1(2);
SCLK=0;
delay1(2);
}
RST=0;
delay1(2);
SCLK=1;
delay1(2);
SCLK=0;
delay1(2);
DSIO=0;
delay1(2);
DSIO=1;
delay1(2);
return dat;
}
void readtime() // ds1302连续读出时间
{
unsigned char i;
for(i=0;i<7;i++)
{
time[i]= read(read_addr[i]);
}
}
void zhuanhuan() //十六进制转十进制
{
nianshi=time[6]/16;
niange=(time[6]&0x0f);
yueshi=time[4]/16;
yuege=(time[4]&0x0f);
// write_com(0x88);
rvshi=time[3]/16;
rvge=(time[3]&0x0f);
shishi=time[2]/16;
shige=(time[2]&0x0f);
//write_com(0x80+0x40+3);
fenshi=time[1]/16;
fenge=(time[1]&0x0f);
// write_com(0x80+0x40+6);
miaoshi=time[0]/16;
miaoge=(time[0]&0x0f);
}
void zhuanhuan1() //十进制转十六进制
{
time[6]=nianshi*16+niange;
time[4]=yueshi*16+yuege;
time[3]=rvshi*16+rvge;
time[2]=shishi*16+shige;
time[1]=fenshi*16+fenge;
time[0]=miaoshi*16+miaoge;
}
void xianshi() //1602显示时间
{
write_com(0x80+3);
write_date(0x32);
write_date(0x30);
write_date('0'+nianshi);
write_date('0'+niange);
write_date('-');
// write_com(0x85);
write_date('0'+yueshi);
write_date('0'+yuege);
write_date('-');
// write_com(0x88);
write_date('0'+rvshi);
write_date('0'+rvge);
write_com(0x80+0x40+4);
write_date('0'+shishi);
write_date('0'+shige);
write_date('-');
//write_com(0x80+0x40+3);
write_date('0'+fenshi);
write_date('0'+fenge);
write_date('-');
// write_com(0x80+0x40+6);
write_date('0'+miaoshi);
write_date('0'+miaoge);
}
void ds1302init() //ds1302初始化
{
unsigned char i;
write(0x8e,0x00);
for(i=0;i<7;i++)
{
write(write_addr[i],time[i]);
}
write(0x8e,0x80);
}
void init() //1602初始化
{
RW=0;
LCDE=0;
write_com(0x38);
write_com(0x0e);
write_com(0x06);
write_com(0x01);
write_com(0x80);
}
void tiaozhuan() //按键跳转函数
{
if(KEY0==0)
{
delay(5);
if(KEY0==0)
{
n++;
while(!KEY0);
}
}
}
void chuandi1() //时间传递函数1
{
miao=miaoshi*10+miaoge;
fen=fenshi*10+fenge;
shi=shishi*10+shige;
rv=rvshi*10+rvge;
yue=yueshi*10+yuege;
nian=nianshi*10+niange;
}
void chuandi2() //时间传递函数2
{
nianshi=nian/10;
niange=nian%10;
yueshi=yue/10;
yuege=yue%10;
rvshi=rv/10;
rvge=rv%10;
shishi=shi/10;
shige=shi%10;
fenshi=fen/10;
fenge=fen%10;
miaoshi=miao/10;
miaoge=miao%10;
}
void keyscan() //按键修改时间函数
{
if(KEY0==0)
{
delay(5);
if(KEY0==0)
{
n++;
while(!KEY0) ;
if(n==1);
chuandi1();
while(n==1)
{
flag=1;
write_com(0x80+0x40+10);//光标回到秒处
write_com(0x0f); //光标闪烁
tiaozhuan();
if(KEY1==0)
{
delay(5);
if(KEY1==0)
{
while(!KEY1);
miao++;
if(miao==60)
miao=0;
chuandi2();
xianshi();
}
}
if(KEY2==0)
{
delay(5);
if(KEY2==0)
{
while(!KEY2);
miao--;
if(miao==-1)
miao=59;
chuandi2();
xianshi();
}
}
// zhuanhuan1();
// write(0x8e,0x00);
// write(write_addr[0],timemiao);
// write(0x8e,0x80) ;
}
while(n==2)
{
//flag=1;
write_com(0x80+0x40+7);
tiaozhuan();
//chuandi1();
if(KEY1==0)
{
delay(5);
if(KEY1==0)
{
while(!KEY1);
fen++;
if(fen==60)
fen=0;
chuandi2();
xianshi();
}
}
if(KEY2==0)
{
delay(5);
if(KEY2==0)
{
while(!KEY2);
fen--;
if(fen==-1)
fen=59;
chuandi2();
xianshi();
}
}
// zhuanhuan1();
//
// write(0x8e,0x00);
// write(write_addr[1],timefen);
// write(0x8e,0x80) ;
}
while(n==3)
{
//flag=1;
write_com(0x80+0x40+4);
tiaozhuan();
//chuandi1();
if(KEY1==0)
{
delay(5);
if(KEY1==0)
{
while(!KEY1);
shi++;
if(shi==24)
shi=0;
chuandi2();
xianshi();
}
}
if(KEY2==0)
{
delay(5);
if(KEY2==0)
{
while(!KEY2);
shi--;
if(shi==-1)
shi=23;
chuandi2();
xianshi();
// zhuanhuan1();
// write(0x8e,0x00); //写入ds1302数据
// write(write_addr[2],timeshi);
// write(0x8e,0x80) ;
}
}
}
while(n==4)
{
write_com(0x80+11) ;
tiaozhuan();
// chuandi1();
if(KEY1==0)
{
delay(5);
if(KEY1==0)
{
while(!KEY1);
rv++;
if(rv==32)
rv=0;
chuandi2();
xianshi();
}
}
if(KEY2==0)
{
delay(5);
if(KEY2==0)
{
while(!KEY2);
rv--;
if(rv==0)
rv=31;
chuandi2();
xianshi();
}
}
// zhuanhuan1();
// write(0x8e,0x00);
// write(write_addr[3],timerv);
// write(0x8e,0x80) ;
}
while(n==5)
{
write_com(0x80+8);
tiaozhuan(); //chuandi1();
if(KEY1==0)
{
delay(5);
if(KEY1==0)
{
while(!KEY1);
yue++;
if(yue==13)
yue=0;
chuandi2();
xianshi();
}
}
if(KEY2==0)
{
delay(5);
if(KEY2==0)
{
while(!KEY2);
yue--;
if(yue==0)
yue=12;
chuandi2();
xianshi();
}
}
// zhuanhuan1();
// write(0x8e,0x00);
// write(write_addr[4],timeyue);
// write(0x8e,0x80) ;
}
while(n==6)
{
write_com(0x80+5);
//chuandi1();
if(KEY1==0)
{
delay(5);
if(KEY1==0)
{
while(!KEY1);
nian++;
if(nian==101)
nian=0;
chuandi2();
xianshi();
}
}
if(KEY2==0)
{
delay(5);
if(KEY2==0)
{
while(!KEY2);
nian--;
if(nian==-1)
nian=100;
chuandi2();
xianshi();
}
}
// zhuanhuan1();
// write(0x8e,0x00);
// write(write_addr[6],timenian);
// write(0x8e,0x80) ;
if(KEY0==0)
{
delay(5);
if(KEY0==0)
{
n=0;
flag=0;
write_com(0x0c) ;
while(!KEY0);
}
}
}
zhuanhuan1();
ds1302init();
}
}
}
void main() //主函数
{
init();
ds1302init();
while(1)
{
keyscan();
readtime();
if(flag==0) //有按键按下时液晶显示停止在最后显示的时间
{
zhuanhuan();
}
xianshi();
}
}
一周热门 更多>