Serial communication between MCS251 microcontroller and PC

Publisher:skyhcgLatest update time:2013-01-25 Source: 21IC Keywords:MCS251 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

  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

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 2 Receive interrupt handler

Figure 3 Send 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.

Keywords:MCS251 Reference address:Serial communication between MCS251 microcontroller and PC

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

Remote Communication between Single Chip Microcomputer and PC Based on MODEM
With the development and maturity of computer technology and communication technology, data communication has become a widely used communication method. It uses communication systems to transmit, exchange and process numbers, letters and characters in binary form between computers. Data communication can be carried out
[Microcontroller]
Sony's new VR helmet development: lighter and compatible with PC/PS5
According to a new patent discovered by foreign media LetsGoDigital, Sony is developing a new PSVR helmet for PS5. This new VR helmet looks like it will have tactile feedback, LED light bars, sensors that can identify whether the helmet is worn correctly, and support PC and PS5 consoles.   LetsGoDigital discovered t
[Mobile phone portable]
Sony's new VR helmet development: lighter and compatible with PC/PS5
PLC and PC fieldbus control system based on Profibus-DP
introduction The development of automation control, computer, communication, network and other technologies has led to profound changes in the field of automation. The rapid development of information technology has gradually formed a fully distributed network integrated automatic control system in the stru
[Industrial Control]
PLC and PC fieldbus control system based on Profibus-DP
Bloomberg: Slowdown in PC demand has slightly eased the chip shortage, and memory chip prices have fallen
Bloomberg columnist Tae Kim published an article on the 18th discussing the global chip shortage. He said that the surge in demand for consumer electronics since the outbreak of the COVID-19 pandemic has led to a continued shortage of chips. However, some signs in recent years show that the chip shortage may be beginn
[Semiconductor design/manufacturing]
PIC16 serial bootloader in C language and PC serial communication program with bootloader in C# language
  New PIC16 Bootloader   After completing HyperBootloader (see my previous essay for details), I decided to rewrite the PIC bootloader. Why? HyperBootloader uses the PC serial communication software - HyperTerminal to transmit Hex data, one line at a time, and each line is delayed by 20ms to wait for the Hyperbootload
[Microcontroller]
PIC16 serial bootloader in C language and PC serial communication program with bootloader in C# language
Industry sources say PC processor prices will rise sharply this year
According to Jiwei.com, industry sources said that due to rising wafer foundry costs, PC processor prices are expected to rise sharply in 2022, especially processors manufactured using process nodes below 7nm. According to digitimes, sources said that suppliers of high-performance CPU, GPU and FPGA chips are expecte
[Home Electronics]
PC control will occupy 40% of the PLC market share?
Some time ago, a senior automation expert in the United States announced that PC control will account for 40% of the PLC market share, which has aroused the interest of many people in the past and present of PC control. In order to understand the origins and development of PC control and whether there is room for deve
[Embedded]
PC control will occupy 40% of the PLC market share?
Design of Data Acquisition System Based on PC104
introduction With the development of microelectronics technology and the Internet, people have a deeper understanding of the Internet. Network terminal products are also attracting more and more attention, and the application of embedded operating systems has also achieved unprecedented development. PC104 is an
[Industrial Control]
Design of Data Acquisition System Based on PC104
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号