|
1. 實驗任務(wù) 利用at89s51單片機的t0、t1的定時計數(shù)器功能,來完成對輸入的信號進行頻率計數(shù),計數(shù)的頻率結(jié)果通過8位動態(tài)數(shù)碼管顯示出來。要求能夠?qū)?-250khz的信號頻率進行準確計數(shù),計數(shù)誤差不超過±1hz。 2. 電路原理圖
圖4.31.1 3. 系統(tǒng)板上硬件連線 (1). 把“單片機系統(tǒng)”區(qū)域中的p0.0-p0.7與“動態(tài)數(shù)碼顯示”區(qū)域中的abcdefgh端口用8芯排線連接。 (2). 把“單片機系統(tǒng)”區(qū)域中的p2.0-p2.7與“動態(tài)數(shù)碼顯示”區(qū)域中的s1s2s3s4s5s6s7s8端口用8芯排線連接。 (3). 把“單片機系統(tǒng)”區(qū)域中的p3.4(t0)端子用導(dǎo)線連接到“頻率產(chǎn)生器”區(qū)域中的wave端子上。 4. 程序設(shè)計內(nèi)容 (1). 定時/計數(shù)器t0和t1的工作方式設(shè)置,由圖可知,t0是工作在計數(shù)狀態(tài)下,對輸入的頻率信號進行計數(shù),但對工作在計數(shù)狀態(tài)下的t0,最大計數(shù)值為fosc/24,由于fosc=12mhz,因此:t0的最大計數(shù)頻率為250khz。對于頻率的概念就是在一秒只數(shù)脈沖的個數(shù),即為頻率值。所以t1工作在定時狀態(tài)下,每定時1秒中到,就停止t0的計數(shù),而從t0的計數(shù)單元中讀取計數(shù)的數(shù)值,然后進行數(shù)據(jù)處理。送到數(shù)碼管顯示出來。 (2). t1工作在定時狀態(tài)下,最大定時時間為65ms,達不到1秒的定時,所以采用定時50ms,共定時20次,即可完成1秒的定時功能。 5. c語言源程序 #include <at89x52.h> unsigned char code dispbit[]={0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f}; unsigned char code dispcode[]={0x3f,0x06,0x5b,0x4f,0x66, 0x6d,0x7d,0x07,0x7f,0x6f,0x00,0x40}; unsigned char dispbuf[8]={0,0,0,0,0,0,10,10}; unsigned char temp[8]; unsigned char dispcount; unsigned char t0count; unsigned char timecount; bit flag; unsigned long x; void main(void) { unsigned char i; tmod=0x15; th0=0; tl0=0; th1=(65536-4000)/256; tl1=(65536-4000)%256; tr1=1; tr0=1; et0=1; et1=1; ea=1; while(1) { if(flag==1) { flag=0; x=t0count*65536+th0*256+tl0; for(i=0;i<8;i++) { temp[i]=0; } i=0; while(x/10) { temp[i]=x%10; x=x/10; i++; } temp[i]=x; for(i=0;i<6;i++) { dispbuf[i]=temp[i]; } timecount=0; t0count=0; th0=0; tl0=0; tr0=1; } } } void t0(void) interrupt 1 using 0 { t0count++; } void t1(void) interrupt 3 using 0 { th1=(65536-4000)/256; tl1=(65536-4000)%256; timecount++; if(timecount==250) { tr0=0; timecount=0; flag=1; } p0=dispcode[dispbuf[dispcount]]; p2=dispbit[dispcount]; dispcount++; if(dispcount==8) { dispcount=0; } } |