利用51单片机做一个简易时钟

利用独立键盘,数码管完成一个简易的时钟,按键一可以修改时间,按键二可以设置闹钟,按键三进行加一操作,按键四进行减一操作。

主函数:

void main()
{
    bsp_init();
    while(1)
    {
        TimeTim();              //正常时间在走
        bsp_KeyScan();          //键盘扫描看哪个功能键被按下
        bsp_KeyProc(keyvalue);  //根据键值进行相应的功能处理 
        SetValueShow();         //数码管上显示的值
        ShowSegCC();            //显示在数码管上
        bsp_CheckAlarm();       //闹钟检查
    }   
}

时钟模块:

unsigned char hour = 2, min = 2, sec = 3;       //设置时间初值
unsigned char ahour = 0, amin = 0, asec = 0;    //设置闹钟初值
unsigned char time = 0;

void delay_us(unsigned char t)
{
    while(--t);
}

void delay_ms(unsigned int t)   //t最大为255
{
    while(t--)
    {
        delay_us(245);
        delay_us(245);
    }
}

void TimeTim()
{
     if(settype == 0 && alarmvalue == 0)     //当不在闹钟状态也不在修改时间状态的时候时间正常走
     {
         if(count == 20)
         {
            count = 0;
            sec++;
            if(sec == 60)
            {
                sec = 0;
                min++;
                if(min == 60)
                {
                    min = 0;
                    hour++;
                    if(hour == 24)
                    {
                        hour = 0;
                    }
                }
            }
         }
    }
}

//检查时间是否到了闹钟时间
void bsp_CheckAlarm(void)
{
    if((hour == ahour) && (min == amin) && (sec == asec))
    {
        hour = 0;
        min = 0;
        sec = 0;
    }
}

键盘模块:

sbit SETTIME = P0^0;    //修改时间
sbit SETALARM = P0^1;   //设置闹钟
sbit INC = P0^2;        //加1
sbit DEC = P0^3;        //减1

unsigned char keyvalue = 0;
unsigned char settype = 0;      //0是初始状态正常显示,1是修改秒,2是修改分,3是修改时
unsigned char alarmvalue = 0;   //0是初始状态,1是修改秒,2是修改分,3是修改时

void bsp_KeyScan(void)
{
    if(SETTIME == 0)//K1按键被按下
    {
        keyvalue = 1;
        while(!SETTIME);//等待按键释放

    } 

    if(SETALARM == 0)
    {
        keyvalue = 2;
        while(!SETALARM);

    }

    if(INC == 0)
    {
        keyvalue = 3;
        while(!INC);
    }

    if(DEC == 0)
    {
        keyvalue = 4;
        while(!DEC);
    }
}

void bsp_KeyProc(unsigned char keyv)
{
    if(keyv == 1)
    {
        settype++;
        if(settype == 4)
        {
            settype = 0;
        }
        keyvalue = 0;    //键值要清0!!!
    }

    if(keyv == 2)
    {
        alarmvalue++;
        if(alarmvalue == 4)
        {
            alarmvalue = 0;
        }
        keyvalue = 0;
    }

    if(keyv == 3)
    {
        timeadd();
        keyvalue = 0;
    }

    if(keyv == 4)
    {
        timedec();
        keyvalue = 0;
    }
}

void timeadd()
{
    //设置时间加1
    if(settype == 1)
    {
        if(sec < 59)
        {
            sec++;
        }

        else
        {
            sec = 0;
        }
    }

    else if(settype == 2)
    {
        if(min < 59)
        {
            min++;
        }

        else
        {
            min = 0;
        }
    }

    else if(settype == 3)
    {
        if(hour < 23)
        {
            hour++;
        }

        else
        {
            hour = 0;
        }
    }

    //设置闹钟加1 
    if(alarmvalue == 1)
    {
        if(asec < 59)
        {
            asec++;
        }

        else
        {
            asec = 0;
        }
    }

    else if(alarmvalue == 2)
    {
        if(amin < 59)
        {
            amin++;
        }

        else
        {
            amin = 0;
        }
    }

    else if(alarmvalue == 3)
    {
        if(ahour < 23)
        {
            ahour++;
        }

        else
        {
            ahour = 0;
        }
    }
}

void timedec()
{

    //设置时钟减1
    if(settype == 1)
    {
        if(sec > 0)
        {
            sec--;
        }

        else
        {
            sec = 59;
        }
    }

    else if(settype == 2)
    {
        if(min > 0)
        {
            min--;
        }

        else
        {
            min = 59;
        }
    }

    else if(settype == 3)
    {
        if(hour > 0)
        {
            hour--;
        }

        else
        {
            hour = 23;
        }
    }


    //设置闹钟减1
    if(alarmvalue == 1)
    {
        if(asec > 0)
        {
            asec--;
        }

        else
        {
            asec = 59;
        }
    }

    else if(alarmvalue == 2)
    {
        if(amin > 0)
        {
            amin--;
        }

        else
        {
            amin = 59;
        }
    }

    else if(alarmvalue == 3)
    {
        if(ahour > 0)
        {
            ahour--;
        }

        else
        {
            ahour = 23;
        }
    }
}

数码管模块:

#define SEGPORT P1
sbit WEI = P2^1;
sbit DUAN = P2^0;

unsigned char code TableCA[16]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0x88,0x83,0xc6,0xa1,0x86,0x8e};  //共阳数码管段选码表,无小数点
unsigned char code TableSel[8]={0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f};//共阴数码管位选码表
unsigned char code TableCC[16]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71}; //共阴数码管段选码表,无小数点
unsigned char Show[8] = {0};
//unsigned int showvalue = 0;
//unsigned int time = 0;

void ShowSegCA(unsigned char t)
{
    SEGPORT = TableCA[t];   
}
//1到255显示
void ShowSegCC(void)
{
    static unsigned char i = 0;

    //消隐
    SEGPORT = 0x00;
    DUAN = 1;
    DUAN = 0;

    //选通位码
    WEI = 1;
    SEGPORT = TableSel[i];
    WEI = 0;

    //选通位码
    DUAN = 1;
    SEGPORT = Show[i];
    DUAN = 0;

    i++;
    if(i == 8)
    {
        i = 0;
    }
    delay_ms(1);
}   

void SetValueShow(void)
{
    if(alarmvalue == 0) //当不在闹钟状态时数码管显示正常的时间
    {
        Show[0] = TableCC[hour/10];
        if(settype == 3)
        Show[1] = TableCC[hour%10] | 0x80;
        else
        Show[1] = TableCC[hour%10];
        Show[2] = 0x40;
        Show[3] = TableCC[min/10];
        if(settype == 2)
        Show[4] = TableCC[min%10] | 0x80;
        else
        Show[4] = TableCC[min%10];
        Show[5] = 0x40;
        Show[6] = TableCC[sec/10];
        if(settype == 1)
        Show[7] = TableCC[sec%10] | 0x80;
        else
        Show[7] = TableCC[sec%10];
    }

    //在闹钟状态数码管显示闹钟设置的时间
    else
    {
        Show[0] = TableCC[ahour/10];
        if(alarmvalue == 3)
        Show[1] = TableCC[ahour%10] | 0x80;
        else
        Show[1] = TableCC[ahour%10];
        Show[2] = 0x40;
        Show[3] = TableCC[amin/10];
        if(alarmvalue == 2)
        Show[4] = TableCC[amin%10] | 0x80;
        else
        Show[4] = TableCC[amin%10];
        Show[5] = 0x40;
        Show[6] = TableCC[asec/10];
        if(alarmvalue == 1)
        Show[7] = TableCC[asec%10] | 0x80;
        else
        Show[7] = TableCC[asec%10];
    }
}   

定时器模块:

unsigned char count = 0;
void Timer0_Init(void)
{
    TMOD |= 0x01;
    TH0 = (65536 - 50000)/256;
    TL0 = (65536 - 50000)%256;
    EA = 1;
    ET0 = 1;
    TR0 = 1;
}

void Timer0(void) interrupt 1
{
    TH0 = (65536 - 50000)/256;
    TL0 = (65536 - 50000)%256;
    count++;
}
©️2020 CSDN 皮肤主题: 编程工作室 设计师:CSDN官方博客 返回首页