Design of Speed ​​Measurement System Based on PIC Microcontroller

Publisher:Yinyue1314Latest update time:2012-01-07 Keywords:MCU Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

0Introduction

In modern industrial measurement systems, the measurement of displacement and rotation speed is a key link. Although the early measurement systems were relatively mature in technology, they were generally composed of discrete components, resulting in low measurement accuracy, poor stability, high cost, and poor anti-interference ability. With the development of electronic technology and computer technology, the measurement system has gradually transformed into an intelligent one. This paper uses PIC microcontrollers to realize real-time measurement of rotation speed. By deriving the measurement error algorithm of the multiple period method, an effective method for determining the period multiple N in the multiple period method is obtained, so that there is a small error in both high and low speed measurements. The hardware structure of this design is simple, the measurement speed is fast, the accuracy is high, and the operation is reliable, which can meet people's increasingly high requirements for speed accuracy and real-time performance.

1 Main performance characteristics of the system

(1) The hardware structure is simple in design, with fast measurement speed, high accuracy and reliable operation.

(2) Using a PIC microcontroller with EEPROM, a simple and reliable power-off protection function is designed.

(3) Two measurement modes: rotation speed and speed (when measuring speed, the circumference of the turntable must be entered in advance using the key).

(4) It has upper and lower limit alarm functions, which is suitable for occasions where there are restrictions on speed or rotation speed.

(5) There are 3 independent buttons for conveniently setting the upper and lower alarm limits, measurement mode and turntable circumference.

2 System Hardware Design


Figure (1) System architecture

PIC16F874A is an 8-bit Flash microcontroller produced by Microchip Corporation of the United States. It has the characteristics of low function, low working voltage, high performance, high speed, full static, strong pin direct drive capability, etc. Due to the use of Harvard dual bus and two-level instruction pipeline structure, instruction execution and instruction fetching operations are carried out simultaneously, so it can achieve a very high execution speed. This series of microcontrollers has only 35 single-byte instructions. Except for the jump instruction which is a 2-cycle instruction, all other instructions are single-cycle instructions. Compared with other 8-bit microcontrollers, this saves 1/2 of the program space and has a speed advantage of 4:1. PIC16F874A has 4K×14 program memory, 192 bytes of general registers, 128 bytes of EEPROM, 33 I/O ports, 3 timers/counters and 2 capture/compare/PWM modules, 2 serial ports, A/D converter with 5 analog input terminals, and clock, reset, watchdog timer, etc. Because the microcontroller has very rich resources, the hardware circuit is simplified, thereby reducing costs.

2.1 Signal input circuit

The device used in the design of this part of the circuit is the OPTC photointerrupter produced by Sharp. In fact, other devices can also be used as long as they can generate a pulse signal that can be detected by the microcontroller. The photointerrupter integrates the GaAs infrared light-emitting diode of the light-emitting part, the photodiode of the light-sensitive part, and the signal processing circuit (amplifier, Schmitt trigger and voltage regulator circuit, etc.) on a chip. This photointerrupter has the following characteristics:

(1) Small size and high reliability;

(2) Few peripheral circuits;

(3) Can be directly connected to TTL, LSTTL, and CMOS devices;

(4) Wide operating voltage range (Vcc=4.5-16V)

2.2 Power-off protection circuit

When the microcontroller is working, if there is a sudden power failure due to some reason, the data in the data memory (RAM) will be lost, and all the information of the previous work will be flushed. Although there is a large-capacity filter capacitor in the main power supply of the microcontroller, when the power is off, the microcontroller can generally maintain operation for about 10ms by relying on the energy stored in the capacitor. For this reason, it is required that once the main power is cut off, the data in the RAM must be promptly transferred to the EEPROM data register within less than 10ms, and the data must be retrieved from the EEPROM when the power is turned on next time to ensure that the microcontroller system can operate normally. Since 128 bytes of EEPROM are integrated in the PIC16F874A microcontroller, the protection circuit is very simple. The specific circuit is shown in Figure (2). Its working principle is as follows: When the RB4-RB7 pins of the B port of the PIC microcontroller are set to input mode, when the input level changes from high to low or from low to high, the microcontroller can generate an interrupt, which is usually called a pin state change interrupt. When the 12V power supply voltage is within the normal range, the level of the MCU RB4 pin is high; when the 12V power supply is cut off, the unidirectional conductivity of the diode D1 blocks the loop of the filter capacitor C3, causing the voltage on the RB4 pin to drop rapidly enough to trigger the level reversal of the internal circuit of the RB4 pin, triggering an interrupt, and completing the burning of the data to be protected into the EEPROM in the interrupt program. At the moment of burning, the working power of the MCU is sufficient to be maintained by the power filter capacitor.


3. Detection Method and Error Handling

Usually, the measurement of rotation speed or speed can be converted into the detection of signal frequency (or period). The three most commonly used methods for detecting signal frequency are: counting method, period method and multiple period method. Among them, the counting method is suitable for measuring high frequency, and it takes a long time to measure low frequency, so the error is large; the period method is suitable for measuring low frequency, and the frequency of the reference pulse required for measuring high frequency signal is high, so the error is large; the common advantage of these two methods is that they are relatively simple to implement. The multiple period method can solve the contradiction between high and low frequency to a certain extent, but it is relatively difficult to implement. Because the multiple period method requires a proper multiple N to be determined in advance, and it is difficult to determine N in advance. If N is not determined properly, it will also increase the detection time or have a large error at high frequency. In actual design, this paper makes some improvements to the multiple period method and proposes a simple algorithm for determining N, which can automatically determine the appropriate N and meet the detection requirements of high and low frequency signals.

The multiple period method is improved from the period method. When measuring the period of a signal, a reference pulse with a fixed frequency is used as the clock signal of the adder counter. The adder counter is started at the beginning of a period of the measured signal; then, the counting is stopped at the end of this period. Assuming that the value of the counter is M at this time and the period of the reference pulse is Tm, then M, Tm and the period T of the measured signal have the following relationship:

T = M * Tm Speed ​​n = 60/T = 60/(M * Tm) (rpm)

In this section, we only discuss the detection method and error processing of rotation speed, because the detection method and error processing of rotation speed and speed are similar. The relationship between rotation speed n and speed v is: v = n * L/ 60 (m/s) (where L is the circumference of the turntable)

The speed corresponding to one Tm cycle is: n/M = 60/(M * M * Tm)

As shown in Figure (3), the maximum error is 2 * n/M , that is, the maximum error = 2 * n /M = 120/(M * M * Tm) .


Figure (3) Relationship between reference pulse signal and detected signal

This explains why the periodic method is suitable for measuring low-frequency signals. From the error relationship, it can be seen that when n is large, the error is large; when n is small, the error is small. From the above formula, we can also get two ways to reduce the error n: one is to increase M; the other is to increase Tm. For a certain detector, Tm is a fixed constant. In this way, the M value can only be increased. However, for a speed n, M within a cycle is also a relatively fixed value. Therefore, to increase the M value, the only way is to increase the number of detection cycles, that is, to expand the measurement from one cycle to multiple cycles. If it is assumed that the count value within N cycles is Mn, then:

It can be seen that these two formulas are contradictory. To reduce the error, N must be increased, and the measurement time will increase accordingly; to reduce the measurement time, N must be reduced, and the error will increase accordingly. Obviously, it is difficult to determine an appropriate N value in advance, because different speeds correspond to different N values.

In view of the problem that it is difficult to determine the value of N, this article adopts a more reasonable method to determine the value of N. For a single-chip microcomputer, the number of bits of its counter is limited, assuming it is P bits. For a speed n, when the counter reaches the full scale, the corresponding error is the smallest. Therefore, in order to minimize the detection error, the N value corresponding to a speed n can be determined based on whether the counter reaches the full scale. Assuming that the single-chip microcomputer instruction execution cycle is Tm, the counter overflow time is Tn, the number of detected signals is N, and the speed at this time is n, there is the following relationship:

Tn = ( ) Tm error: n = 120 / [( )( ) Tm / N]

Speed: n = 60N/ ( ) Tm (rev/min)

From the relationship between n and N, we can see that different n has different N values. To find the N value when the counter reaches full scale, the specific method is: use the counter 1 of the microcontroller to count, and then use software to design a counter to count the measured signal. When counter 1 overflows, the value of the counter can be taken to calculate the required N value. In this way, there is no need to predetermine an N value as in the usual multiple period method.

4 Speed ​​measurement algorithm and system implementation program

This article uses PIC microcontroller to implement the speed measurement system. In the PIC mid-range series microcontroller, timer TMR1 is a standard configuration, which is a 16-bit timer/counter. As shown in Figure (2), we input the detection signal to the INT terminal of the microcontroller. The timing of timer 1 and the interrupt function of INT are used to achieve measurement.

4.1 Main program flow chart see Figure (4)

(1) Initialization program: Initialize timer 1, INT1 and various input and output port settings, clear the display buffer, etc.

(2) Key scanning program: When a key is pressed, the program responds accordingly to set the upper and lower alarm limits, measurement mode, and the circumference of the turntable being measured.

(3) Measurement value comparison and alarm: The value in the display buffer is compared with the upper and lower limit alarm values ​​preset in the EEPROM one by one. When it is greater than the upper limit or less than the lower limit, an alarm is triggered.

(4) Display program: The BCD code in the display buffer is decoded by table lookup and sent to the seven-segment digital tube for display.

4.2 Interrupt service program flow chart see Figure (5)

(1) Interrupt type judgment: When TMR1 interrupts, the number of overflows of counter 1 W = W + 1; when INT interrupts, the number of cycles of the detected signal N = N + 1; when RB4 level changes, the data that needs power-off protection is saved in EEPROM;

(2) The formula for calculating the rotation speed n is: , where Tm is the instruction execution cycle; the formula for calculating the speed is: v =n*L/60 (m/s) where L is the circumference of the wheel.

Figure (5) Interrupt program flow chart


Cycle; The speed is calculated as: v =n*L/60 (m/s) where L is the circumference of the wheel.

Figure (4) System main flow chart


5 Conclusion

This paper introduces a system that uses a single-chip microcomputer as the core device to achieve high-precision rotation speed and speed measurement. After analyzing the error of the speed measurement system, the feasibility and practicality of this method are demonstrated, and the system is successfully used to measure the speed of the winch. During the test, all functions are normal, the operation is reliable, the use is convenient, the effect is good, and the design requirements are met.

The author of this article innovates: he adopts a method of determining the multiple N of the multiple period method based on the overflow of the microcontroller timer, successfully solving the difficulty of determining the multiple N; he designs two measurement modes, namely, rotation speed measurement and speed measurement, and has upper and lower limit alarm functions, which makes the system have a wider application prospect.

References :

[1] Jiang Qingming, Yang Xu, Gan Yongmei, Wang Xiaoyu, Wang Zhaoan. A high-precision speed and acceleration measurement method based on photoelectric encoder [J]. Microcomputer Information, 2004, 6: 48-50.

[2] Zhang Mingfeng. Introduction and Practice of PIC Microcontroller. Beijing University of Aeronautics and Astronautics Press, 1st edition, 2004.

Keywords:MCU Reference address:Design of Speed ​​Measurement System Based on PIC Microcontroller

Previous article:Microcontroller Controlled Analog Phase Shifter
Next article:Using the MCLR pin as an output on a PIC microprocessor

Recommended ReadingLatest update time:2024-11-16 19:54

Voice electronic door lock system based on 16-bit single chip microcomputer
Biometrics is a technology that uses human biological characteristics to authenticate identity. It is currently recognized as the most convenient and secure identification technology. Since each person's biological characteristics are unique and stable over a certain period of time, they are not easy to forge or cou
[Security Electronics]
Voice electronic door lock system based on 16-bit single chip microcomputer
Single-chip single-button control LED desk lamp
C language solution for button control of LED desk lamp brightness Functional requirements: A single button controls the brightness of the LED desk lamp. The default setting is off when powered on. There are 5 gears. First gear: 100% brightness Second gear: 65% brightness Third gear: 35% brightness Fourth gear: 20
[Microcontroller]
The STM32F10x series MCU configures PB3 and PB4 as normal IO ports
For beginners, why can't they control the output when using PB3 and PB4? The following is an analysis and explanation of this issue. First, after the STM32F10x series MCU is reset, PA13/14/15 & PB3/4 are configured as JTAG ports by default. Sometimes, in order to make full use of the resources of the MCU I/O port, we
[Microcontroller]
How to use MCU RAM
Any data on single-chip microcomputers will not fail to mention the issue of memory, but there is no basic explanation that can make people understand it at once. The privilege of being a rookie is also a half-knowledge. I think if I read the memory (mainly RAM) of 51 and make a summary and list it, I think it will def
[Microcontroller]
An article explaining the instruction system of 80C51 microcontroller in detail
An instruction is a command that instructs the computer to perform a certain operation. The computer can only recognize and execute binary code. The language that describes the function of the instruction in binary code is called machine language. Since machine language is not easy for people to recognize, remember,
[Microcontroller]
An article explaining the instruction system of 80C51 microcontroller in detail
Design of USB interface for ECG and blood pressure monitor based on single chip microcomputer
Introduction The home ECG and blood pressure monitoring system consists of a collection and recording device and a host electronic medical record management system. Therefore, the problem of data transmission needs to be solved. The traditional communication interface uses a simple RS-232 serial UART, which is
[Medical Electronics]
Design of USB interface for ECG and blood pressure monitor based on single chip microcomputer
315MHZ wireless software decoding and encoding implemented with pic16f630 MCU. Replace PT2272 and pt2262
#include pic.h     //#include pic16630.h     #define uchar unsigned char    #define uint unsigned int    #define PORTAIT(adr,bit) ((unsigned)(&adr)*8+(bit)) ///絕對尋址位操作指令    #define PORTCIT(adr,bit)((unsigned)(&adr)*8+(bit)) ///絕對尋址位操作指令    static bit KEY1 @ PORTAIT(PORTA,0);    static bit KEY2 @PORTAIT(PORTA,1);   
[Microcontroller]
Design of LVDT displacement measurement sensor based on single chip microcomputer
  0 Introduction   Conventional test instruments and control devices have been replaced by more advanced intelligent instruments, which has brought about tremendous changes in the distance, function, accuracy and automation level of traditional electronic measuring instruments. Correspondingly, various intelligent i
[Microcontroller]
Design of LVDT displacement measurement sensor based on single chip microcomputer
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号