设计电子时钟 要用AT89S51单片机的定时器实现,用4位数码管显示,用点的闪烁表示秒,

我要原理图和C语言程序。我的邮箱woaiwojia6@126.com。谢谢各位大侠了!急用!!!... 我要原理图和C语言程序。我的邮箱woaiwojia6@126.com。谢谢各位大侠了 !急用!!! 展开
 我来答
HYC4152639
2011-12-07
知道答主
回答量:22
采纳率:0%
帮助的人:20.6万
展开全部
/* 实验名称:SUMA & BUZZER
实验描述:一个带有闹钟的数码时钟,加三个键,一个调小时键
一个调分钟键,一个设置闹钟时间键
实验方法:TIMER0中断用来计时,控制数码时钟的时间显示
还可设置半秒或四分之一秒,用来控制音调
TIMER1用来控制音普,,
timer0用MODE2自动加载模式 */

# include<reg52.h>
sbit speaker=P2^3 ;

sbit AA=P2^2 ; //调时用
sbit BB=P2^1 ; //调分用
sbit CC=P2^0 ; // 设置闹钟用
sbit P1_7=P1^7; //小数点
// int code seven_reg[]={0xC0,0xF9,0xA4,0xB0,0x99,0x92,0x82,0xF8,0x80,0x90}; //0123456789
int code seven_reg[]={0x40,0x79,0x24,0x30,0x19,0x12,0x2,0x78,0x00,0x10};
// int code scan[]={0x1F,0x2F,0x4F,0x8F}; //1110,1101,1011,0111
int code scan[]={0x1,0x2,0x4,0x8}; //0001,0010,0100,1000
unsigned int timer0_times;
unsigned int timer1_times;
unsigned int timer0_times_AA; //按纽AA用
unsigned int timer0_times_BB; //按纽BB用
unsigned int timer0_times_CC; // 半秒计时用
unsigned int timer0_times_DD; //四分之一秒用
unsigned int timer0_times_EE; // 闹钟用
typedef struct{
unsigned char second;
unsigned char minute;
unsigned char hour;
unsigned char half_second;
unsigned char alarm_hour;
unsigned char alarm_minute;
unsigned char quarter_second ;} time; //此处是固定格式,不能改
time now;
char mode=0;
int i=0,j=0,k=0;
int code tone[]={1012,956,852,759,716,638,568,506,478,426,379};
//7()低音)1234567(中音)123(高音)
int code song[22][2]={ {6,2},{6,2},{7,4},{6,2},{6,2}, {7,4},{6,2},{7,2},
{8,2},{7,2},{6,2},{7,1},{6,1},{4,4},{3,2},{1,2},
{3,2},{4,2},{3,2},{3,1},{1,1},{0,4}
} ;

/********************************************************************/
void timer1_isr() interrupt TF1_VECTOR using 2
{ TR1=0;
TL1=(65536-tone[(song[j][0])])%256;
TH1=(65536-tone[(song[j][0])])/256;
TR1=1;

speaker=~speaker;
}

/*******************************************************************/
void timer0_isr() interrupt TF0_VECTOR using 1
{

/***************************************************/
if(CC!=0) timer0_times_EE=0;
else
{ timer0_times_EE++;
if(CC==0&&timer0_times_EE==4000) //按1S进入设置闹钟模式
{mode++; //500 ~0.125s

if(mode==2) mode=0;
}
}

/**=调时键设置=*/

if(AA!=0) timer0_times_AA=0;
else
{ timer0_times_AA++;
if(AA==0&&timer0_times_AA==500&&mode==0) //500*0.25ms=0.125s
{ now.hour++;
// timer0_times_AA=0;
if (now.hour==24) now.hour=0;//后边代码不会达到此效果
}
if(AA==0&timer0_times_AA==500&&mode==1) //设置闹钟时间HOUR
{ now.alarm_hour++;
if(now.alarm_hour==24) now.alarm_hour=0;
}
}
/***=调分键设置=***/
if (BB!=0) timer0_times_BB=0;
else
{ timer0_times_BB++;
if(BB==0&&timer0_times_BB==500&&mode==0) //0.125s
{ //timer0_times_BB=0;
now.minute++;
if(now.minute==60) now.minute=0;
}
if(BB==0&&mode==0)
{ if(timer0_times_BB==2000) //0.5s
{ now.minute++;
timer0_times_BB=1000;
if(now.minute==60) now.minute=0;
}
}
if(BB==0&&timer0_times_BB==500&&mode==1) //设置闹钟时间MINUTE
{ now.alarm_minute++;
if(now.alarm_minute==60) now.alarm_minute=0;

}
if(BB==0&&mode==1)
{ if(timer0_times_BB==2000) //0.5s
{ now.alarm_minute++;
timer0_times_BB=1000;
if(now.minute==60) now.minute=0;
}
}
}
/*=自然时间设置=*/
timer0_times_DD++; //四分之一秒
if(timer0_times_DD==1000)
{ now.quarter_second++;
timer0_times_DD=0;
if(now.quarter_second==60) now.quarter_second=0;
} //二分之一秒
timer0_times_CC++;
if(timer0_times_CC==2000)
{now.half_second++;
timer0_times_CC=0;
if(now.half_second==60) now.half_second=0;
}
timer0_times++; //一秒一分一时
if (timer0_times==4000)
{ now.second++;
timer0_times=0;
if(now.second==60)
{ now.minute++;
now.second=0;
if(now.minute==60)
{ now.hour++;
now.minute=0;
if(now.hour==24) now.hour=0;
} } }
/******************扫描显示******************************/
switch(mode)
{
case 0 :{
switch(i){ /*0.005秒选一次*/
case 0:
P1=seven_reg[now.minute%10] ;
if(now.half_second%2==0)
P1_7=1; /*实现让它0.5秒闪一次*/
break;
case 1:
P1=seven_reg[now.minute/10];//小数点不亮同,P1_7=1
if(now.half_second%2==0)
P1_7=1; /*为什么不能放在上一句前面昵????*/
break;
case 2:
P1=seven_reg[now.hour %10];

break;

case 3:
P1=seven_reg[now.hour /10];

break;
}} break;

case 1:{
switch(i){
case 0:
P1=seven_reg[now.alarm_minute%10] ;
break;
case 1:
P1=seven_reg[now.alarm_minute/10];
break;
case 2: P1=seven_reg[now.alarm_hour %10];
break;
case 3: P1=seven_reg[now.alarm_hour /10];
break; }
} break;
}

P3=scan[i];
i++;
if (i==4) i=0;
if(now.quarter_second%2==0)
{ k++;
if(k==(song[j][1]*4))
{ j++;
k=0;
if(j==22) j=0;
} } }
/****************************************************************/
void timer0_initialize()
{ EA=0;
TR0=0;
TMOD=0X12;
TL0=(256-250); //0.025ms 自动加载模式 0.025*4000=1s
TH0=(256-250);
ET0=1;
TR0=1;
EA=1;
}
void timer1_initialize()
{
TR1=0;
TL1=(65536-tone[song[j][0]])%256;
TH1=(65536-tone[song[j][0]])/256;
TMOD=0X12;
ET1=1;
}
main()
{ unsigned char m1=0;
speaker=0;
now.alarm_minute=1;
timer0_initialize();
timer1_initialize();
while(1)
{if(now.alarm_minute!=0)//将闹钟设置为0时,相当于取消闹钟,不会响
{ if(now.hour==now.alarm_hour&&now.minute==now.alarm_minute)
{ if(CC==0&&timer0_times_EE==500) m1=1;
switch (m1)
{ case 0:
TR1=1 ;
break;
case 1 :
TR1=0;
speaker=0;
break;
}}
else
{
TR1=0;
speaker=0;
m1=0;
} } } }
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式