Vous êtes sur la page 1sur 18

Microcontrollers.

Communication:
AVR ATmega32
Joel Castillo Gmez

Communication
Another important feature of microcontrollers is the ability to communicate with other devices:

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.

USART
USART Structure
Ext. Clock Pin - XCK (PB0)

Baud Rate: UBRRH* UBRRL

Receive Pin RxD (PD0)

The Data: UDR


Transmit Pin TxD (PD1)

UCSRA
Status Flags (Bits 7, 6 y 5) RXC, TXC, UDRE

Configuration & Status Registers: UCSRB


Interrupt Enable: (Bits 7, 6 y 5) RXCIE, TXCIE, UDRIE Receive Enable / Transmit Enable (RXEN, TXEN)
Bit 8 (RXB8, TXB8)

UCSRC*
Register Select* (URSEL) 1=UCSRC 0= UBRRH

Mode Select (UMSEL) 0=Asynch. Stop Bit(s) Select (USBS)

Error Detection / Double Speed / Multi-processor Mode

Parity Mode (UPM1, UPM0)

UCSZ2**

Char Size UCSZ1, UCSZ0 **


ucsz2,ucsz1,ucz0=(0,1,0) = 8 bit

USART

USART
Baud Rate Configurator

Config & Status Reg A

Config & Status Reg B


Config & Status Reg C

USART Data

USART
How is the information sent/received in UDR?

Note: Parity checks the number or 1s in the Data. In even parity, when there are even number of 1s, the parity bit is 0. The opposite for odd parity.

Configured trough: UCSZ2, UCSZ1, UCSZ0 UPM1, UPM0 USBS

Velocity or Speed of the Frame is configured trough UBRRH & UBRRL

USART Configuration
Baud Rate Calculation:

USART Configuration
Common Baud Rates @ 8Mhz:

USART Configuration
USART mode Select:

Parity Mode:

Stop Bit Select:

USART Configuration
USART Character Size:

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 in the receive Shift Register) are full, and at the same time there has been a Start bit detected. PE*: This bit is set when the Parity (if it is enabled) did not match at the end of the reception.
* Note: This 2 bits apply to the next character to be read (in UDR)

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.

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. 2. [UCSRC] Set UMSEL properly to get Asynchronous mode:
UMSEL = 0

[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) or UBBR = 51 (Note: Real BAUD = 9615.38 bps & U2X = 0 in UCSRA)

4.
5.

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) [UCSRB] Finally, enable the transmit. [Note: To receive make RXEN = 1]
TXEN = 1

USART Asynchronous
Now that we have the configuration lets make the following program:

USART Asynchronous

Note that we do NOT do any error checking, since we are only transmitting.

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).

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

Vous aimerez peut-être aussi