Serial Communication between PC and MCS-51 Single Chip Microcomputer under VB

Publisher:脑电狂徒Latest update time:2012-01-30 Source: 单片机与嵌入式系统应用 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

In the development of microcomputer control and data acquisition systems, it is often necessary to realize serial communication between PC and single-chip microcomputer through RS232 interface. In the DOS era, programmers need to have considerable hardware knowledge to write serial communication programs under PC. But now under VB, using the existing Microsoft Comm control control, only a small amount of program code is needed to complete the task easily and efficiently. What's more, Visual Basic is a visual programming language widely loved by programmers, and it can also be used to write beautiful applications under Windows.

1. Introduction to Microsoft Comm control

The Microsoft Comm control (MSComm) provided by Microsoft provides programmers with simplified serial communication programming under Windows, so that programmers do not need to master a lot of hardware knowledge. It provides two methods for processing serial communication: one is the event-driven method; the other is the query method.

1. Event-driven approach

This is a very powerful method to handle serial port activities. When the serial port receives or sends a specified amount of data, or when the state changes, the MSComm control will trigger the OnComm event, which can also capture errors in communication. When the application captures these events, it can check the value of the CommEvent property of the MSComm control to know the event or error that occurred, and perform corresponding processing. This method has the advantages of timely program response and high reliability.

2. Query method

You can query the values ​​of certain properties of the MSComm control (such as the CommEvent property and the InBufferCount property) after each important program to detect events and communication errors. This may be more common for small self-contained programs.

The MSComm control has many important properties, some of which are as follows:

· CommPort: Sets or returns the communication port. When it is 1, it corresponds to COM1; when it is 2, it corresponds to COM2.
· Settings: Sets or returns the baud rate, parity, data bits, and stop bit parameters.
· PortOpen: Opens or closes the communication port.
· Input: Reads or deletes the data stream in the buffer.
· Output: Writes data to the send buffer.
· InputLen: Sets and returns the number of bytes read by the Input property from the receive buffer.
· InputMode: Sets and returns the type. When this property is 0, the data retrieved by the Input property is text; when it is 1, the data retrieved by the Input property is binary data. This property is particularly important for communication with the microcontroller.

2. Communication line connection

A serial port of the PC is connected to the 232 level port of the RS232 transceiver MAX232 through a three-wire cross connection via a cable, as shown in Figure 1. The logic level port of MAX232 is connected to the serial port of the microcontroller. Pins 4 and 6, and pins 7 and 8 of the 9-pin connector of the PC RS232 are not connected.


Click here to view the image in a new window
Figure 1 Block diagram of the communication interface circuit between PC and MCU

3. Programming Implementation

1. Implementing the function

Because this program is a communication demonstration program, its functions are relatively simple. The specific function is to use the keyboard to input a 6-byte (12-bit 0~9, A~F) binary number in the PC, and then click the communication command button with the mouse. The PC will send this binary number to the microcontroller. After receiving this number, the microcontroller will send it back as it is. After receiving it, the PC will display it on the window. The experimenter can compare the two data sent and received by naked eyes to check whether the communication is successful.

2. Communication Protocol

Baud rate: 19.2kb/s; no parity check; 8 data bits; 1 stop bit.

3. PC VB program

(1) Add a form to the project, name it frmcomm, and set its Caption property to Communication.
(2) Add two text boxes of the same size to the form, name them txtSend and txtRcv.
(3) Add a command button to the form, name it cmdcomm, and set its Caption property to Communication.
(4) Add an MSComm control to the form, name it MSComm1.
(5) Open the code window and add the following program code to the Click event of the cmdcomm control:

Private Sub cmdcommClick()
Dim Senddat(5) As Byte,Rcvdat() As Byte,
dattemp As Variant,i As Integer
cmdcomm.Enabled=False′ Disable the cmdcomm button
For i=0 To 5′ Get the sending data from the sending text box txtSend
Senddat(i)="&H" & Mid(txtSend.Text,i * 2+1,2)
Next i
MSComm1.CommPort=1′ Set the port number to 1
MSComm1.Settings="19200,N,8,1"′ Set the baud rate and other communication protocols
MSComm1.InputLen=6′ Set to read 6 bytes from the serial port at a time
MSComm1.PortOpen=True′ ​​Open the serial port
MSComm1.InputMode=comInputModeBinary′ Read binary data from the serial port
MSComm1.Output=Senddat′ Send data
Do Until MSComm1.InBufferCount >= 6′ Query mode, wait for 6 bytes to be received
DoEvents
Loop
dattemp=MSComm1.Input′ Read data from the serial port to the variant variable
Rcvdat=dattemp′ Send data to the receiving binary array
txtRcv.Text=""
For i=0 To 5′ Received data is sent to the receiving text box txtRcv to display
txtRcv.Text=txtRcv.Text & Right("0" & ​​Hex(Rcvdat(i)),2)
Next i
MSComm1.PortOpen=False′ Close the serial port
cmdcomm.Enabled=True′ ​​Enable the cmdcomm button
End Sub

(6) Select Start - Run. Use the PC keyboard to enter the 6-byte binary data to be sent in the input text box, and then use the mouse to click the communication button.

4. MCU C51 Program

The crystal oscillator of the MCS-51 microcontroller is 11.0592MHz, and the serial port is set to mode 1, 10-bit asynchronous transmission. The query mode is used for receiving and sending. The program list is as follows:

#include
#include uchar unsigned char
main() {
uchar temp,datmsg[6];
TMOD=0x20; //Set the baud rate to 19.2kb/s
PCON=0x80;
TH1=0xfd; TL1=0xfd;
TR1=1; //Start timer 1
SCON=0x50; //Set the serial port to 10-bit asynchronous transmission and reception, and allow //reception
while(1) {for(temp=0;temp<6;temp++) //Continuously receive 6 //bytes
{while(RI==0); RI=0;
datmsg[temp]=SBUF;
}
for(temp=0;temp<6;temp++) //Continuously send 6 //bytes
{SBUF=datmsg[temp]; while(TI==0);TI=0;
}
}
}

Conclusion

Due to space limitations, this program is only a demonstration reference program and has no practical significance. However, it demonstrates the general method of serial communication between PC and MCU and the method of processing binary data in VB, making the application of MCU and PC more closely integrated. Readers can add some handshake signals and error detection codes, such as parity check, cumulative sum check and cyclic redundancy check (CRC), etc., to make their own application.

Reference address:Serial Communication between PC and MCS-51 Single Chip Microcomputer under VB

Previous article:Issues to note when using MSP430F1121 interrupts
Next article:Design and implementation of I2C serial bus in 8031 ​​single chip microcomputer application system

Recommended ReadingLatest update time:2024-11-16 16:36

Huawei denies rumors, new PCs are already running at full capacity
Huawei employees once again stood up to refute those unfavorable rumors.   Yesterday afternoon (June 12), a domestic media quoted an insider of Huawei Terminal as saying that the so-called discontinuation of Huawei PC production or the so-called cancellation of PC product line is pure nonsense.   "We are already worki
[Embedded]
What will PC/104 be used for after 2020?
The history of PC/104 single-board computers dates back to 1987. More than 30 years later, why are we still talking about this technology in 2020? Especially since electronic technology is advancing every year. However, in markets such as healthcare or defense, you still want to use products that are stable, reliable,
[Industrial Control]
What will PC/104 be used for after 2020?
MCS-51 pin function description
  MCS-51 is a standard 40-pin dual in-line integrated circuit chip   P0.0~P0.7: P0 port 8-bit bidirectional port line.   P1.0~P1.7: P1 port 8-bit bidirectional port line.   P2.0~P2.7: P2 port 8-bit bidirectional port line.   P3.0~P3.7: P3 port 8-bit bidirectional port line.   ALE: Address latch control signal. When th
[Microcontroller]
Intel's Tang Jiong: PC industry competition promotes healthy development and promotes Intel's progress
Since 2019, the PC market has seen a series of changes, the most notable of which is the increasingly fierce "red-blue battle" - the sales of PCs equipped with AMD platforms have increased, and the gap with Intel is narrowing. As a leader in the PC industry, does Intel have a sense of crisis? Faced with the sharp cont
[Embedded]
Intel's Tang Jiong: PC industry competition promotes healthy development and promotes Intel's progress
Power Factor Meter Implemented by MCS-51 Single Chip Microcomputer
1 Introduction At present, there are more and more types of electrical appliances in the power grid, and the operation of the power grid is becoming more and more complicated. It is increasingly important to timely understand the power factor of each branch in the power grid for the dispatch of the entire power grid.
[Microcontroller]
Power Factor Meter Implemented by MCS-51 Single Chip Microcomputer
Pushing floating point numbers onto the stack -- Practical subroutines for MCS-51 microcontrollers
Label: FPUS Function: Push floating point numbers onto the stack Entry condition: Operand is in . Exit information: Operand is pushed to the top of the stack. Affected resources: A, R2, R3 Stack requirement: 5 bytes FPUS: POP ACC ; Save the return address in R2R3 MOV R2,A POP ACC MOV R3,A MOV A,@R0 ; Push the o
[Microcontroller]
ARM abnormal return PC address analysis
Important basics: R15 (PC) always points to the instruction being fetched, and It does not refer to the instruction being "executed" or the instruction being "decoded". In general, People usually agree to use "the instructions being executed as the reference point", which is called the current The first instructio
[Microcontroller]
Design of Asynchronous Serial Communication between TMS320C3x DSP and PC
TMS320C3x DSP is one of the most widely used DSP chips in China. It provides a serial interface that can communicate with external serial devices, supports 8/16/24/32-bit data exchange, and provides great flexibility for designing A/D and D/A interface circuits. However, when the DSP system communicates with the PC,
[Embedded]
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号