51 MCU timing 5 minutes and digital tube display the specific code for 360 seconds (ie 5 minutes)

/*Timer 1, timing mode working mode 1 16-bit counter,
enable buzzer after timing 5 minutes*/
#include <reg52.h> //51 header file
#define uchar unsigned char//Macro definition
#define uint unsigned int//Macro definition
sbit wei = P2^7;//Bit definition digital tube bit selection latch interface
sbit duan = P2^6;//Bit definition digital tube segment selection latch interface
sbit beep = P2^3; //Buzzer interface

//数码管段选表
uchar code leddata[]={
 
                0x3F,  //"0"
                0x06,  //"1"
                0x5B,  //"2"
                0x4F,  //"3"
                0x66,  //"4"
                0x6D,  //"5"
                0x7D,  //"6"
                0x07,  //"7"
                0x7F,  //"8"
                0x6F,  //"9"
                0x77,  //"A"
                0x7C,  //"B"
                0x39,  //"C"
                0x5E,  //"D"
                0x79,  //"E"
                0x71,  //"F"
                0x76,  //"H"
                0x38,  //"L"
                0x37,  //"n"
                0x3E, //"u"
                0x73, //"P"
                0x5C, //"o"
                0x40, //"-"
                0x00, //off
                0x00 //custom
 
                         };

//Millisecond delay function
void delay(uint z) //software delay 1ms
{
    uint x,y;
    for(x = z; x > 0; x--)
        for(y = 114; y > 0 ; y --);
}

//Dynamic display function of two-digit digital tube
void display(uchar i)
{
    uchar bai,shi, ge;
    bai = i / 100; //The integer part of modulo i divided by 100
    shi = i / 10 % 10;// Divide modulo i by 10 and then modulo 10 to take the remainder
    ge = i % 10;//to find the remainder i divide by 10 and take the remainder

    P0 = 0xff; //Clear the broken code
    wei = 1;//Turn on the bit selection
    P0 = 0xfe;//1111 1110 only select the first digital tube
    wei = 0; //Turn off the bit selection

    duan = 1; //Open segment selection
    P0 = leddata[bai];
    duan = 0; //Close segment selection
    delay(5);//Delay 5ms
    
    P0 = 0xff;//Clear broken code
    wei = 1; / /Open the bit selection
    P0 = 0xfd;//1111 1101 only strobe the second digital tube
    wei = 0; //Close the bit selection
    
    duan = 1;//Open the segment selection
    P0 = leddata[shi];
    duan = 0; / /Close segment selection
    delay(5);//Delay 5 milliseconds

    
    P0 = 0xff;//Clear the broken code
    wei = 1; //Open the bit selection
    P0 = 0xfb;//1111 1011 only select the third digital tube
    wei = 0; //Close the bit selection
    
    duan = 1;//Open Segment selection
    P0 = leddata[ge];
    duan = 0; //close segment selection
    delay(5);//delay 5 milliseconds    
}

void main()
{
        uchar a;//50 times count
        uint b;//Second count
        TR1 = 1;//Start T1
        TMOD = 0x10;//T1 is a timer, working mode 1 16-bit counter
        TH1 = 0x4b;
        TL1 = 0xfc;//0x4bfc timing 50ms                
        while(1)
        {
            if(TF1 == 1)//Determine whether T1 overflows
            {
                TH1 = 0x4b;
                TL1 = 0xfc;//0x4bfc timing 50ms
                TF1 = 0;//Clear to facilitate the next Time judgment
                a++;//50ms count plus 1    
            }
            if(a == 20)//Judgment whether it is 1 second
            {
                a = 0;//Clear to record the number of 50ms next time
                b++;//Second plus 1
            }

            display(b);//Display the value of seconds

            if(b == 360)//Check if it is 5 minutes
            {
                TR1 = 0;//Time to close timer 1
                beep = 0; //Enable buzzer
                delay(4000);//Enable time 4 seconds
                beep = 1;//Turn off the buzzer
                while(1); //Stop the program
            }
        }
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324433209&siteId=291194637