給你個(gè)測(cè)轉(zhuǎn)速的參考下
#include<at89x51.h> #define uchar unsigned char #define uint unsigned int sbit key=P3^0; sbit f=P1^1; uint cur_r; uint cur_ms,cur_sec;//當(dāng)前毫秒,秒 uint couter; //脈沖數(shù)量 uchar code design[10]={0x28, 0x7E, 0xA2, 0x62, 0x74, 0x61, 0x21, 0x7A, 0x20, 0x60}; //******************************************************************* //***************initial********************************************* void initial(void) { couter=cur_r=0; TMOD=0x01; TH0=0xb1; TL0=0xe0; EA=1; EX0=0; IT0=1; } //延時(shí)*************************************************************** //delay()************************************************************ void delay(uint i) { uint j; for(;i>0;i--) for(j=0;j<120;j++) {;} } //外部中斷*********************************************************** //caculate外部脈沖數(shù)量*********************************************** void intrp0(void) interrupt 0 { couter++; } //定時(shí)器1中斷******************************************************** //20ms中斷*********************************************************** //編碼器采用100PULSE/R的********************************************* void intrp1(void) interrupt 1 { TH0=0xb1; TL0=0xe0; if(cur_ms<50){cur_ms++;} else { cur_ms=0; cur_sec++; if(cur_sec>=2) { EX0=0; //計(jì)算時(shí)禁止外部中斷,停止計(jì)數(shù) ET0=0; TR0=0; cur_r=couter*3/10; //當(dāng)前轉(zhuǎn)速 cur_ms=0; cur_sec=0; couter=0; EX0=1; //重新開啟脈沖計(jì)數(shù) ET0=1; TR0=1; f=~f; cur_sec=0; } } } //顯示接口*********************************************************** //P0為數(shù)碼管顯示接口,P1為數(shù)碼管控制接口***************************** void display(uint x) { uint g_wei,s_wei,b_wei;//個(gè)位,十位,百位 if(x>=999){x=999;}//限制為999R/MIN b_wei=x/100; s_wei=(x-b_wei*100)/10; g_wei=(x-b_wei*100-s_wei*10)%10; //百位顯示 P0=design[b_wei]; P2=0xfe; delay(5); //十位顯示 P0=design[s_wei]; P2=0xfd; delay(5); //個(gè)位顯示 P0=design[g_wei]; P2=0xfb; delay(5); } //******************************************************************* //main*************************************************************** void main() { initial(); while(1) { display(cur_r); if(key){EX0=0;ET0=0;TR0=0;} //按下停止按鈕 else {EX0=1;ET0=1,TR0=1;} } }
歡迎光臨 (http://www.torrancerestoration.com/bbs/) | Powered by Discuz! X3.1 |