The general software simulation method of expanding the serial port uses 1 I/O port, 1 INT external interrupt and timer. The serial port expanded by this method has two disadvantages: ①, because the INT external interrupt is used, only 2 INT external interrupts can be used to expand 2 serial ports. ②, the efficiency of sending and receiving data in the article is relatively low, which occupies a lot of CPU time and cannot be performed simultaneously with other tasks, so the scope of use is limited.
The method of simulating serial ports proposed in this paper uses only 2 common I/Os and 1 timer. Since there is no need for INT restrictions, multiple serial ports can be expanded, and the function of FIFO is included. This method expands the sending and receiving data of the simulated serial port in the interrupt service, so it is very efficient. Generally, the single-chip microcomputer supports timer interrupts, so this method can be applied to most single-chip microcomputers.
For low-speed single-chip microcomputers (such as 89S51), low-speed serial ports (9600, 4800, etc.) can be expanded, and for high-speed single-chip microcomputers (such as AVR, PIC, C8051, STC12), high-speed serial ports (such as 19200, 28800, 38400, 57600, etc.) can be expanded. At present, the processing speed of single-chip microcomputers is getting higher and higher, and the price is getting cheaper and cheaper. The STC12C1052 chip used in this paper has high speed and low price, and the price is only RMB 3.8 per piece. When developing and designing electronic products, it is required to reduce hardware costs while ensuring performance. Software simulation and expansion of serial ports provides a good way to reduce costs.
1. Principle of serial communication
In asynchronous communication of serial port, data is transmitted in byte frames with bytes as units. The sender and receiver must communicate in the same byte frame format and baud rate. The byte frame format specifies the start bit, data bit, parity bit, and stop bit. The start bit is the beginning of the byte frame, which makes the data line in the logic 0 state, used to indicate to the receiver that the data frame has started to be sent, so as to synchronize the sending and receiving devices. The stop bit is the end of the byte frame, which makes the data line in the logic 1 state, used to indicate to the receiver that the data frame has been sent. The baud rate adopts standard speed, such as 4800, 9600, 19200, 28800, 38400, 57600, etc.
2. Design idea of software UART
In terms of hardware requirements for this design, only any two I/O ports and one timer of the microcontroller are occupied, and the timer interrupt function of the timer is used to achieve accurate baud rate timing. Both sending and receiving are carried out under the control of the timer interrupt.
The idea of data transmission is that when starting byte transmission, the start bit is first sent through TxD, then the data bit and the parity check bit are sent, and finally the stop bit is sent. The transmission process is controlled by the transmission state machine. Each interruption only sends 1 bit, and after several timed interruptions, the transmission of 1 byte frame is completed. The
idea of data reception is that when it is not in the byte frame reception process, each timed interrupt monitors the state of RxD at 3 times the baud rate. When its sampling level is 1, 0, and 0 for 3 consecutive times, it is considered that the start bit is detected, and then a byte frame reception is started. The byte frame reception process is controlled by the receiving state machine. Each interruption only receives 1 bit, and after several timed interruptions, the reception of 1 byte frame is completed.
In order to improve the performance of the serial port, the FIFO function is implemented in both transmission and reception to improve the real-time performance of the communication. The length of the FIFO can be freely defined to meet the different needs of users.
The baud rate is calculated according to the calculation formula. When setting the highest baud rate, the execution time of the simulated serial port program code must be considered. The timing time must be greater than the specified time of the simulated serial port program. The faster the execution speed of the microcontroller, the higher the serial port communication speed can be achieved.
3. Implementation of software UART design
This program is run and tested on the STC12C1052 high-speed microcontroller produced by Hongjing Technology (Shenzhen). The STC12C1052 microcontroller is a single-clock/machine cycle MCS51 core microcontroller, which is fully compatible with the 89C2051 pin. Its operating frequency reaches 35MHz, which is equivalent to the 420MHz 89C2051 microcontroller, and each piece costs RMB 3.8. Due to the high speed of the microcontroller, the software expansion serial port method is more convenient to achieve high-speed serial ports.
In the design of this extended serial port, the crystal oscillator frequency used by STC12C1052 is 22.1184Mhz, and the timing time is calculated at 3 times the baud rate. During the receiving process, this timing is used to sample the receiving start bit. During the sending and receiving process, the frequency is divided by 3 to obtain the standard baud rate timing for data sending and receiving.
3.1、Data definition
Define some resources necessary for the simulated serial port program, such as I/O pins, baud rate, data buffer, etc.
#define Fosc 22118400 //Crystal frequency
#define Baud 38400 //Baud rate
#define BaudT (Fosc/Baud/3/12)
#define BufLong 16 //FIFO length
sbit RxD1=P1^7; //Analog receiving RxD
sbit TxD1=P1^6; //Analog sending TxD
bit Brxd1,Srxd1;//RxD detection level
BYTE Rbuf1[BufLong];//FIFO receiving area
BYTE Rptr1,Rnum1;
BYTE Tbuf1[BufLong];//FIFO sending area
BYTE Tptr1,Tnum1;
BYTE TimCnt1A,TimCnt1B;
BYTE Mtbuf1,Mrbuf1,TxdCnt1,RxdCnt1;
3.2、Data receiving subroutine
During data receiving, the logical bits of RxD are stored sequentially to form byte data. When data receiving is completed and the stop bit is 1, it means that valid data has been received, and the result is stored in the receiving FIFO queue.
void Recv()
{
if(RxdCnt1>0) //Store 8 data bits
{
Mrbuf1>>=1;
if(RxD1==1) Mrbuf1=Mrbuf1|0x80;
}
RxdCnt1--;
if(RxdCnt1==0&& RxD1==1) //Data receiving is completed
{
Rbuf1[Rptr1]=Mrbuf1; //Store in FIFO queue
if(++Rptr1>BufLong-1) Rptr1=0;
if(++Rnum1>BufLong) Rnum1=BufLong;
}
}
3.3. Data sending subroutine
During this program, when the data sending state ends, it checks whether the sending FIFO queue is empty. If it is not empty, it takes out the sending data and then starts the sending state. When in the sending state, it sends the start bit, data bit and stop bit according to the state of the state machine.
void Send()
{
if(TxdCnt1!=0) //Byte sending state machine
{
if(TxdCnt1==11) TxD1=0;//Send start bit 0
else if(TxdCnt1>2) //Send data bit
{ Mtbuf1>>=1; TxD1=CY;}
else TxD1=1; //Send end bit 1
TxdCnt1--;
}
else if(Tnum1>0) //Detect FIFO queue
{
Tnum1--;
Mtbuf1=Tbuf1[Tptr1]; //Read FIFO dataif
(++Tptr1>=BufLong) Tptr1=0;
TxdCnt1=11; //Start sending state machine
}
}
3.4, Interrupt program
The interrupt timing time is 1/3 of the baud rate timing, that is, RxD is sampled at 3 times the baud rate to realize the judgment of the start bit, and the receiving process state machine is started when the start bit arrives. Divide the timer by 3 and call the data sending and receiving process to perform serial communication at an accurate baud rate.
void Uart() interrupt 1 using 1
{
if(RxdCnt1==0 ) //Receiving start identification
{
if(RxD1==0 && Brxd1==0 && Srxd1==1) { RxdCnt1=8; TimCnt1B=0;}
}
Srxd1=Brxd1; Brxd1=RxD1;
if(++TimCnt1B>=3 && RxdCnt1!=0) { TimCnt1B=0; Recv();}//Data receiving
if(++TimCnt1A>=3) { TimCnt1A=0; Send();} //Data sending
}
3.5、Serial port initialization
Open the timer interrupt, set the timer to self-loading mode, set the timer interrupt interval according to the baud rate, start the timer, and initialize the UART variables.
void IniUart()
{
IE="0x82"; TMOD="0x22";
TH0=-BaudT; TL0=-BaudT; TR0=1;
Rptr1=0;Rnum1=0;Tptr1=0;Tnum1=0;
}
4. Conclusion
The uniqueness of the analog serial port design method proposed in this paper lies in: only using any two ordinary I/O pins and one timer interrupt to realize full-duplex serial port, less hardware occupation, and multiple serial port expansion capability; the judgment method of three consecutive samplings is used when judging the start bit of serial port reception, which is simple to implement and has high accuracy; using timer interrupt to realize the sending and receiving of serial port data, and realizing FIFO queue, so that the sending and receiving of serial port work is efficient.
Previous article:Design of Temperature Measurement System Based on DS18820, AT89S52 and RF Chip
Next article:1 to 8 ISP system based on MTV412 microcontroller
Recommended ReadingLatest update time:2024-11-16 16:52
- Popular Resources
- Popular amplifiers
- Wireless Sensor Network Technology and Applications (Edited by Mou Si, Yin Hong, and Su Xing)
- Modern Electronic Technology Training Course (Edited by Yao Youfeng)
- Modern arc welding power supply and its control
- Small AC Servo Motor Control Circuit Design (by Masaru Ishijima; translated by Xue Liang and Zhu Jianjun, by Masaru Ishijima, Xue Liang, and Zhu Jianjun)
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
- 51 STM32 reference study manuals, one-click download without points!
- Has anyone seen this chip?
- How to insert assembly code in Keil 5 version?
- Gaoyun FPGA logic analyzer function usage
- Does anyone have a good thermostat switch?
- Ultra-low power MCU-How to reduce the power consumption of MCU
- [MM32 eMiniBoard Review] Part 4: Questions about multi-channel ADC
- Modbus_RTU (RS485) Fieldbus Remote Inputs/Outputs Modules System Design Based on GD32E231
- Particle Xenon nRF52840 BLE Development Board
- COCOFLY Tutorial - Crazy Shell Drone Series · Quick Start · [6] Use and Introduction of Ground Station Host Computer