# MPLAB programing: Questions on USART initialization code

I have just started using MPLAB for microchip Atmega128. I had a few questions regarding the following code segment for USART initialization which I found in the product documentation.

1. Do I need to declare variables like, URRH, URRL, UCSRB at the start of the program. The documentation didn't specify this but the compiler is giving me

" 'UBRRH' undeclared (first use in this function) "
error.

UCSRB = (1<<RXEN)|(1<<TXEN);

1. As I understand this, RXEN and TXEN are supposed to be 1 bit each, but then how is the above expression valid?
#define FOSC 1843200// Clock Speed
#define BAUD 9600
#define MYUBRR FOSC/16/BAUD-1

void main( void )
{
...
USART_Init ( MYUBRR );
...
}
void USART_Init( unsigned int ubrr )
{
/* Set baud rate */
UBRRH = (unsigned char)(ubrr>>8);
UBRRL = (unsigned char)ubrr;
/* Enable receiver and transmitter */
UCSRB = (1<<RXEN)|(1<<TXEN);
/* Set frame format: 8data, 2stop bit */
UCSRC = (1<<USBS)|(3<<UCSZ0);
}

• You may have to #include the correct header file where these variables are defined. Please see any sample programs that come with the IDE to know which one to include. Otherwise, search the installation folder for .h files to see if any file like atmega128.h header file is present. – AJN May 24 at 7:20
• I searched the net with keywords mplab atmega ubrrh include file and found two useful pages which give a clue to what include file to use. This page gives step by step procedure to start a new project with an automatically filled out template code file. This one gives a link to code to UART usage. The header file missing in your code seems to be avr/io.h. – AJN May 24 at 10:52
• You had to include: xc.h – Mike May 25 at 4:34
• Kindly respond to the comments, and accept an answer as solution once it solved your problem. That's how this site works, – Mitu Raj May 27 at 6:58

Do I need to declare variables like, URRH, URRL, UCSRB at the start of the program

These are likely to be pre-defined macros defined in a header file in the compiler library corresponding to the microcontroller you target. You have to find that header file and include it in your application code.

UCSRB = (1<<RXEN)|(1<<TXEN);

As I understand this, RXEN and TXEN are supposed to be 1 bit each, but then how is the above expression valid

These should be pre-defined macros as well, which represent the bit positions of RXEN and TXEN bits in the register UCSRB.

The expression UCSRB = (1<<RXEN)|(1<<TXEN) is used in the code to set the bits RXEN and TXEN in the register.

Suppose RXEN and TXEN are in the 0th and 1st bit positions, you have to write 0b00000011 to UCSRB register to enable both Receiver and Transmitter. The above expression does the same thing with shift operator and ORing, making it easy to read the intention of the code.

(1 << RXEN)               --> (1 << 0) --> 0b00000001
(1 << TXEN)               --> (1 << 1) --> 0b00000010
(1 << RXEN) | (1 << TXEN) ---------------> 0b00000011


Alternatively you could also have done:

UCSRB |= (1 << RXEN) ;
UCSRB |= (1 << TXEN) ;


Setting the bits of a register without affecting/changing the status of other bits.

• Thanks. Can these macros also be defined directly using binary format like, UCSRB = 0b00001110 – Paavani Khanna May 31 at 7:12
• Also why did you use a binary or (' | ') when assigning value with the second method [ UCSRB |= (1 << RXEN) ]. Does this imply that the new RXEN bit will be OR of the assigned value and the previously stored value – Paavani Khanna May 31 at 7:49
• You can't redefine pre-defined macros if the header is already included. Most compilers throw it as error. But you can assign it any binary value. – Mitu Raj May 31 at 8:01
• Yes, that's right. It does UCSRB = UCSRB | (1 << RXEN). Advantage is that it doesn't affect existing values of the other bits of the register. It sets only RXEN bit. – Mitu Raj May 31 at 8:01