Software UART Design Based on the Universal Pins of Single-Chip Microcomputer

Publisher:满足的36号Latest update time:2010-10-08 Source: 微计算机信息Keywords:MCU Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

introduction

With the continuous deepening of single-chip microcomputer application technology, multi-machine systems composed of single-chip microcomputers have made great progress. Multiple single-chip microcomputers use serial ports to transmit data, forming a complex master-slave communication network. Some single-chip microcomputers in the multi-machine system undertake complex communication tasks. When the computer's serial port cannot meet the needs, the serial port must be expanded. Such as multi-parameter medical monitors, community anti-theft alarm systems, RS485 bus control systems, etc.

At present, there are mainly the following methods to expand the serial port: ①, using serial port expansion chips, such as ST16C550, ST16C554, SP2538, MAX3110, etc. Although the cost is relatively high, the reliability of the system is guaranteed, which is suitable for systems with large data volume and more serial port requirements; ②, using the time-sharing switching method to expand a serial port to communicate with multiple serial port devices. The time-sharing multiplexing method has low cost, but is only suitable for occasions with small data volume, and can only be actively communicated with multiple devices by this single-chip microcomputer, and the real-time performance is poor; ③, using software simulation to expand the serial port, its advantages are also low cost and good real-time performance, but it takes up some CPU time.

The general software simulation method of expanding the serial port uses 1 I/O port, 1 INT external interrupt and timer. This method has two disadvantages: 1. Due to the use of INT external interrupt, only 2 INT external interrupts can be used to expand 2 serial ports. 2. The efficiency of sending and receiving data in this 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 simulated serial port method proposed in this paper only uses 2 ordinary I/Os and 1 timer. Since it does not require the limitation of INT, it can expand multiple serial ports with FIFO function. This method expands the simulated serial port's sending and receiving data in the interrupt service, so it is very efficient. Generally, the microcontroller supports timer interrupt, so this method can be applied to most microcontrollers.

For low-speed microcontrollers (such as 89S51), low-speed serial ports (9600, 4800, etc.) can be expanded. For high-speed microcontrollers (such as AVR, PIC, C8051, STC12), high-speed serial ports (such as 19200, 28800, 38400, 57600, etc.) can be expanded. Currently, the processing speed of microcontrollers is getting higher and higher, and the price is getting cheaper and cheaper. The STC12C1052 chip used in this article 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 provide a good way to reduce costs.

1. Serial communication principle

In the asynchronous communication of the serial port, data is transmitted in byte frames with bytes as the unit. The sender and the receiver must communicate according to 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 puts 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 puts the data line in the logic 1 state, used to indicate to the receiver that the data frame has been sent. The baud rate uses standard speeds, such as 4800, 9600, 19200, 28800, 38400, 57600, etc.

2. Design idea of ​​software UART

In terms of hardware requirements for this design, it only occupies any two I/O ports and one timer of the microcontroller, and uses the timer interrupt function of the timer to achieve precise 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 the byte transmission is started, the start bit is sent first through TxD, then the data bit and the parity check bit are sent, and finally the stop bit is sent. The sending process is controlled by the sending state machine. Only one bit is sent each time the interrupt is interrupted. After several timed interrupts, the sending of one byte frame is completed.

The idea of ​​data reception is that when it is not in the byte frame reception process, each timer interrupt monitors the state of RxD at 3 times the baud rate. When its sampling level is 1, 0, and 0 for three consecutive times, it is considered that the start bit is detected, and a byte frame reception is started. The byte frame reception process is controlled by the receiving state machine. Each interrupt only receives 1 bit, and the reception of 1 byte frame is completed after several timer interrupts.

In order to improve the performance of the serial port, the FIFO function is implemented in both sending and receiving to improve the real-time 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 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 pins. Its operating frequency is 35MHz, which is equivalent to the 420MHz 89C2051 microcontroller. 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 frequency of STC12C1052 is 22.1184Mhz. The timing is calculated as 3 times the baud rate. During the receiving process, this timing is used to sample the receive 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 simulating serial port programs, such as I/O pins, baud rate, data buffer, etc.

#define Fosc 22118400 //Crystal oscillator frequency

#define Baud 38400 //Baud rate

#define BaudT (Fosc/Baud/3/12)

#define BufLong 16 //FIFO length

sbit RxD1=P1^7; //Simulate receiving RxD

sbit TxD1=P1^6; //Simulate sending TxD

bit Brxd1,Srxd1; //RxD detection level

BYTE Rbuf1[BufLong]; //FIFO receiving area

BYTE Rptr1,Rnum1;

BYTE Tbuf1[BufLong]; //FIFO send area

BYTE Tptr1,Tnum1;

BYTE TimCnt1A,TimCnt1B;

BYPASS Mtbuf1,Mrbuf1,TxdCnt1,RxdCnt1;

3.2 Data receiving subroutine

During the data reception process, the logical bits of RxD are stored in sequence to form byte data. When the data reception is completed and the stop bit is 1, it means that valid data has been received, and the result is stored in the receive 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 reception 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 the program, when the data sending state ends, it checks whether the sending FIFO queue is empty. If it is not empty, the sending data is taken out and then the sending state is started; when in the sending state, the start bit, data bit and stop bit are sent 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 termination bit 1

TxdCnt1--;

}

else if(Tnum1>0) //Detect FIFO queue

{

Tnum1--;

Mtbuf1=Tbuf1[Tptr1]; //Read FIFO data

if(++Tptr1>=BufLong) Tptr1=0;

TxdCnt1=11; //Start the sending state machine

}

}

3.4. Interrupt Program

The interrupt timing time is 1/3 of the baud rate timing, that is, the 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. The timing is divided by 3 and the data sending and receiving process is called to perform serial communication at an accurate baud rate.

void Uart() interrupt 1 using 1

{

if(RxdCnt1==0 ) //Receive 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 reception

if(++TimCnt1A>=3) { TimCnt1A=0; Send();} //Data sending

}

3.5. Serial port initialization

Enable 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: using only two ordinary I/O pins and one timer interrupt to realize full-duplex serial port, occupying less hardware and having the ability to expand multiple serial ports; using a continuous three-time sampling method to determine the start bit of the serial port reception, this method is simple to implement and has high accuracy; using the timer interrupt to realize the serial port

The sending and receiving of data, and the implementation of FIFO queue, make the serial port sending and receiving work efficient.

The author has used this method to realize the expansion of 5 serial ports on the STC12C1052 microcontroller in practical applications , which is used for receiving data from multiple modules of medical monitors, and the effect is satisfactory. With the improvement of the processing speed of the microcontroller, this method can replace the serial port expansion chip and greatly reduce the hardware cost of the system. Since it is developed in the sampling C language, it can be easily transplanted to high-speed microcontrollers such as AVR, PIC, and C8051.

Keywords:MCU Reference address:Software UART Design Based on the Universal Pins of Single-Chip Microcomputer

Previous article:Use the serial port debugging wizard or the PC's hyperterminal to debug the serial port program
Next article:Design of fan temperature control system based on single chip microcomputer

Recommended ReadingLatest update time:2024-11-17 02:52

How to use ATtiny85 microcontroller to make a portable ultrasonic rangefinder
In many applications, it is often necessary to measure the distance between an object and a person, device, or vehicle, such as robot motion control, vehicle control, blind canes, medical equipment, etc. There are many ways to measure distance, and using ultrasonic sensors for distance measurement is one of the cheape
[Microcontroller]
How to use ATtiny85 microcontroller to make a portable ultrasonic rangefinder
LCD048 application program based on MSP430 microcontroller
TI's MSP430 series microcontroller is an ultra-low power mixed signal controller, which includes a series of devices, which are composed of different modules for different applications. Among them, the FLASH series makes efficient electronic systems lightweight, and the FLASH memory is also very flexible. At the same t
[Microcontroller]
LCD048 application program based on MSP430 microcontroller
Design and application scope of intelligent test system based on microcontroller
1 Introduction With the popularity of electronic and electrical products in social life, many small and medium-sized enterprises in China have begun to develop and produce such products on their own to meet market demand. However, due to the limitations of human and material resources of small and medium-sized enterpr
[Microcontroller]
Design and application scope of intelligent test system based on microcontroller
The Application and Implementation of Time-Sharing Operating System Idea in Single-Chip Microcomputer Programming
Preface As the main control unit of the embedded system, the single-chip microcomputer, its software is often a microscopic real-time operating system, and most of them are specially designed for a certain application. The system program has the ability of real-time process control or real-time information
[Microcontroller]
The Application and Implementation of Time-Sharing Operating System Idea in Single-Chip Microcomputer Programming
Music player and function generator based on 51 microcontroller Proteus simulation
music player: Actual operating effect: iframe src="https://video.zhihu.com/video/1432808946373898240?player=%7B%22autoplay%22%3Afalse%2C%22shouldShowPageFullScreenButton%22%3Atrue%7D" frameborder="0" /iframe 51 microcontroller Proteus simulation music player Music player schematic diagram Music player code link: h
[Microcontroller]
Music player and function generator based on 51 microcontroller Proteus simulation
High-precision data acquisition device based on PIC series microcontroller
1ADS1210 Pins and Functions ADS1210 is a new type of A/D converter with high precision, wide dynamic range, single 5V power supply and 24-bit resolution. The package types are 18-pin dual in-line and 18-pin surface mount. The pin function description is shown in Table 1. ●Instruction Register (INSR)
[Microcontroller]
High-precision data acquisition device based on PIC series microcontroller
Research on temperature and humidity detection and control system based on single chip microcomputer
introduction Temperature and humidity are common controlled parameters in industrial production. Temperature and humidity are no longer independent quantities, but should be considered comprehensively in system integration. Using a single-chip microcomputer to control temperature and humidity has the advant
[Microcontroller]
Research on temperature and humidity detection and control system based on single chip microcomputer
Design of DDZ thermal instrument detector based on single chip microcomputer control
Introduction Traditional DDZ type thermal instrument detectors are mainly based on analog display, which has the disadvantages of unstable performance and large errors. In order to solve this problem, a thermal instrument detector based on single chip control was developed. It can not only provide signal source in the
[Microcontroller]
Latest Microcontroller Articles
  • Download from the Internet--ARM Getting Started Notes
    A brief introduction: From today on, the ARM notebook of the rookie is open, and it can be regarded as a place to store these notes. Why publish it? Maybe you are interested in it. In fact, the reason for these notes is ...
  • Learn ARM development(22)
    Turning off and on interrupts Interrupts are an efficient dialogue mechanism, but sometimes you don't want to interrupt the program while it is running. For example, when you are printing something, the program suddenly interrupts and another ...
  • Learn ARM development(21)
    First, declare the task pointer, because it will be used later. Task pointer volatile TASK_TCB* volatile g_pCurrentTask = NULL;volatile TASK_TCB* vol ...
  • Learn ARM development(20)
    With the previous Tick interrupt, the basic task switching conditions are ready. However, this "easterly" is also difficult to understand. Only through continuous practice can we understand it. ...
  • Learn ARM development(19)
    After many days of hard work, I finally got the interrupt working. But in order to allow RTOS to use timer interrupts, what kind of interrupts can be implemented in S3C44B0? There are two methods in S3C44B0. ...
  • Learn ARM development(14)
  • Learn ARM development(15)
  • Learn ARM development(16)
  • Learn ARM development(17)
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号