Two communication methods
Serial Communication:
Serial communication is the process of dividing data bytes into bits.
Parallel communication:
Parallel communication usually involves transmitting each bit of a data byte simultaneously using multiple data lines.
Serial communication is divided into two types
Synchronous communication
Synchronous communication is to directly control the clocks of the sender and receiver so that the data transmission of both parties is completely synchronized. At this time, data is sent continuously in the form of bits.
Asynchronous communication
The sender and receiver of asynchronous communication use their own clocks to control the transmission and reception of data. The sender can send characters at any time. At this time, data is sent continuously in the form of frames (a frame data has ten bits: one start bit, eight data bits, and one stop bit).
Baud rate
calculate
The baud rate is used to measure the speed at which digital signals are transmitted. It is the number of binary bits transmitted per second and its unit is: bps.
For example, the most commonly used transmission rate is 9600, which means that 960 frames of data are transmitted per second, and one frame of data is ten binary numbers. That is, 960*10=9600
set up
Generally, timer interrupt 1 (T1) is used as the baud rate generator, and T1 adopts working mode 2.
At this time, the baud rate calculation formula is:
Explain the meaning of several formulas
SMOD: Baud rate selection bit
SMOD belongs to the PCON register and is not bit addressable. It can be used as a baud rate doubling flag. When it is set to 0, it does not double. When it is set to 1, the serial communication mode baud rate doubles.
fosc: crystal oscillator frequency of the microcontroller
The default frequency for 51 MCU simulation is 12Mhz, but the error is relatively large. 11.0529MHz is generally used for accurate timing.
256-T initial: the number of times each timer counts
At this time, the baud rate generator T1 is in working mode 2 (automatic loading of eight-bit data). The initial value is loaded into TH1 and TL1 at the beginning. During normal operation, only TH1 counts. When TH1 overflows, the initial value in TL1 is reassigned to TH1, thus realizing automatic loading.
Note: The range of eight-bit binary numbers is 0~255, a total of 256 numbers
Initial setting of baud rate 9600
1. Set the working mode of timer T1:
TMOD=0X20; //Set timer T1 to working mode 2;
2. Calculate the initial value of T1:
TH1=0XFD;
TL1=0XFD; // and load TH1 and TL1, baud rate is 9600bps
3. Start T1:
TR1=1; // Open T1 timer
PCON=0X80; //Double the baud rate
SCON=0X50; //Set to working mode 1: 8-bit data bit
Selection of serial port working mode
Set SM0 and SM1, set the working mode
The role of TI and RI
TI is the send interrupt flag. When the data is sent, TI is set to 1 by hardware. After TI is set to 1, an interrupt request is made to the CPU. After entering the interrupt function, the software needs to set it to 0 (TI=0;)
RI is the receive interrupt flag. When data reception is completed, RI is set to 1 by hardware. After RI is set to 1, an interrupt request is made to the CPU. After entering the interrupt function, it needs to be set to 0 by software (RI=0;)
Both are in the SCON register
Serial communication experiment source code
#include void delay(unsigned int x); void putchar(unsigned char data1); //Character sending function void putstring(unsigned char *dat); //String sending function void main(void) { unsigned char c = 0; SCON = 0x40; //Serial port working mode setting TMOD = 0x20; //Timer working mode setting PCON = 0x00; //Baud rate multiplication setting TL1 = 0xfd; //Baud rate setting TH1 = 0xfd; TI = 0; // Clear the send interrupt flag TR1 = 1; //Start the timer while(1) { // Skip the author's personal output here, if you need to output // Add output in while, regular C language is enough //For those who really can't write, please see the attachment below } } void delay(unsigned int x) //delay { unsigned char i; while(x--) { for(i = 0;i < 120;i++); } } void putchar(unsigned char data1) { SBUF = data1; //Send the character to be sent into the send buffer while(TI == 0); //Wait for sending to complete TI = 0; //Send interrupt flag please 0 } void putstring(unsigned char *dat) { while(*dat != '') //Judge whether the string has been sent { putchar(*dat); //Send a single character dat++; //Add 1 to the character address, pointing to the next character } } The experimental principle diagram is as follows The schematic diagram is very simple, only RX-TX, TX-RX are needed. The simulated serial port detection here is here! Some friends may find that the simulation is running but the serial terminal cannot be opened... After running, click here You can check what is displayed, otherwise you can't observe it. (Ultimate babysitting tutorial…I tried my best) Attach the packaged project files: https://download.csdn.net/download/Alangman/18480056
Previous article:Ultrasonic module HC-SR04 debugging experience sharing
Next article:Design digital tube with c51 language, interrupt design 1s display 0~F, use proteus simulation
Recommended ReadingLatest update time:2024-11-22 20:47
- Popular Resources
- Popular amplifiers
- 西门子S7-12001500 PLC SCL语言编程从入门到精通 (北岛李工)
- Siemens Motion Control Technology and Engineering Applications (Tongxue, edited by Wu Xiaojun)
- How to read electrical control circuit diagrams (Classic best-selling books on electronics and electrical engineering) (Zheng Fengyi)
- MCU C language programming and Proteus simulation technology (Xu Aijun)
- Naxin Micro and Xinxian jointly launched the NS800RT series of real-time control MCUs
- How to learn embedded systems based on ARM platform
- Summary of jffs2_scan_eraseblock issues
- Application of SPCOMM Control in Serial Communication of Delphi7.0
- Using TComm component to realize serial communication in Delphi environment
- Bar chart code for embedded development practices
- Embedded Development Learning (10)
- Embedded Development Learning (8)
- Embedded Development Learning (6)
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- Intel promotes AI with multi-dimensional efforts in technology, application, and ecology
- ChinaJoy Qualcomm Snapdragon Theme Pavilion takes you to experience the new changes in digital entertainment in the 5G era
- Infineon's latest generation IGBT technology platform enables precise control of speed and position
- Two test methods for LED lighting life
- Don't Let Lightning Induced Surges Scare You
- Application of brushless motor controller ML4425/4426
- Easy identification of LED power supply quality
- World's first integrated photovoltaic solar system completed in Israel
- Sliding window mean filter for avr microcontroller AD conversion
- What does call mean in the detailed explanation of ABB robot programming instructions?
- STMicroelectronics discloses its 2027-2028 financial model and path to achieve its 2030 goals
- 2024 China Automotive Charging and Battery Swapping Ecosystem Conference held in Taiyuan
- State-owned enterprises team up to invest in solid-state battery giant
- The evolution of electronic and electrical architecture is accelerating
- The first! National Automotive Chip Quality Inspection Center established
- BYD releases self-developed automotive chip using 4nm process, with a running score of up to 1.15 million
- GEODNET launches GEO-PULSE, a car GPS navigation device
- Should Chinese car companies develop their own high-computing chips?
- Infineon and Siemens combine embedded automotive software platform with microcontrollers to provide the necessary functions for next-generation SDVs
- Continental launches invisible biometric sensor display to monitor passengers' vital signs
- Modeling, Control and Digital Implementation of Switching Converters
- MSP430 FRAM Quality and Reliability
- There is a problem in using tlv5616, it's very strange
- The first time I drew a board, I couldn't burn the program into the JTAG after it was made. I cried
- How to generate a .txt file on CCS and burn it to MSP430
- Where can I find development information for the Samsung 4412 processor?
- TMS320C6678 memory access performance
- 4-digit LED digital display meter interfaced with PLC
- About Voltage Follower
- Advantages of combining TI analog devices with DSP