51單片機主控的數字頻率計設計資料,我的畢業設計 仿真加源碼
下面是實物圖:
仿真原理圖如下(proteus仿真工程文件可到本帖附件中下載):
單片機源程序如下:
- #include <reg52.h>
- sbit led0=P2^6;//換擋燈
- sbit led1=P2^7;
- void delay_ms(int x);
- unsigned char code table[]={ //不顯示小數點的數碼管段值表
- 0x3f,0x06,0x5b,0x4f,0x66,
- 0x6d,0x7d,0x07,0x7f,0x6f};
- unsigned char code tablewe[]={ //顯示小數點的數碼管段值表
- 0xbf,0x86,0xdb,0xcf,
- 0xe6,0xed,0xfd,0x87,
- 0xff,0xef};
- int count1=0;
- int time0=0;
- unsigned char a,b,c,d;
- unsigned long int f=0;
- void main()
- {
- TMOD=0x51; //定時器1工作方式為16位計數器,定時器0工作方式為16位定時器
- TH0=(65536-46080)/256;//定時50ms
- TL0=(65536-46080)%256;
- EA=1;//開總中斷
- ET0=1;//開定時器0中斷
- TR0=1;//啟動定時器0
- ET1=1;//開定時器1中斷
- IT1=1;//設置計數方式為下降沿計數
- TR1=1;//啟動計數器1
- led0=1;//開始讓兩個燈都滅
- led1=1;
- while(1)
- {
- if(time0==20) //定時1s時間到
- {
- TR1=0; //關閉定時計數器
- TR0=0;
- f=count1*65536+TH1*256+TL1; //1s時間測得的信號下降沿個數,即頻率
- if(f<1000) //如果頻率小于1000Hz
- {
- led0=0;//Hz檔燈亮
- led1=1;
- while(1)
- {
- a=f/100;
- P2=0x8f; //消隱
- P0=table[a]; //數碼管的百位值
- P2=0x8d;
- delay_ms(1); //延時1ms
- b=(f%100)/10;
- P2=0x8f;
- P0=table[b]; //數碼管的十位值
- P2=0x8b;
- delay_ms(1);
- c=f%100%10;
- P2=0x8f;
- P0=table[c]; //數碼管的個位值
- P2=0x87;
- delay_ms(1);
- }
- }
- if(f>=1000&&f<10000) //從1000Hz開始,小數點的位置有所變化,采用四位數碼管顯示
- {
- led0=1;//KHz檔燈亮
- led1=0;
- while(1)
- {
- a=f/1000;
- P2=0x6f;
- P0=tablewe[a]; //小數點的位置在第一位
- P2=0x6e;
- delay_ms(1);
- b=(f%1000)/100;
- P2=0x6f;
- P0=table[b];
- P2=0x6d;
- delay_ms(1);
- c=(f%1000%100)/10;
- P2=0x6f;
- P0=table[c];
- P2=0x6b;
- delay_ms(1);
- d=f%1000%100%10;
- P2=0x6f;
- P0=table[d];
- P2=0x67;
- delay_ms(1);
- }
- }
- if(f>=10000&&f<100000)
- {
- while(1)
- {
- a=f/10000;
- P2=0x6f;
- P0=table[a];
- P2=0x6e;
- delay_ms(1);
- b=(f%10000)/1000;
- P2=0x6f;
- P0=tablewe[b]; //小數點的位置在第二位
- P2=0x6d;
- delay_ms(1);
- c=f%10000%1000/100;
- P2=0x6f;
- P0=table[c];
- P2=0x6b;
- delay_ms(1);
- d=f%10000%1000%100/10;
- P2=0x6f;
- P0=table[d];
- P2=0x67;
- delay_ms(1);
- }
- }
- if(f>=100000&&f<1000000)
- {
- while(1)
- {
- a=f/100000;
- P2=0x6f;
- P0=table[a];
- P2=0x6e;
- delay_ms(1);
- b=(f%100000)/10000;
- P2=0x6f;
- P0=table[b];
- P2=0x6d;
- delay_ms(1);
- c=(f%100000%10000)/1000;
- P2=0x6f;
- P0=tablewe[c]; //小數點的位置在第三位
- P2=0x6b;
- delay_ms(1);
- d=(f%100000%10000%1000)/100;
- P2=0x6f;
- P0=table[d];
- P2=0x67;
- delay_ms(1);
- }
- }
- if(f>=1000000) //超出范圍的顯示
- {
- while(1)
- {
- P2=0xcf;
- P0=0x7f;
- P2=0xce;
- delay_ms(1);
- P2=0xcf;
- P0=0x7f;
- P2=0xcd;
- delay_ms(1);
- P2=0xcf;
- P0=0x7f;
- P2=0xcb;
- delay_ms(1);
- P2=0xcf;
- P0=0x7f;
- P2=0xc7;
- delay_ms(1);
- }
- }
- }
- }
-
- }
- ……………………
- …………限于本文篇幅 余下代碼請從51黑下載附件…………
復制代碼
所有資料51hei提供下載:
51單片機數字頻率計.rar
(3.25 MB, 下載次數: 230)
2017-7-24 13:28 上傳
點擊文件名下載附件
|