51 MCU serial port operation entry experience summary and C source code

Publisher:朱雀Latest update time:2017-10-02 Source: eefocus Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

It feels good to use a computer to control a microcontroller, and then control other peripherals such as relays. How to achieve it? Let's do it step by step.

To realize the microcontroller serial port communication, that is, the communication between the microcontroller and the computer, you need to understand some basic concepts.

(1) Before implementation, please make sure that the serial port is normal. I have done serial port operation before, but it burned and could not be controlled. Later I found that there was an exclamation mark on the serial port in the device manager. There was a problem with the driver. It may be that the development experiment board is not plugged in properly or the USB port is changed (I use a serial port to USB converter for laptop)

(2) Serial port initialization. In this regard, it is best to have a related book to understand timers and interrupts, so that the operation will be easy.

1. Set the working mode of the serial port: set the SCON register.

What is SCON?

The SCON register is an addressable special register of the 51 microcontroller, used for serial data communication control, its byte address is: 98H, bit address: 98H~9FH. The content is:


SM0SM1SM2RENTB8RB8TIRI


SM0 SM1: Serial port working mode selection bit.

SM0SM1 Working Mode Function Baud Rate

00 Mode 0 8-bit synchronous shift register fosc/12        

01 Mode 1 10-bit UART Variable

10 Mode 2 11-bit UART fosc/64 (or fosc/32)

11 Mode 3 11-bit UART Variable

 

-------------------------------------------------- ----------

SM2: Multi-machine communication control bit

REN: Receive enable control, 1 means allowing serial port reception, 0 means prohibiting serial port reception.

TB8: The 9th data bit sent.

RB8: The 9th data bit received.

TI: Transmit interrupt flag. After sending a frame of data, the hardware automatically sets it to 1. The TI bit must be cleared by software.

RI: Receive interrupt flag. After receiving a frame of data, the hardware automatically sets it to 1. The RI bit must be cleared by software.

 

 

Example: Use the serial port debugging assistant to debug the serial port and send hexadecimal numbers to make the corresponding LED light up.

The C program is as follows:

#include
void main()

TMOD=0x20; //Set timer: Timer1 Mode 2 8-bit reload
TH1=0xfd; //Baud rate for 9600 baud @ 11.0592MHz
TL1=0xfd;
SM0=0;               
SM1=1;
REN=1; //Control RI to allow serial reception. SM0SM1 and REN are SCON=0x50;
TR1=1; //Start timer 1
while(1)
   {
   
    if(RI==1) //If RI is 1, the data sent by the serial port is received
    {
    RI=0; Library //Software clear. Must
    P0=SBUF; //SBUF is a buffer. The receiving buffer can only be read but not written, and the sending buffer can only be written but not read.

/* The two share the same byte address and are independent of each other. */
   
     }

   }
}

 

The above is the serial control LED light. If you want to display which lights, you can send them through the serial port debugging software.

Example: Digital tube display. Here only one digit is shown. If there are multiple digits, you can add related program segments.

#include
void main()

TMOD=0x20;
TH1=0xfd;
TL1=0xfd;
SM0=0;
SM1=1;
REN=1; //Control RI
TR1=1;
while(1)
   {
   
    if(RI==1)
    {
    RI=0;
    P0=SBUF; //P0 is the value of the digital tube.             
   
    P2=0x06; //8-bit digital tube, the 7th one, P2 port is chip select,
    }

   }
}

The above program can be debugged and run on AT89s52 microcontroller + Keil.

Related tools:

Serial port debugging assistant, 51 serial port communication calculator.exe, can be searched and downloaded online.


Reference address:51 MCU serial port operation entry experience summary and C source code

Previous article:LCD 12864 Chinese character display program and C language program with normal display order
Next article:MCS-51 core based assembly learning notes

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号