Actions

Difference between revisions of "AVR serial communications"

From Just in Time

(Created page with 'Notes on getting serial communications to work on AVR. ==Registers== ;UCSRB: (USART Control and Status Register B) In this register, set RXEN to enable serial reception and TXEN …')
 
Line 1: Line 1:
 
Notes on getting serial communications to work on AVR.
 
Notes on getting serial communications to work on AVR.
 
==Registers==
 
==Registers==
;UCSRB: (USART Control and Status Register B) In this register, set RXEN to enable serial reception and TXEN to enable serial transmission.
+
;Enabling USART functions: Set RXEN and TXEN in '''UCSRB''' (USART Control and Status Register B) to enable serial reception.
;UBRR: (USART Baud Rate Register) Sets the baud rate. The baud rate is f<sub>osc</sub>/(UBBR+1) '''divided''' by 2, 8 (U2X = 1) or 16 (U2X = 0). U2X is located in UCSRA.
+
;Setting the baudrate: '''UBRR''' (USART Baud Rate Register) Sets the baud rate. The baud rate is f<sub>osc</sub>/(UBBR+1) '''divided''' by 2, 8 (U2X = 1) or 16 (U2X = 0). U2X is located in '''UCSRA'''.
 +
;Choosing the frame format: UCSZ0 - UCSZ1 (in '''UCSRC'''), and  UCSZ2 (in '''UCSRB''') can be used to set the number of databits (USART Character Size: 5, 6, 7, 8, ''reserved'',  ''reserved'', ''reserved'',9) . UPM0 - UPM1 (USART Parity mode: none, ''reserved'', even, odd) and USBS (USART Stop Bit Select: one bit, two bit) are in '''UCSRC'''. Default is ''8n1''. '''Note''': on atmega16, al writes to UCSRC must have bit ''URSEL'' set, otherwise the write will redirect to the UBRRH register.

Revision as of 00:39, 3 December 2009

Notes on getting serial communications to work on AVR.

Registers

Enabling USART functions
Set RXEN and TXEN in UCSRB (USART Control and Status Register B) to enable serial reception.
Setting the baudrate
UBRR (USART Baud Rate Register) Sets the baud rate. The baud rate is fosc/(UBBR+1) divided by 2, 8 (U2X = 1) or 16 (U2X = 0). U2X is located in UCSRA.
Choosing the frame format
UCSZ0 - UCSZ1 (in UCSRC), and UCSZ2 (in UCSRB) can be used to set the number of databits (USART Character Size: 5, 6, 7, 8, reserved, reserved, reserved,9) . UPM0 - UPM1 (USART Parity mode: none, reserved, even, odd) and USBS (USART Stop Bit Select: one bit, two bit) are in UCSRC. Default is 8n1. Note: on atmega16, al writes to UCSRC must have bit URSEL set, otherwise the write will redirect to the UBRRH register.