跑表 精确到0.01s

/************************************
功能:跑表,精确到0.01s
***************************************/

#include <reg51.h>

#define DataPort P1

sbit led1 = P1^0;
sbit Seg_Latch = P2^2;   //段锁存
sbit Bit_Latch = P2^3;   //位锁存
void delay1s(void);
unsigned char Temp[8];
unsigned char code Seg_Code[]=
{
    0x3f,0x06,0x5b,0x4f, //0~3
    0x66,0x6d,0x7d,0x07, //4~7
    0x7f,0x6f,0x77,0x7c, //8~b
    0x39,0x5e,0x79,0x71, //c~f
    0x40 //c~f
};

unsigned char code Bit_Code[] = 
{
        0xfe,0xfd,0xfb,0xf7,
        0xef,0xdf,0xbf,0x7f
};

void display(unsigned char firstbit, unsigned char num)
{    
        unsigned char i;
        for(i=0;i<num;i++)
        { 
            DataPort = 0;           //清除数据
          Seg_Latch = 1;          //开门
          Seg_Latch = 0;          //关门
            
          DataPort = Bit_Code[firstbit + i]; //送位码
          Bit_Latch = 1;          //开门
          Bit_Latch = 0;          //关门
                             
          DataPort = Temp[i] ; //送段码 
          Seg_Latch = 1;          //开门
          Seg_Latch = 0;                     //关门
//            DelayUs2x(10);
        }
}

void main()
{
    unsigned char  sec = 0;    
    unsigned char  min = 0;    
    unsigned char  hour = 0;
    unsigned char count = 0;
    TMOD |= 0x01;
    TH0 = 0xd8;//(65536-10000)
    TL0 = 0xf0;//(65536-10000)
    TR0 = 1;
    while(1)
    {    
        display(0,8);
        if(TF0 == 1)
        {
            TF0 = 0;
            TH0 = 0xd8;
            TL0 = 0xf0;
            count++;
            if(count == 1)
            {
                    count = 0;
                    sec++;
                if(sec == 100)
                {
                        sec = 0;
                        min++;
                        if(min == 60)
                        {
                                min = 0;
                                hour++;
                                if(hour == 24)
                                {
                                        hour = 0;
                                }
                        }
                }
            }
        Temp[0] = Seg_Code[hour/10];
        Temp[1] = Seg_Code[hour%10];
        Temp[2] = Seg_Code[16];
        Temp[3] = Seg_Code[min/10];
        Temp[4] = Seg_Code[min%10];
        Temp[5] = Seg_Code[16];
        Temp[6] = Seg_Code[sec/10];
        Temp[7] = Seg_Code[sec%10];

    }
 }
}

猜你喜欢

转载自blog.csdn.net/paranoid_fy/article/details/81201924