AVR microcontroller and Matlab serial communication timer system

Publisher:快乐的舞蹈Latest update time:2013-11-12 Source: eefocus Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

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.

Reference address:AVR microcontroller and Matlab serial communication timer system

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

DDS is no longer alone and works hand in hand with Matlab to create a simulation business
Direct digital frequency synthesis ( DDS) is a new frequency synthesis method that has developed rapidly in recent years. It has the advantages of fast frequency switching speed, easy to improve frequency resolution, and low hardware requirements. Programmable full digitalization facilitates single-chip integration, h
[Power Management]
DDS is no longer alone and works hand in hand with Matlab to create a simulation business
Application of Matlab image enhancement and restoration technology in SEM images
0 Introduction According to relevant literature at home and abroad, research and development of image processing tools and improvement of image quality are hot topics in today's research. Image enhancement and restoration is a basic image processing technology. It highlights certain information in an image
[Industrial Control]
Application of Matlab image enhancement and restoration technology in SEM images
Matlab Logical Function
all: whether all components in the vector are non-zero any: whether any element is non-zero exist: whether the specified variable or file exists find: returns the index and position of non-zero elements in the matrix is*: is series, most of the functions can be known by their names iscell()
[Embedded]
Design of a Small Temperature Detection System Based on Matlab
1 Introduction Temperature is an important parameter that characterizes the environment. In the engineering field, especially in engineering thermodynamics, temperature detection is very common, and accurate temperature measurement for real-time control is also particularly important. In the control sy
[Test Measurement]
Design of a Small Temperature Detection System Based on Matlab
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号