http://extremeelectronics.co.in/avr-tutorials/using-the-usart-of-avr-microcontrollers/
▲레지스터 정리
▲Reading and Writing Data
(▼AVR Studio 기반)
.
.
.
USART of AVR Microcontrollers.
*UDR – USART Data Register
: Actually this is not one but two register but when you read it you will get the data stored in receive buffer and when you write data to it goes into the transmitters buffer. This important to remember it.
*UCSRA – USART Control and status Register A
: As the name suggests it is used to configure the USART and it also stores some status about the USART. There are two more of this kind the UCSRB and UCSRC.
:이름이 제안하듯이, 이건 사용된다. to 구성하기위해 the USART를
*UBRRH and UBRRL
: This is the USART Baud rate register, it is 16BIT wide so UBRRH is the High Byte and UBRRL is Low byte. But as we are using C language it is directly available as UBRR and compiler manages the 16BIT access.
★ATmega128에서는 UBRR0H와 UBRR0L로 바꿔준다.
So the connection of AVR and its internal USART can be visualized as follows.
▲USART에서 사용되는 레지스터들
UDR, UCSRA, UCSRB, UCSRC, UBRRH, UBRRL.
★BaudRate설정관련 비트는 (링크) 참고,......UBRRH와 UBRRL을 사용한다.
Registers Explained
In order to write programs that uses the USART
you need to understand what each register’s importance.
레지스터의 뭐가 중요한지 이해해야한다.
The scheme behind using the AVR USART
is same as with any other internal peripheral (say ADC).
So if you are new to this topic please see this tutorial,
it shows you the basic idea of using peripherals.
I am not going to repeat what is already there in the datasheets,
I will just tell about what is required for a quick startup.
The datasheets of AVR
provides you
with all the details of every bit of every register
so please refer to it for detailed info.
Note bit names with RED background are of our interest here.
UDR : Explained above.
2개의 레지스터로 구성되어있다.
읽으면 data가 저장된다. in Receive Buffer에.
쓰면 data가 빠져나간다. into the Transmit Buffer에.
UCSRA : USART Control and Status Register A
▼아래 비트들은 임무가 완료되면 각각 Set(=1) 된다.
그리고 program이 UDR을 새로 작성한다.
RXC : this bit is set when the USART has completed receiving a byte
from the host
(may be your PC) and the program should read it from UDR
TXC : this bit set (1) when the USART has completed transmitting a byte
to the host
and your program can write new data to USART via UDR
▼데이터시트에서 뽑아온 UCSRnA 정보이다.
RXCn::This flag bit is set when there are unread data in the receive buffer and cleared when the receive buffer is empty.
TXCn::The TXCn flag bit is automatically cleared when a transmit complete interrupt is executed.
UDREn::(USART Data Register Empty)::If UDREn is one, the buffer is empty
FEn::(Frame Error)::Always set this bit to zero when writing to UCSRnA.
DORn::(Data OverRun)::
A data overrun occurs when the receive buffer is full (two characters)
Always set this bit to zero when writing to UCSRnA.
UPEn: Parity Error
UCSRB: USART Control and Status Register B
RXCIE: Receive Complete Interrupt Enable – When this bit is written one the associated interrupt is enabled.
이 비트가 씌였을때 1로, the 할당된 인터럽트가 Enable 된다. (특수한 경우에 쓰임)
TXCIE: Transmit Complete Interrupt Enable – When this bit is written one the associated interrupt is enabled.
이 비트가 씌였을때 1로, the 할당된 인터럽트가 Enable 된다. (특수한 경우에 쓰임)
RXEN: Receiver Enable - When you write this bit to 1 the USART receiver is enabled.
이 비트가 씌였을때 1로, USART 리시버가 enabled된다.
The normal port functionality of RX pin will be overridden.
So you see that the associated I/O pin now switch to its secondary function,i.e. RX for USART.
이 비트를 쓰면 to 1로, the USART리시버는 enabled된다.
TXEN: Transmitter Enable – As the name says!
UCSZ2: USART Character Size – Discussed later.
For our first example we won’t be using interrupts so we set UCSRB as follows
UCSRB=(1<<RXEN)|(1<<TXEN);
UCSRC: USART Control And Status Register C
IMPORTANT : The UCSRC and the UBRRH (discussed below) register
shares same address
so to determine which register user want to write
is decided with the 7th(last) bit of data
if its 1 then the data is written to UCSRC else it goes to UBRRH.
This seventh bit is called the
URSEL: USART Register Select.
UMSEL: USART Mode Select
– This bit selects between asynchronous and synchronous mode.
As asynchronous mode is more popular with USART
we will be using that.
USBS: USART Stop Bit Select
– This bit selects the number of stop bits in the data transfer.
UCSZ: USART Character size
– These three bits (one in the UCSRB)
selects the number of bits of data
that is transmited in each frame.
Normally the unit of data in MCU
is 8BIT (C type "char")
and this is most widely used
so we will go for this.
Otherwise you can select 5,6,7,8 or 9 bit frames!
So we set UCSRC as follows
UCSRC=(1<<URSEL)|(3<<UCSZ0);
UBRR: USART Baud Rate Register:
This is the USART Baud rate register, it is 16BIT wide
so UBRRH is the High Byte and UBRRL is Low byte.
But as we are using C language
it is directly available as UBRR and compiler manages the 16BIT access.
This register is used by the USART to generate the data transmission at specified speed (say 9600Bps).
To know about baud rate see the previous tutorial. UBRR value is calculated according to following formula.
Where fosc is your CPU frequency say 16MHz
Baud Rate is the required communication speed say 19200 bps (see previous tutorial for more info).
Example:
For above configuration our UBRR value comes to be 51.083 so we have to set
UBRR=51;
in our initialization section. Note UBRR can hold only integer value. So it is better to use the baud rates that give UBRR value that are purely integer or very close to it. So if your UBRR value comes to be 7.68 and you decided to use UBRR=8 then it has high error percentage, and communication is unreliable!
You may also use our Androd App for calculating the UBRR much easily ! It can run on Smartphones and Tablets running Android OS.