This blog is intended for fresh Engineering graduates who are interested in Embedded systems. This is in continuation of last blog. Please SUBSCRIBE to YouTube channel Embedkari for further discussion on Embedded Systems. I recommend to use laptop/PC or at least Tablet for watching related YouTube video.
Universal Synchronous Asynchronous Receiver Transmitter
USART is the simple but very important to understand communication process between two devices. Here is the bit stream format
I am taking the case of Asynchronous USART i.e UART. We need to configure character size, Number of stop bits, Pairity
How USART works in AVR ?
- Single write buffer and two level receive buffer
- UDR0(0xc6) —> TXB and RXB —>UDR0(0xc6)
- The transmit buffer UDR can only be written when the UDRE0 (USART Data Register Empty)Flag in the UCSR0A register is set.
- The TXC flag (UCSRnA.TXC) can be used to check that the transmitter has completed all transfers, and The UCSRnA.TXC must be cleared before each transmission(before UDRn is written) if it is used for this purpose.
- the RXC flag can be used to check that there are no unread data in the receive buffer.
- Clock recovery to detect START bit. Data recovery follows it. Both depends on sampling process.
- As per Arduino-UNO board schematic system frequency fosc=16MHz
- For Async : DDR_XCKn=0,UCPOLn=0,UMSELn=0
USART Register Summary (Extended I/O register space )
The extended I/O memory from address 64 to 255 can only be reached by data addressing, not I/O addressing.
USART Control and Status Registers
- UCSR0A : Status bits, U2X0=1 to double transmission speed in Asynchronous mode
- Frame Error, Data Over Run bits for Receive error detection.
- MPCM(Multi Processor Communication Mode) can be set for Master-Slave multi-processor communication. It may use either extra STOP bit or 9th data bit for making two frame types i.e. First one Address and second data. If slave don’t see the first frame matching to its address, It will ignore data frame
- UCSR0B : Interrupt,Transmitter,Receiver Enable bits, UCSZ02 for character size, Ninth Tx/Rx data bits
- The USART transmitter is enabled by setting the Transmit Enable (TXEN) bit in the UCSRnB register. When the transmitter is enabled, the normal port operation of the TxDn pin is overridden by the USART and given the function as the transmitter’s serial output. Same thing applies for RXD
- UCSR0C :
- USBS0 decides number of stop bits
USART Baud Rate 0 Register USBRR0
USBRRn 12 bits are used for baud rate .
- UCSR0A =0x2 i.e. U2X0=1 double transmission speed
- so clock after divider =16MHz/8 =2Mhz
- UCSR0B=0xB8 = 1011_1000 i.e. All interrupts except transmit complete are enabled
- UCSR0C=0x6 =0000_0110 i.e. UPMSEL0=00(Async) , USB0=0(One stop bit), UPM=00(No Parity)
- USSZ=011 (char size=8 bit)
- USBRR0 : USBR0H | USBR0L = 0x0000_0000_0001_0000
- Formula UBRRn= ( fosc/(8*BAUD) -1)
- fosc=16Mhz ,BAUD=115200 UBRRn= (16000000/(8*115200) -1)=16.36
- We can round-off UBRR0=16 and that matches with the programmed value
- Device datasheet also provides commonly used configuration table
References : All documents are available here. I am referring to most of these :
- Arduino Programming notebook
- Inline Assembler cookbook
- AVR Instruction set reference manual
- ATmega328P datasheet
- ATmega328P datasheet -Automotive version
- Arduino Uno board schematic
Thanks for reading till end. I am trying to improve usability of my site. Did you find this discussion helpful ? If so, Please subscribe to YouTube channel Embedkari as well for additional embedded related stuff.