CT107D 频率计 蓝桥杯频率测量 可以测量0-65khz方波
基于STC的官方示例文件完成编写,推荐STC单片机,性能稳定
使用时请使用杜邦线连接P3_2引脚和555发生的SIGNAL引脚
#include "reg51.h" #include "intrins.h" typedef unsigned char BYTE; typedef unsigned int WORD; WORD count=0,ms=0,last=0; //----------------------------------------------- #define FOSC 11059200L #define T1MS (65536-FOSC/1000) //1T模式 //#define T1MS (65536-FOSC/12/1000) //12T模式 //----------------------------------------------- sfr P0M1 = 0x93; sfr P0M0 = 0x94; sfr P1M1 = 0x91; sfr P1M0 = 0x92; sfr P2M1 = 0x95; sfr P2M0 = 0x96; sfr P3M1 = 0xb1; sfr P3M0 = 0xb2; sfr P4M1 = 0xb3; sfr P4M0 = 0xb4; sfr P5M1 = 0xC9; sfr P5M0 = 0xCA; sfr P6M1 = 0xCB; sfr P6M0 = 0xCC; sfr P7M1 = 0xE1; sfr P7M0 = 0xE2; sfr AUXR = 0x8e; //Auxiliary register sbit P10 = P1^0; //----------------------------------------- //中断服务程序 void exint0() interrupt 0 //INT0中断入口 { count++; } //----------------------------------------------- //----------------------------------------------- /* Timer1 interrupt routine */ void tm1_isr() interrupt 3 { if(ms++>=1000) { last=count; count=0; ms=0; } } //----------------------------------------------- void hdw_init(); void smg_refresh(); void Delay1ms(); unsigned char num[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90}; //uchar code tab[]={0XC0,0XF9,0XA4,0XB0,0X99,0X92,0X82,0XF8,0X80,0X90,0XBF,0XFF}; //1 2 3 4 5 6 7 8 9 - 灭 //uchar code dottab[]={0x40,0x79,0x24,0x30,0x19,0x12,0x02,0x78,0x00,0x10}; //带小数点的数字 0. 1. 2. 3. 4. 5. 6. 7. 8. 9. unsigned char duan[]={0x01,0x02,0x04,0x08,0x10,0x20,0x40,0x80}; unsigned char smg[8]={1,2,3,4,5,6,7,8}; unsigned char x; //----------------------------------------------- void main() { hdw_init(); P0M0 = 0x00; P0M1 = 0x00; P1M0 = 0x00; P1M1 = 0x00; P2M0 = 0x00; P2M1 = 0x00; P3M0 = 0x00; P3M1 = 0x00; P4M0 = 0x00; P4M1 = 0x00; P5M0 = 0x00; P5M1 = 0x00; P6M0 = 0x00; P6M1 = 0x00; P7M0 = 0x00; P7M1 = 0x00; AUXR |= 0x40; //定时器1为1T模式 TMOD = 0x00; //设置定时器为模式0(16位自动重装载) TL1 = T1MS; //初始化计时值 TH1 = T1MS >> 8; TR1 = 1; //定时器1开始计时 ET1 = 1; //使能定时器0中断 INT0 = 1; IT0 = 1; //设置INT0的中断类型 (1:仅下降沿 0:上升沿和下降沿) EX0 = 1; //使能INT0中断 EA = 1; while (1)smg_refresh(); } void Delay1ms() //@12.000MHz { unsigned char i, j; i = 12; j = 169; do { while (--j); } while (--i); } void hdw_init() { P2=0xA0; P0=0x00; P2=0x00; P2=0x80; P0=0xFF; P2=0x00; } void smg_refresh() { smg[0]=last/10000000%10; smg[1]=last/1000000%10; smg[2]=last/100000%10; smg[3]=last/10000%10; smg[4]=last/1000%10; smg[5]=last/100%10; smg[6]=last/10%10; smg[7]=last/1%10; for(x=0;x<8;x++) { P2=0xC0; P0=duan[x]; P2=0x00; P2=0xE0; P0=num[smg[x]]; Delay1ms(); P0=0xFF; P2=0x00; } }
稍作修改,防止count溢出,这里加个判断
#include "reg51.h" #include "intrins.h" typedef unsigned char BYTE; typedef unsigned int WORD; WORD count=0,ms=0,last=0; //----------------------------------------------- #define FOSC 11059200L #define T1MS (65536-FOSC/1000) //1T模式 //#define T1MS (65536-FOSC/12/1000) //12T模式 //----------------------------------------------- sfr P0M1 = 0x93; sfr P0M0 = 0x94; sfr P1M1 = 0x91; sfr P1M0 = 0x92; sfr P2M1 = 0x95; sfr P2M0 = 0x96; sfr P3M1 = 0xb1; sfr P3M0 = 0xb2; sfr P4M1 = 0xb3; sfr P4M0 = 0xb4; sfr P5M1 = 0xC9; sfr P5M0 = 0xCA; sfr P6M1 = 0xCB; sfr P6M0 = 0xCC; sfr P7M1 = 0xE1; sfr P7M0 = 0xE2; sfr AUXR = 0x8e; //Auxiliary register sbit P10 = P1^0; //----------------------------------------- //中断服务程序 void exint0() interrupt 0 //INT0中断入口 { if(count<65530)count++; } //----------------------------------------------- //----------------------------------------------- /* Timer1 interrupt routine */ void tm1_isr() interrupt 3 { if(ms++>=1000) { last=count; count=0; ms=0; } } //----------------------------------------------- void hdw_init(); void smg_refresh(); void Delay1ms(); unsigned char num[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90}; //uchar code tab[]={0XC0,0XF9,0XA4,0XB0,0X99,0X92,0X82,0XF8,0X80,0X90,0XBF,0XFF}; //1 2 3 4 5 6 7 8 9 - 灭 //uchar code dottab[]={0x40,0x79,0x24,0x30,0x19,0x12,0x02,0x78,0x00,0x10}; //带小数点的数字 0. 1. 2. 3. 4. 5. 6. 7. 8. 9. unsigned char duan[]={0x01,0x02,0x04,0x08,0x10,0x20,0x40,0x80}; unsigned char smg[8]={1,2,3,4,5,6,7,8}; unsigned char x; //----------------------------------------------- void main() { hdw_init(); P0M0 = 0x00; P0M1 = 0x00; P1M0 = 0x00; P1M1 = 0x00; P2M0 = 0x00; P2M1 = 0x00; P3M0 = 0x00; P3M1 = 0x00; P4M0 = 0x00; P4M1 = 0x00; P5M0 = 0x00; P5M1 = 0x00; P6M0 = 0x00; P6M1 = 0x00; P7M0 = 0x00; P7M1 = 0x00; AUXR |= 0x40; //定时器1为1T模式 TMOD = 0x00; //设置定时器为模式0(16位自动重装载) TL1 = T1MS; //初始化计时值 TH1 = T1MS >> 8; TR1 = 1; //定时器1开始计时 ET1 = 1; //使能定时器0中断 INT0 = 1; IT0 = 1; //设置INT0的中断类型 (1:仅下降沿 0:上升沿和下降沿) EX0 = 1; //使能INT0中断 EA = 1; while (1)smg_refresh(); } void Delay1ms() //@12.000MHz { unsigned char i, j; i = 12; j = 169; do { while (--j); } while (--i); } void hdw_init() { P2=0xA0; P0=0x00; P2=0x00; P2=0x80; P0=0xFF; P2=0x00; } void smg_refresh() { smg[0]=last/10000000%10; smg[1]=last/1000000%10; smg[2]=last/100000%10; smg[3]=last/10000%10; smg[4]=last/1000%10; smg[5]=last/100%10; smg[6]=last/10%10; smg[7]=last/1%10; for(x=0;x<8;x++) { P2=0xC0; P0=duan[x]; P2=0x00; P2=0xE0; P0=num[smg[x]]; Delay1ms(); P0=0xFF; P2=0x00; } }