Bird
Raised Fist0
Embedded Cprogramming~10 mins

Baud rate configuration in Embedded C - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - Baud rate configuration
Start
↓
Select Clock Frequency
↓
Calculate Baud Rate Register Value
↓
Set Baud Rate Register
↓
Enable UART with Configured Baud Rate
↓
End
This flow shows how to set the baud rate: choose clock, calculate register value, set it, then enable UART.
Execution Sample
Embedded C
unsigned int ubrr = (F_CPU / (16 * BAUD)) - 1;
UBRR0H = (ubrr >> 8);
UBRR0L = ubrr & 0xFF;
UCSR0B |= (1 << RXEN0) | (1 << TXEN0);
Calculate baud rate register and enable UART transmitter and receiver.
Execution Table
StepActionCalculation/ValueRegister SetResult
1Calculate UBRR valueubrr = (16000000 / (16 * 9600)) - 1 = 103N/Aubrr = 103
2Set UBRR0H (high byte)UBRR0H = (103 >> 8) = 0UBRR0H = 0x00High byte set to 0
3Set UBRR0L (low byte)UBRR0L = 103 & 0xFF = 103UBRR0L = 0x67Low byte set to 103
4Enable RX and TXUCSR0B |= (1 << RXEN0) | (1 << TXEN0)UCSR0B bits RXEN0 and TXEN0 setUART enabled for receive and transmit
5EndConfiguration completeN/AUART ready with 9600 baud rate
💡 All steps done, UART configured with desired baud rate.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3After Step 4Final
ubrrundefined103103103103103
UBRR0Hundefinedundefined0000
UBRR0Lundefinedundefinedundefined103103103
UCSR0Binitial valueinitial valueinitial valueinitial valuebits RXEN0 and TXEN0 setbits RXEN0 and TXEN0 set
Key Moments - 3 Insights
Why do we shift ubrr by 8 bits when setting UBRR0H?
Because UBRR is 16 bits but registers are 8 bits each; shifting by 8 gets the high byte (see step 2 in execution_table).
Why subtract 1 in the baud rate calculation formula?
The hardware counts from zero, so subtracting 1 sets the correct baud rate divisor (see step 1 in execution_table).
What happens if we don't enable RX and TX bits in UCSR0B?
UART won't transmit or receive data even if baud rate is set (see step 4 in execution_table).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the value of ubrr after step 1?
A103
B16000000
C9600
D0
💡 Hint
Check the 'Calculation/Value' column in row for step 1.
At which step are the UART transmitter and receiver enabled?
AStep 2
BStep 4
CStep 3
DStep 5
💡 Hint
Look for 'Enable RX and TX' action in execution_table.
If the clock frequency F_CPU was doubled, how would ubrr change at step 1?
Aubrr would double
Bubrr would stay the same
Cubrr would halve
Dubrr would become zero
💡 Hint
Recall ubrr = (F_CPU / (16 * BAUD)) - 1; doubling F_CPU affects numerator.
Concept Snapshot
Baud rate config sets UART speed.
Calculate UBRR: (F_CPU/(16*BAUD))-1.
Set high byte UBRR0H and low byte UBRR0L.
Enable RX and TX bits in UCSR0B.
UART ready to send/receive data.
Full Transcript
This visual trace shows how to configure baud rate in embedded C for UART communication. First, the clock frequency and desired baud rate are used to calculate the UBRR register value. This 16-bit value is split into high and low bytes and set into UBRR0H and UBRR0L registers respectively. Then, the UART transmitter and receiver are enabled by setting bits RXEN0 and TXEN0 in the UCSR0B register. The execution table walks through each step with values and register changes. Variable tracking shows how ubrr and registers change. Key moments clarify why shifting and subtracting 1 are needed and the importance of enabling UART bits. The quiz tests understanding of values and effects of clock changes. This step-by-step helps beginners see exactly how baud rate configuration happens in code.

Practice

(1/5)
1. What does the baud rate setting control in UART communication?
easy
A. The type of data encoding
B. The voltage level of signals
C. The size of data packets
D. The speed of data transmission

Solution

  1. Step 1: Understand baud rate meaning

    Baud rate defines how fast bits are sent per second in serial communication.
  2. Step 2: Identify what baud rate affects

    It controls the speed, not voltage, size, or encoding type.
  3. Final Answer:

    The speed of data transmission -> Option D
  4. Quick Check:

    Baud rate = speed [OK]
Hint: Baud rate equals bits per second speed [OK]
Common Mistakes:
  • Confusing baud rate with voltage levels
  • Thinking baud rate controls data size
  • Mixing baud rate with encoding type
2. Which of the following is the correct way to calculate the UART baud rate register value if the clock frequency is 16 MHz and desired baud rate is 9600?
easy
A. UBRR = (16000000 / (32 * 9600)) - 1
B. UBRR = (16000000 / (16 * 9600)) - 1
C. UBRR = (16000000 / (8 * 9600)) + 1
D. UBRR = (16000000 / (16 * 9600)) + 1

Solution

  1. Step 1: Recall standard baud rate formula

    For normal speed UART, UBRR = (F_CPU / (16 * baud)) - 1.
  2. Step 2: Apply values to formula

    Substitute F_CPU=16,000,000 and baud=9600 gives UBRR = (16000000/(16*9600))-1.
  3. Final Answer:

    UBRR = (16000000 / (16 * 9600)) - 1 -> Option B
  4. Quick Check:

    Standard UART formula = UBRR = (16000000 / (16 * 9600)) - 1 [OK]
Hint: Use formula UBRR = (F_CPU/(16*baud))-1 for normal speed [OK]
Common Mistakes:
  • Using wrong divisor like 8 or 32 instead of 16
  • Adding 1 instead of subtracting 1
  • Mixing up normal and double speed formulas
3. Given the following code snippet, what is the value loaded into UBRR if F_CPU = 8 MHz and baud rate = 115200?
unsigned int ubrr_value = (8000000 / (16 * 115200)) - 1;
UBRR0H = (ubrr_value >> 8);
UBRR0L = ubrr_value;
medium
A. 5
B. 4
C. 3
D. 6

Solution

  1. Step 1: Calculate UBRR value

    Calculate (8000000 / (16 * 115200)) - 1 = (8000000 / 1843200) - 1 ≈ 4.34 - 1 = 3.34, truncated to 3.
  2. Step 2: Check integer truncation

    Since ubrr_value is unsigned int, decimal part is truncated, so ubrr_value = 3.
  3. Final Answer:

    3 -> Option C
  4. Quick Check:

    Integer division truncates to 3 [OK]
Hint: Integer division truncates decimals, so round down [OK]
Common Mistakes:
  • Rounding up instead of truncating
  • Using wrong clock frequency
  • Confusing UBRR with baud rate
4. Identify the error in this baud rate configuration code snippet:
#define F_CPU 16000000
#define BAUD 9600
unsigned int ubrr = F_CPU / 16 / BAUD;
UBRR0H = (ubrr >> 8);
UBRR0L = ubrr;
medium
A. Missing subtraction of 1 in UBRR calculation
B. Wrong bit shift for UBRR0H
C. Incorrect baud rate value
D. F_CPU should be divided by 8, not 16

Solution

  1. Step 1: Review UBRR calculation formula

    The correct formula is UBRR = (F_CPU / (16 * BAUD)) - 1, but code misses the '-1'.
  2. Step 2: Check other parts

    Bit shift and baud rate are correct; division by 16 is standard for normal speed.
  3. Final Answer:

    Missing subtraction of 1 in UBRR calculation -> Option A
  4. Quick Check:

    UBRR formula requires '-1' [OK]
Hint: Always subtract 1 in UBRR formula [OK]
Common Mistakes:
  • Forgetting to subtract 1
  • Shifting bits incorrectly
  • Using wrong clock divisor
5. You want to configure UART for double speed mode with F_CPU = 16 MHz and baud rate 115200. Which formula correctly calculates the UBRR value?
hard
A. UBRR = (F_CPU / (8 * baud)) - 1
B. UBRR = (F_CPU / (16 * baud)) - 1
C. UBRR = (F_CPU / (32 * baud)) - 1
D. UBRR = (F_CPU / (4 * baud)) - 1

Solution

  1. Step 1: Understand double speed mode formula

    In double speed mode, UART uses UBRR = (F_CPU / (8 * baud)) - 1.
  2. Step 2: Compare options to formula

    Only UBRR = (F_CPU / (8 * baud)) - 1 matches the double speed formula correctly.
  3. Final Answer:

    UBRR = (F_CPU / (8 * baud)) - 1 -> Option A
  4. Quick Check:

    Double speed divisor is 8 [OK]
Hint: Double speed uses divisor 8, not 16 [OK]
Common Mistakes:
  • Using normal speed formula for double speed
  • Confusing divisor values
  • Forgetting to subtract 1
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard