PCA can be used to measure pulse width. However, protues does not currently support the simulation function of this series of microcontrollers, and repeated burning is also very troublesome, so it is better to use the basic 51 microcontroller to implement this function first, and then implement PCA to measure pulse width in the following blog post.
The implementation ideas are as follows:
After the highest bit GATEn of TMOD is set, Tn starts counting and is affected by INTn (Pin3.3) and TRn: TRn is 1, and Tn is allowed to count only when the INTn pin input is high. This function can be used to measure the width of the positive pulse on INTn.
First, here's the picture (forgive my drawing skills):
1):1 before the rising edge, initialize TMOD, TRn=1;
2): INTn pin at 2 is at high level, and starts counting and measuring pulse width;
3): INTn pin at 3 is at low level, measurement ends and counting stops TRn=0
Here is the simulation picture:
1). Select 5v square wave as the signal generator level. Note that the inverting end of the signal generator is grounded, otherwise the positive end will only output a 2.5v square wave (the remaining 2.5v outputs an inverted square wave, which can be connected to an oscilloscope for a try), and INTn will never receive a high level, and the expected effect will not be achieved.
2). The T0 timer is used as a counter, and an overflow occurs when a negative pulse is received, which starts T1;
3). T0 and T1 all work in mode 2 automatic loading count value mode.
Then, the code:
Working frequency 12Mhz
#include#include sbit P1_0 = P3^3; #define MakeByte(target, Hi,Lo) \ do{ \ target |= (((Hi)<<4)|(Lo)); \ }while(0); \ #define SetTH(n,val) \ do{ \ TH##n = val; \ }while(0); \ #define SetTL(n,val) \ do{ \ TL##n = val; \ }while(0); \ #define EnableET(n) \ do{ \ ET##n = 0x01; \ IE |= 0x80; \ }while(0); \ unsigned int click; unsigned int oneMs; unsigned char getPlusWidth; int main() { unsigned int totalus=0,maxPlusWidth=0; P3 = 0xFF; getPlusWidth = 0; MakeByte(TMOD,0x0A,0x06); SetTH(0,0xff); SetTL(0,0xff); SetTH(1,0x38); SetTL(1,0x38); EnableET(0); EnableET(1); TR0 = 0x01; while(1) { while(!getPlusWidth); //Wait for INT1 to go low while(INT1==0x01); //Wait for INT1 to reach high level while(INT1==0x00); //Wait for INT1 to be low level, pulse width ends while(INT1==0x01); TR1 = 0x00; totalus = 1000*(oneMs+(click*0.2))+(TL1-TH1); oneMs = 0; } return 0; } //T0 pin receives negative transition void IsrT0() interrupt 1 { TR1 = 0x00; getPlusWidth = 1; TR1 = 0x01; } void IsrT1() interrupt 3 { //Each interruption is 0.2ms click++; if(click == 5) { oneMs++; click=0; } }
Finally, the simulation results are:
500Hz square wave, pulse width 981us
1kHz square wave, pulse width 587us
Previous article:How to realize serial communication between PC and single chip microcomputer
Next article:How to generate the system clock of 51 series microcontroller
Recommended ReadingLatest update time:2024-11-16 13:38
- Popular Resources
- Popular amplifiers
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- Innolux's intelligent steer-by-wire solution makes cars smarter and safer
- 8051 MCU - Parity Check
- How to efficiently balance the sensitivity of tactile sensing interfaces
- What should I do if the servo motor shakes? What causes the servo motor to shake quickly?
- 【Brushless Motor】Analysis of three-phase BLDC motor and sharing of two popular development boards
- Midea Industrial Technology's subsidiaries Clou Electronics and Hekang New Energy jointly appeared at the Munich Battery Energy Storage Exhibition and Solar Energy Exhibition
- Guoxin Sichen | Application of ferroelectric memory PB85RS2MC in power battery management, with a capacity of 2M
- Analysis of common faults of frequency converter
- In a head-on competition with Qualcomm, what kind of cockpit products has Intel come up with?
- Dalian Rongke's all-vanadium liquid flow battery energy storage equipment industrialization project has entered the sprint stage before production
- Allegro MicroSystems Introduces Advanced Magnetic and Inductive Position Sensing Solutions at Electronica 2024
- Car key in the left hand, liveness detection radar in the right hand, UWB is imperative for cars!
- After a decade of rapid development, domestic CIS has entered the market
- Aegis Dagger Battery + Thor EM-i Super Hybrid, Geely New Energy has thrown out two "king bombs"
- A brief discussion on functional safety - fault, error, and failure
- In the smart car 2.0 cycle, these core industry chains are facing major opportunities!
- The United States and Japan are developing new batteries. CATL faces challenges? How should China's new energy battery industry respond?
- Murata launches high-precision 6-axis inertial sensor for automobiles
- Ford patents pre-charge alarm to help save costs and respond to emergencies
- New real-time microcontroller system from Texas Instruments enables smarter processing in automotive and industrial applications
- Experiment of controlling relay with MF RC522 RF card based on Arduino UNO R3
- Problems in transplanting UCOSII to 51 MCU
- LiChuang Mall is right and confident in its mistakes. Everyone should be careful and don't be cheated.
- The CC2530 low-power terminal keeps reconnecting to the network and consumes power
- Crazy Shell AI open source drone OPENMV script burning
- For C5000, how to boot a program larger than 48K?
- Qorvo's technical highlights on WiFi 6
- Introduction to the mobile station development board: STM32G071 NUCLEO-64
- How to locate RFID based on wireless network
- Regarding the issue of using NPN transistors for high voltage conversion output.