我用51单片机和1602和12c887+做个电子钟,却不能正常显示时间,求大神帮帮忙,下面是我的程序
#include<reg52.h>
#define uchar unsigned char
#define uint unsigned int
sbit dula=P2^6;//段选
sbit wela=P2^7;//位选
sbit rs=P3^5;
sbit lcden=P3^4;
sbit rd=P3^7;
sbit rw=P3^6;
sbit s1=P3^0;//功能键
sbit s2=P3^1;//增大键
sbit s3=P3^2;//减小键
sbit s4=P2^4;//闹钟查看键
sbit beep=P2^3;//蜂鸣器
sbit dscs=P1^4;//时钟芯片
sbit dsas=P1^5;
sbit dsrw=P1^6;
sbit dsds=P1^7;
sbit dsirq=P3^3;
bit flag1,flag_ri;//定义两个位变量
uchar count,s1num,flag,t0_num;//其他变量定义
char miao,shi,fen,year,month,day,week,amiao,afen,ashi;
uchar code table[]=" 20 - - ";//液晶固定显示内容
uchar code table1[]=" : : ";
void write_ds(uchar,uchar);//函数声明
void set_alARM(uchar,uchar,uchar);void read_alarm();
uchar read_ds(uchar);void set_time();
void delayms(uint xms)
{
uint i,j;
for(i=xms;i>0;i--)
for(j=110;j>0;j--);
}
void di() //蜂鸣器报警
{
beep=0;
delayms(100);
beep=1;
}
void write_com(uchar com)
{ //写液晶命令函数
rs=0;
lcden=0;
P0=com;
delayms(5);
lcden=1;
delayms(5);
lcden=0;
}
void write_date(uchar date)
{ //写液晶数据函数
rs=1;
lcden=0;
P0=date;
delayms(5);
lcden=1;
delayms(5);
lcden=0;
}
void init()//初始化函数
{
uchar num;
EA=1;//打开总中断
EX1=1;//打开外部中断1
IT1=1;//设置负跳变沿触发中断
flag1=0;//变量初始化
t0_num=0;
s1num=0;
week=1;
dula=0;//关闭数码管
wela=0;
lcden=0;
rd=0;
rw=0;
/*以下几行在首次设置DC12C887时使用,以后不必在写入
write_ds(0x0a, 0x20); //启动DS12887 ,打开振荡器
write_ds(0x0b, 0x26);//设置24小时模式,数据二进制格式,开启闹铃中断
set_time();//设置上电默认时间
---------------*/
write_com(0x38); //液晶初始化,显示模式设置:16×2显示5×7点阵,8位数据接口
write_com(0x0c);
write_com(0x06);
write_com(0x01);
write_com(0x80);
for(num=0;num<15;num++)
{
write_date(table[num]);
delayms(1);
}
write_com(0x80+0x40);
for(num=0;num<11;num++)
{
write_date(table1[num]);
delayms(1);
}
}
void write_sfm(uchar add,char date)
{ //1602液晶刷新时分秒函数
char shi,ge;
shi=date/10;
ge=date%10;
write_com(0x80+add);
write_date(0x30+shi);
write_date(0x30+ge);
}
void write_nyr(uchar add,char date)
{ //1602液晶刷新年月日函数
char shi,ge;
shi=date/10;
ge=date%10;
write_com(0x80+add);
write_date(0x30+shi);
write_date(0x30+ge);
}
void write_week(char we)
{ //写液晶星期显示函数
write_com(0x80+12);
switch(we)
{
case 1: write_date('M');delayms(5);
write_date('O');delayms(5);
write_date('N');
break;
case 2: write_date('T');delayms(5);
write_date('U');delayms(5);
write_date('E');
break;
case 3: write_date('W');delayms(5);
write_date('E');delayms(5);
write_date('D');
break;
case 4: write_date('T');delayms(5);
write_date('H');delayms(5);
write_date('U');
break;
case 5: write_date('F');delayms(5);
write_date('R');delayms(5);
write_date('T');
break;
case 6: write_date('S');delayms(5);
write_date('A');delayms(5);
write_date('T');
break;
case 7: write_date('S');delayms(5);
write_date('U');delayms(5);
write_date('N');
break;
}
}
void keyscan()
{
if(flag_ri==1)
{//按任意键取消报警
if((s1==0)||(s2==0)||(s3==0)||(s4==0))
{
delayms(5);
if((s1==0)||(s2==0)||(s3==0)||(s4==0))
{
while(!(s1&&s2&&s3&&s4));di();
flag_ri=0;//清除报警标志
}
}
}
if(s1==0)//检测S1
{
delayms(5);
if(s1==0)
{
s1num++;//记录按下的次数
if(flag1==1)
if(s1num==4)
s1num==1;
flag=1;
while(!s1);di();
switch(s1num)
{ //光标闪烁点定位
case 1: write_com(0x80+0x40+10);
write_com(0x0f);
break;
case 2: write_com(0x80+0x40+7);
break;
case 3: write_com(0x80+0x40+4);
break;
case 4: write_com(0x80+12);
break;
case 5: write_com(0x80+9);
break;
case 6: write_com(0x80+6);
break;
case 7: write_com(0x80+3);
break;
case 8: s1num=0;
write_com(0x0c);
flag=0;
write_ds(0,miao);
write_ds(2,fen);
write_ds(4,shi);
write_ds(6,week);
write_ds(7,day);
write_ds(8,month);
write_ds(9,year);
break;
}
}
}
if(s1num!=0)//只有当s1按下后,才检测s2和s3
{
if(s2==0)
{
delayms(1);
if(s2==0)
{
while(!s2);di();
switch(s1num)
{ //根据功能键次数调节相应数值
case 1: miao++;
if(miao==60)
miao=0;
write_sfm(10,miao);
write_com(0x80+0x40+10);
break;
case 2: fen++;
if(fen==60)
fen=0;
write_sfm(7,fen);
write_com(0x80+0x40+7);
break;
case 3: shi++;
if(shi==24)
shi=0;
write_sfm(4,shi);
write_com(0x80+0x40+4);
break;
case 4: week++;
if(week==8)
week=1;
write_week(week);
write_com(0x80+12);
break;
case 5: day++;
if(day==32)
day=1;
write_nyr(9,day);
write_com(0x80+9);
break;
case 6: month++;
if(month==13)
month=1;
write_nyr(6,month);
write_com(0x80+6);
break;
case 7: year++;
if(year==100)
year=0;
write_nyr(3,year);
write_com(0x80+3);
break;
}
}
}
if(s3==0)
{
delayms(1);
if(s3==0)
{
while(!s3);di();
switch(s1num)
{ //根据功能键次数调节相应数值
case 1: miao--;
if(miao==-1)
miao=59;
write_sfm(10,miao);
write_com(0x80+0x40+10);
break;
case 2: fen--;
if(fen==-1)
fen=59;
write_sfm(7,fen);
write_com(0x80+0x40+7);
break;
case 3: shi--;
if(shi==-1)
shi=23;
write_sfm(4,shi);
write_com(0x80+0x40+4);
break;
case 4: week--;
if(week==0)
week=7;
write_week(week);
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
1.搞懂1602的显示,能够定位显示字符
2.你时钟芯片的初始化,我看到有81的字符,这种问题的错误一般是时钟芯片的初始化。
两者网上的例程很多的。
一周热门 更多>