Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Question 6 a) Explain why the use of local variables is preferable in C programs

ID: 2265711 • Letter: Q

Question

Question 6 a) Explain why the use of local variables is preferable in C programs designed for b) Explain how the use of functions leads to well-structured C programs and the c) Consider the case of a microcontroller operating at 10 MHz that is required to microcontroller with limited RAM (4 points) benefits of this design approach. (4 points) communicate at 9600 baud with the serial port on a PC. The USART is used in asynchronous mode. 1. Calculate the possible values of the baud rate generator register (SPBRG) for BRGH 0 and 1, respectively. (5 points) Which one of the two values above in (a) is acceptable and why? (2 points) What is the actual baud rate achieved using the value selected in (1)? (5 points) 2. 3.

Explanation / Answer

Answer:-a) Local variables are freed just after the completion of it's scope. For example if a variable is locally defined in a function then as the function completes the variable memory is freed and that location can be used again. Hence in C local variables are preferably used.

b) Function is a block of code that can run independently. The main purpose of use of function is to reduce the overall code size. If a part of code is to be executed again and again then no need to write that part of code at each requirement point rather we define that part of code into a functional block and call that function block whenever that part of code is required. In C functions are widely used. Also the total program is written in a function in C, called main() function.

c)1) For BRGH = 0, SPBRG = [(10*106)/(64 * 9600)] - 1 = 15.27 hence 15 or 0xF
for BRGH = 1, SPBRG = [(10*106)/(16 * 9600)] - 1 = 65.10 hence 65 or 0x41

2) The second value is acceptable since it gives error of 0.16% only while first one gives error of 1.73% in baud rate.

3) The actual baud rate = (10*106)/(16*(65)) = 9615.38 bps

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote