#include #include #include typedef unsigned int uint;typedef unsigned char uchar;typedef unsigned long ulong;// 4 位數(shù)碼管位值寄存器定義uchar show3, show2, show1, show0;// 數(shù)碼管 8 段顯示碼表const char LED_CODE[17] ={ 0XC0, 0XF9, 0XA4, 0XB0, 0X99, 0X92,0X82, 0XF8, 0X80, 0X90, 0X88, 0X83,0XC6, 0XA1, 0X86, 0X8E, 0X00 };// 1s 的時間內(nèi) TMR2 中斷次數(shù)uchar CNT = 0;// 脈沖個數(shù)ulong pulseNum = 0, pulseNums = 0, frequency = 0;// 系統(tǒng)初始化子程序void initial(void);// 求得各個數(shù)碼管的顯示值void show3210(void);// 顯示數(shù)據(jù)void display(void);// CCP2 中斷服務(wù)程序void CCP2_INT(void);// TMR2 中斷服務(wù)程序void TMR2_INT(void);void main(){initial();while(1){show3210();display();}}void initial(void){// 端口初始化TRISB = 0X08; // CCP2 的信號采集引腳為 RB3,將其設(shè)置為輸入模式TRISC = 0X00; // PORTC 設(shè)置為輸出,PORTC 的低 4 位為數(shù)碼管陽極選通位TRISD = 0X00; // PORTD 設(shè)置為輸出,PORTD 為 8 段數(shù)碼管顯示驅(qū)動控制位// 全局初始化RCON |= 0X80; // 使能中斷優(yōu)先級INTCON = 0XC0; // 使能全局中斷和外設(shè)中斷// TMR2 初始化PIR1 = 0X00; // 將 TMR2 與 PR2 匹配中斷標(biāo)志位清零IPR1 = 0X00; // TMR2 與 PR2 匹配中斷優(yōu)先級位為低優(yōu)先級PIE1 |= 0X02; // TMR2 與 PR2 匹配中斷允許位使能PR2 = 249; // 設(shè)置 TMR2 每次中斷溢出的時間T2CON = 0X4F; // TMR2 輸出后分頻比 1:10,TMR2 時鐘預(yù)分頻比 16,使能 TMR2// CCP2 初始化PIR2 = 0X00; // 將 CCP2 中斷標(biāo)志位清零IPR2 = 0X01; // CCP2 中斷優(yōu)先級位為高優(yōu)先級PIE2 |= 0X01; // CCP2 中斷允許位使能 CCP2CON= 0X07; // CCP2 為捕捉模式,每 16 個上升沿捕捉一次}void show3210(void){show3 = 0; show2 = 0; show1 = 0; show0 = 0;frequency = pulseNums * 16; // 求出實際的頻率,由CCP2CON= 0X07;可知if(frequency < 100000 && frequency >= 10000){show3 = 10;} // 頻率超過10 000Hz,首位顯示 A,// 后 3 位為最高的 3 位有效數(shù)字else if(frequency < 1000000 && frequency >= 100000){show3 = 11;} // 頻率超過100 000Hz,首位顯示 b,// 后 3 位為最高的 3 位有效數(shù)字else if(frequency < 10000000 && frequency >= 1000000){show3 = 12;} // 頻率超過1 000 000Hz,首位顯示 C,// 后 3 位為最高的 3 位有效數(shù)字else if(frequency < 100000000 && frequency >= 10000000){show3 = 13;} // 頻率超過10 000 000Hz,首位顯示 d,// 后 3 位為最高的 3 位有效數(shù)字else if(frequency < 1000000000 && frequency >= 100000000){show3 = 14;} // 頻率超過100 000 000Hz,首位顯示 E,// 后 3 位為最高的 3 位有效數(shù)字else if(frequency < 10000000000 && frequency >= 1000000000){show3 = 15;} // 頻率超過1 000 000 000Hz,首位顯示 F,// 后 3 位為最高的 3 位有效數(shù)字else if(frequency >= 10000000000){show3 = 17; show2 = 17; show1 = 17; show0 = 17;}// 不能顯示了,全部亮起來,顯示為 8.8.8.8// 求得 show3,show2,show1,show0 的值if(show3 == 10){frequency /= 100;show2 = frequency / 100;show1 = (frequency % 100) / 10;show0 = frequency % 10;}if(show3 == 11){frequency /= 1000;show2 = frequency / 100;show1 = (frequency % 100) / 10;show0 = frequency % 10;}if(show3 == 12){frequency /= 10000;show2 = frequency / 100;show1 = (frequency % 100) / 10;show0 = frequency % 10;}if(show3 == 13){frequency /= 100000;show2 = frequency / 100;show1 = (frequency % 100) / 10;show0 = frequency % 10;}if(show3 == 14){frequency /= 1000000;show2 = frequency / 100;show1 = (frequency % 100) / 10;show0 = frequency % 10;}if(show3 == 15){frequency /= 10000000;show2 = frequency / 100;show1 = (frequency % 100) / 10;show0 = frequency % 10;}if(show3 == 0){show3 = frequency / 1000;show2 = (frequency % 1000) / 100;show1 = (frequency % 100) / 10;show0 = frequency % 10;}}void display(void){PORTC = 0X01;PORTD=LED_CODE[show3];Delay10KTCYx(1);PORTC = 0X02;PORTD=LED_CODE[show2];Delay10KTCYx(1);PORTC = 0X04;PORTD=LED_CODE[show1];Delay10KTCYx(1);PORTC = 0X08;PORTD=LED_CODE[show0];Delay10KTCYx(1);}#pragma code high_vector = 0x08void high_interrupt (void){_asmgoto CCP2_INT_endasm}#pragma code //中斷服務(wù)程序#pragma interrupt CCP2_INTvoid CCP2_INT(void){PIR2 &= 0XFE; // CCP2 中斷標(biāo)志位清零pulseNum++;}#pragma code low_vector = 0x18void low_interrupt (void){_asmgoto TMR2_INT_endasm}#pragma code#pragma interrupt TMR2_INTvoid TMR2_INT(void){CNT++;PIR1 &= 0XFD; // 將 TMR2 與 PR2 匹配中斷標(biāo)志位清零if(CNT == 100){CNT = 0;pulseNums = pulseNum;pulseNum = 0;}}