帮个忙!着急啊!
时间:10-02
整理:3721RD
点击:
看下小弟编的程序哪里有错,怎么显示不出来!用DS1302+1602液晶显示时间!
#include <reg52.h>
#define uchar unsigned char
#define uint unsigned int
sbit SCLK = P1^0;
sbit DATA = P1^1;
sbit RST = P1^2;
sbit lcden=P3^4;
sbit rs=P3^5;
sbit dula=P2^6;
sbit wela=P2^7;
uint shi,fen,miao,nian,yue,ri,xq;
uchar code table[]=" - - ";
uchar code table1[]=" - - ";
void write_ml(uchar com)//1602写命令
{
rs=0;
lcden=0;
delay(5);
lcden=1;
P0=com;
delay(5);
lcden=0;
}
void write_data(uchar date)//1602写数据
{
rs=1;
lcden=0;
delay(5);
lcden=1;
P0=date;
delay(5);
lcden=0;
}
void 1602init()//1602初始化
{
dula=0;
wela=0;
lcden=0;
write_ml(0x38);//16*2显示,5*7点阵 ,8位数据接口
write_ml(0x0c);//开显示,不显示光标
write_ml(0x06);//读或写一个字节光标左移指针加一
write_ml(0x01);//显示清0,数据指针清0
write_ml(0x80);
for(i=0;i<=15;i++)
{
write_data(table[i]);
delayms(5);
}
write_ml(0x80+0x40);
for(i=0;i<=15;i++)
{
write_data(table1[i]);
delayms(5);
}
}
void delayms(uint x)//延时X微秒
{
uchar i,j;
for(i=x;i>0;i--)
for(j=110;j>0;j--);
}
void delayus(uchar n)//延时 n纳秒
{
uchar i;
for(i=0;i<n;i++);
}
/*********往DS1302写入1Byte数据**************/
void write1302(uchar dat)
{
uchar i;
SCLK=0;
delayus(2);
for(i=0; i<8; i++)
{
DATA=dat&0x01;
delayus(2);
SCLK=1;
delayus(2);
SCLK=0;
dat=dat>>1;
}
}
/***往DS1302写入数据,先写地址,后写命令/数据*******/kkkkkk
void writeset1302(uchar ucAddr, uchar ucDa)
{
RST = 0;
SCLK = 0;
RST = 1;
delayus(2);
write1302(ucAddr); /* 地址,命令 */
write1302(ucDa); /* 写1Byte数据*/
SCLK = 1;
RST =0;
}
/**********从DS1302读取1Byte数据********************/
uchar read1302(void)
{
uchar i,dat;
delayus(2);
for(i=0;i<8;i++)
{
dat>>=1;
if(DATA==1)
dat|=0x80;
SCLK=1;
delayus(2);
SCLK=0;
delayus(2);
}
return(dat);
}
/*****读取DS1302某地址的数据,先写地址,后读命令/数据*********/
uchar readset1302(uchar ucAddr)
{
uchar ucDa;
RST = 0;
SCLK = 0;
RST = 1;
write1302(ucAddr); /* 地址,命令 */
ucDa = read1302(); /* 读1Byte数据 */
SCLK = 1;
RST = 0;
return ucDa;
}
/***************初始化DS1302*******************/
void init1302()//12-12-30-7-23-59-55
{
writeset1302(0x8E,0x00);
writeset1302(0x80,((55/10)<<4|(55%10)));
writeset1302(0x82,((59/10)<<4|(59%10)));
writeset1302(0x84,((23/10)<<4|(23%10)));
writeset1302(0x8A,((7/10)<<4|(7%10)));
writeset1302(0x86,((30/10)<<4|(30%10)));
writeset1302(0x88,((12/10)<<4|(12%10)));
writeset1302(0x8C,((12/10)<<4|(12%10)));
writeset1302(0x90,0xa5);
writeset1302(0x8E,0x80);
}
void 1602dis(uchar wz,uchar sj)//1602显示读回来的数据
{
write_ml(wz);
delay(5);
write_data(sj);
delay(5);
}
void 1302deal()//处理1302读回来的数据并显示到1602
{
uchar k;
k=readset1302(0x81);
miao=(((k&0x70)>>4)*10+(k&0x0f));
1602dis(0x80+0x40+9,miao);
k=readset1302(0x83);
fen=(((k&0x70)>>4)*10+(k&0x0f));
1602dis(0x80+0x40+6,fen);
k=readset1302(0x85);
shi=(((k&0x70)>>4)*10+(k&0x0f));
1602dis(0x80+0x40+3,shi);
k=readset1302(0x87);
ri=(((k&0x70)>>4)*10+(k&0x0f));
1602dis(0x80+9,ri);
k=readset1302(0x89);
yue=(((k&0x70)>>4)*10+(k&0x0f));
1602dis(0x80+6,yue);
k=readset1302(0x8D);
nian=(((k&0xf0)>>4)*10+(k&0x0f));
1602dis(0x80+3,nian);
k=readset1302(0x8B);
xq=(((k&0x70)>>4)*10+(k&0x0f));
1602dis(0x80+13,xq);
}
void main()
{
init1302();
1602init();
while(1)
{
1302deal();
}
}
#include <reg52.h>
#define uchar unsigned char
#define uint unsigned int
sbit SCLK = P1^0;
sbit DATA = P1^1;
sbit RST = P1^2;
sbit lcden=P3^4;
sbit rs=P3^5;
sbit dula=P2^6;
sbit wela=P2^7;
uint shi,fen,miao,nian,yue,ri,xq;
uchar code table[]=" - - ";
uchar code table1[]=" - - ";
void write_ml(uchar com)//1602写命令
{
rs=0;
lcden=0;
delay(5);
lcden=1;
P0=com;
delay(5);
lcden=0;
}
void write_data(uchar date)//1602写数据
{
rs=1;
lcden=0;
delay(5);
lcden=1;
P0=date;
delay(5);
lcden=0;
}
void 1602init()//1602初始化
{
dula=0;
wela=0;
lcden=0;
write_ml(0x38);//16*2显示,5*7点阵 ,8位数据接口
write_ml(0x0c);//开显示,不显示光标
write_ml(0x06);//读或写一个字节光标左移指针加一
write_ml(0x01);//显示清0,数据指针清0
write_ml(0x80);
for(i=0;i<=15;i++)
{
write_data(table[i]);
delayms(5);
}
write_ml(0x80+0x40);
for(i=0;i<=15;i++)
{
write_data(table1[i]);
delayms(5);
}
}
void delayms(uint x)//延时X微秒
{
uchar i,j;
for(i=x;i>0;i--)
for(j=110;j>0;j--);
}
void delayus(uchar n)//延时 n纳秒
{
uchar i;
for(i=0;i<n;i++);
}
/*********往DS1302写入1Byte数据**************/
void write1302(uchar dat)
{
uchar i;
SCLK=0;
delayus(2);
for(i=0; i<8; i++)
{
DATA=dat&0x01;
delayus(2);
SCLK=1;
delayus(2);
SCLK=0;
dat=dat>>1;
}
}
/***往DS1302写入数据,先写地址,后写命令/数据*******/kkkkkk
void writeset1302(uchar ucAddr, uchar ucDa)
{
RST = 0;
SCLK = 0;
RST = 1;
delayus(2);
write1302(ucAddr); /* 地址,命令 */
write1302(ucDa); /* 写1Byte数据*/
SCLK = 1;
RST =0;
}
/**********从DS1302读取1Byte数据********************/
uchar read1302(void)
{
uchar i,dat;
delayus(2);
for(i=0;i<8;i++)
{
dat>>=1;
if(DATA==1)
dat|=0x80;
SCLK=1;
delayus(2);
SCLK=0;
delayus(2);
}
return(dat);
}
/*****读取DS1302某地址的数据,先写地址,后读命令/数据*********/
uchar readset1302(uchar ucAddr)
{
uchar ucDa;
RST = 0;
SCLK = 0;
RST = 1;
write1302(ucAddr); /* 地址,命令 */
ucDa = read1302(); /* 读1Byte数据 */
SCLK = 1;
RST = 0;
return ucDa;
}
/***************初始化DS1302*******************/
void init1302()//12-12-30-7-23-59-55
{
writeset1302(0x8E,0x00);
writeset1302(0x80,((55/10)<<4|(55%10)));
writeset1302(0x82,((59/10)<<4|(59%10)));
writeset1302(0x84,((23/10)<<4|(23%10)));
writeset1302(0x8A,((7/10)<<4|(7%10)));
writeset1302(0x86,((30/10)<<4|(30%10)));
writeset1302(0x88,((12/10)<<4|(12%10)));
writeset1302(0x8C,((12/10)<<4|(12%10)));
writeset1302(0x90,0xa5);
writeset1302(0x8E,0x80);
}
void 1602dis(uchar wz,uchar sj)//1602显示读回来的数据
{
write_ml(wz);
delay(5);
write_data(sj);
delay(5);
}
void 1302deal()//处理1302读回来的数据并显示到1602
{
uchar k;
k=readset1302(0x81);
miao=(((k&0x70)>>4)*10+(k&0x0f));
1602dis(0x80+0x40+9,miao);
k=readset1302(0x83);
fen=(((k&0x70)>>4)*10+(k&0x0f));
1602dis(0x80+0x40+6,fen);
k=readset1302(0x85);
shi=(((k&0x70)>>4)*10+(k&0x0f));
1602dis(0x80+0x40+3,shi);
k=readset1302(0x87);
ri=(((k&0x70)>>4)*10+(k&0x0f));
1602dis(0x80+9,ri);
k=readset1302(0x89);
yue=(((k&0x70)>>4)*10+(k&0x0f));
1602dis(0x80+6,yue);
k=readset1302(0x8D);
nian=(((k&0xf0)>>4)*10+(k&0x0f));
1602dis(0x80+3,nian);
k=readset1302(0x8B);
xq=(((k&0x70)>>4)*10+(k&0x0f));
1602dis(0x80+13,xq);
}
void main()
{
init1302();
1602init();
while(1)
{
1302deal();
}
}
要学会调试和debug,先写一段1602的测试代码,然后把其他的一个一个加进去
分开来,看到底是显示问题还是时钟芯片的问题。程序自己慢慢调
模块的验证,先验证显示有无问题,再验证时间有无错误,最后二者结合起来调试。