摸索了十多天单片机,自己试着写了一个电子时钟的程序
用8个数码管显示 “00-00-00”分别对应时,分,秒。然后再加上了中断调时,和闹铃。效果基本上出来了。但使用闹铃时老是只响四,五下然后就是喇叭发出吱吱的声音,而且不会停下来了。就算按了复位键,时间显示虽然是回到了最初的时间,但喇叭还是发出吱吱的声音。
初学者,望高人指点一下。本人不胜感激!!!
程序如下:
#include<reg51.h>
#define Dataport P1
sbit duansuo=P2^2;
sbit weisuo=P2^3;
sbit yiwei=P0^0;
sbit jia=P0^1;
sbit jian=P0^2;
sbit lings=P0^4;
sbit SPK=P2^0;
sbit likai=P0^3;
unsigned long num=0;
unsigned long ls=720000;
unsigned char code dofly_DuanMa[11]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x40};
unsigned char code dofly_WeiMa[]={0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f};
unsigned char TampData[8];
unsigned char j=1;
bit Flag=0;
bit k=0;
unsigned char numb=0;
void display(unsigned char FirstBit,unsigned char Num);
//void Delay(unsigned int t);
void delayUs2x(unsigned char n);
void delayMs(unsigned char n);
main()
{
unsigned char y;
EA=1;
ET0=1;
EX0=1;
IT0=1;
PX0=1;
TR0=1;
TMOD=0x01;
TH0=(65535-50000/256);
TL0=(65535-50000%256);
while(1)
{
TampData[0]=dofly_DuanMa[num/36000];
TampData[1]=dofly_DuanMa[num%36000/3600];
TampData[3]=dofly_DuanMa[num/600%6];
TampData[4]=dofly_DuanMa[num%600/60];
TampData[6]=dofly_DuanMa[num/10%6];
TampData[7]=dofly_DuanMa[num%10];
if(Flag==1)
{TampData[2]=0;
TampData[5]=0;
}
if(Flag==0)
{
TampData[2]=dofly_DuanMa[10];
TampData[5]=dofly_DuanMa[10];
}
display(0,8);
while((j!=8)&&(ls==num))
{ j++;
for(y=0;y<100;y++)
{
delayUs2x(100);
delayUs2x(200);
SPK=!SPK;
}
delayMs(1);
if(j==8)
SPK=0; //响声次数也不对,老是五下。为什么闹钟响完之后
// 还有频率较低的吱吱声
}
}
}
/*void Delay(unsigned int t)
{
while(--t);
}*/
void display(unsigned char FirstBit,unsigned char Num)
{ unsigned char i;
for(i=0;i<Num;i++)
{
Dataport=0;
duansuo=1;
duansuo=0;
Dataport=dofly_WeiMa[i+FirstBit];
weisuo=1;
weisuo=0;
Dataport=TampData;
duansuo=1;
duansuo=0;
delayUs2x(100);
}
}
void TIME0(void) interrupt 1
{TH0=(65535-50000/256);
TL0=(65535-50000%256);
numb++;
if(numb==20)
{
numb=0;
Flag=~Flag;
num++;
}
}
void kuaiguan()interrupt 0
{unsigned char i=1;
if(!INT0)
{
delayMs(10);
while(!INT0);
}
while(likai)
{
if(yiwei==0)
{
delayMs(10);
if(yiwei==0)
{
while(yiwei==0);
i++;
if(i==5)
i=1;
}
}
if(i==4&jian==0)
{
delayMs(10);
if(jian==0)
{ while(jian==0);
if(num>3600)
num-=3600;
}
}
if(i==3&jian==0)
{
delayMs(10);
if(jian==0)
{ while(jian==0);
if(num>600)
num-=600;
}
}
if(i==2&jian==0)
{
delayMs(10);
if(jian==0)
{ while(jian==0);
if(num>60)
num-=60;
}
}
if(i==1&jian==0)
{
if(jian==0)
{ while(jian==0);
if(num>0)
num--;
}
}
if(i==4&jia==0)
{
delayMs(10);
if(jia==0)
{ while(jia==0);
if(num<792000)
num+=3600;
}
}
if(i==3&jia==0)
{
delayMs(10);
if(jia==0)
{ while(jia==0);
if(num<792000)
num+=600;
}
}
if(i==2&jia==0)
{
delayMs(10);
if(jia==0)
{ while(jia==0);
if(num<792000)
num+=60;
}
}
if(i==1&jia==0)
{
if(jia==0)
{ while(jia==0);
if(num<792000)
num++;
}
}
TampData[0]=dofly_DuanMa[num/36000];
TampData[1]=dofly_DuanMa[num%36000/3600];
TampData[3]=dofly_DuanMa[num/600%6];
TampData[4]=dofly_DuanMa[num%600/60];
TampData[6]=dofly_DuanMa[num/10%6];
TampData[7]=dofly_DuanMa[num%10];
if(Flag==1)//
{TampData[2]=0;
TampData[5]=0;
}
if(Flag==0)
{
TampData[2]=dofly_DuanMa[10];
TampData[5]=dofly_DuanMa[10];
}TampData[7]=dofly_DuanMa[num%10];
display(0,8);
if(lings==0)
{
while(lings==0);
ls=num;
}
}
}
void delayUs2x(unsigned char n)
{
while(n--);
}
void delayMs(unsigned char n)
{
while(n--)
{
delayUs2x(245);
delayUs2x(245);
}
} |