1
\$\begingroup\$

MCU: Atemega328P Datasheet: https://ww1.microchip.com/downloads/en/DeviceDoc/Atmel-7810-Automotive-Microcontrollers-ATmega328P_Datasheet.pdf (Page 143 - USART)

Note: I am beginner :)

Using the BAUD rate formula found in this picture we can calculate the UBRRn value for 9600 BAUD rate. Therefore UBBRn = 103.

enter image description here

103 is loaded into UBRR0H and UBRR0L with UBRR0H taking the 4 most significant bits. According to the datasheet this is the method to load 103: UBRR0H = (103 >> 8); UBRR0L = 103; 103 = 1100111

Wouldn't this just load 1100111 into UBRR0H and shift right 8 bits pushing the entire value out of the register to give 00000000?

And in UBRR0L would this not give 01100111?

Baud Rate Register

FROM DataSheet

enter image description here

\$\endgroup\$
2
  • \$\begingroup\$ becaues the number could be higher than 255? \$\endgroup\$
    – user253751
    Jan 4, 2021 at 19:01
  • \$\begingroup\$ That looks funky .. I would be more specific about it. e.g. // write to register low end UBRR0L = (uint8_t)(103 & 0xFF); // write to register high end UBRR0H = (uint8_t)(103 >> 8) \$\endgroup\$
    – Sorenp
    Jan 4, 2021 at 19:09

2 Answers 2

4
\$\begingroup\$

This is a generic method. The ubrr is an unsigned int which makes it 16 bits wide (2 bytes).

To break this into discrete bytes like in the example, you need to shift the bits right by 8 to get the high byte.

In your specific example it doesn't matter because the high byte is empty anyway, but what if you need a ubrr of 3,096?

Also, you might be confusing order of operations. Your example doesn't load the register then shift it out, it takes the binary representation of 103, shifts it, then applies the result of that to the register.

\$\endgroup\$
0
\$\begingroup\$

For 3,096 then UBRRH = decimal 12 OR binary 1100 UBRRL = decimal 24 OR binary 0001 1000

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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