51 MCU Tutorial Lecture 6_Serial Port Communication

Publisher:qiuxubiaoLatest update time:2016-12-10 Source: eefocus Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

STC51 microcontrollers usually have one serial port, and some have two serial ports. The serial port is generally used for downloading programs and serial communication. Serial communication is particularly suitable for controlling equipment, so industrial computers generally have serial ports.

The serial port pins of the 51 single-chip microcomputer are P3.0 and P3.1, which are RXD and TXD pins respectively. Serial port communication can be used for communication between single-chip microcomputers and other chips, communication between single-chip microcomputers, or communication between single-chip microcomputers and computers. Serial port communication is generally cross-connected. The standard serial port has 9 pins. In general, we only use RXD TXD GND. The remaining pins are communication control pins, which were used to control telephones in the past, but are basically not used now. Therefore, only RXD and TXD are retained on our 51 single-chip microcomputer.

The serial communication protocol is very simple. If the serial ports on the microcontroller are not enough, you can also use any two pins to simulate serial communication.

Generally, we use an 8-bit serial port protocol with a start bit. The baud rate is usually 9600, and there are also 1200, 2400, 4800, 115200, etc.

The program to open the serial port is as follows:
TMOD=0x20;
TH1=0xFD;
TL1=0xFD;
SCON=0X50;
TR1=1;
The program involves the configuration of 5 registers.
TMOD is the timer control register
TH1 and TL1 are the initial value registers of timer 1
SCON is the serial port control register
TR1 is bit 4 in the TCON register, because this register can be bit operated

The program for sending data through the serial port is as follows:
SBUF=55; // Send data 55
while(!TI);
TI=0;
The program involves two registers
SBUF is the serial port data register
TI is bit 1 in the SCON register, bit operation

The program for receiving data through the serial port is as follows:
if(RI)
{
recebuf=SBUF;
RI=0;
}
The program involves two registers.
SBUF is the serial port data register.
RI is bit 0 in the SCON register.

SBUF is both a transmit data register and a receive data register.

The above functional modules can be encapsulated into functions and called directly when used.


Reference address:51 MCU Tutorial Lecture 6_Serial Port Communication

Previous article:51 MCU Tutorial Lecture 6_Serial Port Communication
Next article:51 MCU Tutorial Lecture 5_C51 Program

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号