#include #include #include typedef unsigned int uint;typedef unsigned char uchar;typedef unsigned long ulong;// 4 位数码管位值寄存器定义uchar show3, show2, show1, show0;// 数码管 8 段显示码表const char LED_CODE[17] ={ 0XC0, 0XF9, 0XA4, 0XB0, 0X99, 0X92,0X82, 0XF8, 0X80, 0X90, 0X88, 0X83,0XC6, 0XA1, 0X86, 0X8E, 0X00 };// 1s 的时间内 TMR2 中断次数uchar CNT = 0;// 脉冲个数ulong pulseNum = 0, pulseNums = 0, frequency = 0;// 系统初始化子程序void initial(void);// 求得各个数码管的显示值void show3210(void);// 显示数据void display(void);// CCP2 中断服务程序void CCP2_INT(void);// TMR2 中断服务程序void TMR2_INT(void);void main(){initial();while(1){show3210();display();}}void initial(void){// 端口初始化TRISB = 0X08; // CCP2 的信号采集引脚为 RB3,将其设置为输入模式TRISC = 0X00; // PORTC 设置为输出,PORTC 的低 4 位为数码管阳极选通位TRISD = 0X00; // PORTD 设置为输出,PORTD 为 8 段数码管显示驱动控制位// 全局初始化RCON |= 0X80; // 使能中断优先级INTCON = 0XC0; // 使能全局中断和外设中断// TMR2 初始化PIR1 = 0X00; // 将 TMR2 与 PR2 匹配中断标志位清零IPR1 = 0X00; // TMR2 与 PR2 匹配中断优先级位为低优先级PIE1 |= 0X02; // TMR2 与 PR2 匹配中断允许位使能PR2 = 249; // 设置 TMR2 每次中断溢出的时间T2CON = 0X4F; // TMR2 输出后分频比 1:10,TMR2 时钟预分频比 16,使能 TMR2// CCP2 初始化PIR2 = 0X00; // 将 CCP2 中断标志位清零IPR2 = 0X01; // CCP2 中断优先级位为高优先级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 位有效数字else if(frequency < 1000000 && frequency >= 100000){show3 = 11;} // 频率超过100 000Hz,首位显示 b,// 后 3 位为最高的 3 位有效数字else if(frequency < 10000000 && frequency >= 1000000){show3 = 12;} // 频率超过1 000 000Hz,首位显示 C,// 后 3 位为最高的 3 位有效数字else if(frequency < 100000000 && frequency >= 10000000){show3 = 13;} // 频率超过10 000 000Hz,首位显示 d,// 后 3 位为最高的 3 位有效数字else if(frequency < 1000000000 && frequency >= 100000000){show3 = 14;} // 频率超过100 000 000Hz,首位显示 E,// 后 3 位为最高的 3 位有效数字else if(frequency < 10000000000 && frequency >= 1000000000){show3 = 15;} // 频率超过1 000 000 000Hz,首位显示 F,// 后 3 位为最高的 3 位有效数字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 //中断服务程序#pragma interrupt CCP2_INTvoid CCP2_INT(void){PIR2 &= 0XFE; // CCP2 中断标志位清零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 匹配中断标志位清零if(CNT == 100){CNT = 0;pulseNums = pulseNum;pulseNum = 0;}}