UAT 204 Microcontrollers and Embedded Systems

Worksheet 2 Bits, counters and volatile on the board

Lab 6 hAssesses CLO2, CLO1

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

  1. Run Examples 1 and 2, predicting results for new starting values
  2. Write C that sets and clears GPIO control bits with SDK functions
  3. Compare bit values with hand calculation
  4. Write a uint8_t counter and show wrap-around over serial
  5. Test an interrupt flag with and without volatile, optimisation on; record behaviour
  6. Read three BARR-C rules on types and variables and review the group's code

Record of results

Table 1 Predictions against results (Examples 1 and 2 and the board counter)
ExperimentStarting valuePredictionActual resultMatch? Why?
Table 2 GPIO control bits by hand against the board
GPIO pinOperation (set, clear)Mask by hand (binary or hex)Result seen on the board
Table 3 The volatile experiment with optimisation on
Code versionOptimisation levelBehaviour seenExplanation

Questions

  1. 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?

  2. From Table 3, how did the code without volatile misbehave, and why only with optimisation on?

  3. 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.

  4. 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

CLO2 Program GPIO, timers and interrupts. CLO1 Explain microcontroller and embedded architecture. (CLOs are a web-team draft; the course owner sets the final ones.)

Instructors: answers and teaching notes for this worksheet are in the teaching pack