introduction
This paper designs a timer system based on AVR microcontroller and Matlab serial port communication. This timer system can meet the requirements of accurate timing in sports competitions. In this design, Atmel's ATmega16 microcontroller is mainly used as the signal acquisition system. Through the serial port and the host computer's Matlab communication, a customized communication protocol is defined so that the host computer receives different signals and can control the timer in Matlab. [1] A friendly human-computer interface is designed using the GUI provided by Matlab software. [2] In this system, there can be multiple microcontrollers as slave computers, which can communicate with the host computer through the serial port connected to the ZigBee wireless transmission module. The access of the wireless transmission module can realize remote multi-point signal acquisition, making the system more flexible and greatly enhancing the scalability.
1 Overall system design
This system includes multiple signal acquisition systems with ATmega16 microcontroller as the main controller and a timer program written in Matlab software of the host computer. ZigBee wireless communication modules are connected to the host computer through serial ports to realize data transmission function. The signal of the photoelectric switch captured by ATmega16 is transmitted to Matlab of the host computer to control the start, interrupt and end functions of the timer. The overall block diagram of the system is shown in Figure 1.
Figure 1 Overall block diagram of the system
2 Design of signal acquisition system
The signal acquisition system is mainly composed of a photoelectric switch, a signal level conversion circuit and a single-chip microcomputer.
The through-beam photoelectric switch consists of a transmitter and a receiver, which are separated from each other in structure. When the light beam is interrupted, a switch signal change will be generated. The characteristics of the through-beam photoelectric switch used in this article are: it can distinguish opaque reflective objects; it has a large effective distance, and the detection distance can reach 10 m; it is not easily interfered with and can be used reliably in the wild or in dusty environments.
The high level of the photoelectric switch signal outputs a +12 V digital signal, while the ATmega16 I/O port level is a TTL level with a voltage range of 0~+5 V. Therefore, a level conversion circuit must be designed between the photoelectric switch and the microcontroller. This design considers the use of the optocoupler device TLP521-2, which provides two isolated optocoupler 8-pin plastic packages, which can not only realize the conversion of +12~+5 V voltage, but also isolate the signals at both ends to improve the stability of the circuit. The schematic diagram of the signal level conversion circuit is shown in Figure 2.
Figure 2 Schematic diagram of signal level conversion circuit
The external interrupt of the ATmega16 microcontroller is triggered by pins INT0 and INT1. As long as the interrupt is enabled, even if pins INT0 and INT1 are configured as outputs, the interrupt will be triggered when the level changes appropriately. By setting the ATmega16 control register MCUCR and the control and status register MCUCSR, the interrupt can be triggered by a falling edge, a rising edge, or a low level. [3] This article uses the E3FDS10C4 photoelectric switch, which has an NPN normally open output. When the light is blocked, the signal output jumps from a high level to a low level, that is, the interrupt signal input to the ATmega16 external interrupt pin is a falling edge. In this design, the two photoelectric switch signals are connected to the external interrupt INT0 and INT1 pins of the ATmega16, and the external interrupt related registers are set as follows:
MCUCR=0x0A; //INT0, INT1 falling edge trigger interrupt
GICR=0xC0; //Enable INT0, INT1
When the photoelectric switch has a falling edge, the program enters the corresponding external interrupt processing program and sets the external interrupt flag. The main program flow chart of the single-chip microcomputer is shown in Figure 3. [page]
Figure 3 MCU main program flow chart
3 Serial communication between the lower computer and the upper computer
The lower computer of this system is a single-chip microcomputer, and the signal voltage is TTL level 0~+5 V, while the upper computer is a PC, and the serial port level is RS232 level -10~+10 V. In order to realize serial communication, it needs to go through MAX232 level conversion to match. Considering the flexibility and convenience of the application, as well as the communication between multiple lower computers and one upper computer, this design connects the ZigBee wireless communication module between the single-chip microcomputer and the upper computer, using the SZ05-ZigBee wireless communication module. The wiring diagram of the SZ05ZigBee wireless communication module is shown in Figure 4.
Figure 4 SZ05 ZigBee wireless communication module wiring diagram
The module data interface includes TTL level receiving and sending interface and RS232 sending and receiving interface. It can also send data in broadcast mode and according to the target address. In addition to realizing the general point-to-point data communication function, it can also realize data communication between multiple points. The serial communication method is simple and convenient, which can greatly shorten the module embedding and matching time process. It has the advantages of long communication distance, strong anti-interference ability, flexible networking, reliable and stable performance, etc. [4]
Short the CONFIG jumper of the SZ05ZigBee wireless communication module, and the system enters the configuration state. Open the computer's hyperterminal, and the settings of the hyperterminal are: baud rate 38400, data bit 8, check NONE, stop bit 1, flow control none. The main parameter configurations are as follows: network type network - star network; transmission mode - master-slave mode; data type - HEX; data bit - 8+0+1; baud rate - 38400; device type connected to the microcontroller end - terminal device (DEVICE jumper shorted); device type connected to the host computer end - central node (CENTER port shorted to ground). Each terminal device needs to set its own different MAC_ADDR device address, ranging from 0000 to 0xFFFE. In master-slave mode, the terminal device does not need a target address to send data, and the data is sent to the central node by default, while the central node must add the target address of the terminal device when sending data.
When the wireless communication module is powered on during the test, it will automatically send a string of random data. In order to prevent interference, a custom communication protocol is defined, which defines 3 bytes of data as a frame, and the data packet format is 0xAABB××. Data is received only when the data header AA BB is detected to ensure the accuracy of the received data.
4 Implementation of serial communication and timer based on Matlab GUI
4.1 Matlab programming of serial port RS232
Starting from Matlab version 6.0, the software has added a device control box function, providing official support for serial port communication based on the RS232/RS485 communication standard. Using the serial port class and functions such as fopen and fread in the toolbox, real-time serial communication can be performed reliably. [5]
The complete process from establishing serial port communication to ending serial port communication includes the following steps[6]:
① Create a serial port object for the application. The function to implement this function is obj=serial("port","PropertyName", PropertyValue,...). The parameter port is the complete serial port name, such as COM1; PropertyName is the serial port communication parameters, such as baudrate, startbits, etc.
② Connect and open the serial port. The function is fopen(obj).
③ Set or modify serial communication parameters. Before you can effectively perform serial communication, you must set the correct serial communication parameters. The function that implements this function is props=set(obj, "PropertyName", PropertyValue, ...).
④ Close the serial port and release the storage space occupied by the serial port object. The function to close the serial port is: fclose(obj). The function to release the memory space occupied by the serial port object is delete(obj). The function to release the storage space occupied by the serial port object in the Matlab workspace is clear obj.
To realize automatic sending and receiving of data, it is also necessary to define the serial port interrupt processing function and the way to trigger the serial port interrupt is as follows:
① How to trigger serial port interrupt. The events related to serial port reading and writing include: Bytes available, Output empty. There are two types of Bytes available events: one is that the system generates this event when the number of characters received reaches the manually set number; the other is that the system generates this event when the specified character is received. This article adopts the former, and the interrupt is triggered when the number of received bytes reaches 3. [page]
The Bytes available event needs to be set in advance. You can use the functions set(obj, "BytesAvailableFcnMode", "byte") and set(obj, "BytesAvailableFcnCount", 3). The above two functions are set to trigger a serial port interrupt when the serial port detects that there are 3 characters of data in the input buffer.
② Serial port interrupt processing function. The serial port interrupt processing function can be defined according to user needs. For example, the serial port read interrupt processing function can be defined as follows: obj. BytesAvailableFcn = @receiveData. receiveData is the serial port read interrupt processing function. In the read interrupt processing function, the serial port read operation can be performed, that is, the data in the input buffer area is read into the user-defined storage variable for subsequent data processing and analysis. [7]
4.2 Matlab timer implementation based on GUI interface
The overall control interface is designed using Matlab's GUI. The interface functions include: setting serial port parameters such as port number, baud rate, data bit, check bit and stop bit; timer display window, which can display the time of the timer in real time; and buttons for opening the serial port, starting timing, stopping, resetting, etc. The designed GUI control interface is shown in Figure 5.
Figure 5 GUI control interface
The signal sent by the microcontroller triggers the serial port interrupt on Matlab, and the flag FLAGA/FLAGB is set, which can stop the timer. At the same time, on the GUI interface, the flag FLAGA/FLAGB can also be set manually to stop the timer. The program flow chart of the timing part is shown in Figure 6.
Figure 6 Flowchart of the timing part
After the Matlab file is compiled, the generated executable program still needs the support of the Matlab environment. In order to make this design more widely used by general users, it needs to be published to run on machines that do not have Matlab installed. The last task is to package the Matlab component runtime environment (MCR). Package the MCR together with the executable program and copy it to other machines that do not have Matlab installed. Install the MCR on the machine. After the installation is complete, you can directly run the compiled Matlab executable program on the machine. [8]
5 Issues that need attention in system design and application
In actual testing, photoelectric switches can generally work stably when the ambient illumination is high. However, avoid placing the sensor optical axis directly against strong light sources such as sunlight and incandescent lamps. When the angle between the sensor (light receiver) optical axis and the strong light source cannot be changed, a shading plate or a shading tube can be installed around the sensor to ensure that the photoelectric switch works reliably.
In addition, when configuring the ZigBee wireless communication module, you should pay attention to selecting the appropriate communication channel to avoid interference from WiFi. In the same network, whether it is a terminal device or a central node, the ID settings must be the same so that the modules can communicate normally. In the same network, the serial port timeout settings of all modules must be the same, otherwise the serial port communication cannot be synchronized and the communication will fail.
Conclusion
In this design, the ATmega16 microcontroller is mainly used as the signal acquisition system, and the signal communication is carried out through the serial port and the host computer Matlab. The communication is realized by the ZigBee wireless communication module, which greatly enhances the flexibility and scalability of the system. This paper uses Matlab GUI to develop an interface containing general interface elements. On this basis, the serial port communication function and the timer timing function are added. The human-machine friendly interface designed by the GUI makes the system simple and easy to understand and more convenient to operate.
Previous article:Atmel QTouch-based ATmega48 sensor button design
Next article:Design of CAN Bus Analyzer Based on AVR Microcontroller
Recommended ReadingLatest update time:2024-11-16 16:52
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
- This week's highlights
- MicroPython version 1.13 released
- How to create a compilation environment for the SINA33 development board
- What are the main parameters of electronic components? What are their respective characteristics?
- Regarding the issue of analog quantity acquisition, please ask the seniors
- [RVB2601 Creative Application Development] MP3 Player
- HuaDa HC32A460 Series Introduction (Part 2)
- 【IoT Development Notes】Gizwits Cloud Device Transplantation RT-Thread
- Semiconductor Industry Online Member System Questionnaire Survey Come and participate to win a 50 yuan Jingdong card! It can be done in 3 minutes~
- DC charging efficiency issues