C/C++ And Microcontroller Questions Flashcards

(21 cards)

1
Q

What is the difference between volatile and const?

A

Const = keyword for constant variable and their purpose is to be read-only
Volatile= keyword for variables that can be modified outside normal program flow. Such as in interrupts.
- Compiler doesn’t optimise these variables

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Why is volatile important in embedded systems?

A

Volatile is important to prevent the compiler from optimising away reads which causes incorrect behaviour
- hardware registers can change automatically
- interrupts can modify variables asynchronously

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Difference between stack and heap?

A

Stack = fast and automatically managed
- used for local variables and function calls
Heap = slow and manually allocated/freed

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What is segmentation fault?

A

Illegal access of memory
- occurs when a program tries to access memory it’s not allowed to.
- usually causes the operating system to terminate the program

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Explain pointers and pointer arithmetic

A

Pointers are variables that hold a memory address that points towards another variable
- pointer arithmetic modifies the memory address by the size of the data type

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Pass-by-value vs pass-by-reference

A

Value = pass a direct copy of a variable’s value into a function
Reference = memory address of the variable is passed into a function

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What is a watchdog timer?

A

A timer that resets the MCU if the firmware hangs or doesn’t “feed the watchdog” in time

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

GPIO input vs output

A

Input = purpose is to read signals
Output = drive signals

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What is an interrupt?

A

A hardware or software event that halts the program to handle the event in an ISR

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Polling vs interrupts

A

Polling = blocks the cpu until an event occurs
- less efficient
Interrupts = event driven, more efficient and real-time

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What is debouncing?

A

Removing mechanical switch noise
Can be done by:
- State machine
- software filter
- hardware RC filter
- delay

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What causes debouncing?

A

When a button is pressed, the physical contacts vibrate and make/break the electrical connection multiple times
- the microcontroller may read these bounces as multiple presses.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

How does a delay fix debouncing?

A
  • you record the initial state change and wait a few milliseconds
  • if the switch’s state is the same after the delay the input is valid
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

How does a state machine prevent debouncing?

A
  • the state machine will have two states idle and debouncing
    START: Idle
  • once the button is pressed. GO TO: debounce state

DEBOUNCE: if the value of the button changes and the timer ends while in debounce stay in debounce
- if the timer ends without a new value from the button finish debouncing and go back to IDLE

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

How can a software filter prevent debouncing?

A

When a change in the input signal is detected:
- the software waits for a short period (has to be longer than the bounce duration)
- if the signal is still in the new state it is valid.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

How does an RC circuit prevent debouncing

A
  • Specifically use a RC low-pass filter
  • purpose is to make the signal into a clean and gradual signal
17
Q

What is PWM used for?

A
  • controlling motors
  • LEDs
  • brightness of lights
  • servos
  • efficient power supplies

Does this by varying duty cycle

18
Q

How does PWM work?

A

Constantly turns ON and OFF the power supply at a set frequency
Duty cycle = ratio of the “on” time to the total period of the pulse

19
Q

How does an ADC work?

A

ANALOGUE-TO-DIGITAL CONVERTER
- converts an analogue signal into digital values by taking samples at a specific sampling rate.

20
Q

What does mutable mean?

A

A data structure that can be changed

21
Q

What does immutable mean?

A

A type of data structure that can’t be changed