1
\$\begingroup\$

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);
}
\$\endgroup\$
4
  • 4
    \$\begingroup\$ 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. \$\endgroup\$ – AJN May 24 at 7:20
  • 1
    \$\begingroup\$ 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. \$\endgroup\$ – AJN May 24 at 10:52
  • 1
    \$\begingroup\$ You had to include: xc.h \$\endgroup\$ – Mike May 25 at 4:34
  • 1
    \$\begingroup\$ Kindly respond to the comments, and accept an answer as solution once it solved your problem. That's how this site works, \$\endgroup\$ – Mitu Raj May 27 at 6:58
1
\$\begingroup\$

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.

\$\endgroup\$
4
  • \$\begingroup\$ Thanks. Can these macros also be defined directly using binary format like, UCSRB = 0b00001110 \$\endgroup\$ – Paavani Khanna May 31 at 7:12
  • \$\begingroup\$ 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 \$\endgroup\$ – Paavani Khanna May 31 at 7:49
  • \$\begingroup\$ 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. \$\endgroup\$ – Mitu Raj May 31 at 8:01
  • \$\begingroup\$ 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. \$\endgroup\$ – Mitu Raj May 31 at 8:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.