秒倒计时,时间到了蜂鸣器发声
仿真原理图如下(proteus仿真工程文件可到本帖附件中下载)
单片机源程序如下:
#include <reg51.h>
unsigned char count=0;
sbit LED_B1=P3^6;
sbit LED_A1=P3^7;
sbit fmq=P1^0;
unsigned char counter;
unsigned char NSF=10;
typedef unsigned int u16; //对数据类型进行声明定义
typedef unsigned char u8;
sbit k1=P3^1;
sbit k2=P3^0;
sbit beep=P1^0;
u8 start=0;
void delayms(unsigned int xms);
void Init(void)
{
TMOD=0x01;
TH0=(65536-10000)/256;
TL0=(65536-10000)%256;
TR0=1;
ET0=1;
EA=1;
}
void timer0(void) interrupt 1
{
TMOD=0x01;
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
TR0=1;
ET0=1;
EA=1;
count++;
if(count>19)
{
count=0;
NSF--;
}
}
void delayms(unsigned int xms)
{
unsigned int i,j;
for(i=xms;i>0;i--)
for(j=110;j>0;j--);
}
unsigned char table[]={0x3,0xcf,0x2a,0x4a,0xc6,0x52,0x12,0xcb,0x2,0x42};
void display1(unsigned char num1,unsigned char num2)
{
LED_A1=1;
P0=table[num1];
delayms(10);
LED_A1=0;
LED_B1=1;
P0=table[num2];
delayms(10);
LED_B1=0;
}
void main()
{
while(1)
{
EA=1; beep=1;
Init();
while(NSF!=0)
{
display1(NSF/10,NSF%10);
delayms(10);
}
Keil代码与Proteus仿真下载:
30秒倒计时.zip
(81.06 KB, 下载次数: 115)
|