Baud rate configuration in Embedded C - Time & Space Complexity
When setting baud rates in embedded systems, the code often calculates values based on input parameters.
We want to know how the time to compute these values changes as inputs vary.
Analyze the time complexity of the following code snippet.
unsigned int calculateBaudRate(unsigned int clockFreq, unsigned int baudRate) {
unsigned int divisor = clockFreq / (16 * baudRate);
return divisor;
}
This code calculates the divisor needed to set the baud rate for serial communication.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: A simple arithmetic division and multiplication.
- How many times: The operations run once per function call.
Explain the growth pattern intuitively.
| Input Size (clockFreq, baudRate) | Approx. Operations |
|---|---|
| Small values (e.g., 10, 10) | 3 operations |
| Medium values (e.g., 1000000, 9600) | 3 operations |
| Large values (e.g., 100000000, 115200) | 3 operations |
Pattern observation: The number of operations stays the same regardless of input size.
Time Complexity: O(1)
This means the time to calculate the baud rate divisor does not grow with input size; it stays constant.
[X] Wrong: "The calculation time increases if the clock frequency or baud rate is larger."
[OK] Correct: The calculation uses simple arithmetic operations that take the same time regardless of the numbers' size.
Understanding how simple calculations behave helps you explain efficiency clearly and shows you can analyze code beyond just loops.
"What if the function had to calculate baud rates for an array of values instead of just one? How would the time complexity change?"
Practice
Solution
Step 1: Understand baud rate meaning
Baud rate defines how fast bits are sent per second in serial communication.Step 2: Identify what baud rate affects
It controls the speed, not voltage, size, or encoding type.Final Answer:
The speed of data transmission -> Option DQuick Check:
Baud rate = speed [OK]
- Confusing baud rate with voltage levels
- Thinking baud rate controls data size
- Mixing baud rate with encoding type
Solution
Step 1: Recall standard baud rate formula
For normal speed UART, UBRR = (F_CPU / (16 * baud)) - 1.Step 2: Apply values to formula
Substitute F_CPU=16,000,000 and baud=9600 gives UBRR = (16000000/(16*9600))-1.Final Answer:
UBRR = (16000000 / (16 * 9600)) - 1 -> Option BQuick Check:
Standard UART formula = UBRR = (16000000 / (16 * 9600)) - 1 [OK]
- Using wrong divisor like 8 or 32 instead of 16
- Adding 1 instead of subtracting 1
- Mixing up normal and double speed formulas
unsigned int ubrr_value = (8000000 / (16 * 115200)) - 1; UBRR0H = (ubrr_value >> 8); UBRR0L = ubrr_value;
Solution
Step 1: Calculate UBRR value
Calculate (8000000 / (16 * 115200)) - 1 = (8000000 / 1843200) - 1 ≈ 4.34 - 1 = 3.34, truncated to 3.Step 2: Check integer truncation
Since ubrr_value is unsigned int, decimal part is truncated, so ubrr_value = 3.Final Answer:
3 -> Option CQuick Check:
Integer division truncates to 3 [OK]
- Rounding up instead of truncating
- Using wrong clock frequency
- Confusing UBRR with baud rate
#define F_CPU 16000000 #define BAUD 9600 unsigned int ubrr = F_CPU / 16 / BAUD; UBRR0H = (ubrr >> 8); UBRR0L = ubrr;
Solution
Step 1: Review UBRR calculation formula
The correct formula is UBRR = (F_CPU / (16 * BAUD)) - 1, but code misses the '-1'.Step 2: Check other parts
Bit shift and baud rate are correct; division by 16 is standard for normal speed.Final Answer:
Missing subtraction of 1 in UBRR calculation -> Option AQuick Check:
UBRR formula requires '-1' [OK]
- Forgetting to subtract 1
- Shifting bits incorrectly
- Using wrong clock divisor
Solution
Step 1: Understand double speed mode formula
In double speed mode, UART uses UBRR = (F_CPU / (8 * baud)) - 1.Step 2: Compare options to formula
Only UBRR = (F_CPU / (8 * baud)) - 1 matches the double speed formula correctly.Final Answer:
UBRR = (F_CPU / (8 * baud)) - 1 -> Option AQuick Check:
Double speed divisor is 8 [OK]
- Using normal speed formula for double speed
- Confusing divisor values
- Forgetting to subtract 1
Ready to prove it?
Pass the test and this pattern moves unknown
