求助下,大神帮忙看下这个程序,怎么闹钟一直不能用
时间:10-02
整理:3721RD
点击:
利用51学习板上的1206液晶显示和键盘来控制闹钟的响声,没有用时钟芯片
#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 key1=P3^0;
sbit key2=P3^1;
sbit key3=P3^2;
sbit beep=P2^3;
sbit rd=P3^7;//rd置零,利用矩阵键盘代替独立键盘(注意两者区别,接地端)
uchar count,shi,fen,miao,s1num,num;
uchar code table[]="2013-4-15 hh ";
uchar code table1[]=" 00:00:00 ggggg";//定义初始值
void delay(uint z)
{ uint x,y;
for(x=z;x>0;x--)
for(y=110;y>0;y--);
}
void nz()
{
//if(shi==00&fen==01&miao==00)
//{
TR0=0;
beep=0;
delay(5);
//}
}
void write_com(uchar com)
{
rs=0;
lcden=0;
P0=com;
delay(5);
lcden=1;
delay(5);
lcden=0;
}
void write_date(uchar com)
{
rs=1;
lcden=0;
P0=com;
delay(5);
lcden=1;
delay(5);
lcden=0;
}
void rade_date(uchar com)
{
rs=1;
lcden=0;
P0=com;
delay(5);
lcden=1;
}
void init()//初始函数
{
uchar num;
dula=0;//关闭数码管
wela=0;
lcden=0;
/*fen=59;
miao=53;
shi=23;//初始化显示数据*/
write_com(0x38);//设置开启液晶16*2显示
write_com(0x0c);//设置开显示,与光标有关
write_com(0x06);//写入一个字符后指针加一
write_com(0x01);//显示清零,指针数据清零
write_com(0x80);//写入第一行
for(num=0;num<15;num++)//?
{
write_date(table[num]);
delay(20);
}
write_com(0x80+0x40);//写入第二行
for(num=0;num<20;num++)//?
{
write_date(table1[num]);
delay(20);
}//写入数据
TMOD=0x01;//选择定时器模式
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
EA=1;
ET0=1;
TR0=1;//开启定时器
}
void write_sfm(uchar add,uchar date)
{
uchar shi,ge;
shi=date/10;
ge=date%10;
write_com(0x80+0x40+add);//第二行时分秒地址
write_date(0x30+shi);
write_date(0x30+ge);//由于液晶管内部数字显示,见图片
}
void keyscan()
{rd=0;
if(key1==0)
{
delay(5);
if(key1==0)
{s1num++;
while(!key1);//检测键盘是否按下
if(s1num==1)
{
TR0=0;//关闭定时器,钟表不走
write_com(0x80+0x40+8);//读取光标地址
write_com(0x0f);//光标闪烁
}
if(s1num==2)
{
write_com(0x80+0x40+5);//读取光标地址
}
if(s1num==3)
{
write_com(0x80+0x40+2);//读取光标地址
}
if(s1num==4)
{
s1num=0;
write_com(0x0c);//光标不闪烁
TR0=1;
}
if(s1num!=0)
{
if(key2==0)
{
delay(5);
if(key2==0)
{
while(!key2);
if(s1num==1)
{
miao++;
if(miao==60)
miao=0;
write_sfm(10,miao);
write_com(0x80+0x40+8);
}
if(s1num==2)
{
fen++;
if(fen==60)
fen=0;
write_sfm(7,fen);
write_com(0x80+0x40+5);
}
if(s1num==3)
{
shi++;
if(shi==24)
shi=0;
write_sfm(4,shi);
write_com(0x80+0x40+2);
}
}
}
if(key3==0)
{
delay(5);
if(key3==0)
{
while(!key3);
if(s1num==1)
{
miao--;
if(miao==-1)
miao=59;
write_sfm(8,miao);
write_com(0x80+0x40+8);
}
if(s1num==2)
{
fen--;
if(fen==-1)
fen=59;
write_sfm(5,fen);
write_com(0x80+0x40+5);
}
if(s1num==3)
{
shi--;
if(shi==-1)
shi=23;
write_sfm(2,shi);
write_com(0x80+0x40+2);
}
}
}
}
}
}
}
void TO_time() interrupt 1
{
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
count++;
if(count==18)
{count=0;
miao++;
if(miao==60)
{
miao=0;
fen++;
if(fen==60)
{fen=0;
shi++;
if(shi==24)
{
shi=0;
}
write_sfm(2,shi);//数字代表小时的地址,而且write函数和小时对应的if并列
}
write_sfm(5,fen);
}
write_sfm(8,miao);
}
}
void main()
{
init();//初始化
//rade_com(0x80+0x40);//写入第二行
for(num=0;num<20;num++)//?
{
rade_date(table1[num]);
delay(20);
if(shi==00&fen==00&miao==10)
nz();}
while(1)//检验数据是否读入
{
keyscan();
}
}
#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 key1=P3^0;
sbit key2=P3^1;
sbit key3=P3^2;
sbit beep=P2^3;
sbit rd=P3^7;//rd置零,利用矩阵键盘代替独立键盘(注意两者区别,接地端)
uchar count,shi,fen,miao,s1num,num;
uchar code table[]="2013-4-15 hh ";
uchar code table1[]=" 00:00:00 ggggg";//定义初始值
void delay(uint z)
{ uint x,y;
for(x=z;x>0;x--)
for(y=110;y>0;y--);
}
void nz()
{
//if(shi==00&fen==01&miao==00)
//{
TR0=0;
beep=0;
delay(5);
//}
}
void write_com(uchar com)
{
rs=0;
lcden=0;
P0=com;
delay(5);
lcden=1;
delay(5);
lcden=0;
}
void write_date(uchar com)
{
rs=1;
lcden=0;
P0=com;
delay(5);
lcden=1;
delay(5);
lcden=0;
}
void rade_date(uchar com)
{
rs=1;
lcden=0;
P0=com;
delay(5);
lcden=1;
}
void init()//初始函数
{
uchar num;
dula=0;//关闭数码管
wela=0;
lcden=0;
/*fen=59;
miao=53;
shi=23;//初始化显示数据*/
write_com(0x38);//设置开启液晶16*2显示
write_com(0x0c);//设置开显示,与光标有关
write_com(0x06);//写入一个字符后指针加一
write_com(0x01);//显示清零,指针数据清零
write_com(0x80);//写入第一行
for(num=0;num<15;num++)//?
{
write_date(table[num]);
delay(20);
}
write_com(0x80+0x40);//写入第二行
for(num=0;num<20;num++)//?
{
write_date(table1[num]);
delay(20);
}//写入数据
TMOD=0x01;//选择定时器模式
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
EA=1;
ET0=1;
TR0=1;//开启定时器
}
void write_sfm(uchar add,uchar date)
{
uchar shi,ge;
shi=date/10;
ge=date%10;
write_com(0x80+0x40+add);//第二行时分秒地址
write_date(0x30+shi);
write_date(0x30+ge);//由于液晶管内部数字显示,见图片
}
void keyscan()
{rd=0;
if(key1==0)
{
delay(5);
if(key1==0)
{s1num++;
while(!key1);//检测键盘是否按下
if(s1num==1)
{
TR0=0;//关闭定时器,钟表不走
write_com(0x80+0x40+8);//读取光标地址
write_com(0x0f);//光标闪烁
}
if(s1num==2)
{
write_com(0x80+0x40+5);//读取光标地址
}
if(s1num==3)
{
write_com(0x80+0x40+2);//读取光标地址
}
if(s1num==4)
{
s1num=0;
write_com(0x0c);//光标不闪烁
TR0=1;
}
if(s1num!=0)
{
if(key2==0)
{
delay(5);
if(key2==0)
{
while(!key2);
if(s1num==1)
{
miao++;
if(miao==60)
miao=0;
write_sfm(10,miao);
write_com(0x80+0x40+8);
}
if(s1num==2)
{
fen++;
if(fen==60)
fen=0;
write_sfm(7,fen);
write_com(0x80+0x40+5);
}
if(s1num==3)
{
shi++;
if(shi==24)
shi=0;
write_sfm(4,shi);
write_com(0x80+0x40+2);
}
}
}
if(key3==0)
{
delay(5);
if(key3==0)
{
while(!key3);
if(s1num==1)
{
miao--;
if(miao==-1)
miao=59;
write_sfm(8,miao);
write_com(0x80+0x40+8);
}
if(s1num==2)
{
fen--;
if(fen==-1)
fen=59;
write_sfm(5,fen);
write_com(0x80+0x40+5);
}
if(s1num==3)
{
shi--;
if(shi==-1)
shi=23;
write_sfm(2,shi);
write_com(0x80+0x40+2);
}
}
}
}
}
}
}
void TO_time() interrupt 1
{
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
count++;
if(count==18)
{count=0;
miao++;
if(miao==60)
{
miao=0;
fen++;
if(fen==60)
{fen=0;
shi++;
if(shi==24)
{
shi=0;
}
write_sfm(2,shi);//数字代表小时的地址,而且write函数和小时对应的if并列
}
write_sfm(5,fen);
}
write_sfm(8,miao);
}
}
void main()
{
init();//初始化
//rade_com(0x80+0x40);//写入第二行
for(num=0;num<20;num++)//?
{
rade_date(table1[num]);
delay(20);
if(shi==00&fen==00&miao==10)
nz();}
while(1)//检验数据是否读入
{
keyscan();
}
}
我是来参考学习下的!怎么没人回答啊