51单片机实现倒计时

51单片机实现倒计时

实验环境


软件: Keil5+Proteus7

元件: AT89C51 * 1,RESPACK-8 * 1,7S3G-COM-CATHODE * 2

image-20220325121254367

仿真图


image-20220325120724059

代码实现


#include<reg51.h>
#define uchar unsigned char 
#define uint unsigned int
uchar num=60,shi,ge;

uchar code TAB[] = {
    
    
    0x3F, /*0*/
    0x06, /*1*/
    0x5B, /*2*/
    0x4F, /*3*/
    0x66, /*4*/
    0x6D, /*5*/
    0x7D, /*6*/
    0x07, /*7*/
    0x7F, /*8*/
    0x6F, /*9*/
};

void T0INTinit()
{
    
    
    TMOD = 0x01;
    //设置50毫秒延迟
    TH0 = (65536 - 50000) / 256;
    TL0 = (65536 - 50000) % 256;
    EA = 1;		//开总中断
    ET0 = 1;	//允许T1中断
    TR0 = 1;
}

void T0INT() interrupt 1
{
    
    
    uchar i;
    TH0 = (65536 - 50000) / 256;
    TL0 = (65536 - 50000) % 256;
    i++;
    //20次50毫秒就是1秒
    if (i == 20)
    {
    
    
       i = 0;
       num--;
       //当倒计时为0的时候,重新开始倒计时
       if (num == 0)
         num = 60;
    }
}

void main()
{
    
    
    T0INTinit();
    while (1)
    {
    
    
        shi = num / 10;
        ge = num % 10;
        P0 = TAB[shi];
        P2 = TAB[ge];
    }
}

成果图


请添加图片描述

猜你喜欢

转载自blog.csdn.net/qq_47200222/article/details/123732462