<meter id="pryje"><nav id="pryje"><delect id="pryje"></delect></nav></meter>
          <label id="pryje"></label>

          新聞中心

          EEPW首頁 > 嵌入式系統(tǒng) > 設(shè)計應(yīng)用 > 51單片機1秒定時C語言程序(通過調(diào)試)

          51單片機1秒定時C語言程序(通過調(diào)試)

          作者: 時間:2016-11-17 來源:網(wǎng)絡(luò) 收藏
          #include // 晶振為12MHz,

          #include
          #include
          #include

          本文引用地址:http://www.ex-cimer.com/article/201611/315384.htm

          /***************************
          * 定義硬件引腳連接
          ****************************/
          sbit p1_0=P1^0;
          /*************************
          * 定義全局變量
          ***********************/
          // Byte data intrCounter; // 計時器中斷次數(shù)
          // Byte data beginNum; // 開始倒計時的時間
          //Byte data counterBack; // 將中斷次數(shù)放在里面以備后用
          unsigned char intrCounter;
          unsigned char beginNum;
          unsigned char counterBack;
          void IT0_Init(void);
          void Timer0_Overflow();
          void GetCounter(void);
          void main()
          {
          p1_0=1;
          GetCounter() ;每一秒改變一次;
          p1_0=!p1_0;;
          }
          /*********************************
          * GetCounter
          *
          * Description: 取得預(yù)先設(shè)置的倒計時時間
          *******************************/
          void GetCounter(void)
          {
          //beginNum=1;//在所有開關(guān)都沒有撥動的時候倒計時為1秒,比設(shè)置為0秒要好
          intrCounter=20;//每一秒對應(yīng)的中斷次數(shù)為20次
          }
          /*****************************
          * IT0_Init
          * Description: 初始化計時器T0的狀態(tài)
          ********************************/
          void IT0_Init(void)
          {
          TMOD=0x01;//設(shè)置T0在方式1下工作
          TH0=0x3C;
          TL0=0xAF;//這兩個寄存器存的是計數(shù)器的計數(shù)開始的值,計算發(fā)現(xiàn)這兩個值累加至溢出后正好是50ms
          ET0=1;//使T0中斷可以溢出
          EA=1;//開啟總中斷
          TF0=0;//溢出位清零
          TR0=1;//開啟T0
          }

          /******************************
          * Timer0_Overflow() interrupt 1
          * Description: 中斷溢出服務(wù)程序, 采用的是中斷方式1, 后面最好不加using選擇寄存器組以免與系統(tǒng)用在主程序的寄存器沖突

          *********************************/
          void Timer0_Overflow() interrupt 1
          {
          static unsigned char second=20;//用20次中斷來判斷1秒
          TH0=0x3C;
          TL0=0xAF;
          second--;
          intrCounter--;

          if (second==0)//每隔一秒的操作
          {

          second=20;//重新賦值每秒計數(shù)器
          }
          if (intrCounter==0)
          {
          TR0=0;//關(guān)閉T0計數(shù)器
          }



          關(guān)鍵詞: 51單片機1秒定時C語言程

          評論


          技術(shù)專區(qū)

          關(guān)閉
          看屁屁www成人影院,亚洲人妻成人图片,亚洲精品成人午夜在线,日韩在线 欧美成人 (function(){ var bp = document.createElement('script'); var curProtocol = window.location.protocol.split(':')[0]; if (curProtocol === 'https') { bp.src = 'https://zz.bdstatic.com/linksubmit/push.js'; } else { bp.src = 'http://push.zhanzhang.baidu.com/push.js'; } var s = document.getElementsByTagName("script")[0]; s.parentNode.insertBefore(bp, s); })();