When developing various embedded application systems with single-chip microcomputers, asynchronous serial communication is a communication mode that is often used. In many applications, it is also required to realize multi-channel asynchronous serial communication. Most of the single-chip microcomputers of various manufacturers that we are familiar with usually only provide one hardware UART module on the chip, which can be used to easily realize one-channel serial communication. The PIC series of single-chip microcomputers are no exception. Among its rich product family members, except for some models of the high-end series (PIC17/18) with two-channel hardware UART modules on the chip, most other models have only one UART on the chip, and some low-end and cheap PIC single-chip microcomputers do not even have hardware UART. In order to improve the performance-price ratio of the system, design engineers are required to use software to increase the realization of one or more channels of asynchronous serial communication. Many engineers are skeptical about the reliability and efficiency of UART implemented by software. In fact, the key issue is to see how the software uses to achieve reliable UART functions.
Before discussing the specific implementation method, let's briefly review the format definition of asynchronous serial communication. To send a complete byte of information, there must be a "start bit", "several data bits", "parity bit" and "stop bit"; the time width of each bit of information must be defined - the number of information bits sent per second, which is the "baud rate". The baud rates commonly used in microcontroller systems range from 300 to 19,200 b/s. When the baud rate is 1200b/s, the time width of each information bit is 1/1200≈833μs; when there is no data communication, the idle state of the data line should be high, the "start bit" is low, the low bit of the data bit is sent first and followed by the parity bit (if any), and the "stop bit" is high, as shown in Figure 1.
Figure 1
According to the most basic asynchronous serial communication timing in Figure 1, there are many ways to implement UART in software on microcontrollers of different architectures. Data reception is the key, because asynchronous communication has no reference clock signal, the sender may send data at any time, and the system should receive serial data in a timely and accurate manner at any time when the serial data arrives. In comparison, it is relatively easy for this machine to send serial data, as long as the duration of the sent level is timed. According to different receiving techniques and the characteristics of PIC microcontrollers, two commonly used and very reliable methods are introduced here.
1 Triple-speed sampling method
As the name implies, the triple-speed sampling method samples the receiving pin Rx at a frequency three times the baud rate to ensure that the "start bit" is detected, and the sampling time interval can be adjusted; the sampling point of the valid data bit is controlled at the middle 1/3 of the code element to minimize bit errors and improve the accuracy of reception. We enlarge the start bit and some data bits in Figure 1, as shown in Figure 2, and divide each information bit into three equal parts, with the time width of each part set to ts for easy analysis.
Figure 2
When the information bits are sampled at a triple frequency, each information bit will likely be sampled three times. When in idle state and detecting the start bit, the earliest moment when the start bit is detected at a low level will fall in the S0 shadow area, although the specific sampling point will change randomly in this S0 shadow area each time. After the start bit is detected at a low level, the interval of 4×ts is exactly the middle 1/3 of the first data bit (Ds shadow area in Figure 2). The sampling intervals of the data bits, check bits, and stop bits thereafter are all 3×ts, and all sampling points fall in the middle 1/3 of the code element, making the sampled data the most reliable.
When the PIC microcontroller uses this method to implement software UART, the hardware only needs to define two I/O pins arbitrarily and initialize them as input (serial data reception) and output (serial data transmission) respectively; the software only needs to implement timed sampling. The time interval can be realized by using different timers (TMR0, TMR1, TMR2, etc.) through timed interrupts on mid-range and above microcontrollers with interrupt mechanisms, and can be realized by controlling the time consumed by each main cycle on low-end PIC microcontrollers without interrupts. For a baud rate of 1200 b/s, the code element width is 833μs, and the sampling time interval is 278μs. The entire serial reception or transmission is a process control problem, which is most efficient and simple to implement using a state machine. Figure 3 shows the reference state machine transfer process of serial reception.
Figure 3
The online supplement of this journal (www.dpj.com.cn) introduces a simple C language reference source program. This program implements 1200b/s full-duplex serial communication, 1 start bit, 8 data bits, no parity bit, 1 stop bit, no frame error, etc. The compilation environment is HITECH-PICC compiler V8.00PL4 or higher.
In the program of the network supplement version, the key part is the interrupt service of TMR0. TMR0 interrupts once every 278μs or so, and the interrupt response of TMR0 is the realization of the full-duplex communication process of software UART receiving and sending. After the efficient code compilation of Hitech-PICC, there are about 150 single-word instruction codes, and the entire interrupt service takes about 35 instruction cycles on average, that is, one software UART occupies about 12% of the operating bandwidth of the MCU at a working frequency of 4 MHz. In theory, as long as the MCU has enough operating bandwidth for other tasks, the receiving and sending codes are copied one or more times (independent data structure) in this interrupt service program, and multi-channel software UART can be realized. Of course, if the baud rate of each channel is different, the sampling frequency must be three times the highest baud rate. The sampling point intervals of different baud rates are adjusted independently.
The biggest advantage of this method is that the hardware and software configuration is extremely flexible: the receiving and sending pins can be defined arbitrarily; the sampling timing can be realized by different timers; using the same timing sampling, it is convenient to realize multiple software UARTs, etc. The disadvantages are: regardless of whether there is data communication or not, it always occupies the MCU operation bandwidth; the baud rate of serial communication cannot be too high, and the PIC microcontroller working at 4 MHz can generally achieve 2400bps full-duplex communication. Of course, high baud rate communication can be achieved by increasing the oscillation frequency of the MCU. When the PIC microcontroller works at 20 MHz, it is more than enough to achieve 9600b/s.
2 Start bit interrupt capture, timing sampling method
The hardware condition for implementing this method is that the PIC microcontroller has an external pulse falling edge interrupt trigger function. In mid-range and above PIC microcontrollers, there are RB0/INT external interrupt pins, CCP1/CCP2 pulse edge capture pins, PORTB 4/5/6/7 level change interrupt pins, etc., which can meet the requirements. In addition, a timer is required to correctly sample the received code element or send a serial data stream in a timed interrupt mode. The key asynchronous reception working principle is shown in Figure 4.
Figure 4
Assume that the serial data bit width is td. The falling edge of the start bit (point A in Figure 4) triggers an interrupt and responds to the interrupt immediately. In this interrupt service, immediately turn off the interrupt enable bit (subsequent data stream changes do not need to trigger an interrupt), turn on the timer, and generate a timer interrupt after 1.5td to sample the first data bit (make sure that the S0 sampling point falls at the center of the data bit); at the end of processing the falling edge interrupt service, check whether the receiving end is still at 0 level to distinguish narrow pulse interference. All sampling intervals after the first data bit is sampled at S0 are 1td, until the stop bit is received, turn off the timer interrupt, reopen the falling edge capture interrupt, and prepare to receive the next byte.
The state machine control flow of asynchronous data reception and transmission is almost the same except that the start bit judgment and timing time parameter settings are different from the above methods, and will not be repeated here.
The advantage of this method is that it can achieve a higher communication baud rate. For systems that do not communicate frequently, this software UART consumes almost no MCU operating bandwidth, and 9600b/s reception or transmission can be easily implemented on a PIC microcontroller running at 4 MHz; in addition, since the falling edge interrupt can wake up the sleeping microcontroller, it is very easy to implement the communication wake-up function. The disadvantage is that full-duplex communication is not possible (unless a separate timer is used to implement the transmission timing), and the asynchronous receiving pin must have the ability to trigger the falling edge interrupt.
The two methods introduced above have been well verified in actual product design, the most typical of which is the infrared automatic meter reading system. This system requires 38 kHz infrared modulation for both transmission and reception, and 1 200bps half-duplex communication for serial data. When this UART is implemented with software and the pulse width modulation PWM output of the 38 kHz carrier of the CCP module of the PIC microcontroller is fully utilized, all design requirements can be met except for an integrated infrared receiver and an infrared emitting diode outside the microcontroller, without any other peripheral devices, thus minimizing hardware design, reducing costs, and improving system reliability and performance-price ratio.
The above focuses on the introduction of the basic principles, and I hope it will be helpful to everyone. There is not much discussion on the reliability processing of received data. Those who are interested can sample the data multiple times when the sampling time arrives to eliminate interference errors; or if you have other processing techniques, you are welcome to communicate with the author further.
Previous article:High-precision data acquisition device based on PIC series microcontroller
Next article:Application of Single Chip Microcomputer in Automatic Blood Pressure Monitoring System
- 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
- How to build a simple and efficient isolated power supply below 2W
- Six common circuit board surface treatment processes
- [N32L43X Review] 1. Getting Started Environment Configuration
- Tektronix THz Radar Signal Test Solution
- [RISC-V MCU CH32V103 Review] - 5: Failed to leave API and enter Assembly, returned to API......
- BQ76940 sampling chip damage issue
- [RVB2601 Creative Application Development] LoRa Gateway Wireless Host Computer
- Newcomer Report
- There is a program with a total of 10 feeders
- Funny point, STM32 settings