Today I studied the serial port communication of 51 single-chip microcomputer. The single-chip microcomputer used is the Puzhong Technology Development Board, but in fact the hardware used is the same as that of other brands of single-chip microcomputers, there is no difference. Let me summarize my understanding and opinions.
Communication Principles
The communication principle can be roughly divided into two methods: serial and parallel. Each has its own advantages and disadvantages. I will not go into details here. The method used is serial communication. Let me briefly introduce the principle of serial communication. The figure above shows
two devices, one transmission line, and each group of data is transmitted. The total length is not necessarily 8 bits, which is determined by the internal setting of the 51 single-chip microcomputer. There are many interface methods for communication between devices. I use the serial interface on 51. The hanging chart:
SBUF: refers to the two buffer registers in the serial port, one is the sending register and the other is the receiving register. They are completely independent in physical structure, but the addresses are overlapping. They are both byte-addressed registers, and the byte addresses are all 99H,
TXD: Transmit (tx) Data; RXD: Receive (rx) Data;
Remember the two ports by abbreviations. I often can't remember (shame), so the upper SBUF is the sending, and the lower one is the receiving register. The data sent or received will be temporarily stored in it. Just assign values directly when programming. TH1 and TL1 are the clock configuration system, which is mainly used to control the baud rate and the total number of bits sent per second. (When debugging, be sure to set the baud rate accordingly)
Control register SCON: The internal structure is shown in the figure below. It is mainly used to set the serial port working mode, receive and send control, and control the status bit
. SM0 and SM1 are the control working mode shown in the figure below, which control the number of bits of each group of total data (start and stop bits + data bits). Shift is to send input and output data one pulse at a time.
SM2 is the multi-machine communication control bit, in mode 2 and 3. SM2 controls whether RB8 will trigger the RI interrupt. When SM2=1, R8=1 activates the interrupt (the interrupt reads the data away), and R8=0 does not activate it; SM2=0 loses the control function. Regardless of whether RB is 0, RI can be activated. In mode 0, SM2 must be 0; in mode 1, SM2=0, when the stop bit is received, the R1 interrupt is turned on.
REN allows serial reception bit. REN=1 turns on reception, otherwise data cannot be received.
TB, used only in modes 2 and 3, is the parity check bit
RB. In modes 2 and 3, it is still used as a parity check bit. In mode 1, it is used to store the data stop bit, which is used to automatically set RI to 1 and start the interrupt.
PCON: used to control whether the baud rate is doubled, and SMOD=1, the baud rate is doubled. SMOD=0 at reset;
OK, the general communication principle is here, and the following is an explanation of its use!
Programming
Steps:
1. Determine the working mode of TMOD (counter)
2. Configure the initial values of TH1 and TL1
3. Configure SCON and PCON
4. Enable the interrupt enable bit (general interrupt, serial port interrupt), configure the interrupt (mainly SBUF reads the send data in the interrupt), configure TCON to enable the interrupt, that is, set TR1 to 1.
The above code: acts as the serial communication input value and then returns
include
typedef unsigned char u8;
void ready(void)
{
TMOD = 0X20;
TH1=0XFF;
TL1=0XF9; //Baud rate 9600
SCON=0X50;//0101 0000
PCON=OX80; //1000 0000
EA=1; //Open the general interrupt
ES=1; //Open serial port interrupt, equivalent to ET1 ET0
TR1=1; //Turn on the counter. When it overflows, it will give SMOD a pulse to receive and read data to set the baud rate.
//When the stop bit is received, RI=1 triggers an interrupt
}
void main(void)
{
ready();
while(1);
}
void time1(void) interrupt 4
{
static u8 result;
result=SBUF; //Enter configuration interrupt and read data
RI=0;
SBUF=result; // Input data into SBUF and send
while(!TI); //Wait for sending to complete
TI=0;
}
A simple serial communication is completed.
Previous article:51 MCU Learning Log-Instruction System
Next article:data,bdata,idata,pdata,xdata,code storage types and storage areas
- Popular Resources
- Popular amplifiers
- Learn ARM development(16)
- Learn ARM development(17)
- Learn ARM development(18)
- Embedded system debugging simulation tool
- A small question that has been bothering me recently has finally been solved~~
- Learn ARM development (1)
- Learn ARM development (2)
- Learn ARM development (4)
- Learn ARM development (6)
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
- CGD and Qorvo to jointly revolutionize motor control solutions
- CGD and Qorvo to jointly revolutionize motor control solutions
- Keysight Technologies FieldFox handheld analyzer with VDI spread spectrum module to achieve millimeter wave analysis function
- Infineon's PASCO2V15 XENSIV PAS CO2 5V Sensor Now Available at Mouser for Accurate CO2 Level Measurement
- Advanced gameplay, Harting takes your PCB board connection to a new level!
- Advanced gameplay, Harting takes your PCB board connection to a new level!
- A new chapter in Great Wall Motors R&D: solid-state battery technology leads the future
- Naxin Micro provides full-scenario GaN driver IC solutions
- Interpreting Huawei’s new solid-state battery patent, will it challenge CATL in 2030?
- Are pure electric/plug-in hybrid vehicles going crazy? A Chinese company has launched the world's first -40℃ dischargeable hybrid battery that is not afraid of cold
- ECG test
- Problems reading data and displaying it
- How to enable TI 15.4-Stack to support 470M frequency band
- EEWORLD University Hall----Live Replay: Rochester Rochester Electronics tells you in detail-Challenges and solutions after semiconductor production suspension
- The fifth pitfall of domestic FPGA chips: "No devices found" reappears
- What should I do if I can't find an LCD screen of the right size?
- Allwinner heterogeneous multi-core AI intelligent vision V853 development board evaluation + openwrt firmware packaging pack report mbr error solution
- TI TMS320F28335 program SVPWM source program
- 【Smart Kitchen】Environmental Sensor PCB Design
- What is the frequency domain and why is it so important for RF design, analysis and test?