Serial port programming
Before the serial port works, it should be initialized, mainly to set the timer 1 that generates the baud rate, serial port control and interrupt control. The specific steps are as follows:
Determine the working mode of T1 (program the TMOD register), calculate the initial value of T1, and load TH1 and TL1;
Start T1 (program the TR1 bit in TCON). Determine the serial port control (program the SCON register);
When the serial port works in interrupt mode, interrupt settings must be performed (programming the EIP register).
#include "reg52.h" //This file defines some special function registers of the microcontroller
typedef unsigned int u16; //declare and define the data type
typedef unsigned char u8;
/***********************************************************************************
* Function name: UsartInit()
* Function : Set the serial port
* Input : None
* Output : None
***********************************************************************************/
void UsartInit()
{
SCON=0X50; //Set to work mode 1
TMOD=0X20; //Set the counter working mode 2
PCON=0X80; //Double the baud rate
TH1=0XF3; //Set the initial value of the counter, note that the baud rate is 4800
TL1=0XF3;
ES=1; //Open receive interrupt
EA=1; //Open the general interrupt
TR1=1; //Open the counter
}
/***********************************************************************************
* Function name: main
* Function : Main function
* Input: None
* Output : None
***********************************************************************************/
void main()
{
UsartInit(); // Serial port initialization
while(1);
}
/***********************************************************************************
* Function name: Usart() interrupt 4
* Function : Serial communication interrupt function
* Input : None
* Output : None
***********************************************************************************/
void Usart() interrupt 4
{
u8 receiveData;
receiveData=SBUF; //Output received data
RI = 0; // Clear the receive interrupt flag
SBUF=receiveData; //Put the received data into the send register
while(!TI); //Wait for sending data to complete
TI=0; //Clear the send completion flag
}
Previous article:[51 Single Chip Microcomputer] My roommate learned how to use digital tubes using a King of Time computer.
Next article:How 80C51 serial port works
- Popular Resources
- Popular amplifiers
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- Innolux's intelligent steer-by-wire solution makes cars smarter and safer
- 8051 MCU - Parity Check
- How to efficiently balance the sensitivity of tactile sensing interfaces
- What should I do if the servo motor shakes? What causes the servo motor to shake quickly?
- 【Brushless Motor】Analysis of three-phase BLDC motor and sharing of two popular development boards
- Midea Industrial Technology's subsidiaries Clou Electronics and Hekang New Energy jointly appeared at the Munich Battery Energy Storage Exhibition and Solar Energy Exhibition
- Guoxin Sichen | Application of ferroelectric memory PB85RS2MC in power battery management, with a capacity of 2M
- Analysis of common faults of frequency converter
- In a head-on competition with Qualcomm, what kind of cockpit products has Intel come up with?
- Dalian Rongke's all-vanadium liquid flow battery energy storage equipment industrialization project has entered the sprint stage before production
- 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
- SIMterix-Simplis Collection
- The most annoying thing about making a power supply is drawing the board yourself
- The first FreeRTOS check-in station is open: Application scenario station, closing time is August 14
- [Evaluation of EC-01F-Kit, the NB-IoT development board of Anxinke] Evaluation summary
- Basic points of DSP C language
- Isolated DC-DC power supply module, input and output common ground problem.
- PCB Star Decoration
- Introduction to TTL level, CMOS level, and RS232 level
- RedMonk Programming Language Survey: Python Remains at No. 2
- What is the functional difference between these two capacitors?