Serial Communication
Thareendhra Keerthi Wijayasiriwardhane
         Department of Industrial Management
         Faculty of Science
         University of Kelaniya
         thareen@kln.ac.lk
Learning Outcomes
   On completion of this session, you should be able to
       Discuss serial vs. parallel communications in the context
        of embedded systems
       Contrast asynchronous vs. synchronous communications
       Explain asynchronous data transfer protocol
       Explain need of Line Drivers/USART to USB interfaces
       Explain how Tx and Rx transmit and receive data
       Explain the functionality of USART registers
       Construct a microcontroller-based system and program it
        to communicate with another system
                                                                2
Serial Communication
   Used by embedded systems to communicate with other
    systems
       What are other systems?
       Sends 1 bit at a time
       Why not parallel?
                                                     3
USART
   A peripheral device that implements both
    asynchronous and synchronous serial communications
       Asynchronous
       Synchronous
   Why separate
    peripheral
    device for data
    communication?
                                                         4
Asynchronous Data Transfer
 Each character is framed between a start bit and a stop
  bit and then transmitted
 Why baud rate is important?
 How can receiver recognize transmitted characters?
                                                            5
Connection over Serial Port
   Using a Line Driver
       Why not connect directly?
                                    6
Arduino Severino
   ATmega328P with MAX232
                             7
Connection over USB Port
   Using a UART to USB interface controller
                                               8
Arduino Duemilanove
   ATmega328P
    with FT232RL
                      9
Connection over USB Port
   Using another Microcontroller as UART to USB
    interface controller
                                                   10
Arduino Uno
   ATmega328P
    with ATmega16U2
                      11
Asynchronous Transmitter
    Which flag (UDRE or TXC) gives maximum throughput?
    What’s the purpose of TXC flag?
                                                     12
Asynchronous Receiver
    What’s the role of Pin Control?
    What’s the need of Shift Registers?
                                           13
USART Registers
   UBRRn (USART Baud Rate Register n)
       Used to set baud rate
       16-bit register but only 12 bits are used
                                                    14
USART Registers
   UDRn (USART Data Register n)
       Refers to Transmit Data Buffer that holds the character
        to be transferred to Transmit Shift Register
       Also refers to Receive Data Buffer that holds the
        character transferred from Receive Shift Register
                                                                  15
USART Registers
   UCSRnA (USART Control and Status Register n A)
       RXCn (Receive Complete) flag bit is set when UDRn
        contains unread data
       TXCn (Transmit Complete) flag bit is set when entire
        frame in Transmit Shift Register has been shifted out and
        UDRn is empty
       UDREn (USART Data Register Empty) flag bit is set
        when UDRn becomes empty
                                                                16
USART Registers
   UCSRnB (USART Control and Status Register n B)
       RXCIEn (Receive Complete Interrupt Enable) bit and
        TXCIEn (Transmit Complete Interrupt Enable) bit
        enable interrupts on RXCn and TXCn flags, respectively
       UDRIEn (USART Data Register empty Interrupt
        Enable) bit enables interrupt on UDREn flag
       RXENn (Receiver ENable) and TXENn (Transmitter
        ENable) bits enable RX and TX, respectively
           What is the need of enabling and disabling RX and TX?
                                                                    17
USART Registers
   UCSRnC (USART Control and Status Register n C)
       UCSZn0 (USART Character SiZe), UCSZn1 bits and
        UCSZn2 bit of UCSRnB specify character size for
        communication
                UCSZn2   UCSZn1   UCSZn0   Character Size
                  0        0        0          5-bit
                  0        0        1          6-bit
                  0        1        0          7-bit
                  0        1        1          8-bit
                  1        1        1          9-bit
                                                            18
            System 16
charEncloser - Receives characters
    from PC, encloses them with
   square brackets and transmits
  enclosed characters back to PC
                                     19
ATmega328P USART Pinout
                          20
Hardware
   Schematic Diagram
                        21
Hardware
   Wiring Diagram
                     22
Firmware
   Algorithm
                23
Setting Baud Rate
   Select a standard baud rate from 2400, 4800, 9600,
    14400, 19200, 28800, 38400, 57600, 76800, 115200,
    230400, 460800, to 921600 bps
       Why not other values?
   Set the baud rate
       Determine and
        set the value of UBRR
       Why do you need to add UL suffix?
                                                         24
Baud Rate Error
   Determine actual baud rate
   Calculate baud rate error
 What would happen if a baud rate
  with higher error % is used?
 How to minimize baud rate error?
                                     25
C Program
            26
Extending IDE with a Terminal
   What’s a
    Terminal
    Emulator?
                                27
Communicating over Terminal
                             What are CR
                              and LF?
                                        28
Firmware
   Algorithm for Arduino
                            29
Firmware
   Source Code (using Arduino Wrapper Functions)
                                                    30
Serial Monitor
   Terminal emulator of Arduino IDE
                                       31
            System 17
  charEncloserUsingInterrupts -
   Receives characters from PC,
encloses them with square brackets
 and transmits enclosed characters
    back to PC using interrupts
                                 32
Firmware
   Algorithm
                33
C Program
            34
Using Labeled Constants
                          35
Questions
            36