求一个99秒倒计时单片机程序和仿真,基于51单片机。

2024-12-12 14:52:07
推荐回答(3个)
回答1:

#include "reg51.h"
sbit start=P3^2; //外部中断0引脚 开始
sbit rst=P3^3; //外部中断1引脚 复位
sbit breakk=P3^4; //暂停倒计时
sbit led=P3^5; //Led灯
sbit beep=P3^7; //蜂鸣器控制引脚

sbit P30=P3^0; //数码管位选
sbit P31=P3^1;

void didi(void);
unsigned char code duanma[]=
{
0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0xff
};

char time=99; //倒计时缓存
char flag_t=0; // 1秒辅助计时
char now=0; // 0停止 1 计时 2报警 3复位

unsigned char g=0,s=3,i=0;

void delay(unsigned int n)
{
while(--n);
}

void t0_srv() interrupt 1 using 1
{
TH0=0X3C;
TL0=0XB0;
flag_t++;
if(flag_t>=20){ //20*50ms=1s
flag_t=0;
time--;
if(time<=0){
// beep=0;
didi();
didi();
didi();
led=0;
TR0=0;
now=2;
time=0;
}
}
}
void button_start() interrupt 0 using 1 //外部中断0
{
delay(1500);
if(start==0) {
now=1;
TR0=1;
}
else return;
while(start==0);
delay(1500);

}
void button_rst() interrupt 2 using 1
{
delay(1500);
if(rst==0) {
TR0=0;
time=99;// 3;
i=0;
led=1;
now=0;
}
else return;
while(rst==0);
delay(1500);

}

void disp()
{

g=time%10;
s=time/10;
g=duanma[g];
s=duanma[s];

P0=~g;
P31=0;
delay(250);
P31=1;

P0=~s;
P30=0;
delay(250);
P30=1;

}
void key()
{
if(breakk==0)delay(1500);
else return;
if(breakk==0) {
if(now==1){
i++;
if(i%2)TR0=0;
if(!(i%2))TR0=1;
}
}
else return;
while(breakk==0);
delay(1500);
}
/* 延时250us*k */
void delay250(unsigned int k)
{
unsigned int j,i;
for (i=0;i {
for(j=0;j<25;j++);
}
}
void didi(void)
{
unsigned int j;
for (j=0;j<400;j++) //loop 400,响0.2s
{
beep=0;
delay250(2);
beep=1;
delay250(2);
}
delay250(800); //stop 0.2s

}

main()
{
TMOD=0X01;//定时器0工作在方式1,
EA=1;
IT0=1;
ET0=1;
TH0=0X3C;//在12M 晶振下,定时时间为50ms
TL0=0XB0;

EX0=1;
EX1=1;

//TR0=1;
while(1){
disp();
key();
}

}

回答2:

10分?

回答3:

代做