本帖最后由 a1995 于 2017-12-24 17:07 编辑
这个程序是我按照自己的理解只是把程序的函数名改了一下可是不知道为什么下到单片机中运行就不能像例程中一样显示日期和时间,拜托各位前辈指点一下。还有它下面提示:CALLER2: ?C_C51STARTUP
CALLER1: ?PR?TIMER0?1602汉譥加时糭显示
SEGMENT: ?PR?_WRITE_TIME?1602汉譥加时糭显示
#include<reg52.h>
#define uchar unsigned char
#define uint unsigned int
sbit RS = P3^4; //LCD命令或数据选择端
sbit RW = P3^5; //LCD读写选择端
sbit EN = P3^6; //LCD使能端
sbit we = P2^6; //数码管位选
sbit du = P2^7; //数码管段选
sbit key2 = P3^0; //S2独立键盘
sbit key3 = P3^1; //S3独立键盘
sbit key4 = P3^2;//S4独立键盘
sbit key5 = P3^3; //S5独立键盘
uchar num,key2num,count,hour,min,sec; //定义字符数量、键盘初始化、计数器、时、分、秒
uchar code display[]={ //年月日
0x08,0x0f,0x10,0x0e,0x0c,0xff,0x04,0x00,
0x0f,0x09,0x0f,0x09,0x0f,0x09,0x11,0x00,
0x0f,0x09,0x09,0x0f,0x09,0x09,0x0f,0x00
};
void delay(uint xms) //延时函数 单位ms
{
uint i,j;
for(i=xms;i>0;i--)
for(j=112;j>0;j--);
}
void write_com(uchar com) //LCD总线写命令
{
RS = 0;
EN = 0;
P0 = com;
delay(5);
EN = 1;
delay(5);
EN = 0;
}
void write_data(uchar date) //LCD总线读命令
{
RS = 1;
EN = 0;
P0 = date;
delay(5);
EN = 1;
delay(5);
EN = 0;
}
void write_coordinate_xy(uchar x,y) //LCD显示位置定义
{
uchar address; //y为0的时候在第一行,为1时在第二行
if(y==0)
address=0x80+x;
else
address=0xc0+x;
write_com(address);
}
void write_string(uchar x,y,uchar *s)//LCD显示字符串
{
write_coordinate_xy(x,y); //设置显示位置
while(*s);
{
P0=*s;
write_data(*s);
s++;
}
}
void write_time(uchar add,date)//LCD时间显示10位和个位,写时、分、秒、函数
{
uchar hour ,ge;
hour = date/10;
ge = date%10;
write_com(0x80+0x40+add);
write_data(0x30+hour);
write_data(0x30+ge);
}
void write_character() //写入固定的汉字和符号
{
write_com(0x40);//设定CGRAM的地址
for(num=0;num<24;num--) //将自定义字形码写入
{
write_data(display[num]);
}
write_com(0x84);//年
{
write_data(0);
}
write_com(0x87);//月
{
write_data(1);
}
write_com(0x8a);//日
{
write_data(2);
}
write_string(2,1,":");
write_string(5,1,":");
write_string(9,1,"QX-MCU");
}
void init() //定时器和LCD初始化
{
du=0;we=0;RW=0;
hour=0;min=0;sec=0;key2num=0;count=0;
TMOD=0x01;
TH0=(65536-45872)/256;
TL0=(65536-45872)%256;
EA=1;
ET0=1;
TR0=1;
write_com(0x38);
write_com(0x0c);
write_com(0x06);
write_com(0x01);
write_string(0,0,"2017");
write_string(5,0,"12");
write_string(8,0,"21");
write_time(0,hour);
write_time(3,min);
write_time(6,sec);
write_character();
TMOD=0x01;//设置定时器0工作模式1
TH0=(65536-50000)/256;//定时器装初值
TL0=(65536-50000)%256;
EA=1; //开总中断
ET0=1; //开定时器0中断
TR0=1; //启动定时器0
}
void keyscan() //按键检测
{
key5=0;
if(key2==0)
{
delay(5);
if(key2==0)
{
key2num++;
while(!key2);
if(key2num==1)
{
TR0=0;
write_com(0x80+0x40+7);
write_com(0x0f);
}
if(key2num==2)
{
write_com(0x80+0x40+4);
}
if(key2num==3)
{
write_com(0x80+0x40+1);
}
if(key2num==4)
{
key2num=0;
write_com(0xc0);
TR0=1;
}
}
}
if(key2num!=0)
{
if(key3==0)
{
delay(5);
if(key3==0)
{
while(!key3);
if(key2num==1)
{
sec++;
if(sec==60)
sec=0;
write_time(6,sec);
write_com(0x80+0x40+7);
}
if(key2num==2)
{
min++;
if(min==60)
min=0;
write_time(3,min);
write_com(0x80+0x40+4);
}
if(key2num==3)
{
hour++;
if(hour==24)
hour=0;
write_time(0,hour);
write_com(0x80+0x40+1);
}
}
}
if(key4==0)
{
delay(5);
if(key3==0)
{
while(!key3);
if(key2num==1)
{
sec--;
if(sec==-1)
sec=59;
write_time(6,sec);
write_com(0x80+0x40+7);
}
if(key2num==2)
{
min--;
if(min==-1)
min=59;
write_time(3,min);
write_com(0x80+0x40+4);
}
if(key2num==3)
{
hour--;
if(hour==-1)
hour=23;
write_time(0,hour);
write_com(0x80+0x40+1);
}
}
}
}
}
void main() //主程序
{
du=0;we=0;RS=0;
init();
while(1)
{
keyscan();
}
}
void timer0() interrupt 1//中断服务程序
{
TH0=(65536-45872)/256;
TL0=(65536-45872)%256;
count++;
if(count==20)
{
count=0;
sec++;
if(sec==60)
{
sec=0;
min++;
if(min==60)
{
min=0;
hour++;
if(hour==24)
{
hour=0;
}
write_time(0,hour);
}
write_time(3,min);
}
write_time(6,sec);
}
}
|