1024手机基地看电影,午夜福利视频导航,国产精品福利在线一区,亚洲欧美日韩另类成人,在线观看午夜日本理论片,成年超爽免费网站,国产精品成人免费,精品动作一级毛片,成人免费观看网站,97精品伊人久久大香蕉

標(biāo)題: 數(shù)碼管時(shí)鐘 [打印本頁(yè)]

作者: csdn    時(shí)間: 2018-5-28 13:29
標(biāo)題: 數(shù)碼管時(shí)鐘
#include<reg52.h>
#include<intrins.h>
#define uint unsigned int
#define uchar unsigned char

#define LS P0                                //段選

sbit LED=P2^0;                        //設(shè)置狀態(tài),0-LED指示燈亮

sbit LSA=P2^2;                        //位選
sbit LSB=P2^3;
sbit LSC=P2^4;

sbit KEY_Set=P3^1;                //設(shè)置鍵,0-按下
sbit KEY_Move=P3^0;                //移位鍵,0-按下
sbit KEY_Add=P3^2;                //數(shù)加鍵,0-按下
sbit KEY_Reduce=P3^3;                //數(shù)減鍵,0-按下

bit Flash=1;

uchar Setflag=1;                //設(shè)置標(biāo)志位 0-設(shè)置狀態(tài),1-非設(shè)置狀態(tài)
uchar Moveflag=2;                //移位標(biāo)志位 0-分針,1-時(shí)針,2-無(wú)效
uchar Addflag=1;                //數(shù)加標(biāo)志位 0-數(shù)加,1-無(wú)效
uchar Reduceflag=1;                //數(shù)減標(biāo)志位 0-數(shù)減,1-無(wú)效


static uchar shi=0,fen=0,miao=0;                //時(shí)/分/秒-高低八位

uchar code DIG_CODE[11]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};//顯示0~9的值

uchar T=0;                        //計(jì)算定時(shí)器循環(huán)次數(shù)

void Delay(uchar t)
{
    unsigned char a,b;
                for(;t>0;t--)
                        for(b=13;b>0;b--)
                                        for(a=20;a>0;a--);
}

void Init()
{
        TMOD=0x11;                        //定時(shí)器0中斷方式1,定時(shí)器1中斷方式1

        //50ms
        TH0=(65536-50000)/256;                        //賦初值
        TL0=(65536-50000)%256;

        EA=1;                        //總中斷打開(kāi)

        ET0=1;                        //中斷 定時(shí)器0打開(kāi)

        IT0=1;                        //下降沿有效

        TR0=1;                        //定時(shí)器0啟動(dòng)
                                //50ms時(shí)間到,跳轉(zhuǎn)到中斷程序中
}

void Timer0() interrupt 1
{
        TH0=(65536-50000)/256;                        //定時(shí)T*50ms--T=20--1s
        TL0=(65536-50000)%256;
        T++;
}

// void Timer1() interrupt 1
//{
//        switch(Moveflag)
//        {
//                case 0:                                 break; //miao
//                case 1:                                 break; //shi
//                case 2:                                 break; //feng
//        }
//}
void Clock()
{
        if(Setflag==1)                //時(shí)鐘狀態(tài)
        {
        TR1=1;
                if(T==20)
                {
                        T=0;
                        miao++;
                        if(miao==60)
                        {       
                                miao=0;
                                fen++;
                                if(fen==60)
                                {
                                        fen=0;
                                        shi++;
                                        if(shi==24)
                                        {
                                                shi=0;
                                        }
                                }
                               
                        }
                }          
        }
        else                //設(shè)置狀態(tài)
          {
                TR1=0;
                if(Moveflag==0)                //移位鍵有效-秒針
                {
                        if(Addflag==0)                //數(shù)加鍵有效,數(shù)加
                        {
                                miao++;
                                if(miao==60)
                                {
                                        miao=0;
                                }
                                Addflag=1;
                        }
                        if(Reduceflag==0)                //數(shù)減鍵有效,數(shù)減
                        {
                                miao--;
                                if(miao==-1)
                                {
                                        miao=59;
                                }
                                Reduceflag=1;
                        }
                }
                if(Moveflag==1)                //移位鍵有效-分針
                {
                        if(Addflag==0)                //數(shù)加鍵有效,數(shù)加
                        {
                                fen++;
                                if(fen==60)
                                {
                                        fen=0;
                                }
                                Addflag=1;
                        }
                        if(Reduceflag==0)                //數(shù)減鍵有效,數(shù)減
                        {
                                fen--;
                                if(fen==-1)
                                {
                                        fen=59;
                                }
                                Reduceflag=1;
                        }
                }
                if(Moveflag==2)                //移位鍵有效-時(shí)針
                {
                        if(Addflag==0)                //數(shù)加鍵有效,數(shù)加
                        {
                                shi++;
                                if(shi==24)
                                {
                                        shi=0;
                                }
                                Addflag=1;
                        }
                        if(Reduceflag==0)                //數(shù)減鍵有效,數(shù)減
                        {
                                shi--;
                                if(shi==-1)
                                {
                                        shi=23;
                                }
                                Reduceflag=1;
                        }
                }
        }       
}

void Key_Scan()
{
        static uchar SaveSet=1;                //保存設(shè)置鍵狀態(tài) 0-按下,1-彈起
        static uchar SaveMove=1;                //保存移位鍵狀態(tài) 0-按下,1-彈起
        static uchar SaveAdd=1;                //保存數(shù)加鍵狀態(tài) 0-按下,1-彈起
        static uchar SaveReduce=1;                //保存數(shù)減鍵狀態(tài) 0-按下,1-彈起
       
        uchar Set=1;                //當(dāng)前設(shè)置按鍵狀態(tài) 彈起
        uchar Move=1;                //當(dāng)前設(shè)置按鍵狀態(tài) 彈起
        uchar Add=1;                //當(dāng)前設(shè)置按鍵狀態(tài) 彈起
        uchar Reduce=1;                //當(dāng)前設(shè)置按鍵狀態(tài) 彈起
       
//獲取各按鍵狀態(tài)       
        if(KEY_Set==0)                //設(shè)置鍵按下
        {
                Delay(1);
                if(KEY_Set==0)
                {
                        Set=0;
                }
                else
                {
                        Set=1;
                }
        }
        else
        {
               
                Set=1;
        }
       
        if(KEY_Move==0)                //移位鍵按下
        {
                Delay(1);
                if(KEY_Move==0)
                {
                        Move=0;
                }
                else
                {
                        Move=1;
                }
        }
        else
        {
                Move=1;
        }
       
        if(KEY_Add==0)                //數(shù)加鍵按下
        {
                Delay(1);
                if(KEY_Add==0)
                {
                        Add=0;
                }
                else
                {
                        Add=1;
                }
        }
        else
        {
                Add=1;
        }
       
        if(KEY_Reduce==0)                //數(shù)減鍵按下
        {
                Delay(1);
                if(KEY_Reduce==0)
                {
                        Reduce=0;
                }
                else
                {
                        Reduce=1;
                }
        }
        else
        {
                Reduce=1;
        }
       
//按鍵按下?tīng)顟B(tài)處理
        if(SaveSet!=Set)                //設(shè)置鍵狀態(tài)變化
        {
                if(Set==0)                //當(dāng)前設(shè)置鍵按下
                {
                        if(Setflag==1)                //不在設(shè)置狀態(tài)
                        {
                                LED=0;                //指示燈亮
                                Setflag=0;                //置為設(shè)置狀態(tài)
                                Moveflag=0;                //移位鍵有效-分針
                                TR0=0;                //定時(shí)器停止
                        }
                        else
                        {
                                LED=1;                //指示燈滅
                                Setflag=1;                //退出設(shè)置狀態(tài)
                                Moveflag=2;                //移位鍵無(wú)效
                                TR0=1;                //定時(shí)器啟動(dòng)
                                T=0;
                        }
                }
        }
       
        if(SaveMove!=Move)                //移位鍵狀態(tài)變化
        {
                if(Setflag==0)
                {
                        if(Move==0)
                        {
                                Moveflag++;
                                if(Moveflag==3)        Moveflag=0;
                        }
                }
        }
       
        if(SaveAdd!=Add)                //數(shù)加鍵狀態(tài)變化
        {
                if(Setflag==0)
                {
                        if(Add==0)
                        {
                                Addflag=0;
                        }
                }
        }
       
        if(SaveReduce!=Reduce)
        {
                if(Setflag==0)
                {
                        if(Reduce==0)
                        {
                                Reduceflag=0;
                        }
                }
        }
       
        SaveSet=Set;                //保存設(shè)置鍵狀態(tài)
        SaveMove=Move;                //保存移位鍵狀態(tài)
        SaveAdd=Add;                //保存數(shù)加鍵狀態(tài)
        SaveReduce=Reduce;                //保存數(shù)減鍵狀態(tài)
}

void Display()
{
                Clock();
                        LSA=0;LSB=0;LSC=0;        LS=DIG_CODE[miao%10];        Delay(1);        LS=0x00;//秒針低位-加 消隱
                        LSA=1;LSB=0;LSC=0;        LS=DIG_CODE[miao/10];        Delay(1);        LS=0x00;//秒針高位-加 消隱
                        LSA=0;LSB=1;LSC=0;        LS=0x40;        Delay(1);
                        LSA=1;LSB=1;LSC=0;        LS=DIG_CODE[fen%10];        Delay(1);        LS=0x00;//分針低位-加 消隱
                        LSA=0;LSB=0;LSC=1;        LS=DIG_CODE[fen/10];        Delay(1);        LS=0x00;//分針高位-加 消隱
                        LSA=1;LSB=0;LSC=1;        LS=0x40;        Delay(1);
                        LSA=0;LSB=1;LSC=1;        LS=DIG_CODE[shi%10];        Delay(1);        LS=0x00;//時(shí)針低位-加 消隱
                        LSA=1;LSB=1;LSC=1;        LS=DIG_CODE[shi/10];        Delay(1);        LS=0x00;//時(shí)針高位-加 消隱
}

main()
{
        Init();                        //執(zhí)行初始化
        while(1)
        {
                Key_Scan();
                Display();
        }
}







歡迎光臨 (http://www.raoushi.com/bbs/) Powered by Discuz! X3.1