修改单片机C语言程序:0~59秒计数器;1个独立按键控制,第一次按按键,实现暂停,第二次按按键,实现复位

2025-02-23 10:45:21
推荐回答(2个)
回答1:

#include

#include

#define uint unsigned int

#define uchar unsigned char

sbit KEY = P3^0;

uchar aa,num,flag;

uchar code table[]={

    0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,

    0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71};

void delay(uint ms)

{

    uchar t;

    while(ms--)  for(t = 0; t < 123; t++);

}

void main()

{

    num = 0;

    aa = 0;

    TMOD = 0x01;

    TH0 = (65536 - 50000) / 256;

    TL0 = (65536 - 50000) % 256;

    TR0 = 1;

    ET0 = 1;

    EA = 1;

    P0 = table[0];

    P2 = table[0];

    while(1)  {

      if(!KEY) {

        delay(10);

        if(!KEY) {

          while (!KEY);

          flag++;

          if(flag == 1)  TR0 = 0;

          if(flag == 2)  {

            P0 = table[0];

            P2 = table[0];

            flag = 0;

    aa = 0;

            num = 0;

            TR0 = 1;

    } } } }

}

void timer0() interrupt 1 

{

    TH0 = (65536 - 50000) / 256;

    TL0 = (65536 - 50000) % 256;

    aa++;

    if(aa == 20)  {

      aa = 0;

      num++;

      if(num == 60)  num = 0;

      P0 = table[num / 10];

      P2 = table[num % 10];  

    } 

仿真实验截图如下:

回答2:

#include
#include
unsigned char i, j;
unsigned char code table[] = {
0xc0, 0xf9, 0xa4, 0xb0, 0x99,
0x92, 0x82, 0xf8, 0x80, 0x90
};
sbit QI_DONG = P1^1;
sbit TING_ZH = P1^2;
sbit QING_LY = P1^3;

bit k;
//---------------------------------------------
void Delay_1ms(unsigned int x)
{
unsigned int i, j;
for(i = 0; i < x; i++) for(j = 0; j <= 148; j++);
}
//---------------------------------------------
void main( )
{
TMOD = 0x01; //;T0定时方式1
TH0 = (65536-50000) / 256; //;50ms@12MHz
TL0 = (65536-50000) % 256;
TR0 = 1; //;定时器初始化结束.
ET0 = 1; //;开启定时中断.
EX0 = 1;
EX1 = 1;
EA = 1;
i = 0;
j = 0;
P0 = 0xC0;
P2 = 0xC0;
while(1) {
if (QI_DONG == 0) {
Delay_1ms(10); while(QI_DONG == 0); k = 1;
}
if (TING_ZH == 0) {
Delay_1ms(10); while(TING_ZH == 0); k = 0;
}
if (QING_LY == 0) {
Delay_1ms(10); while(QING_LY == 0);
i = 0; j = 0; P0 = 0xC0; P2 = 0xC0;
}
}
}
//---------------------------------------------
void timer0(void) interrupt 1
{
TL0 = (65536-50000) % 256;
TH0 = (65536-50000) / 256; //;50ms@12MHz
if (k) {
j++;
if (j == 20) {
j = 0;
i++;
if (i == 100) i = 0;
P0 = table[i / 10];
P2 = table[i % 10];
}
}
}