The conventional method of measuring the frequency of pulses is to use two timers of the 51 single-chip microcomputer to measure, one timer for timing and one timer for counting. Is there a better way to meet the needs without using so much hardware and only use one timer? Of course, timer 2 has an input capture function. P1.1 is connected to an external pulse. When a pulse comes, timer 2 enters the interrupt service program and interrupts once. It also has a timing function. The following is the main program:
#include #define uint unsigned int #define uchar unsigned char #define ulong unsigned long uint timer2_count; uint cap_count; uchar code dispbit[]={0x01,0x02,0x04,0x08,0x10,0x20,0x40,0x80}; // define bit selection starting from the second digit uchar code dispcode[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0xff,0xbf}; //define segment code uchar dispbuf[8]={0,0,0,0,0,0,10,10}; //display buffer uchar temp[8]; uchar dispcount; uint x; bit flag; sbit L1 = P0^7; //sbit L2 = P0^1; sbit W1=P1^2; sbit W2=P1^3; sbit W3=P1^4; sbit W4=P1^5; sbit W5=P1^6; sbit W6=P1^7; void display_wei(uchar dipcount) { switch (dipcount){ case 0: W1=1;W2=W3=W4=W5=W6=0; break; case 1: {W2=1;W1=W3=W4=W5=W6=0;} break; case 2: {W3=1;W2=W1=W4=W5=W6=0;} break; case 3: {W4=1;W2=W3=W1=W5=W6=0;} break; case 5: {W5=1;W2=W3=W4=W1=W6=0;} break; case 6: {W6=1;W2=W3=W4=W5=W1=0;} break; } } void timer2_init() { ET2=1; //Start timer 2 T2CON=0X09; //Set T2 to capture mode, and generate an interrupt on the falling edge TH2=(65535-5000)/256; TL2=(65535-5000)%256; EA=1; //Enable interrupt TR2=1; //Start the counter } void main() { timer2_init(); while(1) { uchar i; if(flag==1) { flag=0; x=cap_count; //Frequency formula The number of pulses measured in one second //x= x*563; //x=x/10; for(i=0;i<8;i++) { temp[i]=0; // array clear } i=0; //x=123456; while(x/10) { temp[i]=x%10; x=x/10; i++; } temp[i]=x; for(i=0;i<6;i++) { dispbuf[i]=temp[i]; //Display 6-digit frequency (cache) } cap_count=0; TR2=1; } } } void timer2_interrupt(void) interrupt 5 using 1 //interrupt service routine { if(EXF2) //Captured interrupt { //L1=!L1; cap_count++; EXF2=0; } if(TF2) { TH2=(65535-5000)/256; TL2=(65535-5000)%256; timer2_count++; if(timer2_count==400) { //L2=!L2; flag=1; timer2_count=0; TR2=0; //stop counting } TF2=0; P0=dispcode[dispbuf[dispcount]]; //display data //P1 = ((P1&0x02)|dispbit[dispcount+2]); //bit selection display_wei(dispcount); dispcount++; if(dispcount==6) { dispcount=0; } } }
Previous article:51 MCU timer counter principle and application (square wave, pwm, pulse counting, high level pulse width measurement)
Next article:51 single chip microcomputer to make pulse generator
Recommended ReadingLatest update time:2024-11-23 18:15
- Popular Resources
- Popular amplifiers
- 西门子S7-12001500 PLC SCL语言编程从入门到精通 (北岛李工)
- MCU C language programming and Proteus simulation technology (Xu Aijun)
- 100 Examples of Microcontroller C Language Applications (with CD-ROM, 3rd Edition) (Wang Huiliang, Wang Dongfeng, Dong Guanqiang)
- Fundamentals and Applications of Single Chip Microcomputers (Edited by Zhang Liguang and Chen Zhongxiao)
- 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?
- STMicroelectronics discloses its 2027-2028 financial model and path to achieve its 2030 goals
- 2024 China Automotive Charging and Battery Swapping Ecosystem Conference held in Taiyuan
- State-owned enterprises team up to invest in solid-state battery giant
- The evolution of electronic and electrical architecture is accelerating
- The first! National Automotive Chip Quality Inspection Center established
- BYD releases self-developed automotive chip using 4nm process, with a running score of up to 1.15 million
- GEODNET launches GEO-PULSE, a car GPS navigation device
- Should Chinese car companies develop their own high-computing chips?
- Infineon and Siemens combine embedded automotive software platform with microcontrollers to provide the necessary functions for next-generation SDVs
- Continental launches invisible biometric sensor display to monitor passengers' vital signs
- [Xiangyin Review] JBL QUANTUM TWS Review——The Pioneer of 1,000-yuan Dual-mode Gaming Noise Cancelling Headphones
- i.MX6ULL Embedded Linux Development——by DDZZ669
- The three-phase high current (30A~40A) of the motor is connected through the PCB, and the heat dissipation hole blind hole problem
- Common problems when isolating signal and power designs
- STM32CubeMX uses LL library to set PWM output
- Unboxing of SenSortile.Box and solutions and suggestions for some issues
- Is there any component that can directly replace upc151? Can any big brother help me?
- EEWORLD University ---- Key Isolation Gate Driver Specifications
- RTT Studio is an open source and free one-stop development tool for embedded systems. It’s really great to use it!
- PCB Design and Wiring Skills Album