您好,歡迎來(lái)電子發(fā)燒友網(wǎng)! ,新用戶?[免費(fèi)注冊(cè)]

您的位置:電子發(fā)燒友網(wǎng)>源碼下載>C/C++語(yǔ)言編程>

計(jì)時(shí)器與計(jì)頻器實(shí)驗(yàn)及其幾種典型的應(yīng)用案例分享

大?。?/span>0.1 MB 人氣: 2017-09-06 需要積分:0

1、計(jì)時(shí)器實(shí)驗(yàn)I-垂詢方式-高低位燈互閃

/* ch07-5-1.c - 計(jì)時(shí)器實(shí)驗(yàn)I-垂詢方式-高低位燈互閃 */
//==聲明區(qū)================================
#include??// 定義8x51暫存器之標(biāo)頭檔,P2-17~19?
#define?LED?P1???// 定義LED接至Port 1
#define?count?50000?// T0(MODE 1)之計(jì)量值,約0.05秒(7-8頁(yè))
#define? ?TH_M1 (65636-count)/256?// T0(Mode 1)計(jì)量高8位元?
#define? ?TL_M1 (65636-count)%256?// T0(Mode 1)計(jì)量低8位元?
//==主程序================================
main()?????// 主程序開(kāi)始?
{?int? i;????// 宣告i變數(shù)?
?TMOD &= 0xf1;??// 設(shè)定T0為mode 1(7-3頁(yè))
?TMOD |= 0x01;??// 設(shè)定T0為mode 1(7-3頁(yè))
?LED=0xf0;???// LED初值=1111 0000,右4燈亮?
?while(1)???// 無(wú)窮循環(huán),程序一直跑?
?{?for (i=0;i<5;i++)//?for循環(huán),計(jì)時(shí)中斷5次?
???{ ?TH0=TH_M1;?// 設(shè)置高8位元?
???TL0=TL_M1;?// 設(shè)置低8位元?
???TR0=1;??// 啟動(dòng)T0(7-7頁(yè))
???while(TF0==0);// 等待溢位(TF0==1)
???TF0=0;??// 溢位後,清除TF0,關(guān)閉T0
??}????// for循環(huán)計(jì)時(shí)結(jié)束?
??LED=~LED;??// 輸出反相?
?}????// while循環(huán)結(jié)束?
}?????// 主程序結(jié)束?
?
?

2、計(jì)時(shí)器實(shí)驗(yàn)2-中斷方式-高低位燈互閃

/* ch07-5-2.c - 計(jì)時(shí)器實(shí)驗(yàn)2-中斷方式-高低位燈互閃? */
//==聲明區(qū)================================
#include??// 定義8x51暫存器之標(biāo)頭檔,P2-17~19?
#define?LED?P1???// 定義LED接至Port 1
#define?count?50000?// T0(MODE 1)之計(jì)量值,約0.05秒?
#define? ?TH_M1 (65636-count)/256?// T0(Mode 1)計(jì)量高8位元?
#define? ?TL_M1 (65636-count)%256?// T0(Mode 1)計(jì)量低8位元?
int? IntCount=0;??// 宣告IntCount變數(shù),計(jì)算T0中斷次數(shù)?
//==主程序================================
main()?????// 主程序開(kāi)始?
{?IE=0x82;???// 啟用T0中斷(6-4頁(yè))
?TMOD &= 0xf1;TMOD |= 0x01;?// 設(shè)定T0為mode 1(7-3頁(yè))
?TH0=TH_M1; TL0=TL_M1;??// 設(shè)置T0計(jì)數(shù)量高8位元、低8位元?
?TR0=1;????// 啟動(dòng)T0
?LED=0xf0;???//?LED初值=1111 0000,右4燈亮?
?while(1);???// 無(wú)窮循環(huán),程序停滯?
}??????// 主程序結(jié)束?
//== T0中斷子程序- 每中斷5次,LED反相 ================
void timer0(void) interrupt 1?// T0中斷子程序開(kāi)始?
{?TH0=TH_M1; TL0=TL_M1;??// 設(shè)置T0計(jì)數(shù)量高8位元、低8位元?
?if (++IntCount==5)???// 若T0已中斷5次數(shù)?
?{?IntCount=0;????// 重新計(jì)次?
??LED^=0xff;????// 輸出相反?
?}???????// if敘述結(jié)束?
}????????// T0中斷子程序
?
?

3、計(jì)時(shí)器實(shí)驗(yàn)3 -60秒計(jì)數(shù)器,每1分鐘LED反相1次

/* ch07-5-3.c - 計(jì)時(shí)器實(shí)驗(yàn)3 -60秒計(jì)數(shù)器,每1分鐘LED反相1次 */
//==聲明區(qū)================================
#include??// 定義8x51暫存器之標(biāo)頭檔,P2-17~19?
#define?SEG?P2???// 定義七節(jié)顯示器接至Port 2
#define?SCANP?P1??// 定義掃瞄線接至Port 1
sbit? LED=P0^7;???// 聲明LED接至P0.7
/*聲明T0計(jì)時(shí)相關(guān)聲明*/? // THx TLx 計(jì)算參考?
#define? count_M1?? 50000???// T0(MODE 1)之計(jì)量值,0.05s?
#define? TH_M1? (65636-count_M1)/256// T0(MODE 1)計(jì)量高8位元?
#define? TL_M1? (65636-count_M1)%256// T0(MODE 1)計(jì)量低8位元?
int count_T0=0; ?????// 計(jì)算 T0中斷次數(shù)?
/*聲明T1掃瞄相關(guān)聲明*/
#define? count_M2?? 250???// T1(MODE 2)之計(jì)量值,0.25ms
#define? TH_M2? (256-count_M2)?// T1(MODE 2)自動(dòng)載入計(jì)量??
#define? TL_M2? (256-count_M2)?// T1(MODE 2)計(jì)數(shù)量?
char count_T1=0;????// 計(jì)算 T1中斷次數(shù) ?
/* 聲明七節(jié)顯示器驅(qū)動(dòng)信號(hào)陣列(共陽(yáng)) */
char code TAB[10]={?0xc0, 0xf9, 0xa4, 0xb0, 0x99,?//數(shù)字0-4
?????0x92, 0x83, 0xf8, 0x80, 0x98 };?//數(shù)字5-9
char disp[2]={ 0xc0, 0xc0 };?// 聲明顯示區(qū)陣列初始顯示00
/* 聲明基本變量 */
char seconds=0;???// 秒數(shù)?
char scan=0;???// 掃瞄信號(hào)?
//==主程序================================
main()?????// 主程序開(kāi)始?
{?IE=0x8a;???// 1000 1010,啟用TF0、TF1中斷(6-4頁(yè))
?TMOD=0x21;???// 0010 0001,T1采mode 2、T0采mode 1
?TH0=TH_M1; TL0=TL_M1;// 設(shè)置T0計(jì)數(shù)量高8位元、低8位元?
?TR0=1;????// 啟動(dòng)T0(7-7頁(yè))
?TH1=TH_M2; TL1=TL_M2;// 設(shè)置T1自動(dòng)載入值、計(jì)數(shù)量?
?TR1=1;????// 啟動(dòng)T1
?LED=1;????// 關(guān)閉LED
?while(1);???// 無(wú)窮回圈,程序停滯?
}??????// 主程序結(jié)束??
//== T0中斷子程序- 計(jì)算并顯示秒數(shù) ==================
void T0_1s(void) interrupt 1?// T0中斷子程序開(kāi)始?
{?TH0=TH_M1; TL0=TL_M1;??// 設(shè)置T0計(jì)數(shù)量高8位元、低8位元?
?if (++count_T0==20)???// 若中斷20次,即0.05x20=1秒?
?{?count_T0=0;????// 重新計(jì)次?
??seconds++;????// 秒數(shù)加1
??if (seconds==60) ??// 若超過(guò)60秒?
??{?seconds=0;???// 秒數(shù)歸0,重新開(kāi)始?
???LED=~LED; ???// 切換LED
??}??????// if敘述結(jié)束(超過(guò)60秒)
?}???????// if敘述結(jié)束(中斷20次)
?disp[1]=TAB[seconds/10]; ?// 填入十位數(shù)顯示區(qū)?
?disp[0]=TAB[seconds%10]; ?// 填入個(gè)位數(shù)顯示區(qū)?
}????????// T0中斷子程序結(jié)束?
//===T1中斷子程序 - 掃瞄 =========================
void T1_8ms(void) interrupt 3?// T1中斷子程序開(kāi)始?
{?if (++count_T1==32)???// 若中斷32次,即0.25mx32=8ms
??{?count_T1=0;???// 重新計(jì)次?
???if (++scan==3) scan=1;// 若超過(guò)十位數(shù),顯示個(gè)位 ?
???SEG=0xff;???// 關(guān)閉7段顯示器?
???SCANP=~scan;??// 輸出掃瞄信號(hào)?
???SEG=disp[scan-1];?// 輸出顯示信號(hào)?
??}??????// 結(jié)束if判斷(中斷32次)
}????????// T0中斷子程序結(jié)束?
?
?

4、計(jì)頻器實(shí)驗(yàn)

/* ch07-5-6.c - 計(jì)頻器實(shí)驗(yàn) - 由P3.5輸入信號(hào),按下PB0於1秒後顯示其頻率 */
//==聲明區(qū)================================
#include??// 定義8x51暫存器之標(biāo)頭檔,P2-17~19?
#define?SEG??P2??// 定義七節(jié)顯示器接至Port 2
#define?SCANP?P1??// 定義掃瞄線接至Port 1
sbit? PB0=P3^2;???// 聲明PB0按鈕,接至P3.2
char code TAB[11]={?0xc0, 0xf9, 0xa4, 0xb0, 0x99,???// 數(shù)字0-4
????? ??0x92, 0x83, 0xf8, 0x80, 0x98 , 0xbf };?// 數(shù)字5-9,及-號(hào)?
char disp[4]={ 0xc0, 0xc0, 0xc0, 0xc0}; // 聲明顯示區(qū)陣列初始顯示0000
/* 聲明T0計(jì)時(shí)相關(guān)聲明 */????// THx TLx 計(jì)算參考 7-8頁(yè)?
#define? count_M1?? 50000????// T0(MODE 1)之計(jì)量值,0.05s?
#define? H_0? (65636-count_M1)/256??// T0(MODE 1)計(jì)量高8位元?
#define? L_0? (65636-count_M1)%256??// T0(MODE 1)計(jì)量低8位元?
char times=0; ???// 計(jì)算 T0中斷次數(shù)?
/* 聲明基本變量 */
bit? status_F = 1;??// 狀態(tài)旗標(biāo)?
char? scan = 1;???// 掃瞄信號(hào)?
unsigned int freq = 0;?// 頻率變量?
sfr16? DPTR = 0x82;??// 聲明DPTR
void delay1ms(int);??// 聲明延遲函數(shù)?
void measure(void); ?// 聲明量測(cè)函數(shù)?
void display(void); ?// 聲明顯示函數(shù)?
//==主程序======================================
main()?????// 主程序開(kāi)始?
{?while(1)???// 無(wú)窮回圈,程序一直跑?
?{?if (PB0==0) ?// 若按下PB0
??{?status_F=0;?// 則進(jìn)入量測(cè)階段?
???measure();?// 呼叫量測(cè)函數(shù)?
???while(status_F==0); }?// 等待0,量測(cè)完畢?
??else display();?// 顯示階段?
?}?????//?while回圈結(jié)束?
}??????// 主程序結(jié)束?
//==量測(cè)函數(shù)====================================
void measure(void)??// 量測(cè)函數(shù)開(kāi)始
{?SCANP=0xff;???// 關(guān)閉顯示器?
?IE=0x8a;???// 10001010,啟用T0、T1中斷
?TMOD=0x51;???// 7-3 頁(yè)?
?/*0101 0001:T1為計(jì)數(shù)器、T0為計(jì)時(shí)器,都采mode 1*/
?TH0=H_0;TL0=L_0;?// 設(shè)置T0計(jì)數(shù)量之高、低八位元?
?TH1=0;TL1=0;??// 設(shè)置T1歸零?
?TR0=1;TR1=1;??// 啟動(dòng)T0、T1
}??????// 量測(cè)函數(shù)結(jié)束?
//===T0_1S=================================
void T0_1S(void) interrupt 1 ?// T0_1S中斷副程序開(kāi)始?
{?TH0=H_0;TL0=L_0;?// 設(shè)置T0計(jì)數(shù)量之高、低八位元?
?if (++times==20)?// 若達(dá)到1秒?
?{?times=0;??// 重新計(jì)次?
??status_F=1;??// 完成量測(cè)?
??TR1=0;TR0=0;?// 關(guān)閉T1、T0
??DPL=TL1;??// 計(jì)數(shù)量之低八位元?
??DPH=TH1;??// 計(jì)數(shù)量之高八位元?
??freq=DPTR;??// 計(jì)數(shù)量放入freq變量?
??if (freq>=10000)// 超過(guò)10 kHz
??{?disp[3]=TAB[10];???// 負(fù)號(hào)填入千位數(shù)顯示區(qū)?
???disp[2]=TAB[10];???// 負(fù)號(hào)填入百位數(shù)顯示區(qū)?
???disp[1]=TAB[freq/10000];?// 填入十位數(shù)顯示區(qū)?
???disp[0]=TAB[(freq/1000)%10];}// 填入個(gè)位數(shù)顯示區(qū)?
??else???????// 低於10 kHz
??{?disp[3]=TAB[freq/1000];??// 填入千位數(shù)顯示區(qū) ????
???disp[2]=TAB[(freq/100)%10];?// 填入百位數(shù)顯示區(qū)?
???disp[1]=TAB[(freq/10)%10];?// 填入十位數(shù)顯示區(qū)?
???disp[0]=TAB[freq%10];}??// 填入個(gè)位數(shù)顯示區(qū)?
?}?????????// 結(jié)束if判斷(達(dá)到1秒)
}??????????//?T0_1S中斷副程序結(jié)束?
//===顯示函數(shù)================================
void display(void)???// 顯示函數(shù)開(kāi)始?
{?char i;?????// 聲明變量?
?while (PB0==1)???// 若按下PB0
?{?scan=0x01;???// 初始掃瞄信號(hào)?
??for (i=0;i<4;i++)?// 掃瞄4次?
??{?SEG=0xff;??// 關(guān)閉7段顯示器?
???SCANP=~scan;?// 輸出掃瞄信號(hào)?
???SEG=disp[i];?// 輸出顯示信號(hào)?
???delay1ms(4); ?// 延遲4ms
???scan<<=1;??// 下一個(gè)掃瞄信號(hào)?
?? }?????//?for結(jié)束掃瞄4次?
?}??????// 結(jié)束while(按下PB0)
}???????// 顯示函數(shù)結(jié)束?
//===延遲函數(shù)================================
void delay1ms(int x)??// 延遲函數(shù)開(kāi)始?
{?int i,j;????// 聲明變量?
?for(i=0;i ??for(j=0;j<120;j++); // 數(shù)120次,約1ms
}???????// 延遲函數(shù)結(jié)束?

非常好我支持^.^

(0) 0%

不好我反對(duì)

(0) 0%

      發(fā)表評(píng)論

      用戶評(píng)論
      評(píng)價(jià):好評(píng)中評(píng)差評(píng)

      發(fā)表評(píng)論,獲取積分! 請(qǐng)遵守相關(guān)規(guī)定!

      ?