In developing embedded applications using SCM system, the asynchronous serial communication is usually used as a communication mode, many applications also require multi-way asynchronous serial communication. We usually familiar with the various manufacturers of microcontrollers, most of the chip provides a hardware UART display module only, use it all the way serial communication can be easily achieved. PIC MCU is no exception, in their rich family members, in addition to high-end series (PIC17/18) some models with a two-chip hardware UART modules, most of the other models only way chip UART, some low-end cheap The PIC microcontroller is not even with hardware UART. In order to improve system performance and low cost to increase by software engineers to design all the way or way to achieve asynchronous serial communication. Many engineers use the UART software reliability and efficiency in the skeptical, in fact, the software key issue is to look at the manner in which to achieve reliable UART function.
In the discussion of specific implementations, we first take a brief look at the definition of asynchronous serial communication format. Send a full byte of information, there must be "start bit", "number of data bits," "parity bit" and "stop bit"; must define the time width of each message - to send the information bits per second the number is "baud rate." SCM system commonly used baud rates from 300 ~ 19 200 b / s. When the baud rate of 1200b / s, each information bit of the time width of 1 / 1200 ≈ 833μs; no data communication, data line idle state should be high, "start bit" is low, data followed by a low starting position and the parity bit (if any), "stop bit" for the high
As the name suggests is a three-speed sampling frequency to three times the baud rate sampling of the receiver Rx pin to ensure the detection of the "start bit", but also can adjust the sampling time interval; will effectively control the data bits in the symbol sampling points the middle 1 / 3 to minimize errors, improve receiver accuracy. Figure 1 we start bit and enlarge the part of the data shown in Figure 2, to be divided into three equal parts of each information bit, set the width of each equal parts of time ts, to facilitate analysis.
The third harmonic sampling bits of information, each information bit will be sampled to three. When in idle state and detects the start bit, the earliest detected time of the start bit low S0 shadow will fall, although each specific sampling points in the shadow of this S0 random changes. After the start bit is detected low, the time interval of 4 × ts, exactly in the middle of the first data bit 1 / 3 (shaded areas in Figure 2 Ds). After the data bits, parity bit and stop bit sampling interval is 3 × ts, all the sampling sites, fall symbol of the middle 1 / 3, the sampling data are most reliable.
PIC microcontrollers implement software UART using this method, the hardware as long as the arbitrary definition of two I / O pins, respectively, initialized to the input (serial data receiving) and output (serial data to send) to; software as long as the realization of time sampling time interval over there in the mid-range microcontroller interrupt mechanism can use different timers (TMR0, TMR1, TMR2, etc.) achieved through the timer interrupt, in the low-end PIC microcontroller without interruption of the main loop to control the consumption per time to achieve. For the 1200 b / s baud rate, symbol width of 833μs, the sampling interval is the 278μs. Receive or send the serial is a process control problem, with the state machine to achieve the most efficient way to easy. Figure 3 shows the reference receiver state machine serial transfer.
Journal of the network to add Edition, we introduce a simple C-language reference source. This paragraph Program for 1200b / s full-duplex serial communication, a start bit, 8 data bits, no parity, 1 stop bit, no frame errors discrimination. Compiler environment HITECH-PICC compiler V8.00PL4 or later.
Additional version of the program in the network, the key part of TMR0 interrupt service. TMR0 interrupt every 278μs time around, TMR0 interrupt response is the software UART receive and transmit full-duplex communication process implementation. By Hitech-PICC efficient code compiled, about 150 word instruction code, the interrupt service an average of about 35 instruction cycles, that is, the way the software UART to achieve operating frequencies in the 4 MHz MCU takes to run about 12% of the bandwidth. Theoretically, as long as the operation of MCU with sufficient bandwidth to other tasks, in this interrupt service routine within the code and then receive and send a copy or copies (data structure independent), can achieve multi-channel software UART. Of course, if different baud rate per channel, the sampling frequency must be three times the highest baud rate. Interval between sampling points of different independent baud rate adjustment.
The greatest advantage of this method is extremely flexible hardware and software configurations: pins can be sent to receive any definition; sampling time can be achieved with different timers; using the same sampling time can easily achieve multiple software UART and so on. Drawback is: no matter whether the data communication, and always take MCU operating bandwidth; serial communication baud rate is not too high, 4 MHz PIC microcontroller generally work to achieve 2400bps full duplex communication. Of course, you can increase the oscillation frequency of the MCU for high baud rate communication, when operating in 20 MHz PIC microcontroller, the realization of 9600b / s more than enough.
没有评论:
发表评论