上面是加了蜂鸣器的电路图
实验的要求是:
目前前三点已经可以通过下面的编程达到要求,第四点,我在电路图上加了蜂鸣器,但是不会写程序,急!!!!!!
编程:#include <reg51.h>
#define uchar unsigned char
#define uint unsigned int
sbit set=P1^0;
sbit save=P1^5;
sbit rselect=P1^1;
sbit lselect=P1^2;
sbit add=P1^3;
sbit reduce=P1^4;
uchar code tab[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};
uchar disp_buf[6];
uchar disp_bit=0;
uint hour,min,sec;
uint select_num=0;
uint hour_adj,min_adj,sec_adj;
uchar count;
uchar key_num=0;
void key_scan()
{
if(!set)
{
hour_adj=hour;
min_adj=min;
sec_adj=sec;
key_num++;
}
while(!set);
if(key_num%2==1)
{
if(rselect==0)
{
select_num++;
if(select_num==4)
select_num=1;
}
while(!rselect);
if(lselect==0)
{
select_num--;
if(select_num<=0)
select_num=3;
}
while(!lselect);
}
if(!add&&(key_num%2))
{
switch(select_num)
{
case 1 :
{
hour_adj++;
if(hour_adj==24)
hour_adj=0;
break;
}
case 2 :
{
min_adj++;
if(min_adj==60)
min_adj=0;
break;
}
case 3 :
{
sec_adj++;
if(sec_adj==60)
sec_adj=0;
break;
}
default:break;
}
while(!add);
}
if(!reduce&&(key_num%2))
{
switch(select_num)
{
case 1 :
{
hour_adj--;
if(hour_adj<=0)
hour_adj=23;
break;
}
case 2 :
{
min_adj--;
if(min_adj<=0)
min_adj=59;
break;
}
case 3 :
{
sec_adj--;
if(sec_adj<=0)
sec_adj=59;
break;
}
default:break;
}
while(!reduce);
}
if(!save&&(key_num%2))
{
select_num=0;
hour=hour_adj;
min=min_adj;
sec=sec_adj;
while(!save);
}
}
/*Ö÷º¯Êý*/
void main()
{
TMOD=0x11;
TH0=0xf7;
TL0=0x00;
TH1=0x4c;
TH0=0x00;
ET0=1;
ET1=1;
EA=1;
TR0=1;
TR1=1;
PT1=1;
hour=20;
min=02;
sec=07;
count=0;
while(1)
{
key_scan();
if(key_num%2)
{
switch(select_num)
{
case 1 :
{
if(count<=10)
{
disp_buf[0]=hour_adj/10;
disp_buf[1]=hour_adj%10;
}
else
{
disp_buf[0]=0x40;
disp_buf[1]=0x40;
}
disp_buf[2]=min_adj/10;
disp_buf[3]=min_adj%10;
disp_buf[4]=sec_adj/10;
disp_buf[5]=sec_adj%10;
break;
}
case 2 :
{
if(count<=10)
{
disp_buf[2]=min_adj/10;
disp_buf[3]=min_adj%10;
}
else
{
disp_buf[2]=0x40;
disp_buf[3]=0x40;
}
disp_buf[0]=hour_adj/10;
disp_buf[1]=hour_adj%10;
disp_buf[4]=sec_adj/10;
disp_buf[5]=sec_adj%10;
break;
}
case 3 :
{
if(count<=10)
{
disp_buf[4]=sec_adj/10;
disp_buf[5]=sec_adj%10;
}
else
{
disp_buf[4]=0x40;
disp_buf[5]=0x40;
}
disp_buf[0]=hour_adj/10;
disp_buf[1]=hour_adj%10;
disp_buf[2]=min_adj/10;
disp_buf[3]=min_adj%10;
break;
}
default : break;
}
}
if(key_num%2==0)
{
disp_buf[0]=hour/10;
disp_buf[1]=hour%10;
disp_buf[2]=min/10;
disp_buf[3]=min%10;
disp_buf[4]=sec/10;
disp_buf[5]=sec%10;
}
}
}
void timer0() interrupt 1
{
TH0=0xf7;
TL0=0x00;
P2=~(0x01<<disp_bit);
P0=tab[disp_buf[disp_bit]];
disp_bit++;
if(disp_bit==6) disp_bit=0;
}
void timer1() interrupt 3
{
TH1=0x4c;
TL1=0x00;
if(++count==20)
{
count=0;
if(++sec==60)
{
sec=0;
if(++min==60)
{
min=0;
if(++hour==24)
{
hour=0;
}
}
}
}
}
|