Discuss the implementation method of PIC microcontroller software asynchronous serial port

Publisher:名字太长了吗Latest update time:2011-10-21 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

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.

Reference address:Discuss the implementation method of PIC microcontroller software asynchronous serial port

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

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号