With the rapid development and widespread application of computer technology, the master-slave working mode of the upper computer and the lower computer is adopted by industrial control and automatic control systems. Due to the strong analysis ability of PC, faster processing speed and flexible and convenient use of single-chip microcomputer, PC is generally used as the upper computer and single-chip microcomputer as the lower computer. The two receive and send data and transmit instructions through RS2232 or RS2485. The single-chip microcomputer can process data and control tasks independently, and also transmit data to the PC, which processes or displays the data.
This article introduces the application of PC and MCU communication in a waveform generator based on C8051F MCU. The amplitude and frequency of the sine wave are displayed on the host computer, and the amplitude and frequency of the waveform can be changed by sending instructions from the host computer.
1 Hardware circuit design
The MCS251 microcontroller has a full-duplex serial communication port UART, which uses its RXD and TXD to communicate with the outside world. It has two physically completely independent receiving and sending buffers SBUF inside, which can send and receive data at the same time. Therefore, serial communication between the microcontroller and the PC can be convenient. The microcontroller serial port has three leads: TXD (send data), RXD (receive data) and GND (signal ground). Therefore, when the communication distance is short, the zero MO2DEM method and simple three-wire structure can be used. The IBM2PC has two standard RS2232 serial ports, whose electrical levels use EIA levels, while the serial communication of the MCS251 microcontroller is full-duplex communication by TXD (send data) and RXD (receive data), and their electrical levels are TTL levels; in order to reliably communicate serially between the PC and the MCS251, a level conversion chip is required. We use the dedicated chip MAX232 produced by MAXIM for conversion.
The circuit is shown in Figure 1.
Figure 1 Conversion chip circuit
2 System Software Design
Software design is divided into upper computer software design and lower computer software design. Although these two parts are written and run on different machines, their work is corresponding: one sends and the other receives.
In order to ensure the reliability of data communication, a communication protocol should be formulated, and then each program should be compiled according to the protocol. The communication protocol is now agreed as follows: Both the PC and the MCU can send and receive. Both the upper and lower computers use the query method to send control characters and data, and the interrupt method to receive control characters and data. RS2232 serial port asynchronous communication is adopted, with 1 start bit, 8 data bits, 1 stop bit, no parity check, and a baud rate of 9 600 b/s. Every time the PC sends data to the MCU, it first sends a handshake signal "FF". After the lower MCU receives the handshake signal, it sends an answer signal "EE" to the PC. After the upper PC receives the answer signal, it sends the data. Similarly, when the MCU wants to send data to the PC, it also sends a contact signal "EB90" to the PC first. After receiving the contact signal, the PC sends an answer signal "EBOO". After receiving the answer signal, the MCU sends the data.
2.1 Host computer communication program design
The serial port of the PC uses the INS8250 chip. There are two asynchronous communication ports in the PC, one is COM1, and its port address range is 3F8H-3 FFH, and the other is COM2, and its port address is 2F8H-2FFH. It can achieve long-distance communication with the MODEM. Its baud rate is the standard baud rate (50-9 600) b/s.
There are 10 register ports inside the 8250, one of which is a divisor latch, which can determine the baud rate of asynchronous communication by programming the divisor size. The frequency used by the 8250 is a 2 MHz reference clock input signal, so the required baud rate (shift pulse) must be generated by frequency division. The value of the divisor latch must be set when the 8250 is initialized, that is, the highest bit (DLAB) of the communication line controller is set to 1, and then the high 8 bits and low 8 bits of the divisor latch are written to the port address 3F8H and 3F9H (COM1) twice. The frequency of the clock signal used by the 8250 when transmitting or receiving serial data is the number.
The data transmission baud rate is 16 times, that is, baud rate = 16 × divisor/1 843 200.
Serial communication on Windows platform is mostly realized by using API functions provided by it. This method requires many low-level settings, so it is cumbersome and difficult to understand. ActiveX technology launched by Microsoft provides another method to realize serial communication. This method is not only relatively simple, but also very practical.
Especially in the visual object-oriented programming environment of Visual C++, the serial port can be truly regarded as an object. When programming, only simple settings are required, and it is also easy to understand. The host computer communication program in this article uses the serial communication control MSComm (Mi2crosoft Communication Control) provided by VC++. The main steps are as follows: Open the VC++ 610 integrated development environment, select the menu item File/New, and select MFC App2Wizard (exe) in the Projects tab in the dialog box that appears to generate a dialog-based application. Select the menu item Project/Add to project/Components and Controls?, select Microsoft Communications Control under the Registered ActiveX Controls folder in the pop-up dialog box, select Registered ActiveX Controls to register MSComm, and drag the control from the toolbox to the dialog box to insert the control. Select ClassWizard to add a member variable to the newly created communication control and name it m_commute.[page]
The serial port properties can be set through this member variable. The following is to initialize the serial port by setting the control properties:BOOL CMyCOMMDlg: : OnlnitDialog( )
{
CDialog: : OnlnitDialog() ;
//Here is the code automatically generated by the application framework
m_commute.SetCommPort(1) ; //Use serial port 1
m_commute. SetSettings ("9600, N, 8, 1"); //Baud rate is 9600, no parity check, 8 data bits, 1 stop bit
m_commute. SetRThreshold (10); // Trigger a receive event every time 10 characters are received
m_commute.SetSThreshold(0) ; //Do not trigger the sending event
m_commute. SetInputLen (10); // Each read operation takes 10 characters from the buffer
m_commute. SetInputMode (1); //Binary data transmission mode
m_commute.SetPortOpen(TRUE) ; //Open the serial port
return TRUE; //return
}
Event-driven is a very effective way to handle serial port interactions. The CommEvent property of the OnComm event can capture and check communication events and errors. When a communication event or error occurs, the OnComm event will be triggered, the value of the CommEvent property will be changed, and the application will check the CommEvent property value and respond accordingly. Double-click the CMSComm control in the dialog box and add the code for the OnComm event:
if (m_commute.GetCommEvent() == 2) //Judge whether the comEvReceive event occurs
{
shortm_size =m_commute. GetinBufferCount( ) ;
if (m_size == 11) // The buffer is full with 11 characters
{
m_commute.SetInputLen(L) ; //Read received data
VAR IANT vRep ly = m_commute.GetInput(); //Process received data
}
}
2.2 Lower computer communication program design
In the communication between the host computer and multiple single-chip microcomputers, in order to ensure reliable communication between the host and the slave, it is necessary to ensure that the communication interface has an identification function. The control bit SM2 in the single-chip microcomputer serial port control register SCON is set to meet this requirement. And the SM2 control bit only works in the serial working mode 2 and mode 3 of the MCS51 single-chip microcomputer. When the serial port receives in mode 2 or mode 3, if SM2 = 1, it means that the multi-machine communication function bit is set. At this time, there are two possible situations: when the 9th bit of data received is 1, the data is loaded into SBUF, and RI = 1 is set to send an interrupt request to the CPU; if the 9th bit of data received is 0, no interrupt occurs and the information is lost. If SM2 = 0, the 9th bit of data received, whether it is 0 or 1, generates the RI = 1 interrupt flag, and the received data is loaded into SBUF. According to the above situation, the MCS-51 multi-machine communication process is arranged as follows:
(1) Initially, the SM2 bit of all slaves is set to 1, and they are in a state of receiving only address frames (the 9th bit of the serial frame is 1), and do not respond to data frames (the 9th bit of the serial port is 0).
(2) When the slave receives the address frame sent by the host, it compares the received address with the local address. If the address matches the local address, SM2 is cleared to receive the data sent by the host. For slaves whose addresses do not match, SM2 = 1 is maintained, so they cannot receive the data information sent by the host.
(3) When the host changes to contact another slave, it can send out address frames to find other slaves. When the previously addressed slave analyzes that the host is addressing other slaves, it restores its SM2 = 1 and waits for the host to address it again.
(4) When the slave wants to call the host, it can first send a handshake signal. After the host detects the slave's call, it sends a response signal. After the slave receives the host's response, it can send data to the host. The host uses this signal to determine the state of the slave and respond accordingly.
[page]
When designing a single-chip microcomputer communication program, the efficiency of the single-chip microcomputer must be fully utilized. Since single-chip microcomputers are mostly used in control situations with strong real-time requirements, timely response and control of the object's actions should be given priority to minimize the CPU time occupied by auxiliary operations such as communication. Based on the above considerations, when designing the single-chip microcomputer communication program, the author divides the communication program into three parts: receiving interrupt processing program, sending interrupt processing program and communication processing program, and cleverly combines these three parts to form the communication program of the entire single-chip microcomputer. Figure 2 shows the receiving interrupt processing subroutine, and Figure 3 shows the sending interrupt subroutine.Figure 2 Receive interrupt handler
Figure 3 Send interrupt handler
3 Conclusion
This article presents a solution for asynchronous serial communication between the host PC and the slave microcontroller. The host computer can send control instructions to the slave computer through a friendly user interface. This solution runs stably and effectively during actual operation, thus effectively solving the communication problem between the host computer and the slave computer.
Previous article:Design of a two-color clock without hands based on single chip microcomputer
Next article:LED dot matrix electronic display production
Recommended ReadingLatest update time:2024-11-16 17:41
- Popular Resources
- Popular amplifiers
- Siemens Motion Control Technology and Engineering Applications (Tongxue, edited by Wu Xiaojun)
- Virtualization Technology Practice Guide - High-efficiency and low-cost solutions for small and medium-sized enterprises (Wang Chunhai)
- 2024 DigiKey Innovation Contest
- MCU C language programming and Proteus simulation technology (Xu Aijun)
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
- Can the comment statement in C language be written in the IF judgment condition in KEIL5?
- How to handle the error in calling the ti.osal.ae674 library when porting the FATFS routine of pdk_omapl138?
- Problems with functionality after reset
- ZTE's 7nm chip mass production still needs time to catch up with high-end chip track
- Comparison of Three Synchronous Designs in FPGA
- Looking for package SOT128-1
- 【Qinheng RISC-V core CH582】Timer PWM drive servo
- Selection and use of bypass capacitors
- CircuitPython Holiday Wreath
- Application and advantages of mica capacitors