機(jī)電之家資源網(wǎng)
單片機(jī)首頁(yè)|單片機(jī)基礎(chǔ)|單片機(jī)應(yīng)用|單片機(jī)開(kāi)發(fā)|單片機(jī)文案|軟件資料下載|音響制作|電路圖下載 |嵌入式開(kāi)發(fā)
培訓(xùn)信息
贊助商
定時(shí)計(jì)數(shù)器T0作定時(shí)應(yīng)用技術(shù)(二)
定時(shí)計(jì)數(shù)器T0作定時(shí)應(yīng)用技術(shù)(二)
 更新時(shí)間:2008-7-27 16:03:27  點(diǎn)擊數(shù):2
【字體: 字體顏色
1.實(shí)驗(yàn)任務(wù)
  用AT89S51的定時(shí)/計(jì)數(shù)器T0產(chǎn)生2秒鐘的定時(shí),每當(dāng)2秒定時(shí)到來(lái)時(shí),更換指示燈閃爍,每個(gè)指示閃爍的頻率為0.2秒,也就是說(shuō),開(kāi)始L1指示燈以0.2秒的速率閃爍,當(dāng)2秒定時(shí)到來(lái)之后,L2開(kāi)始以0.2秒的速率閃爍,如此循環(huán)下去。0.2秒的閃爍速率也由定時(shí)/計(jì)數(shù)器T0來(lái)完成。
2.電路原理圖

                圖4.16.1
3.系統(tǒng)板硬件連線(xiàn)
(1).把“單片機(jī)系統(tǒng)”區(qū)域中的P1.0-P1.3用導(dǎo)線(xiàn)連接到“八路發(fā)光二極管指示模塊”區(qū)域中的L1-L4上
4.程序設(shè)計(jì)內(nèi)容
(1).由于采用中斷方式來(lái)完成,因此,對(duì)于中斷源必須它的中斷入口地址,對(duì)于定時(shí)/計(jì)數(shù)器T0來(lái)說(shuō),中斷入口地址為000BH,因此在中斷入口地方加入長(zhǎng)跳轉(zhuǎn)指令來(lái)執(zhí)行中斷服務(wù)程序。書(shū)寫(xiě)匯編源程序格式如下所示:
                            ORG 00H
                            LJMP START
                            ORG 0BH        ;定時(shí)/計(jì)數(shù)器T0中斷入口地址
                            LJMP INT_T0
START:               NOP          ;主程序開(kāi)始
                            .
                            .
 
INT_T0:             PUSH ACC                  ;定時(shí)/計(jì)數(shù)器T0中斷服務(wù)程序
                            PUSH PSW
                            .
                            .
                            POP PSW
                            POP ACC
                            RETI                   ;中斷服務(wù)程序返回
                            END
(2).定時(shí)2秒,采用16位定時(shí)50ms,共定時(shí)40次才可達(dá)到2秒,每50ms產(chǎn)生一中斷,定時(shí)的40次數(shù)在中斷服務(wù)程序中完成,同樣0.2秒的定時(shí),需要4次才可達(dá)到0.2秒。對(duì)于中斷程序,在主程序中要對(duì)中斷開(kāi)中斷。
(3).由于每次2秒定時(shí)到時(shí),L1-L4要交替閃爍。采用ID來(lái)號(hào)來(lái)識(shí)別。當(dāng)ID=0時(shí),L1在閃爍,當(dāng)ID=1時(shí),L2在閃爍;當(dāng)ID=2時(shí),L3在閃爍;當(dāng)ID=3時(shí),L4在閃爍
5.程序框圖
T0中斷服務(wù)程序框圖

主程序框圖
          

      圖4.16.2
6.匯編源程序
TCOUNT2S                EQU 30H
TCNT02S                    EQU 31H
ID                                 EQU 32H
                                      ORG 00H
                                      LJMP START
                                      ORG 0BH
                                      LJMP INT_T0
START:                        MOV TCOUNT2S,#00H
                                     MOV TCNT02S,#00H
                                     MOV ID,#00H
                                      MOV TMOD,#01H
                                      MOV TH0,#(65536-50000) / 256
                                      MOV TL0,#(65536-50000) MOD 256
                                      SETB TR0
                                      SETB ET0
                                      SETB EA
                                      SJMP $
INT_T0:                      MOV TH0,#(65536-50000) / 256
                                      MOV TL0,#(65536-50000) MOD 256
                                      INC TCOUNT2S
                                      MOV A,TCOUNT2S
                                      CJNE A,#40,NEXT
                                      MOV TCOUNT2S,#00H
                                      INC ID
                                      MOV A,ID
                                      CJNE A,#04H,NEXT
                                      MOV ID,#00H
NEXT:                          INC TCNT02S
                                      MOV A,TCNT02S
                                      CJNE A,#4,DONE
                                      MOV TCNT02S,#00H
                                      MOV A,ID
                                      CJNE A,#00H,SID1
                                      CPL P1.0
                                      SJMP DONE
SID1:                            CJNE A,#01H,SID2
                                      CPL P1.1
                                      SJMP DONE
SID2:                           CJNE A,#02H,SID3
                                      CPL P1.2
                                      SJMP DONE
SID3:                            CJNE A,#03H,SID4
                                      CPL P1.3
SID4:                            SJMP DONE
DONE:                        RETI
                                      END
7.C語(yǔ)言源程序
#include
unsigned char tcount2s;
unsigned char tcount02s;
unsigned char ID;
void main(void)
{
  TMOD=0x01;
  TH0=(65536-50000)/256;
  TL0=(65536-50000)%6;
  TR0=1;
  ET0=1;
  EA=1;
  while(1);
}
void t0(void) interrupt 1 using 0
{
  tcount2s++;
  if(tcount2s==40)
    {
      tcount2s=0;
      ID++;
      if(ID==4)
        {
          ID=0;
        }
    }
  tcount02s++;
  if(tcount02s==4)
    {
      tcount02s=0;
      switch(ID)
        {
          case 0:
            P1_0=~P1_0;
            break;
          case 1:
            P1_1=~P1_1;
            break;
          case 2:
            P1_2=~P1_2;
            break;
          case 3:
            P1_3=~P1_3;
            break;
        }
    }
  • 上一篇: 4×4矩陣式鍵盤(pán)識(shí)別技術(shù)
  • 下一篇: 定時(shí)計(jì)數(shù)器T0作定時(shí)應(yīng)用技術(shù)(一)
  • 發(fā)表評(píng)論   告訴好友   打印此文  收藏此頁(yè)  關(guān)閉窗口  返回頂部
    熱點(diǎn)文章
     
    推薦文章
     
    相關(guān)文章
    網(wǎng)友評(píng)論:(只顯示最新5條。)
    關(guān)于我們 | 聯(lián)系我們 | 廣告合作 | 付款方式 | 使用幫助 | 機(jī)電之家 | 會(huì)員助手 | 免費(fèi)鏈接

    點(diǎn)擊這里給我發(fā)消息66821730(技術(shù)支持)點(diǎn)擊這里給我發(fā)消息66821730(廣告投放) 點(diǎn)擊這里給我發(fā)消息41031197(編輯) 點(diǎn)擊這里給我發(fā)消息58733127(審核)
    本站提供的機(jī)電設(shè)備,機(jī)電供求等信息由機(jī)電企業(yè)自行提供,該企業(yè)負(fù)責(zé)信息內(nèi)容的真實(shí)性、準(zhǔn)確性和合法性。
    機(jī)電之家對(duì)此不承擔(dān)任何保證責(zé)任,有侵犯您利益的地方請(qǐng)聯(lián)系機(jī)電之家,機(jī)電之家將及時(shí)作出處理。
    Copyright 2007 機(jī)電之家 Inc All Rights Reserved.機(jī)電之家-由機(jī)電一體化網(wǎng)更名-聲明
    電話(huà):0571-87774297 傳真:0571-87774298
    杭州濱興科技有限公司提供技術(shù)支持

    主辦:杭州市高新區(qū)(濱江)機(jī)電一體化學(xué)會(huì)
    中國(guó)行業(yè)電子商務(wù)100強(qiáng)網(wǎng)站

    網(wǎng)站經(jīng)營(yíng)許可證:浙B2-20080178-1