LAB 4: UART
ADMINISTRATIONS
Week 10 (11/19): Term project check point #1
Make
your robot car move
Forward, backward, turn left, turn right
Avoid
Obstacles
UART
UART: Universal Asynchronous Receiver Transmitter
Two
wires: Rx, Tx
Full duplex
Asynchronous
No common clock required
Without common clock
How
do they communicate??
Rx
Tx
Tx
Rx
UNIVERSAL DATA RATES
Two devices agree on same data rate
Baudrate:
second)
1200, 2400, 4800, 9600, , (bits per
A
(1 MHz system clock)
Baudrate
generator
9600
UART
(8 MHz system clock)
UART
9600 Baudrate
generator
Baudrate: 9600
bps
Length of a bit =
1/9600 (seconds)
UART DATA
Send one character at a time
Data
One start bit
Pull-up the line for one or two slots
Simple error checking: parity (optional)
Pull-down the line
7 or 8 bits data
One or two stop bit
This bit became parity bit
when parity checking is
enabled
Even parity: If the data has odd number of 1, parity bit = 1 (make
it even); else parity bit = 0
Odd parity: If the data has even number of 1, parity bit = 1 (make
it odd); else parity bit = 0
Four parameters for UART communication
Baudrate, data-bit, parity, stop-bit
We wrote: 9600 8N1,
Means: baudrate=9600, 8-bit data, no parity, 1 stop bit
APPLICATIONS
All you need to do is properly
configure UART for both sides
(baudrate, data-bit, parity, stop-bit),
and start sending/receiving data
RS232 IC
USB
UART
MSP43
0
Todays
Labs
COM PORT
Other
Sensors
PC or
other
systems
USB
serial
IC
(FT232)
Other
systems
RS485 IC
RS485 IC
UART MODULES ON MSP430
Two UART modules on MSP430F1611
USART0
USART
and USART1
Universal
Synchronous/Asynchronous Receive
Transmit
The USART0 module support 3 protocols
UART
SPI
I2C
The
USART1 module support 2 protocols
UART
SPI
Two protocols cannot operate simultaneously on
one module
UART MODULES ON MSP430
There are
multiplexed with
digital I/O pins,
remember to set
the PxSEL
register properly
SPI
pins
UART pins
I2C pins
USART CONNECTIONS ON TAROKO
Taroko
MSP430F161
1
USART
1
USART
0
Taroko Schematic
The UART pins and I2C
pins are connected to the
expansion connector, so
you can connect external
sensors through UART or
I2C interface on USART0
module
FT232
USB
serial
IC
UART
I2C
Expansion
Connector
(U2)
PC
SP
I
CC2420
Radio IC
The USART0 module is
shared
If you connected an external
sensor through UART/I2C
interface
When you are using radio,
you cannot read sensor
When you are reading
sensor, you cannot use radio
Taroko use SPI on
USART0 module to
control the radio
IC.
UART MODE
Three or more devices (multiprocessor format)
Character Format
Tx
Rx
Tx
Rx
Tx
Rx
Rx
Tx
Rx
Tx
Two devices
Seldom use,
we wouldnt
talk about it
For
multiprocessor
format, not use
Usually we use
8-bit data
UART RECEIVE AND TRANSMIT
Receive
Transmit
Set URXEx= 1 to
enable the
receiver
Set UTXEx= 1 to
enable the
transmitter
When the module
detects a valid start
bit, it will start
collect the rest of
the bits
When data is
written to the
transmit buffer
(UxTXBUF), a
transmission start
When all the bits are
collected, the received
character will be
written to a buffer
register (UxRXBUF)
and issues an
interrupt.
To transmit another byte. You MUST
wait until the previous data has
transmitted.
If it is ready to transmit
A flag UTXIFGx will set to 1
Two ways to detect
An Interrupt
A while loop to check the flag
BAUD RATE GENERATION
UART clock source frequency must larger than (3*baud
rate)!
DCO (SMCLK)
UART Clock
Source
32768 Hz
(ACLK)
Desire baud
rate
For Example:
UART Clock Source =
32768
Desire baud rate = 2400
Divider = 32768/2400 =
Fractional
13.653
part
UxBR = 13,
=
0.653
The divider setting controlled by three 8-bit registers
Divide
r
UxBR0, UxBR1, and UxMCTL
The equation
UxBR = (UxBR1 << 8) + UxBR0
Total bits
1 start, 8 data, 1 parity, 1 stop
11 bits in total (n = 11)
Integer
part
MODULATIONS
UxMCTL determine the fractional part
Bit
0
Bit
7
UxMCTL 0 1 1 0 1 0 1 1
m7
m6
m5
m4
m3 m2
m10
m1 m0
m9 m8
1 n 1
mi 0.653
n i 0
For Example:
UART Clock Source =
32768
Desire baud rate = 2400
Divider = 32768/2400 =
13.653
UxBR = 13,
=
0.653
You knowthe
0.653 n 7 of mi should be 7
msummation
n 1
But
i 0
what is the right combination
DETERMINING THE UXMCTL
Individual bit error
Baud rate, BRCLK, and UxBR are known
Choose a combination of m0 m7
For
UxMCTL
Bit
0 1 1 0 1 0 1 01
Bit
7
each bit, calculate the bit error
m
m
m
Find the maximum bit error among all the
bits
7
m4
m3 m2
m10
This is the bit error of this combination
Choose another combination and calculate again
Select the combination that has minimum bit
error
m1 m0
m9 m8
EXAMPLE
n 1
m
i 0
Bit
0
Bit
7
0 1 1 0 1 0 1 1
m0
m8
m1
m9
m2 m3
m10
m4
Useful resource!!!
m5
m6
m7
0.653 n 7
Bit
0
Bit
7
1 0 1 1 0 1 0 1
m0
m8
http://mspgcc.sourceforge.net/baudrate.html
m1
m9
m2 m3
m10
m4
m5
m6
m7
UART INTERRUPTS
Two interrupt vectors
Transmission interrupt vector
Reception interrupt vector
Transmission interrupt
When UxTXBUF (transmit buffer) is ready to accept another character
(previous transmission is complete)
The interrupt request is clear automatically when
An interrupt request is generated
UTXIFGx interrupt flag is set
The request is serviced
Or, a character is written to UxTXBUF
Reception interrupt
When a character is received
An interrupt request is generated
URXIFGx interrupt flag is set
The interrupt request is clear automatically when
The request is serviced
Or, UxRXBUF is read
WAIT FOR TRANSMISSION
transmit another byte. You MUST wait
until the previous data has transmitted
To
Two ways to do
By a while loop
By Interrupt
1.Enable transmit interrupt
2.When it is ready to transmit
another byte, it will generate
an interrupt request
3.Transmit next byte in the
ISR
Program will
branch to the ISR,
need extra CPU
cycles
1.No matter you enable or
disable the transmit interrupt,
the UTXIFGx will set to 1 when it
is ready to transmit another byte
2.Use a while loop to check the
flag
While(!(IFG1 & UTXIFG0)
3.Transmit next byte after the
while loop
Could possibly hang
in the while loop
UXCTL
You MUST held
UART logic in reset
state when you
setting the UART
registers
UXTCTL
Select
clock
source
If you want to
disable the
transmitter, wait
until this bit is set
to 1
UXRCTL
Error Detection
Framing
error
A low stop bit detected (stop bit must
be high)
Parity
error
When you enable parity check, an
parity error detected
Overrun
error
A new character received before
previous character has been read
Break
detect
More than 10 bits of zero detected
OTHER REGISTERS
Baud rate control registers
UxBR0,
UxBR1, and UxMCTL
Receive and transmit buffer
UxRXBUF
and UxTXBUF
Module enable registers
Enable/disable
For UART0
transmitter and receiver
For UART1
INTERRUPT REGISTERS
Interrupt enable registers
Interrupt
flag registers
For UART0
For UART0
For UART1
For UART1
INITIALIZATION/RECONFIGURATION UART
There are specific process flow to follow
1.
2.
3.
4.
5.
Set SWRST
Initialize all UART registers when SWRST=1
Enable UART module (ME1 or ME2)
Clear SWRST (SWRST=0)
Enable interrupts (optional)
Failure to follow this process may result in
unpredictable USART behavior
PROBLEMS WITH CLOCK SOURCE
To generate appropriate baud rate, you must know the
frequency of UART clock source
Two clock source avaible
ACLK = 32768 Hz
SMCLK = DCO (frequency??)
UART clock source frequency must larger than (3*baud
rate)!
For 32768 clock source, max baud rate is 10922 (9600)
Problems
What if you need faster baud rate
Error increase when baud rate is close to clock rate
We need faster clock source
SET DCO
We talk about how to adjust DCO frequency in lab 2
But it is not very accurate
We can use a more accurate clock to set the frequency of
DCO precisely
Use timer capture mode
Latch the current timer counter value into TxCCRx register
Use an accurate clock as a capture source
Example:
Timer clock source = DCO
Use DCO as timer clock source
Capture source = ACLK/8 = 32768/8 = 4096
Capture at every
raising edge
Capture_
1
When a capture occurred,
the value of timer counter is
copy to the TxCCRx register
Capture_2
You can adjust the DCO by setting the
registers until you get the desire frequency
you want
Capture
source
Timer Clock source
Counter increase at
every raising edge
Hz
If Capture_1 = 3000
Capture_2 = 3250
Freq. of DCO = (3250 3000) * 4096 =
1024000 Hz
You know the frequency of capture source.
frequency of timer clock source
= (Capture_2 Capture_1) * Freq. of capture
sourcev
TODAYS LABS
Lab 1: Set DCO
You dont have to write the routine by yourself, use the sample code
on course webpage (sample code form TI)
The routine sets the ACLK to 32768/8 = 4096 Hz
It use a variable DELTA to set the frequency of DCO
DONT set the DCO too high or too low (check datasheet)
Use program in previous lab
See the LED flashed at a certain rate
If DCO = 1000000 Hz
i = 125000
The LED should flash approximately
every second
int is 16-bit long
Set DCO
This while loop takes
exactly 11 cycles for 16bit operation
LAB 2: SEND TO PC
Use the sample file on course webpage
Setting UART registers
UART setting
Baudrate
= 57600, 8-bit data, no parity, 1 stop bit
Use the program in Lab 3, sense the infrared
sensor
Send the distance value to your computer
Use a terminal program (realterm) to receive the
values
LAB 3: RECEIVE FROM PC
From the terminal program on PC, send the
following byte sequences (in Hexidecimal) to
Taroko
5A
A5 53 54 41 52 54 0D 0A
5A A5 53 4C 45 45 50 0D 0A
UART setting
Baudrate
= 9600, 8-bit data, no parity, 1 stop bit
Configure your Taroko to receive these two
sequence correctly
TAROKO USB DRIVER
Download Taroko USB virtual com port driver
http://www.ftdichip.com/Drivers/CDM/CDM%202.02.04.exe
Install driver
Plug Taroko to you computers USB port, you should see
Device
manager
TERMINAL PROGRAM
Realterm
http://realterm.sourceforge.net/
Download Realterm program
http://sourceforge.net/project/showfiles.php?group_id=67297
Install the program
Open the program by this way
Enter
baud rate
Click Start-up -> click run
"C:\Program Files\BEL\Realterm\realterm.exe" baud=9600
port=63
Enter
COM
port
REALTERM
Displa
y
format
REALTERM
UART
setting
Click here can
open/close the
port
REALTERM
Capture data
to a file
REALTERM
Send out
data