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
- Learn ARM development(16)
- Learn ARM development(17)
- Learn ARM development(18)
- Embedded system debugging simulation tool
- A small question that has been bothering me recently has finally been solved~~
- Learn ARM development (1)
- Learn ARM development (2)
- Learn ARM development (4)
- Learn ARM development (6)
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- LED chemical incompatibility test to see which chemicals LEDs can be used with
- Application of ARM9 hardware coprocessor on WinCE embedded motherboard
- What are the key points for selecting rotor flowmeter?
- LM317 high power charger circuit
- A brief analysis of Embest's application and development of embedded medical devices
- Single-phase RC protection circuit
- stm32 PVD programmable voltage monitor
- Introduction and measurement of edge trigger and level trigger of 51 single chip microcomputer
- Improved design of Linux system software shell protection technology
- What to do if the ABB robot protection device stops
- CGD and Qorvo to jointly revolutionize motor control solutions
- CGD and Qorvo to jointly revolutionize motor control solutions
- Keysight Technologies FieldFox handheld analyzer with VDI spread spectrum module to achieve millimeter wave analysis function
- Infineon's PASCO2V15 XENSIV PAS CO2 5V Sensor Now Available at Mouser for Accurate CO2 Level Measurement
- Advanced gameplay, Harting takes your PCB board connection to a new level!
- Advanced gameplay, Harting takes your PCB board connection to a new level!
- A new chapter in Great Wall Motors R&D: solid-state battery technology leads the future
- Naxin Micro provides full-scenario GaN driver IC solutions
- Interpreting Huawei’s new solid-state battery patent, will it challenge CATL in 2030?
- Are pure electric/plug-in hybrid vehicles going crazy? A Chinese company has launched the world's first -40℃ dischargeable hybrid battery that is not afraid of cold
- RT-Thread device framework learning RTC device
- Orcad learning notes (three) class and sub class in PCB
- [Hua Diao Experience] 17 Beetle ESP32C3 and WS2812 screen music visualization rhythm light
- EEWORLD University----42/5000 Power over Ethernet (PoE) Training Series
- How to effectively program a microcontroller active buzzer driver
- MSP430 SPI reads AFE4400 register value code
- WPG Live Broadcast Registration | Thundercomm, Lianda, Qualcomm IOT Platform Solutions and Success Stories
- The rain is a surprise in spring, the valley is clear, the summer is full of grains, and the summer heat is connected
- Microchip Live FAQ|ADAS Platform Root of Trust
- Scaling of DSP data