Microcontroller Programming Interview Questions and Answers

Wu Jianying Microcontroller Development Board Address

Taobao Store:Wu Jianying’s Shop

Address:https://item.taobao.com/item.htm?_u=ukgdp5a7629&id=524088004171

Microcontroller Programming Interview Questions and Answers

In the new year of 2018, many people are preparing for new jobs. So why not learn some microcontroller programming interview questions to be fully prepared? Below are three common microcontroller programming interview questions along with their answers.

Microcontroller Programming Interview Questions and Answers

Question 1: The Difference Between char *p=”abc” and char p[]=”abc”

Answer:

1. When a string appears, the compiler automatically adds a 0 as a terminator. For example, when writing “abc” in code, the compiler actually stores “abc\0”.

2. Is “abc” a constant?

Answer: Sometimes it is, sometimes it is not.

In the case where it is not a constant:

When “abc” is used as the initial value of a character array, it is not a constant, such as:

char str[] = “abc”;

Since a character array is defined, it essentially reserves space to store “abc”, and because a character array stores characters one by one, the compiler interprets this statement as:

char str[3] = {‘a’,’b’,’c’};

Therefore, the final result of char str[] = “abc”; is:

char str[4] = {‘a’,’b’,’c’,’\0′};

To expand, if char str[] = “abc”; is written inside a function, then “abc\0” is not a constant and should be placed on the stack.

In the case where it is a constant:

When assigning “abc” to a character pointer variable, such as:

char* ptr = “abc”;

Since a regular pointer is defined and no space is reserved to store “abc”, the compiler has to find a place to store “abc”. Clearly, treating “abc” as a constant and placing it in the program’s constant area is a suitable choice for the compiler. Therefore, even though ptr’s type is not const char*, and ptr[0] = ‘x’; can compile, executing ptr[0] = ‘x’; will cause a runtime exception because this statement attempts to modify something in the program’s constant area.

I remember reading in a book that writing char* ptr = “abc”; was originally not allowed in the C++ standard, but because this writing style was so common in C, it had to be allowed for compatibility. Although it is allowed, the recommended way should be const char* ptr = “abc”; so that if later ptr[0] = ‘x’ is written, the compiler will not allow it to compile, thus avoiding the runtime exception mentioned above.

Furthermore, if char* ptr = “abc”; is written inside a function, although “abc\0” is placed in the constant area, ptr itself is just a regular pointer variable, so ptr is placed on the stack, but what it points to is in the constant area.

3. The type of string constants can be understood as the type of the corresponding character constant array, such as the type of “abcdef” can be considered as const char[7].

4. If you really need to use “abcd” as a pointer, it is recommended to write const char * p=”abcd”;

If initializing a string array, it is recommended to write char p[]=”abcd”;

If p is a pointer and needs to be initialized, it should be char *p;p=malloc(STR_SIZE);strcpy(p,”abcd”);

Question 2: Methods and Discussion for Microcontroller Software Reset (Soft Reset)

1. Watchdog; 2. ((void(code *)(void))0x0000)(); 3. Control one pin of the microcontroller to trigger RSTRST; 4. Control one pin of the microcontroller to reset power; 5. Use the built-in software reset instruction or watchdog instruction of the microcontroller; 6. Goto method.

Method 1: “Watchdog” is a good way for microcontroller soft reset and is basically the only method. However, not all microcontrollers have watchdog functionality, and it is not a foolproof solution.

Method 2: This is not a reset, it merely transfers the program to address 0 for execution, which is less direct than using a JMP. Currently, very few microcontrollers or users have added the user’s program starting address during Boot load not being 0x0000, so it is necessary to find these specific microcontroller startup addresses. In Keil C51, this can be implemented as: void soft_reset(void){ ((void (code *) (void)) 0x0000) ();} or void (*reset)()=0x0000; use the statement: soft_reset(); where software reset is needed, which generally achieves a software reset.

Method 3: A software-implemented hard reset. This requires sacrificing one microcontroller pin and increases the complexity of the external circuit, which is not advisable.

Method 4: Similar to method 3, it also requires sacrificing one microcontroller pin and increases the complexity of the external circuit, which is not advisable. However, it should not be considered merely as a reset; it should be called a power-on reset.

Method 5: Atmel 89C does not have an internal watchdog, while S has one, which can be done with just one instruction. For example, STC microcontrollers have a software reset instruction, namely ISP_CONTR, located at address 0E7H (i.e., str ISP_CONTR=0xE7), MOV ISP_CONTR,#00100000B (in C language, ISP_CONTR=0x20). The internal watchdog is also a single instruction MOV WDT_CONTR,#00111100B! The STC 51 series microcontroller datasheet indicates that traditional 8051 microcontrollers do not support this function in hardware, and users must implement it through software simulation, which is quite complicated. Now, STC has launched an enhanced 8051 based on customer requirements, adding the ISP_CONTR special function register to implement this function. Users only need to control two bits of the ISP_CONTR special function register, SWBS/SWRST, to achieve a system reset.

Method 6: If the program starts running from the beginning (power-on reset location) and there is only one loop, then using goto is acceptable. For example, set a start: at the beginning of main(), and set a condition in the program’s only loop, then use the goto command. However, it is important to note that if this is in an interrupt routine, the interrupt register will still be set, and same-level interrupts cannot be executed. Therefore, the interrupt register must be cleared first, EA = 0. Only the RETI instruction can clear the interrupt register. The 51 microcontroller has two levels of interrupt priority, so it requires executing the RETI instruction twice. This is very simple in assembly but relatively difficult to implement in C. However, the goto command should be avoided as much as possible because it can lead to chaotic execution, and it cannot jump outside the function to execute a command.

In summary, method 5 is the most concise and convenient, while method 2 is also a good option.

Question 3:

int main()

{ int a=5;

int*q;

q=(int*)&a;

printf(“%d\n”,*q);

}

The code is correct. During the interview, they may ask what would happen if the cast in q=(int *)&a; is removed.

The above content summarizes three common microcontroller programming interview questions. Do you know them all? If not, take a closer look at the answers!

This article is sourced from the internet. If the original author does not support our reposting, please contact us for deletion. Thank you!

For those who like this article,please like it!

Microcontroller Programming Interview Questions and Answers

Microcontroller Programming Interview Questions and Answers

Technology Comes from Accumulation, Success Comes from Persistence

—— Microcontroller Expert Wu Jianying

Leave a Comment