拟用DS1302+LCD1602实现电子时钟,实现显示时间功能,下一步准备增加整点报时(蜂鸣器),闹钟功能。但第一步功能实现出现问题
以下程序根据资料提供的完整代码,手打入keil,编译生成hex文件,放在proteus仿真。
问题概述:程序编译没有报错和报警,也可以仿真,但在LCD1602上不能显示时间
源程序:
#include <reg51.h>
#include <absacc.h>
#include <intrins.h>
#define uchar unsigned char
#define uint unsigned int
sbit T_RST = P1^2; //DS1302复位线引脚
sbit T_CLK = P1^3;
sbit T_IO = P1^4;
sbit RS=P1^7;
sbit RW=P1^6;
sbit EN=P1^5;
sbit key0=P3^0;
sbit key1=P3^1;
sbit key2=P3^2;
sbit ACC7 =ACC^7;
sbit ACC0 =ACC^0;
uchar datechar[]={"DATE:"};
uchar timechar[]={"TIME:"};
uchar datebuffer[8]={0,0,0x2d,0,0,0x2d,0,0};
uchar timebuffer[8]={0,0,0x3a,0,0,0x3a,0,0};
uchar data ttime[3]={0x00,0x00,0x00};
uchar data tdata[3]={0x00,0x00,0x00};
//程序开始
void WriteB(uchar ucDa)
{
uchar i;
ACC = ucDa;
for(i=8;i>0;i--)
{
T_IO = ACC0;
T_CLK = 1;
T_CLK = 0;
ACC = ACC >> 1;
}
}
uchar ReadB(void)
{
uchar i;
for(i=8;i>0;i--)
{
ACC = ACC >>1;
ACC7 = T_IO;
T_CLK = 1;
T_CLK = 0;
}
return(ACC);
}
void v_W1302(uchar ucAddr,uchar ucDa)
{
T_RST = 0;
T_CLK = 0;
nop_();
_nop_();
T_RST = 1;
nop_();
_nop_();
WriteB(ucAddr);
WriteB(ucDa);
T_CLK = 1;
T_RST = 0;
}
uchar uc_R1302(uchar ucAddr)
{
uchar ucDa=0;
T_RST = 0;
T_CLK = 0;
T_RST = 1;
WriteB(ucAddr);
ucDa = ReadB();
T_CLK = 1;
T_RST =0;
return(ucDa);
}
//LCD查询忙函数
void fbusy()
{
P2 = 0xff;
RS = 0;
RW = 1;
EN = 1;
EN = 0;
while((P2&0x80))
{
EN = 0;
EN = 1;
}
}
//LCD写命令函数
void wc51r(uchar j)
{
fbusy();
EN = 0;
RS = 0;
RW = 0;
EN = 1;
P2 = j;
EN = 0;
}
//LCD写数据函数
void wc51ddr(uchar j)
{
fbusy();
EN = 0;
RS = 1;
RW = 0;
EN = 1;
P2 = j;
EN = 0;
}
void init()
{
wc51r(0x01);
wc51r(0x38);
wc51r(0x0c);
wc51r(0x06);
}
//延时程序
void delay(uint i)
{
uint y,j;
for (j=0;j<i;j++)
{
for (y=0;y<0xff;y++) {;}
}
}
void main(void)
{
uchar i,set;
uchar data temp;
SP=0X50;
delay(10);
init();
wc51r(0x80);
for (i=0;i<5;i++) wc51ddr(datechar[ i]);
wc51r(0xc0);
for (i=0;i<5;i++) wc51ddr(timechar[ i]);
while(1)
{
P3=0XFF;
if(key0==0)
{
delay(10);
if(key0==0)
{
while(key0==0);
set++;
if(set==6) set=0;
}
}
if(key1==0)
{
delay(10);
if(key1==0)
{
while (key1==0);
switch(set)
{
case 1:
tdata[0]++;
if(tdata[0]==100) tdata[0]=0;
temp=(tdata[0]/10)*16+tdata[0]%10;
v_W1302(0x8e,0);
v_W1302(0x8c,temp);
v_W1302(0x8e,0x80);
break;
case 2:
tdata[1]++;
if(tdata[1]==13) tdata[1]=1;
temp=(tdata[1]/10)*16+tdata[1]%10;
v_W1302(0x8e,0);
v_W1302(0x88,temp);
v_W1302(0x8e,0x80);
break;
case 3:
tdata[2]++;
if(tdata[2]==32) tdata[2]=1;
temp=(tdata[2]/10)*16+tdata[2]%10;
v_W1302(0x8e,0);
v_W1302(0x86,temp);
v_W1302(0x8e,0x80);
break;
case 4:
ttime[2]++;
if(ttime[2]==24) ttime[2]=0;
temp=(ttime[2]/10)*16+ttime[0]%10;
v_W1302(0x8e,0);
v_W1302(0x84,temp);
v_W1302(0x8e,0x80);
break;
case 5:
ttime[1]++;
if(ttime[1]==60) ttime[1]=0;
temp=(ttime[2]/10)*16+ttime[0]%10;
v_W1302(0x8e,0);
v_W1302(0x84,temp);
v_W1302(0x8e,0x80);
break;
}
}
}
if(key2==0)
{
delay(10);
if (key2==0)
{
while (key2==0);
switch(set)
{
case 1:
tdata[0]--;
if (tdata[0]==0xff) tdata[0]=99;
temp=(tdata[0]/10)*16+tdata[0]%10;
v_W1302(0x8e,0);
v_W1302(0x8c,temp);
v_W1302(0x8e,0x80);
break;
case 2:
tdata[1]--;
if (tdata[1]==0x00) tdata[1]=12;
temp=(tdata[1]/10)*16+tdata[1]%10;
v_W1302(0x8e,0);
v_W1302(0x88,temp);
v_W1302(0x8e,0x80);
break;
case 3:
tdata[2]--;
if (tdata[2]==0x00) tdata[2]=31;
temp=(tdata[2]/10)*16+tdata[2]%10;
v_W1302(0x8e,0);
v_W1302(0x86,temp);
v_W1302(0x8e,0x80);
break;
case 4:
ttime[2]--;
if (ttime[2]==0xff) ttime[2]=23;
temp=(ttime[2]/10)*16+ttime[2]%10;
v_W1302(0x8e,0);
v_W1302(0x84,temp);
v_W1302(0x8e,0x80);
break;
case 5:
ttime[1]--;
if (ttime[1]==0xff) ttime[1]=59;
temp=(ttime[1]/10)*16+ttime[1]%10;
v_W1302(0x8e,0);
v_W1302(0x82,temp);
v_W1302(0x8e,0x80);
break;
}
}
}
temp=uc_R1302(0x8d);
tdata[0]=(temp/16)*10+temp%16;
datebuffer[0]=0x30+temp/16;
datebuffer[1]=0x30+temp%16;
temp=uc_R1302(0x89);
tdata[1]=(temp/16)*10+temp%16;
datebuffer[3]=0x30+temp/16;
datebuffer[4]=0x30+temp%16;
temp=uc_R1302(0x87);
tdata[2]=(temp/16)*10+temp%16;
datebuffer[6]=0x30+temp/16;
datebuffer[7]=0x30+temp%16;
temp=uc_R1302(0x85);
temp=temp&0x7f;
ttime[2]=(temp/16)*10+temp%16;
timebuffer[0]=0x30+temp/16;
timebuffer[1]=0x30+temp%16;
temp=uc_R1302(0x83);
ttime[1]=(temp/16)*10+temp%16;
timebuffer[3]=0x30+temp/16;
timebuffer[4]=0x30+temp%16;
temp=uc_R1302(0x81);
temp=temp&0x7f;
ttime[0]=(temp/16)*10+temp%16;
timebuffer[6]=0x30+temp/16;
timebuffer[7]=0x30+temp%16;
wc51r(0x86);
for (i=0;i<8;i++) wc51ddr(datebuffer[ i]);
wc51r(0xc6);
for (i=0;i<8;i++) wc51ddr(timebuffer[ i]);
}
}
proteus仿真:
仿真时LCD无显示,向大家求助
|