A Simple Method of Measuring Variable Period Pulse Signals with 51 Single Chip Microcomputer

Publisher:YaqiLatest update time:2012-10-30 Source: 21ic Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

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.

Reference address:A Simple Method of Measuring Variable Period Pulse Signals with 51 Single Chip Microcomputer

Previous article:8-bit MCU IPCORE design compatible with 51 instructions
Next article:89C52 PWM Output Program

Latest Microcontroller Articles
  • Download from the Internet--ARM Getting Started Notes
    A brief introduction: From today on, the ARM notebook of the rookie is open, and it can be regarded as a place to store these notes. Why publish it? Maybe you are interested in it. In fact, the reason for these notes is ...
  • Learn ARM development(22)
    Turning off and on interrupts Interrupts are an efficient dialogue mechanism, but sometimes you don't want to interrupt the program while it is running. For example, when you are printing something, the program suddenly interrupts and another ...
  • Learn ARM development(21)
    First, declare the task pointer, because it will be used later. Task pointer volatile TASK_TCB* volatile g_pCurrentTask = NULL;volatile TASK_TCB* vol ...
  • Learn ARM development(20)
    With the previous Tick interrupt, the basic task switching conditions are ready. However, this "easterly" is also difficult to understand. Only through continuous practice can we understand it. ...
  • Learn ARM development(19)
    After many days of hard work, I finally got the interrupt working. But in order to allow RTOS to use timer interrupts, what kind of interrupts can be implemented in S3C44B0? There are two methods in S3C44B0. ...
  • Learn ARM development(14)
  • Learn ARM development(15)
  • Learn ARM development(16)
  • Learn ARM development(17)
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号