51 microcontroller serial communication programming

Publisher:纯真年代Latest update time:2023-06-13 Source: elecfans Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

51 microcontroller is the collective name for all microcontrollers compatible with the Intel 8031 ​​instruction system. The ancestor of this series of microcontrollers is Intel's 8004 microcontroller. Later, with the development of Flash rom technology, the 8004 microcontroller made great progress and became one of the most widely used 8-bit microcontrollers. Its representative model is the AT89 series of ATMEL Company. It is widely used in industrial measurement and control systems. Many companies have launched compatible models of the 51 series, which will occupy a large market for a long time in the future. The 51 microcontroller is a basic entry-level microcontroller, and it is also the most widely used one. It should be noted that 51 series microcontrollers generally do not have self-programming capabilities.


1. The serial communication programming process on the PC is as follows:

1. Create a project

Open VC++6.0 and create a dialog-based MFC application SCommTest (consistent with my source code, it will be more convenient for you later);


2. Insert the MSComm control into the project

Select the Components and Controls... option in the Add To Project submenu under the Project menu, double-click the Registered ActiveX Controls item in the pop-up dialog box (wait a moment, this process is slow), then all registered ActiveX controls will appear in the list box. Select Microsoft Communications Control, version 6.0, click the Insert button to insert it into our Project, and accept the default options. (If you cannot see Microsoft Communications Control, version 6.0 in the control list, it may be that you did not select ActiveX when installing VC6. Reinstall VC6 and select ActiveX.)

At this time, you can see the CMSComm class in the ClassView window (note: this class cannot be seen in ClassWizard, and the same is true for reconstructing the clw file), and a phone icon appears in the Controls toolbar (as shown in Figure 1 icon), what you need to do now is to use the mouse to drag this icon into the dialog box. After the program is run, this icon will not be visible.

This 51 microcontroller serial communication programming

3. Use ClassWizard to define the CMSComm class control object

Open the ClassWizard-》Member Viariables tab, select the CSCommTestDlg class, and add the control variable for IDC_MSCOMM1: m_ctrlComm. At this time, you can take a look. //{{AFX_INCLUDES() #include "mscomm. h" //}}AFX_INCLUDES (Run the program at this time, if there is an error, start from the beginning again).


4. Add controls to the dialog box

Add two edit boxes to the main dialog box, one for receiving display data with the ID of IDC_EDIT_RXDATA, and the other for inputting the sending data with the ID of IDC_EDIT_TXDATA. Add a button whose function is to send the contents of the edit box once pressed. Send it once and set its ID to IDC_BUTTON_MANUALSEND. Don’t forget to select the Miltiline and Vertical Scroll properties in the Properties-》Styles of the receiving edit box. If you want to enter multiple lines of text in the sending edit box, you can also select Miltiline.

Then open the ClassWizard-》Member Viariables tab, select the CSCommTestDlg class, add the CString variable m_strRXData for IDC_EDIT_RXDATA, and add the CString variable m_strTXData for IDC_EDIT_TXDATA. Description: m_strRXData and m_strTXData are used to put received and sent character data respectively.


5. Add serial port event message processing function OnComm()

Open ClassWizard-》Message Maps, select class CSCommTestDlg, select IDC_MSCOMM1, double-click the message OnComm, change the function name to OnComm in the pop-up dialog box, (just easy to remember) OK.

This function is used to handle serial port message events. For example, every time the serial port receives data, a message event that there are characters in the serial port receiving data buffer will be generated. The function we just added will be executed. We use the OnComm() function By adding the corresponding processing code, you can achieve the functions you want. Please add the following code to the function:

void CSCommTestDlg::OnComm()

{

// TODO: Add your control notification handler code here

VARIANT variant_inp;

COleSafeArray safearray_inp;

LONG len, k;

BYTE rxdata[2048]; //Set BYTE array An 8-bit integerthat is not signed.

CString strtemp;

if (m_ctrlComm.GetCommEvent()==2) //The event value is 2 indicating that there are characters in the receive buffer

{//////// Below you can add processing code according to your own communication protocol

variant_inp=m_ctrlComm.GetInput(); //Read buffer

safearray_inp=variant_inp;//VARIANT type variable is converted to ColeSafeArray type variable

len=safearray_inp.GetOneDimSize();//Get the effective data length

for(k=0;k《len;k++)《/len;k++)

safearray_inp.GetElement(&k, rxdata+k);//Convert to BYTE array

for(k=0;k《len;k++) p="" Convert the array into a cstring variable

{

BYTE bt=*(char*)(rxdata+k); //Character type

strtemp.Format("%c", bt);//Send characters into the temporary variable strtemp for storage

m_strRXData+=strtemp;//Add the string corresponding to the receiving edit box

}

m_strRXData+=“rn”; //Newline

}

UpdateData(FALSE);//Update the content of the edit box

}


Reference address:51 microcontroller serial communication programming

Previous article:Structural design of assembly language and its application in Tetris
Next article:Design of electronic code lock with STC89C52 microcontroller as the core

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号