|
本帖最后由 wasj 于 2017-11-30 13:03 编辑
本人刚开始学习单片机,根据郭天祥的视频学习制作万年历,结果在设置闹钟时出现问题,到达设定时间时蜂鸣器没有反应,不知道哪里出了问题,希望各位前辈给予解答。还有就是,如果要添加温度的话,要如何添加?程序:#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 s1=P1^0;//按键
sbit s2=P1^1;//+1
sbit s3=P1^2;//-1
sbit s4=P1^3;//闹钟
sbit rd=P3^7;
sbit dscs=P1^4;
sbit dsas=P1^5;
sbit dsrw=P1^6;
sbit dsds=P1^7;
sbit dsirq=P3^3;
sbit beep=P2^3;//蜂鸣器
uchar count,s1num;
uchar flag,flag1;//暂停与运行
char miao,shi,fen;
uchar ashi,afen,amiao;
char nian,yue,ri,week;
uchar code table[]=" 2017-11-29 WED";
uchar code table1[]=" : : ";
void delay(uint z)//延时
{
uint x,y;
for(x=z;x>0;x--)
for(y=110;y>0;y--);
}
void write_com(uchar com)//1602
{
rs=0;
lcden=0;
P0=com;
delay(5);
lcden=1;
delay(5);
lcden=0;
}
void write_date(uchar date)//1602
{
rs=1;
lcden=0;
P0=date;
delay(5);
lcden=1;
delay(5);
lcden=0;
}
void di();
void set_time();
void write_ds(uchar add,uchar date);
uchar read_ds(uchar add);
void set_alarm(uchar ashi,uchar afen,uchar amiao);
void init()//初始化
{
uchar num;
EA=1;EX0=1;IT0=1;
dula=0;
wela=0;
lcden=0;
set_time();
set_alarm(19,11,00);//输入闹钟时间
write_ds(0x0a,0x20);//打开振荡器(打开DS12887时钟,时间开始动)
write_ds(0x0b,0x26);//定时器开启
read_ds(0x0c);
// fen=59;
// miao=53;
// shi=23;
write_com(0x38);
write_com(0x0c);
write_com(0x06);
write_com(0x01);//清零
write_com(0x80);//第一行
for(num=0;num<15;num++)//NUM?table????????
{
write_date(table[num]);
delay(5);
}
write_com(0x80+0x40);//第二行
for(num=0;num<12;num++)
{
write_date(table1[num]);
delay(5);
}
/* 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 write_nyr(uchar add,uchar date)//?????????年月日
{
uchar 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) //1602液晶刷新星期函数
{ write_com(0x80+12);
switch(we)
{ case 1: write_date('M');delay(5); write_date('O');delay(5); write_date('N'); break;
case 2: write_date('T');delay(5); write_date('U');delay(5); write_date('E'); break;
case 3: write_date('W');delay(5); write_date('E');delay(5); write_date('D'); break;
case 4: write_date('T');delay(5); write_date('H');delay(5); write_date('U'); break;
case 5: write_date('F');delay(5); write_date('R');delay(5); write_date('I'); break;
case 6: write_date('S');delay(5); write_date('A');delay(5); write_date('T'); break;
case 7: write_date('S');delay(5); write_date('U');delay(5); write_date('N'); break; } }
void keyscan()
{
rd=0;//
if(flag1==1){//按任意键停止
if(s2==0)
{
delay(10);
if(s2==0)
{while(!s2);
flag1=0;}
}
if(s3==0)
{
delay(10);
if(s3==0)
{while(!s3);
flag1=0;}
}}
if(s1==0)//?????
{
delay(5);//??
if(s1==0)//?????0
{ s1num++;
flag=1;flag1=0;
while(!s1);
if(s1num==1)//???
{
TR0=0;//????
write_com(0x80+0x40+10);//????
write_com(0x0f);//????
}
}
if(s1num==2)//???
{
write_com(0x80+0x40+7);//?分
}
if(s1num==3)
{
write_com(0x80+0x40+4);//?时
}
if(s1num==4)
{
write_com(0x80+12);//?周
}
if(s1num==5)
{
write_com(0x80+9);//?日
}
if(s1num==6)
{
write_com(0x80+6);//?月
}
if(s1num==7)
{
write_com(0x80+3);//?年
}
if(s1num==8)
{
s1num=0;//??归零
write_com(0x0c);//??????
flag=0;write_ds(0,miao);
write_ds(2,fen);
write_ds(4,shi);
write_ds(6,ri);
write_ds(7,week);
write_ds(8,yue);
write_ds(9,nian);
//TR0=1;
}
}
if(s1num!=0)
{
if(s2==0)
{
delay(5);
if(s2==0)//+1
{
while(!s2);//????
if(s1num==1)
{
miao++;
if(miao==60)
miao=0;
write_sfm(10,miao);
write_com(0x80+0x40+10);
}
if(s1num==2)
{
fen++;
if(fen==60)
fen=0;
write_sfm(7,fen);
write_com(0x80+0x40+7);
}
if(s1num==3)
{
shi++;
if(shi==24)
shi=0;
write_sfm(4,shi);
write_com(0x80+0x40+4);
}
if(s1num==4)
{
week++;
if(week==8)
week=1;
write_week(week);
write_com(0x80+12);
}
if(s1num==5)
{
ri++;
if(ri==31)
ri=1;
write_nyr(9,ri);
write_com(0x80+9);
}
if(s1num==6)
{
yue++;
if(yue==13)
yue=1;
write_nyr(6,yue);
write_com(0x80+6);
}
if(s1num==7)
{
nian++;
if(nian==100)
nian=0;
write_nyr(3,nian);
write_com(0x80+3);
}
}
}
if(s3==0) //-1
{ delay(1); if(s3==0)
{ while(s3==0); 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); write_com(0x80+12) ; break;
case 5: ri--; if(ri==0) ri=31; write_nyr(9,ri); write_com(0x80+9) ; break;
case 6: yue--; if(yue==0) yue=12; write_nyr(6,yue); write_com(0x80+6) ; break;
case 7: nian--; if(nian==-1) nian=99; write_nyr(3,nian); write_com(0x80+3) ; break; } } } }
}
void write_ds(uchar add,uchar date)//DS12887 地址,数据
{
dscs=0;
dsas=1;
dsds=1;
dsrw=1;
P0=add;
dsas=0;
dsrw=0;
P0=date;
dsrw=1;
dsas=1;
dscs=1;
}
uchar read_ds(uchar add)//DS12887 地址,数据
{
uchar ds_date;
dsas=1;
dsds=1;
dsrw=1;
dscs=0;
P0=add;
dsas=0;
dsds=0;
P0=0xff;
ds_date=P0;
dsds=1;
dsas=1;
dscs=1;
return ds_date;
}
void set_time()//写入时间
{
//write_ds(0,00);//秒
write_ds(4,19);//分
write_ds(2,10);//时
write_ds(9,2017);//年
write_ds(8,11);//月
write_ds(6,29);//日
write_ds(7,1); //星期
}
void set_alarm(uchar ashi,uchar afen,uchar amiao)//闹钟时间
{
write_ds(1,amiao);
write_ds(3,afen);
write_ds(5,ashi);
}
void di()
{ uint i=0;
while(i==50)
{beep=0;
delay(50);
beep=1;
delay(100);
beep=0;
delay(50);
beep=1;i++;}
}
void main()
{
init();//???
/*while(1){ //蜂鸣器调试
di();}*/
di();
while(1)
{
keyscan();
if(flag1==1)
di();
if(flag==0)
{
miao=read_ds(0);
fen=read_ds(2);
shi=read_ds(4);
/*ri=read_ds(6);
yue=read_ds(8);
nian=read_ds(9);
week=read_ds(7);*/
write_sfm(10,miao);
write_sfm(7,fen);
write_sfm(4,shi);
/*write_nyr(3,ri);
write_nyr(6,yue);
write_nyr(9,nian);
write_week(week);*/
}}
// while(1);
}
/*void timer0() 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(4,shi);//?????4??
}
write_sfm(7,fen);
}
write_sfm(10,miao);
}
}*/
void exter () interrupt 0
{while(1){
uchar c;
//beep=0;
flag1=1;
c=read_ds(0x0c);
}}
|
|