Académique Documents
Professionnel Documents
Culture Documents
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)
UCSRA
Status Flags (Bits 7, 6 y 5) RXC, TXC, UDRE
UCSRC*
Register Select* (URSEL) 1=UCSRC 0= UBRRH
UCSZ2**
USART
USART
Baud Rate Configurator
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.
USART Configuration
Baud Rate Calculation:
USART Configuration
Common Baud Rates @ 8Mhz:
USART Configuration
USART mode Select:
Parity Mode:
USART Configuration
USART Character Size:
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
3.
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)