Pulse signals are a common type of signal in engineering, such as the output signals of photoelectric displacement, speed, rotation speed and other sensors, and pulse width and pulse period are their basic parameters, which often need to be measured. It is relatively easy to measure pulse signals with equal periods. You only need to measure any pulse width or period. In recent years, many MCS-51 users have developed new products by measuring the basic parameters of pulse signals. For example, Shandong University's "ZBCY-I Intelligent Javelin Speed Tester" uses the measurement of a pulse width to obtain the average speed of the javelin within a certain flight distance. It is relatively difficult to measure the period of a pulse signal with a continuous variable period. Many measurement personnel first divide this type of signal into two and then invert it. Then use two timers/counters to measure the positive pulse width of the two pulse signals to obtain the period. This method has a large hardware overhead and a complex software design. This article will introduce a practical method of directly measuring the period of a continuously variable period pulse signal using a timer/counter inside the MCS-51 microcontroller.
When using the internal timer of 8031 to measure the period of a continuously variable period pulse signal, the key is how to control the start and stop of the timer/counter. Take timer T1 as an example. When it works in mode 1, it works as a 16-bit counter, consisting of two special function registers THl as the high 8 bits and TLl as the low 8 bits. Its control logic is shown in Figure 1.
Figure 1 Timing, counter mode 1 control logic
When the gate bit GATE of the timer/counter mode control register TMOD = 0, the start and stop of the counter are only controlled by TR1. As long as TR1 is set. The counter is selected regardless of the level of INT1. Therefore, in the main program, first set the EA bit in IE to introduce the signal into INT1, and reset and set the TR1 bit in TCON in the INT1 interrupt service program to realize the stop of the counter, data reading
, counter initialization and start. Its working process is shown in Figure 2.
Figure 2 Timing, counter pulse cycle diagram
Where: t1-interrupt service program execution time
t2-counter counting time
Then: the i-th pulse cycle ti=t1+t2(i=1,2,3,…).
It can be seen that only one timer/counter inside 8031 can measure each cycle of a variable period pulse signal.
The flowchart of the timer T1 interrupt service program is shown in Figure 3. [page]
The interrupt service program list of timer Tl is as follows:
INT1: CLR TR1; turn off counter 1
PUSH ACC; ACC is pushed into the stack
PUSH 00H; R0 is pushed
into the stack CLR C; clear the carry flag
MOV A, TL1; the low 8 bits of the count value → accumulator
ADD A, #23H; counter value + interrupt execution time 32us (12MHz crystal oscillator) +
; minimum interrupt response time 3-pulse period
MOV R0, A; the low 8 bits are temporarily stored in R0
MOV A, TH1
ADDC A, #00H
MOVX @DPTR, A; the high 8 bits of the pulse period → the RAM unit pointed to by @DPTR
INC DPTR; point to the next RAM unit
MOV A, R0
MOVX @DPTR, A; the low 8 bits of the pulse period → the RAM unit pointed to by DPTR
INC DPTR
INC R1; the number of pulse periods plus one
MOV TH1, #00H; clear counter 1
MOV TL1, #00H
POP 00H
POP ACC
SETB TR1 ;Restart counting
RETI ;Interrupt return
The minimum pulse period that can be sampled by the interrupt service program is 32us. If the field protection in the program is removed and the count value is converted into a pulse period after sampling, the pulse period that can be sampled can be further reduced. The maximum pulse period that can be sampled is 65535us (when the crystal oscillator is 12MHz). If it is greater than this value, the user can modify the program slightly. Therefore, there is actually no limit on the maximum period. Assuming that the pulse signal is sampled for 100 cycles and the sampled values are stored in the external RAM starting from 50H, the main program is as follows:
MAIN:
PUSH TMOD; push the special register TMOD TCON IE into the stack
PUSH TCON
PUSH IE
MOV THI,#00H; clear the counter
MOV TL1,#00H
MOV TMOD. 10H; Counter 1 working mode 1
SETB EA; Enable interrupt
SETB EX1; External interrupt 1
SETB IT1; INT1 negative jump triggers interrupt
MOV DPTR, #50H; RAM first address for storing pulse cycle
MOV R1, #00H; R1 controls sampling cycle
SETB TR1; Counter 1 starts timing
CJNE R1, #64H, $; Wait for 100 cycles for sampling to end
CLR TR1; Turn off counter
POP IE
POP TCON
POP TMOD
The first pulse cycle sampled above is inaccurate and should be discarded during data processing. It can also be automatically discarded by adding a judgment statement in the interrupt service program. From the above, it can be seen that this method does not require additional hardware, the software is simple, and the range of pulse cycles that can be measured is large, but the first cycle of the pulse signal cannot be measured.
Previous article:8-bit MCU IPCORE design compatible with 51 instructions
Next article:89C52 PWM Output Program
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
- [NXP Rapid IoT Review] Offline Compilation and Testing Summary
- [GD32F350/GD32E103] Build VSF-based WebUSB DFU using open source toolchain
- How to determine whether the power supply needs a PFC circuit?
- RFMD and Qorvo prototype boards vs. honeycomb heat sinks
- Is there any big guy who does MEMS process research and development? Please guide me
- Question about LED flashing (newbie question)
- Learn about vehicle-to-everything (V2X) technology in one article
- Evaluation of the domestic FPGA Gaoyun GW1N-4 series development board - Software Part 1
- R329 Zhouyi AIPU environment construction and simulation practice
- Current sensor circuit inquiry?