新聞中心

        EEPW首頁 > 嵌入式系統 > 設計應用 > 單片機C語言程序設計:10s 的秒表

        單片機C語言程序設計:10s 的秒表

        作者: 時間:2013-08-03 來源:網絡 收藏

        /* 名稱:10s 的

        說明:首次按鍵計時開始,再次按鍵暫停,第三次按鍵清零。


        */

        #includereg51.h>

        #define uchar unsigned char

        #define uint unsigned int

        sbit K1=P3^7;

        uchar

        i,Second_Counts,Key_Flag_Idx;

        bit Key_State;

        uchar

        DSY_CODE[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};

        //延時

        void DelayMS(uint ms)

        {

        uchar t;

        while(ms--) for(t=0;t120;t++);

        }

        //處理按鍵事件

        void Key_Event_Handle()

        {

        if(Key_State==0)

        {

        Key_Flag_Idx=(Key_Flag_Idx+1)%3;

        switch(Key_Flag_Idx)

        {

        case 1:EA=1;ET0=1;TR0=1;break;

        case 2:EA=0;ET0=0;TR0=0;break;

        case 0:P0=0x3f;P2=0x3f;i=0;Second_Counts=0;

        }

        }

        }

        //主程序

        void main()

        {


        Second_Counts=0;

        Key_Flag_Idx=0; //按鍵次數(取值 0,1,2,3)

        Key_State=1; //按鍵狀態

        TMOD=0x01; //定時器 0 方式 1


        TH0=(65536-50000)/256; //定時器 0:15ms

        TL0=(65536-50000)%256;

        while(1)

        {

        if(Key_State!=K1)

        {

        DelayMS(10);

        Key_State=K1;

        Key_Event_Handle();

        }

        }

        }

        /T0 中斷函數

        void DSY_Refresh() interrupt 1

        {

        TH0=(65536-50000)/256; //恢復定時器 0 初值

        TL0=(65536-50000)%256;

        if(++i==2)


        {

        i=0;

        Second_Counts++; //50ms*2=0.1s 轉換狀態

        P0=DSY_CODE[Second_Counts/10];

        P2=DSY_CODE[Second_Counts%10];

        if(Second_Counts==100) Second_Counts=0; //滿 100(10s)后顯示 00

        }

        }

        單片機C語言程序設計:10s 的秒表

        c語言相關文章:c語言教程




        評論


        相關推薦

        技術專區

        關閉
        主站蜘蛛池模板: 原平市| 惠东县| 霍邱县| 临武县| 武川县| 宝清县| 沧源| 武城县| 秭归县| 资阳市| 囊谦县| 灵武市| 韩城市| 五家渠市| 张掖市| 元江| 米脂县| 昭平县| 兴安县| 宁化县| 泽州县| 金沙县| 石门县| 枣强县| 巴中市| 满洲里市| 遂川县| 探索| 渑池县| 永登县| 漠河县| 鱼台县| 孟村| 白城市| 慈利县| 松潘县| 平凉市| 广西| 彰化市| 微博| 新绛县|