修改程序使数码管暂停计数:#include <REGX51.H> unsigned char x[]={0xC0,0xF9,0XA4,0XB0,0X99,0X92,0X82,0XF8,0X80,0X90,0xBF,0xff}; unsigned char sec=0,min=0,hour=0; unsigned char a=0; void delay(unsigned int xms) //@12.000MHz { unsigned char i, j; while(xms) { i = 2; j = 239; do { while (--j); } while (--i); xms=xms-1; } } void fun(unsigned char location,number) { switch(location) { case 0:P3=0x01;break; case 1:P3=0x02;break; case 2:P3=0x04;break; case 3:P3=0x08;break; case 4:P3=0x10;break; case 5:P3=0x20;break; case 6:P3=0x40;break; case 7:P3=0x80;break; } P0=x[number]; delay(2); P0=0xff; } void main(void) { TMOD=0X01; TL0=(65536-10000)%256; TH0=(65536-10000)/256; TR0=1; ET0=1; EA=1; while(1) { if(P1_2==0) { delay(20); while(P1_2==0); delay(20); sec++; } if(P1_1==0) { delay(20); while(P1_1==0); delay(20); min++; } if(P1_0==0) { delay(20); while(P1_0==0); delay(20); hour++; } if(P2_0==0) { delay(20); while(P2_0==0); delay(20); sec=0; min=0; hour=0; } fun(0,sec%10); fun(1,sec/10); fun(2,10); fun(3,min%10); fun(4,min/10); fun(5,10); fun(6,hour%10); fun(7,hour/10); } } void timer0_start(void) interrupt 1 { TL0=(65536-10000)%256; TH0=(65536-10000)/256; a++; if(a==100) { sec++; a=0; if(sec==60) { min++; sec=0; } if(min==60) { hour++; min=0; } } if(sec==20&&min==1&&hour==0) { P2_0=1; P2_1=0; } }
时间: 2023-08-04 14:05:27 浏览: 159
51单片机定时器数码管30秒倒计时三个按键控制开始暂停.docx
在程序中加入一个标志变量,代表是否暂停计数。可以在按下按钮时修改该变量的值,使数码管停止计数。修改后的程序如下所示:
```
#include <REGX51.H>
unsigned char x[]={0xC0,0xF9,0XA4,0XB0,0X99,0X92,0X82,0XF8,0X80,0X90,0xBF,0xff};
unsigned char sec=0,min=0,hour=0;
unsigned char a=0;
bit pause_count = 0; //新增的标志变量
void delay(unsigned int xms) //@12.000MHz
{
unsigned char i, j;
while(xms)
{
i = 2;
j = 239;
do
{
while (--j);
} while (--i);
xms=xms-1;
}
}
void fun(unsigned char location,number)
{
switch(location)
{
case 0:P3=0x01;break;
case 1:P3=0x02;break;
case 2:P3=0x04;break;
case 3:P3=0x08;break;
case 4:P3=0x10;break;
case 5:P3=0x20;break;
case 6:P3=0x40;break;
case 7:P3=0x80;break;
}
P0=x[number];
delay(2);
P0=0xff;
}
void main(void)
{
TMOD=0X01;
TL0=(65536-10000)%256;
TH0=(65536-10000)/256;
TR0=1;
ET0=1;
EA=1;
while(1)
{
if(P1_2==0)
{
delay(20);
while(P1_2==0);
delay(20);
sec++;
}
if(P1_1==0)
{
delay(20);
while(P1_1==0);
delay(20);
min++;
}
if(P1_0==0)
{
delay(20);
while(P1_0==0);
delay(20);
hour++;
}
if(P2_0==0)
{
delay(20);
while(P2_0==0);
delay(20);
sec=0;
min=0;
hour=0;
}
if(!pause_count) //新增的判断语句
{
fun(0,sec%10);
fun(1,sec/10);
fun(2,10);
fun(3,min%10);
fun(4,min/10);
fun(5,10);
fun(6,hour%10);
fun(7,hour/10);
}
}
}
void timer0_start(void) interrupt 1
{
TL0=(65536-10000)%256;
TH0=(65536-10000)/256;
a++;
if(a==100)
{
sec++;
a=0;
if(sec==60)
{
min++;
sec=0;
}
if(min==60)
{
hour++;
min=0;
}
}
if(sec==20&&min==1&&hour==0)
{
P2_0=1;
P2_1=0;
}
}
```
阅读全文