UAT 204 Microcontrollers and Embedded Systems
Worksheet 2 Bits, counters and volatile on the board
Objective
Use fixed-width types and bit manipulation correctly, understand the effect of volatile and review code against BARR-C
Equipment and materials
- Pico 2 with the C/C++ SDK
- Computer with Python and a serial terminal
- Module 2 example code
- BARR-C coding standard
Safety
- Low-voltage bench work over USB; no flying
Steps
- Run Examples 1 and 2, predicting results for new starting values
- Write C that sets and clears GPIO control bits with SDK functions
- Compare bit values with hand calculation
- Write a uint8_t counter and show wrap-around over serial
- Test an interrupt flag with and without volatile, optimisation on; record behaviour
- Read three BARR-C rules on types and variables and review the group's code
Record of results
| Experiment | Starting value | Prediction | Actual result | Match? Why? |
|---|---|---|---|---|
| GPIO pin | Operation (set, clear) | Mask by hand (binary or hex) | Result seen on the board |
|---|---|---|---|
| Code version | Optimisation level | Behaviour seen | Explanation |
|---|---|---|---|
Questions
At what value did the group's uint8_t counter wrap, and if it read 250 and then 2, what is the difference computed as uint8_t? Why is that difference still correct across the wrap?
From Table 3, how did the code without volatile misbehave, and why only with optimisation on?
If you overwrite the whole GPIO register instead of setting one bit, what happens to the other pins the group is using? Explain with the results in Table 2.
Does volatile solve sharing a variable between two tasks? If not, what should be used instead?
Check before you hand in
What to hand in
- Predictions against results
- Bit code and counter log
- volatile experiment notes with explanation
- List of BARR-C fixes