UART to Serial Terminal(转载)

  前一篇《UART Explained》介绍了UART的基本信息,重点分析了UART的信号。本文摘录的文章则重点介绍了波特率(Baud Rate)相关的内容,波特率越高,传输速度越快,但实际使用时波特率是越高越好吗,多少合适?文中给出了答案,具体如下。

  Although the PIC32 is an elegant and powerful microcontroller, it doesn't stand so tall when compared against a PC in terms of raw computing power! Sometimes it makes sense to use the feasability of the microcontroller to work as a sensor or an embedded controller of some type but still be able to communicate with a more powerful computer. Sometimes you might just want a quick visual display or a printf terminal for debugging. Whatever the reason, this tutorial will provide a basic guide to PIC32 / PC communication.

UART

  The module used for such communication is the PIC's Universal Asynchronous Receiver/Transmitter (UART) pronounced "you-art". For those familiar with computer music, the MIDI protocol uses a UART to do its serial communication. For those familiar with the Arduino, the TX/RX pins on an Arduino are the two main UART pins. In its most basic form the UART hardware consists of a transmit (TX) line and a receive (RX) line. The software configures how fast data is sent (the baud rate) and the specifics of the protocol.

Baud Rate

  Since the UART module is asynchronous, there is no external clock line like the synchronous protocols SPI or I2C. Instead, both communicating devices must have their own clock sources configured to the same frequency. As long as these clocks match one another, data should be transferred smoothly. On the PIC32, the UxBRG special function register configures this rate called the baud rate. Note that the x in UxBRG is a placeholder for the reference number of the UART that is being configured. Common baud rates include 4800, 9600, 19200, 57600, and 115200 bits/s. Different baud rates can be chosen by the PIC32 but you must always make sure both devices are very stable at the selected rate. Sometimes it's best to select a lower baud rate to reduce the chance of errors. Since there is no shared clock, the hardware is simpler but there is an increased chance for the clocks to be out of sync causing incorrect data to be interpreted on the receiving end. For this reason, lower baud rates like 9600 are very popular.

  The equation to configure the baud rate using the UxBRG register is below:

8N1

  Like with most peripherals, the configuration options can be overwhelming! We'll be configuring our UART to use the standard 8N1 format. 8N1 meaning that each data trasfer consists of 8 bits of data, no parity bits, and 1 stop bit.

  The parity bit provides an added layer of error detection. The parity bit is generated by the transmitting unit through a calculation done with the data being sent. If the receiving unit then calculates the same parity bit from the received data, it is assumed that everything is A-O-K. If a different parity bit is calculated, an error is triggered. For example, if odd parity checking is used, the transmitting unit would add up all "1" bits from the data being sent and set the parity bit to 1 if they are odd (like 01001100) or to 0 if even (like 01001101). The receiving unit would use the same calculation and compare the received parity bit to the calculated parity bit. Note that parity checking is done in hardware by the UART unit. However, once a parity error is flagged by the hardware, the programmer must deal with the error in software. Since parity checking will only take you so far (two corrupted bits is treated as no error!), you may desire something more powerful like a Cyclic Redundancy Check (CRC) which is outside the scope of this tutorial.

  The stop bit signifies the end of a data transfer. There will almost always be either one or two stop bits. If the stop bits read by the receiving unit don't match those expected, the receiver will assume its clock drifted out of phase and a the software must deal with this error. It is also implied that a start bit is being sent as well with each transmission. The start bit allows the clock on the receiving end to do a phase lock with the data being received. Otherwise, the receiving end might end up sampling on the data transition edges instead of right in the middle of each bit. The data received would then look like utter nonsense!

  So, in an 8N1 configuration, each byte of data sent is transmitted in the form of a 10-bit frame with the addition of one start bit, one stop bit, and no parity bits.

  The example below configures the PIC32's UART1 module to an 8N1 format with a baud rate of 19200 bits/s and enables both transmit and receive functions (in full duplex mode).


int FPb = 40000000; // peripheral bus clock frequency set at max SYSCLK

int desired_baud = 19200;

U1MODE = 0; // disable autobaud, TX and RX enabled only, 8N1, idle=HIGH

U1STA = 0x1400; // enable TX and RX

U1BRG = (FPb / (16*desired_baud)) - 1

   

U1MODESET = 0x8000; // enable UART1

  Note that in the example above, we are using the standard baud mode set whenUxMODE<3> = 0. The UART clock is only able to take on specific frequencies and this bit helps determine how close the desired baud rate will be to the actual baud rate. When BRGH = 1 (i.e. UxMODE<3> = 1), high-speed mode is activated and the actual baud rate will probably be closer to the desired baud rate. However, each bit received on the UxRX pin will only be sampled once. When BRGH = 0 (i.e. UxMODE<3> = 0), multiple samples are taken on the UxRX pin to determine whether a HIGH or LOW is present. Even though the high-speed mode may have a more accurate baud rate clock, in standard-mode there is less chance to make receiving errors. See the UART reference manual section 21.6 for more information.

  The following table provides a set of desired baud rates, the value you would set BRGH to for this baud rate and the error.

  可以看到,实际波特率和目标波特率会有一些误差,如果通信双方的误差都有点大,那就很容易导致通信错误,所以很多串口通信的协议里都会有校验码,如GPS协议,通过数据帧的校验码可以过滤掉因传输导致的错误,避免异常。

  原文中还有参考代码和调试串口的神器,具体参见: http://umassamherstm5.org/tech-tutorials/pic32-tutorials/pic32mx220-tutorials/uart-to-serial-terminal

  另外,有关串口通信时序错误的详细解析,可参考:http://www.robotroom.com/Asynchronous-Serial-Communication-2.html(好似被墙,pdf档在此)

  波特率计算工具:http://mspgcc.sourceforge.net/baudrate.html

时间: 2024-07-31 00:53:29

UART to Serial Terminal(转载)的相关文章

RS-232 vs. TTL Serial Communication(转载)

RS-232串口一度像现在的USB接口一样,是PC的标准接口,用来连接打印机.Modem和其他一些外设.后来逐渐被USB接口所取代,现在PC上已经看不到它的身影了.开发调试时如果用到串口,一般都是用USB转串口头,如下图所示.左图为USB-to-RS-232,右图为USB-to-TTL.USB-to-RS-232和USB-to-TTL因电气特性不同,所以应用场合也不同,不能互换.本文所转载的文章,重点介绍了RS-232和TTL串口的异同. One of the tools we use most

OpenRisc入门(9)-基于or1200最小sopc系统搭建(三)--串口

最近在弄openrisc,之前有人在弄,并且写了一篇master thesis,我已经上传了: http://download.csdn.net/detail/rill_zhen/5303401 下面的内容应该就是根据论文中的指导完成的,但是,不是我完成的,所以转载如下: 接上一篇(原创)基于or1200最小sopc系统搭建(二)--QuartuII工程及DE2平台下载现再为构建的or1200最小系统添加上串口.先进行仿真,再在DE2上验证,在hyperterminal上显示hello worl

Oracle hang 之sqlplus -prelim使用方法

第一章  Oracle hang 之sqlplus -prelim使用方法  很多情况下,Oracle hang导致sqlplus无法连接,从而无法获得Oracle系统和进程状态,使得定位问题缺少强有力的依据.所幸的是Oracle 10g推出了sqlplus -prelim选项,在Oracle挂起时依然能使用sqlplus,从而能获得数据库状态. 使用方法如下 引用 $ sqlplus -prelim "/as sysdba" SQL*Plus: Release 10.2.0.4.0

Oracle的v$process性能视图

1.表结构 SQL> desc v$process 名称                                      是否为空? 类型 ----------------------------------------- -------- ---------------------------- ADDR                                               RAW(8) PID                                  

Oracle数据库编译存储过程假死问题

Oracle数据库教程编译存储过程假死问题 这种情况下如果强行终止存储过程编译,再次Recompile存储过程会发现还是挂死的,这个主要是由于强行终止后会话为INACTIVE状态,但是该会话却没有被真正的释放.使用如下语句查询出挂起的会话: SELECT V.OSUSER, V.PROCESS, V.PROGRAM, v.MACHINE, V.TERMINAL, V.MODULE, V.USERNAME, V.STATUS, V.SID, V.SERIAL#, 'alter system kil

Solaris SPARC Boot Sequence

整理自网络 介绍Solaris系统启动的过程The following represents a summary of the boot process for a Solaris 2.x system on Sparc hardware. Power On: Depending on the system involved, you may see some output on a serial terminal immediately after power on. This may tak

0624使用10035事件跟踪无法执行的sql语句

[20160624]使用10035事件跟踪无法执行的sql语句.txt --昨天看一份awr报表,链接如下: http://www.itpub.net/thread-2061952-1-1.html --摘要如下: Top 10 Foreground Events by Total Wait Time Event                              Waits     Total Wait Time (sec)    Wait Avg(ms)  % DB time    W

[20121214]数据库错误记录.txt

[20121214]数据库错误记录.txt --自己参照许多文档,写了一个检测数据库错误的例子:--以sys用户登录,建立表CREATE TABLE SYS.ERROR_LOG(  IP_ADDRESS       VARCHAR2(30 BYTE),  USERNAME         VARCHAR2(30 BYTE),  INSTANCE_NUMBER  NUMBER(2),  THEDATE          DATE,  ERRORMSG         VARCHAR2(4000 B

RS-232, RS-422, RS-485 Serial Communication General Concepts(转载)

前面转载的几篇文章重点介绍了UART及RS-232.在工控领域除了RS-232以外,常用的串行通信还有RS-485.本文转载的文章重点介绍了RS-232.RS-422和RS-485. Overview This article explains the general concepts of the serial communication protocols RS-232, RS-422, and RS-485, including basic concepts like baud rate,