STC8G基于单片机+TM1637的数码管显示程序

180it 2022-04-02 PM 924℃ 0条

Snipaste_2022-04-02_18-57-26.png
Snipaste_2022-04-02_18-57-34.png

#include <STC8G.h>
#include <intrins.h>

#define u8 unsigned char
#define u16 unsigned int

sbit DIO = P1^0;
sbit CLK = P1^1;

//u8 num[10]={0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7f,0x6F};
u8 num[10]={0xBF,0x86,0xDB,0xCF,0xE6,0xED,0xFD,0x87,0xFf,0xEF};
u16 ms,second,min,hour;

void Delay2us()                //@11.0592MHz
{
        unsigned char i;

        _nop_();
        i = 5;
        while (--i);
}

void Delay3us()                //@11.0592MHz
{
        unsigned char i;

        i = 9;
        while (--i);
}

void Delay5us()                //@11.0592MHz
{
        unsigned char i;

        _nop_();
        i = 16;
        while (--i);
}

void I2CStart()
{
        CLK = 1;
        DIO = 1;
        Delay2us();
        DIO = 0;
}

void I2CAsk()
{
        CLK = 0;
        Delay5us();
        while(DIO);
        CLK = 1;
        Delay2us();
        CLK = 0;
}

void I2CStop()
{
        CLK = 0;
        Delay2us();
        DIO = 0;
        Delay2us();
        CLK = 1;
        Delay2us();
        DIO = 1;
}

void I2CWriteByte(u8 dat)
{
        u8 i;
        for(i=0;i<8;i++)
        {
                CLK = 0;
                if(dat&0x01)
                {
                        DIO = 1;
                }
                else
                {
                        DIO = 0;
                }
                Delay3us();
                dat>>=1;
                CLK = 1;
                Delay3us();
        }
}

void Display(u8 m,u8 s)
{
        I2CStart();
        I2CWriteByte(0x40);
        I2CAsk();
        I2CStop();
        I2CStart();
        I2CWriteByte(0xC0);
        I2CAsk();
        I2CWriteByte(num[m/10]);
        I2CAsk();
        I2CWriteByte(num[m%10]);
        I2CAsk();
        I2CWriteByte(num[s/10]);
        I2CAsk();
        I2CWriteByte(num[s%10]);
        I2CAsk();
        I2CWriteByte(0xFF);
        I2CAsk();
        I2CWriteByte(0xFF);
        I2CAsk();
        I2CStop();
        I2CStart();
        I2CWriteByte(0x8F);
        I2CAsk();
        I2CStop();
}

void Timer0Init(void)                //1毫秒@11.0592MHz
{
        AUXR |= 0x80;
        TMOD &= 0xF0;
        TL0 = 0xCD;
        TH0 = 0xD4;
        ET0 = 1;
        EA = 1;
        TF0 = 0;
        TR0 = 1;
}

void TM0_Isr() interrupt 1
{
        ms++;
        if(ms==1000)
        {
                ms=0;
                second++;
                if(second==60)
                {
                        second=0;
                        min++;
                        if(min==60)
                        {
                                min=0;
                                hour++;
                                if(hour==24)
                                {
                                        hour=0;
                                }
                        }
                }
        }
}

void main()
{
        P1M0 = 0x00;
        P1M1 = 0x00;
        Timer0Init();
        while(1)
        {
                Display(min,second);
        }
}

支付宝打赏支付宝打赏 微信打赏微信打赏

如果文章或资源对您有帮助,欢迎打赏作者。一路走来,感谢有您!

标签: none

STC8G基于单片机+TM1637的数码管显示程序