找回密碼
 立即注冊

QQ登錄

只需一步,快速開始

搜索
查看: 4312|回復(fù): 0
打印 上一主題 下一主題
收起左側(cè)

(分享設(shè)計)秒表設(shè)計,按鍵控制

[復(fù)制鏈接]
跳轉(zhuǎn)到指定樓層
樓主
ID:68009 發(fā)表于 2014-11-21 16:23 | 只看該作者 |只看大圖 回帖獎勵 |倒序瀏覽 |閱讀模式
//簡易秒表,T1定時器,方式1,9999秒定時
#include<reg51.h>
#define uchar unsigned char
#define TH1_DATA (65536-50000)/256
#define TL1_DATA (65536-50000)%256
#define count_1s 20
int time_value=0;
#define SET_PORT P0
sbit DIS_TENS=P1^1;      //顯示數(shù)碼秒值十位的公共端
sbit DIS_UNIT=P1^0;                 //顯示數(shù)碼秒值個位的公共端
sbit DIS_HUND=P1^2;                 //顯示數(shù)碼秒值百位的公共端
sbit DIS_THOU=P1^3;             //顯示數(shù)碼秒值千位的公共端
sbit key=P3^2;                         //定義按鍵接口P3^2
bit flag_1s;             //定義1秒時間到標(biāo)志
void delay(uchar);               

uchar dis_number[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};          //0~9的字形碼

void main(void)
{
        uchar time_thou=0,time_hund=0,time_tens=0,time_unit=0;
        TMOD=0x10;
        TH1=TH1_DATA;
        TL1=TL1_DATA;
        ET1=1;
        EA=1;
        TR1=1;                                                  //啟動定時器
        IT0=1;                                              //外部中斷0
        EX0=1;
        EA=1;

        while(1)
        {
           if(flag_1s == 1)                          //如果1s到了
           {
            flag_1s=0;                                  //標(biāo)記重新清0
                time_value++;                          //1S次數(shù)加1
                        if(time_value >= 10000) //如果到了10000秒
                        {
                                time_value=0;          //1s的次數(shù)歸0
                        }
            time_thou=time_value / 1000;                        //計算當(dāng)前秒表計時值的千位
                time_hund=(time_value % 1000) / 100;        //計算當(dāng)前秒表計時值的百位
                time_tens=(time_value % 100) / 10;                //計算當(dāng)前秒表計時值的十位
                time_unit=time_value % 10;                                //計算當(dāng)前秒表計時值的個位
                }
                //顯示千位數(shù)字
                SET_PORT=dis_number[time_thou];                   //送段碼
                DIS_THOU=0;                                                                //送位碼
                delay(4);                                                                //延時
                DIS_THOU=1;                                                                //關(guān)閉當(dāng)前顯示位
                //顯示百位數(shù)字
        SET_PORT=dis_number[time_hund];   
                DIS_HUND=0;
                delay(4);
                DIS_HUND=1;          
                //顯示十位數(shù)字
                SET_PORT=dis_number[time_tens];   
                DIS_TENS=0;
                delay(4);
                DIS_TENS=1;                               
                //顯示個位數(shù)字
                SET_PORT=dis_number[time_unit];          
                DIS_UNIT=0;
                delay(4);
                DIS_UNIT=1;       
        }
}
void int_t3() interrupt 3 using 1
{
        static uchar count_50ms=0;
        TH1=TH1_DATA;
        TL1=TL1_DATA;
        count_50ms++;
        if(count_50ms >= count_1s)       
        {
                count_50ms=0;
                flag_1s=1;                
        }

}
void int_t1() interrupt 1 using 1                 //當(dāng)按鍵按下時
{
        TR1=~TR1;                                                         //定時器的狀態(tài)取反
}
void delay(uchar x)
{
        uchar i,j;
        for(i=x;i>0;i--)
        {
                for(j=250;j>0;j--)
                {
                        ;
                }
        }
}
   

評分

參與人數(shù) 1黑幣 +50 收起 理由
admin + 50 共享資料的積分獎勵!

查看全部評分

分享到:  QQ好友和群QQ好友和群 QQ空間QQ空間 騰訊微博騰訊微博 騰訊朋友騰訊朋友
收藏收藏2 分享淘帖 頂 踩
回復(fù)

使用道具 舉報

您需要登錄后才可以回帖 登錄 | 立即注冊

本版積分規(guī)則

手機版|小黑屋|51黑電子論壇 |51黑電子論壇6群 QQ 管理員QQ:125739409;技術(shù)交流QQ群281945664

Powered by 單片機教程網(wǎng)

快速回復(fù) 返回頂部 返回列表