What is the difference between volatile and const?
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
Why is volatile important in embedded systems?
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
Difference between stack and heap?
Stack = fast and automatically managed
- used for local variables and function calls
Heap = slow and manually allocated/freed
What is segmentation fault?
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
Explain pointers and pointer arithmetic
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
Pass-by-value vs pass-by-reference
Value = pass a direct copy of a variable’s value into a function
Reference = memory address of the variable is passed into a function
What is a watchdog timer?
A timer that resets the MCU if the firmware hangs or doesn’t “feed the watchdog” in time
GPIO input vs output
Input = purpose is to read signals
Output = drive signals
What is an interrupt?
A hardware or software event that halts the program to handle the event in an ISR
Polling vs interrupts
Polling = blocks the cpu until an event occurs
- less efficient
Interrupts = event driven, more efficient and real-time
What is debouncing?
Removing mechanical switch noise
Can be done by:
- State machine
- software filter
- hardware RC filter
- delay
What causes debouncing?
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 does a delay fix debouncing?
How does a state machine prevent debouncing?
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 can a software filter prevent debouncing?
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 does an RC circuit prevent debouncing
What is PWM used for?
Does this by varying duty cycle
How does PWM work?
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
How does an ADC work?
ANALOGUE-TO-DIGITAL CONVERTER
- converts an analogue signal into digital values by taking samples at a specific sampling rate.
What does mutable mean?
A data structure that can be changed
What does immutable mean?
A type of data structure that can’t be changed