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.
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
}
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
- Popular Resources
- Popular amplifiers
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- LED chemical incompatibility test to see which chemicals LEDs can be used with
- Application of ARM9 hardware coprocessor on WinCE embedded motherboard
- What are the key points for selecting rotor flowmeter?
- LM317 high power charger circuit
- A brief analysis of Embest's application and development of embedded medical devices
- Single-phase RC protection circuit
- stm32 PVD programmable voltage monitor
- Introduction and measurement of edge trigger and level trigger of 51 single chip microcomputer
- Improved design of Linux system software shell protection technology
- What to do if the ABB robot protection device stops
- 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
- TI CC1310 series chip question and answer sharing
- What is the difference between NFC and RFID?
- Some technical documents share candence and power semiconductors
- What is a smart home? What are the smart home design principles?
- The way out for electronic companies during the epidemic
- 【Project source code】Two-person buzzer based on FPGA
- Comparison of the internal structures of TI's three major series of DSPs
- CC2530 wireless lighting experiment
- About Kalman Filter Algorithm
- How much did you lose in Big A today?