6. Communication

Embed Size (px)

Citation preview

  • 8/3/2019 6. Communication

    1/18

    Microcontrollers.

    Communication:AVR ATmega32Joel Castillo Gmez

  • 8/3/2019 6. Communication

    2/18

    Communication

    Another important feature of microcontrollers is the ability to

    communicate with other devices:

  • 8/3/2019 6. Communication

    3/18

    USART

    To do this the microcontroller has a protocol called USART (Universal

    Synchronous and Asynchronous serial Receiver and Transmitter)

    Its main features are:

    - Full Duplex Operation (Can receive/transmit at the same time)- Asynchronous or Synchronous

    - High Resolution Baud Rate Generator (minimum error)

    - 5 to 9 Data Bits, 1 or 2 Stop Bits and Odd or Even Parity

    - 2X Speed Asynchronous Comm Mode

    - 3 Interrupt sources. (Tx Complete, Rx Complete, TxReg[UDR] Empty)

    We will review its characteristics and setup.

  • 8/3/2019 6. Communication

    4/18

    USART

    USART Structure

    The Data:

    UDR

    Configuration & Status Registers:

    UCSRA UCSRB UCSRC*

    Error Detection /

    Double Speed /

    Multi-processor

    Mode

    Receive Enable /Transmit Enable

    (RXEN, TXEN)

    Receive PinRxD

    (PD0)

    Transmit Pin

    TxD

    (PD1)

    Baud Rate:

    UBRRH*

    UBRRL

    Ext. Clock

    Pin - XCK

    (PB0)

    Status Flags

    (Bits 7, 6 y 5)

    RXC, TXC, UDRE

    Interrupt Enable:

    (Bits 7, 6 y 5)

    RXCIE, TXCIE, UDRIE

    Bit 8

    (RXB8,

    TXB8)

    UCSZ2**

    Register

    Select*

    (URSEL)

    1=UCSRC

    0= UBRRH

    Mode

    Select

    (UMSEL)

    0=Asynch.

    Char Size

    UCSZ1, UCSZ0 **ucsz2,ucsz1,ucz0=(0,1,0)

    = 8 bit

    Parity

    Mode(UPM1,

    UPM0)

    Stop Bit(s)Select

    (USBS)

  • 8/3/2019 6. Communication

    5/18

    USART

  • 8/3/2019 6. Communication

    6/18

    USART

    Baud Rate Configurator

    Config & Status Reg A

    Config & Status Reg B

    USART Data

    Config & Status Reg C

  • 8/3/2019 6. Communication

    7/18

    USART

    How is the information sent/received in UDR?

    UCSZ2, UCSZ1, UCSZ0

    UPM1, UPM0

    USBS

    Configured trough:

    Velocity or Speed of the Frame is

    configured trough UBRRH & UBRRL

    Note: Parity checks the

    number or 1s in the Data.

    In even parity, when thereare even number of 1s, the

    parity bit is 0. The opposite

    for odd parity.

  • 8/3/2019 6. Communication

    8/18

    USART Configuration

    Baud Rate Calculation:

  • 8/3/2019 6. Communication

    9/18

    USART Configuration

    Common Baud Rates @ 8Mhz:

  • 8/3/2019 6. Communication

    10/18

    USART Configuration

    USART mode Select:

    Parity Mode:

    Stop Bit Select:

  • 8/3/2019 6. Communication

    11/18

    USART Configuration

    USART Character Size:

  • 8/3/2019 6. Communication

    12/18

    USART Error Checking

    UCSRA USART Control and Status Register A

    Inside this register we have 3 bits for Error Checking:

    FE*: This bit is set when there is no STOP bit detected.

    DOR: This bit is set when the buffer (2 characters in UDR + 1 char inthe receive Shift Register) are full, and at the same time there hasbeen a Start bit detected.

    PE*: This bit is set when the Parity (if it is enabled) did not match atthe end of the reception.

    * Note: This 2 bits apply to the next character to be read (in UDR)

  • 8/3/2019 6. Communication

    13/18

    USART Asynchronous

    We will review the Asynchronous Mode of operation to show

    how to communicate with another device:

    - In this mode, only Tx and Rx pins are needed (no XCK) since

    there is no Synchronization.

  • 8/3/2019 6. Communication

    14/18

    USART Asynchronous

    So, if we wanted to communicate (just transmit) with another device @9600 bps and the frame format = (8 bits, Even Parity, 1 Stop bit):

    1. [UCSRC] Set UMSEL properly to get Asynchronous mode:UMSEL = 0

    2. [UCSRC & UCSRB*] Configure the frame format:UCSZ2*, UCSZ1, UCSZ0 = 3 or (0, 1, 1) -> 8-bits data. *Note, no need to write UCSRB

    UPM1, UPM0 = 2 or (1, 0) -> Even parity.

    USBS = 0 -> 1 Stop bit

    3. [UBRRH & UBRRL] Calculate & Load the BaudRate:UBBR = (8,000,000 Hz) / (16 * 9600) 1 = 51.0833 OR

    UBBR = (8,000,000 Hz) / (8 * 9600) 1 = 103.166

    UBBR = 103 (Note: Real BAUD = 9615.38 bps & U2X = 1 in UCSRA) orUBBR = 51 (Note: Real BAUD = 9615.38 bps & U2X = 0 in UCSRA)

    4. Remember to make URSEL = 1 in [UCSRC] in order to write to UCSRC.(Note: If URSEL bit = 0, the contents will be written to UBRRH)

    5. [UCSRB] Finally, enable the transmit. [Note: To receive make RXEN = 1]TXEN = 1

  • 8/3/2019 6. Communication

    15/18

    USART Asynchronous

    Now that we have the configuration lets make the following

    program:

  • 8/3/2019 6. Communication

    16/18

    USART Asynchronous

    Note that we do NOT

    do any error checking,

    since we are only

    transmitting.

  • 8/3/2019 6. Communication

    17/18

    USART Asynchronous

    Activities:

    - Configure the USART @ 1 Mbps (8 bits, odd parity & 1 stop

    bit) & transmit an incrementing register every 100 ms (using

    interrupts to count the 100 ms).

  • 8/3/2019 6. Communication

    18/18

    Reference:

    ATmega32(L) (Rev. 2503P-AVR-07/10)