The STC12C5608AD microcontroller has a CAP function, which can be used to measure the frequency of a signal. The principle is to measure the time between two rising edges or falling edges. This time is the period of the signal, and the reciprocal of the period is the frequency. The following is the program to achieve this:
/****************************************************** * * Name: CAP function measures signal frequency * Main frequency: 12MHz * Author: Ration * Date: December 7, 2013 (edited) * Copyright: Copyright belongs to "MCU Learning Network". Please keep integrity when disseminating. *************************************************/ #include//Call the header file, you can download the header file of STC12C5608AD from the official website unsigned char counter=0; unsigned char out_data[2]={0x00}; void UART_Init() { TMOD=0x22; TH1=0xF3; TL1=0xF3; //Set the baud rate to 2400 TH0=0XEC; //Value under 12M TL0=0XEC; SCON=0X50; TR1=1; TR0=1; } void CAP_Init() { CMOD = 0X04; // ECF = 1; Enable PCA interrupt flag CCAPM1 = 0X11; // CAPN0 = 1; Falling edge capture. ECCF0 = 1; Enable capture interrupt. CL = 0; CH = 0; // PCA counter cleared CCAP1L = 0; CCAP1H = 0; // capture register clear EPCA_LVD = 1; // Enable PCA interrupt CR = 1; } void send232byte(unsigned char bytebuf) { TI=0; SBUF=bytebuf; while(!TI); } void delay() { unsigned int x; for(x=0;x<65535;x++); } void PCAIRQ(void) interrupt 6 { if(CCF1==1) // If the interrupt is generated by PCA capture { CF=0; CCF1=0; out_data[0]=CCAP1L; out_data[1]=CCAP1H; CH=0; CL=0; } } void main(void) { unsigned int tmp; unsigned long dat; UART_Init(); CAP_Init(); EA = 1; while(1) // infinite loop { tmp=out_data[1]<<8; tmp+=out_data[0]; dat=50000/tmp; //Calculate frequency value send232byte(dat>>8); send232byte(dat); delay(); } }
In the program, T1 timer is used as serial port baud rate generator to send frequency value to serial port, T0 is used as PCA clock generator, and it is set to interrupt once every 20us. Therefore, the measured value tmp*20 is the period in microseconds, which is converted into frequency, that is, 1/tmp*20/1000000, that is, 50000/tmp.
Previous article:51 MCU driver 1602 program
Next article:Solution to 51 MCU PWM duty cycle less than 0
- Popular Resources
- Popular amplifiers
- Naxin Micro and Xinxian jointly launched the NS800RT series of real-time control MCUs
- How to learn embedded systems based on ARM platform
- Summary of jffs2_scan_eraseblock issues
- Application of SPCOMM Control in Serial Communication of Delphi7.0
- Using TComm component to realize serial communication in Delphi environment
- Bar chart code for embedded development practices
- Embedded Development Learning (10)
- Embedded Development Learning (8)
- Embedded Development Learning (6)
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- Intel promotes AI with multi-dimensional efforts in technology, application, and ecology
- ChinaJoy Qualcomm Snapdragon Theme Pavilion takes you to experience the new changes in digital entertainment in the 5G era
- Infineon's latest generation IGBT technology platform enables precise control of speed and position
- Two test methods for LED lighting life
- Don't Let Lightning Induced Surges Scare You
- Application of brushless motor controller ML4425/4426
- Easy identification of LED power supply quality
- World's first integrated photovoltaic solar system completed in Israel
- Sliding window mean filter for avr microcontroller AD conversion
- What does call mean in the detailed explanation of ABB robot programming instructions?
- Melexis uses coreless technology to reduce the size of current sensing devices
- Melexis uses coreless technology to reduce the size of current sensing devices
- Vicor high-performance power modules enable the development of low-altitude avionics and EVTOL
- Chuangshi Technology's first appearance at electronica 2024: accelerating the overseas expansion of domestic distributors
- Chuangshi Technology's first appearance at electronica 2024: accelerating the overseas expansion of domestic distributors
- "Cross-chip" quantum entanglement helps build more powerful quantum computing capabilities
- Ultrasound patch can continuously and noninvasively monitor blood pressure
- Ultrasound patch can continuously and noninvasively monitor blood pressure
- Europe's three largest chip giants re-examine their supply chains
- Europe's three largest chip giants re-examine their supply chains
- Please tell me the circuit of transmitting RS485 signal of power supply on a pair of twisted pair wires at the same time
- Questions about vhdl testbench
- Analysis and simulation of a large step-up ratio four-terminal DC/DC converter
- Can the AGND and DGND pins of VS1053 be connected directly?
- Principle and application of capacitive sensor
- Electronic dimming ballast controller
- The difference between OTL and OCL circuits
- Implementation of Parallel Variable Length Decoder Based on FPGA
- Selling a Digilent Basys3 FPGA development board for 800 yuan
- QCA9531 and AR9342 support USB camera mini embedded AP module