/*-------------------------------------------------
Function: Connect the serial port to the computer, download the program, turn on the power
Open the serial port debugging assistant and set the baud rate to 2400 with no parity check.
The crystal oscillator is 12MHz, and the format used for sending and receiving is the same.
Character format, after setting correctly, the acceptance box can display:
The UART test, please enter the information in the send area
----------------------------------------*/
#include
//Include the header file. Generally, no modification is required. The header file contains the definition of special function registers. /*------------------------------------------------
Function declaration
----------------------------------------*/
void SendStr(unsigned char *s);
/*------------------------------------------------
Serial port initialization
----------------------------------------*/
void InitUART (void)
{
SCON = 0x50; // SCON: Mode 1, 8-bit UART, receive enabled
TMOD |= 0x20; // TMOD: timer 1, mode 2, 8-bit reinstall
//TH1 = 0xF3; // TH1: reload value 2400
TH1=0xfd;
TL1=0xfd; //11.0592MHZ crystal oscillator 9600 baud rate corresponding to the initial value
TR1 = 1; // TR1: timer 1 is turned on
EA = 1; // Enable general interrupt
// ES = 1; // Enable serial port interrupt
}
/*------------------------------------------------
Main function
----------------------------------------*/
void main (void)
{
InitUART();
SendStr("The UART test, please enter information in the send area");
ES = 1; //Open serial port interrupt
while (1)
{
}
}
/*------------------------------------------------
Send a byte
----------------------------------------*/
void SendByte(unsigned char dat)
{
SBUF = dat;
while(!TI);
TI = 0;
}
/*------------------------------------------------
Send a string
----------------------------------------*/
void SendStr(unsigned char *s)
{
while(*s!='\0')// \0 indicates the end of the string, by checking whether the string ends
{
SendByte(*s);
s++;
}
}
/*------------------------------------------------
Serial port interrupt program
----------------------------------------*/
void UART_SER (void) interrupt 4 //Serial interrupt service routine
{
unsigned char Temp; //define temporary variable
if(RI) //Judge whether a receive interrupt is generated
{
RI=0; // Flag cleared
Temp=SBUF; //Read the value in the buffer
P1=Temp; //Output the value to port P1 for observation
SBUF=Temp; //Send the received value back to the computer
}
if(TI) //If it is a send flag, clear it
TI=0;
}
Previous article:51 MCU interrupt implementation process
Next article:Keil C51 redirect printf to serial port
- 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
- Share the open source information of the creative Coke power bank I collected, including schematics, PCB, and code
- Real-time control technologies meet the demands of real-time industrial communications – Part 3
- Share a STMF105RBT6 and 103RCT6_keil_C++ source program
- MSP430 MCU Example 3 - Colored Light Control
- EEWORLD University-What is an isolated gate driver?
- Burning image and mounting USB disk with busybox file system failed
- EEWORLD University Hall----Open Source PWM Robotic Arm (Version 51)
- MakeCode Extension for the LSM6DSO Sensor
- PCB reliability test
- It is rare for home appliance manufacturers to issue price increase letters, simply because of rising costs of chips and other items!