0
\$\begingroup\$

My doubt is about the way in which the baudrate value is written into UBRRn registers.

For eg:

Lets say i want to write the value 25 into UBBRn registers to configure the microcontroller to communicate at a particular baud rate.

I use the following code to do that

unsigned int ubrr = 25; // 0000 0000 0001 1001 (16 bit binary representation)

UBRR0H = (ubrr>>8);     // Moving the lower Byte 0001 1001 to UBRR0H 
UBRR0L = (ubrr);        // Moving  0000 0000 to UBRR0L

Why are we moving the lower byte to the high register UBRR0H and the higher byte to UBRR0L (after shifting).

Generally when we are moving a 16 bit value to two 8 bit registers,

  • The Lower byte will be moved to the Lower register (UBRR0L) and
  • Higher byte will be moved to the High register (UBRR0H)

Why is ATmega328p reversing this trend,

Is this a design issue,or there is any specific reason to do this ?

Also UBBR0H has only 4 bits and the upper nibble will be discarded ?

I would like to point out the default code in the datasheet is working perfectly for me,I am just curious why it is done like that?

\$\endgroup\$
1
\$\begingroup\$

Why are we moving the lower byte to the high register UBRR0H and the higher byte to UBRR0L (after shifting).

You aren't.

ubrr >> 8 takes the upper 8 bits of ubrr and shifts them into the lower 8 bits, where they are written to UBRR0H. It doesn't modify the value of ubrr.

Writing a 16-bit value, like ubrr, to an 8-bit register, like UBRR0L, truncates the value to its lower 8 bits.

|improve this answer|||||
\$\endgroup\$

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.