Title: Lab 4: UART
1Lab 4 UART
2Administrations
- Week 10 (11/19) Term project check point 1
- Make your robot car move
- Forward, backward, turn left, turn right
- Avoid Obstacles
3UART
- UART Universal Asynchronous Receiver Transmitter
- Two wires Rx, Tx
- Full duplex
- Asynchronous
- No common clock required
- Without common clock
- How do they communicate??
A Rx Tx
B Tx Rx
4Universal data rates
- Two devices agree on same data rate
- Baudrate 1200, 2400, 4800, 9600, , (bits per
second)
A (1 MHz system clock)
B (8 MHz system clock)
9600
9600
Baudrate generator
Baudrate generator
UART
UART
Baudrate 9600 bps
Length of a bit 1/9600 (seconds)
5UART Data
This bit became parity bit when parity checking
is enabled
- Send one character at a time
- Data
- One start bit
- Pull-down the line
- 7 or 8 bits data
- One or two stop bit
- Pull-up the line for one or two slots
- Simple error checking parity (optional)
- 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 baudrate9600, 8-bit data, no parity, 1
stop bit
6Applications
All you need to do is properly configure UART for
both sides (baudrate, data-bit, parity,
stop-bit), and start sending/receiving data
Other Sensors
PC or other systems
COM PORT
RS232 IC
Todays Labs
MSP430
USB
UART
USB serial IC (FT232)
Other systems
RS485 IC
RS485 IC
7UART Modules On MSP430
- Two UART modules on MSP430F1611
- USART0 and USART1
- USART
- 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
8UART Modules On MSP430
There are multiplexed with digital I/O pins,
remember to set the PxSEL register properly
SPI pins
UART pins
I2C pins
9USART Connections On Taroko
Taroko
MSP430F1611
FT232 USB serial IC
PC
USART1
USART0
UART I2C
SPI
Expansion Connector (U2)
CC2420 Radio IC
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
Taroko use SPI on USART0 module to control the
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
10UART Mode
- Two devices
- Three or more devices (multiprocessor format)
- Character Format
A Rx Tx
B Tx Rx
Seldom use, we wouldnt talk about it
A Rx Tx
B Tx Rx
C Tx Rx
For multiprocessor format, not use
Usually we use 8-bit data
11UART Receive and Transmit
When all the bits are collected, the received
character will be written to a buffer register
(UxRXBUF) and issues an interrupt.
When the module detects a valid start bit, it
will start collect the rest of the bits
Set URXEx 1 to enable the receiver
- 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
When data is written to the transmit buffer
(UxTXBUF), a transmission start
Set UTXEx 1 to enable the transmitter
12Baud Rate Generation
UART clock source frequency must larger than
(3baud rate)!
DCO (SMCLK)
Divider
UART Clock Source
Desire baud rate
32768 Hz (ACLK)
- For Example
- UART Clock Source 32768
- Desire baud rate 2400
- Divider 32768/2400 13.653
- UxBR 13, 0.653
- The divider setting controlled by three 8-bit
registers - UxBR0, UxBR1, and UxMCTL
- The equation
- UxBR (UxBR1 ltlt 8) UxBR0
- Total bits
- 1 start, 8 data, 1 parity, 1 stop
- 11 bits in total (n 11)
Integer part
Fractional part
13Modulations
- UxMCTL determine the fractional part
- You know the summation of mi should be 7
- But what is the right combination
- For Example
- UART Clock Source 32768
- Desire baud rate 2400
- Divider 32768/2400 13.653
- UxBR 13, 0.653
Bit 0
Bit 7
0 1 1 0 1 0 1 1
UxMCTL
m0
m1
m2
m3
m4
m5
m6
m7
m8
m9
m10
14Determining the UxMCTL
- Individual bit error
- Baud rate, BRCLK, and UxBR are known
- Choose a combination of m0 m7
- For each bit, calculate the bit error
- Find the maximum bit error among all the bits
- This is the bit error of this combination
- Choose another combination and calculate again
- Select the combination that has minimum bit error
UxMCTL
Bit 0
Bit 7
0 1 1 0 1 0 1 1
m0
m1
m2
m3
m4
m5
m6
m7
m8
m9
m10
15Example
Bit 7
Bit 0
Bit 7
Bit 0
1 0 1 1 0 1 0 1
0 1 1 0 1 0 1 1
m0
m1
m2
m3
m4
m5
m6
m7
m0
m1
m2
m3
m4
m5
m6
m7
m8
m9
m10
m8
m9
m10
1 2.54
2 -2.25
3 0.29
4 2.83
5 -1.95
6 0.59
7 -4.20
8 -1.66
9 0.88
10 -3.91
11 -1.37
- Useful resource!!!
- http//mspgcc.sourceforge.net/baudrate.html
16UART 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) - An interrupt request is generated
- UTXIFGx interrupt flag is set
- The interrupt request is clear automatically when
- 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
17Wait For Transmission
- To transmit another byte. You MUST wait until the
previous data has transmitted - Two ways to do
- By a while loop
- No matter you enable or disable the transmit
interrupt, the UTXIFGx will set to 1 when it is
ready to transmit another byte - Use a while loop to check the flag
- While(!(IFG1 UTXIFG0)
- Transmit next byte after the while loop
- By Interrupt
- Enable transmit interrupt
- When it is ready to transmit another byte, it
will generate an interrupt request - Transmit next byte in the ISR
Program will branch to the ISR, need extra CPU
cycles
Could possibly hang in the while loop
18UxCTL
You MUST held UART logic in reset state when you
setting the UART registers
19UxTCTL
Select clock source
If you want to disable the transmitter, wait
until this bit is set to 1
20UxRCTL
Error Detection 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
21Other Registers
- Baud rate control registers
- UxBR0, UxBR1, and UxMCTL
- Receive and transmit buffer
- UxRXBUF and UxTXBUF
- Module enable registers
- Enable/disable transmitter and receiver
For UART0
For UART1
22Interrupt Registers
- Interrupt enable registers
- Interrupt flag registers
For UART0
For UART1
For UART0
For UART1
23Initialization/Re-configuration UART
- There are specific process flow to follow
- Set SWRST
- Initialize all UART registers when SWRST1
- Enable UART module (ME1 or ME2)
- Clear SWRST (SWRST0)
- Enable interrupts (optional)
- Failure to follow this process may result in
unpredictable USART behavior
24Problems 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
(3baud 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
25Set 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
- Use DCO as timer clock source
Example Timer clock source DCO Capture source
ACLK/8 32768/8 4096 Hz If Capture_1
3000 Capture_2 3250 Freq. of DCO (3250
3000) 4096 1024000 Hz You can adjust the DCO
by setting the registers until you get the desire
frequency you want
When a capture occurred, the value of timer
counter is copy to the TxCCRx register
Capture_1
Capture at every raising edge
Capture_2
Capture source
Timer Clock source
You know the frequency of capture source.
frequency of timer clock source (Capture_2
Capture_1) Freq. of capture sourcev
Counter increase at every raising edge
26Todays 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
16-bit operation
27Lab 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
28Lab 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
29Taroko USB driver
- Download Taroko USB virtual com port driver
- http//www.ftdichip.com/Drivers/CDM/CDM202.02.04.
exe - Install driver
- Plug Taroko to you computers USB port, you
should see
Device manager
30Terminal Program
- Realterm
- http//realterm.sourceforge.net/
- Download Realterm program
- http//sourceforge.net/project/showfiles.php?group
_id67297 - Install the program
- Open the program by this way
- Click Start-up -gt click run
- "C\Program Files\BEL\Realterm\realterm.exe"
baud9600 port63
Enter baud rate
Enter COM port
31Realterm
Display format
32Realterm
Click here can open/close the port
UART setting
33Realterm
Capture data to a file
34Realterm
Send out data