Skip to main content

Microcontrollers

Learning Targets​

  • Situation: Sampling, communication and output updates sharing one controller.
  • Targets: Peripheral timing, queue ownership, threshold state and failure handling.
  • Scope: Simulated voltage monitor; optional verified low-voltage hardware.

Peripheral Responsibilities​

  • Microcontroller: CPU, memory and embedded peripherals.
  • GPIO: Digital level input/output with pin-multiplexing and electrical limits.
  • Timer: Counting, capture, compare and hardware-triggered events.
  • ADC: Valid reference/range and acquisition time despite a simple software API.
  • Serial peripheral: Buffered transfer with explicit frame format.
fevent=ftimerP(TOP+1)f_{\mathrm{event}}=\frac{f_{\mathrm{timer}}}{P(\mathrm{TOP}+1)}
  • Convention: Prescale divisor P; count from zero through TOP inclusive.
  • Example: 1 MHz, P = 1, TOP = 999 → 1 kHz.
  • PWM: 250 high ticks of 1000 → 25%; ideal 3.3/0 V average 0.825 V.
  • Acquisition: Previous 10 kΩ/20 pF model requiring at least 1.802 µs for twelve-bit half-LSB settling.
Timer-triggered ADC acquisition and conversion, a short interrupt storing each sample, background transmission and a separate PWM output
Figure 1. Timer-triggered ADC acquisition and conversion, a short interrupt storing each sample, background transmission and a separate PWM output.

Capture and Ownership​

  • Hardware trigger: Sampling independent of variable main-loop execution time.
  • Short interrupt: Sample/index capture and queue insertion; slow display/transmission in background.
  • Shared state: Atomic access, critical section or explicit buffer handoff.
  • Volatile distinction: Compiler-access behavior; no automatic atomicity.
  • Circular queue: Producer/consumer positions, full/empty distinction and chosen overflow policy.
  • Capacity example: Initially empty 64-sample queue at 1 kS/s → 64 sample slots, about 64 ms without consumption.
  • Deadline: Worst-case blocking and bursts, not average CPU utilization alone.

Worked Monitor​

  • Reset state: Event inactive.
  • Assertion: Code at least 2100.
  • Clear: Code at most 2000.
  • Between thresholds: Previous state retained.
  • Hysteresis span: 100 codes × 0.805664 mV ≈ 80.57 mV.
  • Input sequence: 1990, 2050, 2110, 2050, 1990 → inactive, inactive, active, active, inactive.
  • Latency: Persistent transition waiting almost one sample interval plus conversion/processing.
  • Short event: Possible miss without input capture or another event mechanism.

Startup and Failure​

  • Initialization: Inactive outputs → clocks/pins/reference → queue/handlers → timer and conversion enable.
  • Watchdog: Progress-based servicing; no replacement for independent output protection.
  • Brownout/reset: Defined external output state and valid supply range.
  • Invalid sample: Explicit flag; no conversion to plausible valid data.
  • Calibration storage: Write endurance and interrupted-write handling.
  • Applications: Calibrated integration, DDS control and bounded feedback; physical units only after sensor calibration.
  • Control limits: Sampling/filter delay, actuator saturation and verified driver path.

Practice​

  • Preparation: 1 MHz timer model, 1 kS/s ADC, twelve-bit 3.3 V convention, 64 usable queue slots.
  • Prediction: TOP 999, hysteresis sequence and overflow during prolonged consumption pause.
  • Execution: Ten samples per input level; separate 20 ms and 100 ms background pauses; at least 200 ms runs.
  • Observation: Acquisition timestamps, occupancy, dropped count and fault state.
TestExpectedSimulation
Trigger spacing1 ms—
2050 after 1990Inactive—
2050 after 2110Active—
100 ms pauseCapacity exceeded—
  • Optional hardware: Known low-voltage board, verified pin ratings, reference and input source; harmless indicator only.
  • Final record: Circuit model, acquisition schedule, frame format, error budget and failure tests.

Exercises​

  1. Timer: TOP for 2 MHz, P = 2, 1 kHz events.
  2. PWM: Average for 40% duty with ideal 0/3.3 V levels.
  3. Queue: Approximate coverage of 128 slots at 1 kS/s.
  4. State: Code 2050 after an active 2110 sample.
  5. Scheduling: Adequacy of a 64-slot queue during 100 ms with no consumer at 1 kS/s.
Answers and reasoning
  1. Answer: 999 under inclusive TOP counting.
  2. Answer: 1.32 V.
  3. Answer: Approximately 128 ms.
  4. Answer: Active; code within the hysteresis band.
  5. Answer: Insufficient; about 100 arriving samples for 64 available slots, with explicit overflow handling required.

Continue​

Reference​

  • Reading: Paul Horowitz and Winfield Hill, The Art of Electronics, third edition, Chapter 15.
  • Lesson material: Original explanations, calculation examples and exercises; reference sections for further study.