|
51單片機(jī)頻率采集+lcd1602+按鍵控制PWM
仿真原理圖如下(proteus仿真工程文件可到本帖附件中下載)
51hei.png (51.48 KB, 下載次數(shù): 70)
下載附件
2019-11-11 13:41 上傳
單片機(jī)源程序如下:
- #include<reg52.h>
- #include "lcd1602.h"
- #define uchar unsigned char//宏定義
- #define uint unsigned int//宏定義
- sbit ji=P2^1;
- sbit jia_key=P3^2;
- sbit jian_key=P3^3;
- uchar sw,w,q,b,s,g,num=0,gao_num=1;di_num=3;
- void timer_init ();
- void delay (unsigned int a);
- unsigned long fre; //頻率
- unsigned char time; //時(shí)間
- unsigned long count=0; //計(jì)數(shù)
- void delay(uint z) //延時(shí)子程序
- {
- uint x,y;
- for(x = z;x > 0;x--)
- for(y = 125;y > 0;y--);
- }
- /***********************************************************/
- /**************************************************/
- void timer_init (void) //定時(shí)器初始化
- {
- TMOD = 0x06; //計(jì)數(shù)器0工作 工作方式2,自動(dòng)重裝初值
- TH0 = 0; //計(jì)數(shù)器初值為0
- TL0 = 0;
- TR0 = 1; //計(jì)數(shù)器開始計(jì)數(shù)
- ET0 = 1; //打開計(jì)數(shù)器0中斷
- RCAP2H = (65536 - 62500) / 256; //在程序初始化的時(shí)候給RCAP2H和RCAP2L賦值
- RCAP2L = (65536 - 62500) % 256; //TH2與TL2將會(huì)在中斷產(chǎn)生時(shí)斷使TH2 = RCAP2H,TL2 = RCAP2L.
- TH2 = RCAP2H; //12M晶振喜愛每次中斷 62.5ms
- TL2 = RCAP2L;
- ET2 = 1; //打開定時(shí)器2中斷
- TR2 = 1; //定時(shí)器2開始計(jì)時(shí)
- EA = 1; //打開總中斷
-
- }
- void timer2 (void) interrupt 5 //定時(shí)器2中斷62.5ms 服務(wù)函
- {
- time++;
- TF2 = 0; //定時(shí)器2的中斷標(biāo)志位TF2不能由硬件清零,所有要在中斷服務(wù)程序中將其清零
- if (time == 16) //定時(shí)1s時(shí)間到
- {
- time = 0; //計(jì)時(shí)清零
- EA = 0; //關(guān)閉中斷
- fre =((long)count*256 + TL0); //count*256強(qiáng)制轉(zhuǎn)換為long型,否則將不產(chǎn)生進(jìn)位
- TL0 = 0; //清零計(jì)數(shù)器0計(jì)數(shù)
- TH0 = 0;
- count = 0; //清零計(jì)數(shù)器計(jì)數(shù)
- EA = 1; //打開中斷
-
- }
- }
- /****************************************************/
- void timer0 (void) interrupt 1 //計(jì)數(shù)器0中斷
- {
-
- count++;
- }
- /*****************************************************/
- void display_frequency(unsigned long fre)
- {
-
- unsigned char bai,shi,ge,xiao,xiaoo;
-
- bai=fre/10000;
- shi=fre%10000/1000;
- ge=fre%1000/100;
- xiao=fre%100/10;
- xiaoo=fre%10;
- LCD1602_write_com(0x80+7); //??????????2??9? ???????1??+8??
- LCD1602_write_data(0x30+bai); //bai????2??8? ???????,
- LCD1602_write_com(0x80+8);
- LCD1602_write_data(0x30+shi); //??????,?????1 ??shi?????
- LCD1602_write_com(0x80+9);
- LCD1602_write_data(0x30+ge); //ge???shi?
- LCD1602_write_com(0x80+10);
- LCD1602_write_data(0x30+xiao);
- LCD1602_write_com(0x80+11);
- LCD1602_write_data(0x30+xiaoo);
- LCD1602_write_com(0x80+12);
- LCD1602_write_word("hz");
- }
- void key()
- {
- if(jia_key==0)
- {
- delay(5);
- if(jia_key==0)
- {
- num++;
- if(num==4)
- num=3;
- while(jia_key==0);
- }
- }
- if(jian_key==0)
- {
- delay(5);
- if(jian_key==0)
- {
- if(num!=0)
- num--;
- else
- num=0;
- while(jian_key==0);
- }
- }
- }
- void dispose()
- {
- switch(num)
- {
- case 0:gao_num=1;di_num=3;break;
- case 1:gao_num=2;di_num=2;break;
- case 2:gao_num=3;di_num=1;break;
- case 3:gao_num=4;di_num=0;break;
- }
- }
- void qudong()
- {
- uchar i;
- if(di_num!=0)
- {
- for(i=0;i<di_num;i++)
- {
- ji=0;
- display_frequency(fre);
- // delay(2);
- }
- }
- for(i=0;i<gao_num;i++)
- {
- ji=1;
- display_frequency(fre);
- // delay(2);
- }
- }
-
- void main ()
- {
- timer_init ();//定時(shí)/計(jì)數(shù)器初始化
- Init_LCD1602();
-
- while (1)
- {
- // ji=0;
- // display_frequency(fre);
- key();
- dispose();
- qudong();
- }
-
- }
復(fù)制代碼
所有資料51hei提供下載:
51單片機(jī).rar
(866.53 KB, 下載次數(shù): 22)
2019-11-11 11:18 上傳
點(diǎn)擊文件名下載附件
下載積分: 黑幣 -5
原理圖1.rar
(31.96 KB, 下載次數(shù): 17)
2019-11-11 11:20 上傳
點(diǎn)擊文件名下載附件
下載積分: 黑幣 -5
|
評(píng)分
-
查看全部評(píng)分
|