1 / 10

Using the USART

Using the USART. Configuration and Programming. Configuring the USART. Several I/O registers are used to: Control the configuration of the USART Baud rate (bits/s) Parity (even/odd/none) Number of data bits (4-9) Framing ( 1 or 2 stop bits)

pravat
Télécharger la présentation

Using the USART

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Using the USART Configuration and Programming CE-2810 Dr. Mark L. Hornick

  2. Configuring the USART Several I/O registers are used to: • Control the configuration of the USART • Baud rate (bits/s) • Parity (even/odd/none) • Number of data bits (4-9) • Framing ( 1 or 2 stop bits) • Represent the status of a recent transmission or reception • Frame errors • Data overrun errors • Tx complete • Rx complete CS-280 Dr. Mark L. Hornick

  3. Lots of I/O registers make configuring the USART a bit complicated: UCSRA – USART Control/Status Register A Mainly status flags UCSRB – USART Control/Status Register B Flags for enabling transmitting, receiving, interrupts UCSRC – USART Control/Status Register C Mode selection UBRRH, UBRRL– USART Baud Rate Registers (16 bit combined) Baud rate selection UDR– USART Data Register Does two things at once: • Contains data to be transmitted, or • Data that is received Full documentation on the USARTcan be found (around p 146) of theAtmega32.pdf reference manual CE-2810 Dr. Mark L. Hornick

  4. UBRRL/UBRRH are used to set transmit and receive speed URSEL – Register select UCSRC I/O address overlaps with address of UBRRH. Set to 0 if writing this Register as UBRRH; otherwise UCSRC UBRR11:0 – Baud Rate select 12-bit register specifying the USART transmit/receive baud rate. Use the following table for calculating the value to place in these bits. BAUD is in bits/sec, fosc is the Atmega32 clock frequency. CE-2810 Dr. Mark L. Hornick

  5. UCSRB/C serve multiple purposes UCSZ2, UCSZ1, UCSZ0 – Character size select Together, these set the number of data bits being transmitted or received Set these to (0:1:1) for 8 data bits; see reference manual for other settings URSEL– Register select UCSRC I/O address overlaps with address of UBRRH. Set to 1 when using this Register as UCSRC; otherwise UBRRH UMSEL– Mode select Set to 0 for Asynchronous mode UPM1, UPM0 – Parity mode select Parity disabled (0:0), Even (1:0), Odd (1:1), Reserved (0:1) USBS– Stop bit select Set to 0 for framing using a single Stop Bit UCPOL– Clock polarity (do not use) CE-2810 Dr. Mark L. Hornick

  6. UCSRB/C continued RXCIE – Receive complete interrupt enable Set to enable Receive Complete Interrupt. TXCIE – Transmit complete interrupt enable Set to enable Transmit Complete Interrupt. UDRIE – Data Register Empty interrupt enable Set to enable UDRE Interrupt. RXEN– Receiver Enable Set to enable the USART Receiver TXEN– Transmitter Enable Set to enable the USART Transmitter UCSZ2 – see previous slide explaining UCSRC bits • RXB8 – contains the 9th data bit when receiving 9-bit data • TXB8 – contains the 9th data bit when transmitting 9-bit data CE-2810 Dr. Mark L. Hornick

  7. UCSRA contains status bits you read RXC – Receive complete Set when data is received in the Receive Buffer (RXB) TXC – Transmit complete Set when data in the Transmit Buffer has been completely transmitted and no new data in UDR; manually cleared by writing a 1 to this bit UDRE – Data Register Empty Set when Transmit Buffer is ready to accept new data to be transmitted FE – Frame Error Set when Data in Receive Buffer had a frame error detected DOR – Data Overrun Set when Receive Buffer is full (but unread), and new data is coming in PE – Parity Error Set when Data in Receive Buffer had a parity error detected (if parity checking was enabled) U2X – Transmission speed doubler Leave at 0 (default). When explicitly set to 1, doubles the transmission speed of the USART MPCM– Multiprocessor Comm mode Leave at 0 (default) for normal communication CE-2810 Dr. Mark L. Hornick

  8. The UDR register contains the value the USART transmits or receives • UDR– USART Data Register • TXB and RXB I/O addresses overlap • When UDR is written, the contents are shifted to the Transmit Buffer (TXB). This takes a short period of time • When UDR is read, the contents of the Receive Buffer (RXB) are first shifted into UDR, so that UDR contains the contents of RXB CE-2810 Dr. Mark L. Hornick

  9. How to use the USART • Set baud rate via UBRRH and UBRRL • Enable Transmit and Receive via UCSRB • Disable all USART interrupts via UCSRB • Set Parity, #Data bits, #Stop bits, Async via UCSRC • To Transmit: • Poll UDRE bit (bit 5) in USCRA until set (ready to transmit) • Once set, place character to be written in UDR register, which causes the USART hardware to transmit that character • To Receive: • Poll RXC bit in UCSRA until set (character was received) • Once set, received char will be in UDR register, and the UDR register will be able to be read. CS-280 Dr. Mark L. Hornick

  10. The USART can receive while it is transmitting But the TXB and RXB buffers can each hold only one character • So you must keep polling RXC to avoid dropping received characters • And you must poll UDRE before sending another character to ensure that TXC has been emptied CS-280 Dr. Mark L. Hornick

More Related