Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Serial Interfaces (UART: Universal Asynchronous RX/TX) : LPC17xx

Download as pdf or txt
Download as pdf or txt
You are on page 1of 37

LPC17xx

Serial Interfaces

(UART: Universal Asynchronous Rx/Tx)


Serial Interfaces: UARTs 0…3
UART: Universal Asynchronous Rx/Tx

o 4 modules (UART0, 1, 2, 3).


o 16 byte Receive and Transmit FIFOs with DMA support.
o Fractional divider for baud rate control, auto-baud capabilities,
and implementation of software or hardware flow control.
o EIA-485/RS-485 and 9-bit mode support (UART1).
n Allows both software address detection and automatic address
detection using 9-bit mode.
n Auto Direction Control.
o Control line RTS/DTS to enable and disable the driver.
o Address Match Register: store the station address in a RS-485 milti-drop
communication.
o Delay Value register: set the delay between the last stop-bit and the
deassertion of the DIR line.
o Modem control support (UART1).
o IrDA support for infrared communication (UART3).
o Maximum possible speed of the UART ~ 6 Mbps.
UART: Universal Asynchronous Rx/Tx

UART0,2,3
UART: TX Character Framing

o Start Bit.
o Data Bits of 5, 6, 7 or 8.
o Parity Bit.
o Stop Bit of 1, 1.5 or 2.

n Example: 8 bits data, No Parity, 1 stop bit


One frame

Serial port Start b b


3.3V
0 b 2 b 3 b 4 b 5 b 6 b 7 Stop
1 0V
UART: RS-232

o RS-232 is a popular communications interface for connecting modems


and data acquisition devices (i.e. GPS receivers, electronic balances,
data loggers, ...) to computers.
o RS-232 can be plugged straight into the computer’s serial port (know
as COM or Comm port).
o RS-232 Line Driver
n Each signal (TxD, RxD) on the interface connector with reference to a signal ground.
n The “idle” state (MARK) has the signal level negative with respect to common whereas
the active state (SPACE) has the signal level positive respect to the same reference.

LPC176x

+3.3V
0.1µF
16 0.1µF DB9 female
1
0.1µF 3 MAX 2 +5.5V Vss
4
3232 5
6 -5.5V
0.1µF 0.1µF 9
5 4
P0.3 9 8 TxD 8
RxD0 3
P0.2 10 7 RxD 7
TxD0 2
6
15 1
UART: Transmitter

o Parallel-to-serial conversion
o Non-FIFO Mode
n Transmit Holding Register (THR) and Transmit Shift Register (TSR)
o FIFO Mode
n Transmit (TX) FIFO and Transmit Shift Register (TSR)
o 16x timing for bit shifting.
o Character Framing.
o Parity Insertion.
o TX FIFO interrupt and status.
UART: Transmitter

o Non FIFO mode:


n Write Data to Transmit Holding Register (THR).
n Data in THR is transferred to Transmit Shift Register (TSR) when TSR is
empty.
n TSR shifts the data out on the TX output pin.

o FIFO mode:
n Write Data to Transmit Holding
Register (THR).
n Transmit data is queued in TX FIFO.
n Data in TX FIFO is transferred to
Transmit Shift Register (TSR) when TSR is empty.
n TSR shifts data out on TX output pin.
UART: Receiver

o Non FIFO mode:


n Incoming data is received in the Receive Shift Register (RSR).
n Received data is transferred to the RHR.
n Error tags associated with data in RHR can be read via LSR.
n Read RHR to read the
data out.

o FIFO mode:
n Incoming data is received in the
Receive Shift Register (RSR).
n Received data is queued in the RX FIFO.
n Error tags associated with data in
RHR can be read via LSR.
n Read RHR to read the data out.
UART: RX Character Validation

o Start bit detection and validation:


n HIGH to LOW transition indicates a start bit.
n Start bit validated if RX input is still LOW during mid bit sampling.
o Data, parity and stop bits are sampled at mid bit.
o A valid stop bit is HIGH when the stop bit is sampled.
UART: RX Error Reporting

o Line Status errors


n Error tags are associated with each byte:
o Framing error if stop bit is not detected.
o Parity error if parity bit is incorrect.
o Break detected if RX input is LOW for duration of one character time and stop bit
is not detected.
o Overrun error if character is received in RSR when RX FIFO is
full.
n Non-FIFO mode:
o RHR has a data byte and data received in RSR.
o RSR data overwrites RHR data.

n FIFO mode:
o RX FIFO is full and data is received in RSR.
o Data in RX FIFO is not overwritten by data in RSR.
UART: Baudrate

o Configurable baudrate:

n UnDLM, UnDLL are 8 bits data.


n 1 ≤ MULVAL ≤15
Reset
n 0 ≤ DIVADDVAL ≤14
n DIVADDVAL ≤ MULVAL

n Example -> UART0 (19200 baudios)


LPC_UART0->LCR |= DLAB_ENABLE; // importante poner a 1
LPC_UART0->DLM =0;
PCLK [ Hz ] 100 6 / 4
LPC_UART0->DLL =81; U 0 DL16 = = = 81.38
16 ⋅ Vt [baudios ] 16 ⋅ 19200
LPC_UART0->LCR &= ~DLAB_ENABLE;// importante poner a 0
UART: Register Map (I)
UART: Register Map (II)
UART: Rx Buffer Register ,Tx Holding Register
UART: Divisor Latch LSB, MSB Registers

PCLK[Hz] 1006 /4
n Example set baudrate (FR=1): U0DL16 = = = 81
16 ⋅Vt [baudios] 16 ⋅19200
DL=F_pclk/16*baud; // Round to the nearest whole!!!!
LPC_UART0->DLL= DL%256; //LSB
LPC_UART0->DLM= DL/256; //MSB
UART: Interrupt Enable Register
UART: Interrupt Identification Register
UART: Interrupt Handling
UART: FIFO Control Register
UART: Line Control Register
UART: Line Status Register
UART: Line Status Register (continued)
UART: Software example (I)
void uart0_init(int baudrate) {
LPC_UART0->LCR |= DLAB_ENABLE; // importante poner a 1
LPC_PINCON->PINSEL0 = (1 << 4) | (1 << 6); // Change P0.2 and P0.3 mode to TXD0 and RXD0 LPC_UART0->DLM = DL / 256; // parte alta
LPC_UART0->DLL = DL % 256; // parte baja
// Set 8N1 mode (8 bits/dato, sin paridad, y 1 bit de stop)
LPC_UART0->LCR |= CHAR_8_BIT | STOP_1_BIT | PARITY_NONE; LPC_UART0->LCR &= ~DLAB_ENABLE; // importante poner a 0
uart0_set_baudrate(baudrate); // Set the baud rate
if (FR=1)
LPC_UART0->IER = THRE_IRQ_ENABLE | RBR_IRQ_ENABLE; // Enable UART TX and RX interrupt (for LPC17xx UART)
NVIC_EnableIRQ(UART0_IRQn); // Enable the UART interrupt (for Cortex-CM3 NVIC) DL=162.73
} Baudrate= 9585 bps

static int uart0_set_baudrate(unsigned int baudrate) {


int errorStatus = -1; //< Failure

// UART clock (FCCO / PCLK_UART0)


// unsigned int uClk = SystemFrequency / 4;
unsigned int uClk =SystemCoreClock/4;
unsigned int calcBaudrate = 0;
unsigned int temp = 0;

unsigned int mulFracDiv, dividerAddFracDiv;


unsigned int divider = 0;
unsigned int mulFracDivOptimal = 1;
unsigned int dividerAddOptimal = 0;
unsigned int dividerOptimal = 0;

unsigned int relativeError = 0;


unsigned int relativeOptimalError = 100000;

uClk = uClk >> 4; /* div by 16 */

/*
* The formula is :
* BaudRate= uClk * (mulFracDiv/(mulFracDiv+dividerAddFracDiv) / (16 * DLL)
*
* The value of mulFracDiv and dividerAddFracDiv should comply to the following expressions:
* 0 < mulFracDiv <= 15, 0 <= dividerAddFracDiv <= 15
*/
for (mulFracDiv = 1; mulFracDiv <= 15; mulFracDiv++) {
for (dividerAddFracDiv = 0; dividerAddFracDiv <= 15; dividerAddFracDiv++) {
temp = (mulFracDiv * uClk) / (mulFracDiv + dividerAddFracDiv);

divider = temp / baudrate;


if ((temp % baudrate) > (baudrate / 2))
UART: Software example (I)

void tx_cadena_UART0(char *cadena)


{
ptr_tx=cadena;
tx_completa=0;
LPC_UART0->THR = *ptr_tx; // IMPORTANTE: Introducir un carácter al comienzo para iniciar TX o
} // activar flag interrupción por registro transmisor vacio

void UART0_IRQHandler(void) {

switch(LPC_UART0->IIR&0x0E) {

case 0x04: /* RBR, Receiver Buffer Ready */


*ptr_rx=LPC_UART0->RBR; /* lee el dato recibido y lo almacena */
if(*ptr_rx++ ==13){ /* Caracter return --> Cadena completa */
*ptr_rx=0; /* Añadimos el caracter null para tratar los datos recibidos como una cadena*/
rx_completa = 1; /* rx completa */
ptr_rx=buffer; /* puntero al inicio del buffer para nueva recepción */
}
break;

case 0x02: /* THRE, Transmit Holding Register empty */


if(*ptr_tx!=0)
LPC_UART0->THR = *ptr_tx++; /* carga un nuevo dato para ser transmitido */
else
tx_completa=1;
break;
}
}
UART1: Modem
UART1: RS-485

o What is RS-485?
n RS-485 is a EIA standard interface which is very common in the data
acquisition world.

n RS-485 provides balanced transmission line which also can be shared in


Multi-drop mode.

n It allows high data rates communications over long distances in real world
environments.

o How fast can RS-485 be?


n RS-485 was designed for greater distance and higher baudrates than RS-
232.

n According to the standard, 100kbit/s is the maximum speed and distance


up to 4000 feet (1200 meters) can be achieved.
UART1: RS-485 Line Driver

o Balanced Line Drivers


n Voltage produced by the driver appears across a pair of signal wires that
transmit only one signal. Both wires are driven opposite.

n RS-485 driver has always the “Enable” direction control signal.

n Differential system provides noise immunity, because much of the


common mode signal can be rejected by the receiver. So ground shifts and
induced noise signals can be nullified.
UART1: RS-485 Network

o RS-485 provides Half-


Duplex, Multi-drop
communications over MASTER SLAVE-1

a single twisted pair


cable.

SLAVE-2 SLAVE-3
n The standard specifies
up to 32 drivers and
32 receivers can share
a multidrop network.

n Terminator resistors
avoid reflected signal
UART1: RS-485 Half and Full Duplex

Half-Duplex

Full-Duplex
UART1: RS-485 vs RS-232
UART1: RS-485 modes of operation

o Normal Multi-drop Mode (NMM)


o Auto Address Detection (AAD)
o Auto Direction Control
o Output inversion
UART: Devices with RS-232 interface

o GPS Module (VK16E GMOUSE GPS Module SIRF III)

o Built with fast positioning and the


ability to track 20 satellites SIRF III
generation chip.
o Built-in backup battery.
o Built-in high gain LNA.
o With selectable baud rate:
4800,9600,19200,38400
o NMEA format (ASCII):
o RMC, GSV, GSA, GGA, VTG, GLL.
Data example: in Polytechnic School !!!

o The default output for the 9600 baud


standard.
UART: Devices with RS-232 interface

o 2.4G Wireless WIFI Module (TLG10UA03)


• Full support for serial transparent data transfer mode, really Positive Serial Plug and Play.
• The new AT command set, all based on ASCII.
• Complete TCP / IP protocol stack to support DHCP protocol and DNS dynamic IP address assignment
domain name resolution function.
• Built-in WEB server, implemented using IE browser.
• Remote configuration via the wireless network module parameters.
• Supports Frequency range: 2.412 ~ 2.484 GHz.
• Supports two types of wireless networks:
Infrastructure Network (Infra) and ad hoc networks (Adhoc).
• Support multiple security authentication mechanisms:
WEP64/WEP128 / TKIP / CCMP (AES)
WEP/WPA-PSK/WPA2-PSK.
• Support for fast networking.
• Supports wireless roaming.
• Support for multiple network protocols: TCP / UDP / ICMP / DHCP / DNS / HTTP
• Supports automatic two operating mode and command support transparent transmission mode.
• Support AT command set controls.
• Support for a variety of parameter configuration mode: Serial / WEB server / Wireless connection.
UART: Devices with RS-232 interface

o Digital Compass (HMR3200/HMR3300)

• The HMR3200 is a two-axis precision compass with


three orthogonal magnetoresistive sensors, and can
be used in either vertical or horizontal orientations.

• The HMR3300 includes a MEMS accelerometer for a


horizontal three-axis, tilt compensated precision
compass for performance up to a ±60° tilt range.

o Optical Fingerprint Sensor (EM404)

• EM404 integrated fingerprint verification module is


the latest release of HF&CCTV. It consists of optical
fingerprint sensor, high performance DSP processor
and Flash.

• It boasts of functions such as fingerprint enrollment,


fingerprint deletion, fingerprint verification,
fingerprint upload, fingerprint download, etc.
UART: Devices with RS-232 interface

o Camera module (CMOS 1/4 inches Image Sensor JPG)


• Command instruction:
•1 Reset command: 56002600

•2 Photographing command: 5600360100

•3 Read the data length of the captured image: 5600340100


Return: 76 00 34 00 04 00 00 XX YY
XX YY ------- picture data length, XX is the high byte, YY is the low byte.

•4 Read the picture data: 56 00 32 0C 00 0A 00 00 XX XX 00 00 YY YY ZZ ZZ


Return: 7600320000 (interval) FF D8. . . . . FF D9 (interval) 7600320000
00 00 XX XX ------ starting address (starting address must be a multiple of 8, generally 00 00)
00 00 YY YY ------ picture data length (high byte first, then low byte) ZZ ZZ -------- interval (= XX XX * 0.01
milliseconds, preferably a small number, such as 00 0A)
Note: JPEG picture file data must begin with FF D8, and end with FF D9.

•5 Stop shooting: 5600360103

•6 Set camera image compression command: 56 00 31 05 01 01 12 04 XX


XX generally is 36 (range: 00 ---- FF)

•7 Set the camera image size: (default size: 320 * 240)


320 * 240: 56 00 31 05 04 01 00 19 11
640 * 480: 56 00 31 05 04 01 00 19 00

•8 Get into sleep mode: 56 00 3E 03 00 01 01

•9 Modify baud rate: 56 00 24 03 01 XX XX


UART: Devices with RS-232 interface

MLX90614 Infrared Bluetooth to Serial Slave Fingerprint Module Capacitive


Thermometer Module Sensor Em401

TC35 SMS Module Board pH sensor Module (AtlasScientific) NFC Reader Module (pn532)

You might also like