Mechatronics · Module 8 of 10
Microcontrollers and Embedded Control
The microcontroller is where a mechatronic system decides. It reads sensors, runs the control logic, and drives actuators, and its timers turn a fixed clock into precise PWM and delays.
Readiness check
This module is the decision block. Tick only what you can do closed-notes.
- Divide a clock frequency by a whole number.
- Convert a frequency to a period and back.
- Recall that a program reads inputs and writes outputs.
- Recall that a timer counts clock pulses.
- Work with microseconds and milliseconds.
The core idea
A microcontroller is a processor, memory, and input-output peripherals on one chip. Its timers divide the clock: a PWM frequency is the clock over the prescaler times the counter top plus one, and a delay of a set time needs that time divided by the tick period, where the tick period is the prescaler over the clock.
PWM freq = fclk / (prescaler × (TOP + 1))tick period = prescaler / fclkticks = delay × fclk / prescalerThe microcontroller is the small computer that makes a mechatronic system act, integrating a processor, flash and RAM memory, and peripherals such as digital pins, analog-to-digital converters, and timers on a single chip. Its job is the sense, decide, actuate loop in software: read the conditioned and digitised sensors, compute the control law, and write the actuator commands. The peripherals matter as much as the processor. General-purpose pins read and drive digital signals; the on-chip ADC reads analog sensors; and timers turn the fixed clock into precise timing without tying up the processor. A timer counts clock pulses through a prescaler that divides the clock; when it counts up to a value TOP and resets, it produces a PWM waveform whose frequency is fclk divided by the prescaler and by TOP plus one, because the count spans zero to TOP inclusive. The same timer measures a delay: each tick lasts the prescaler divided by the clock, so a wanted delay needs delay times clock over prescaler ticks. Interrupts let the chip react to events the instant they happen instead of polling, which keeps a real-time loop responsive.
The skills, taught in order
Five skills turn a microcontroller into a controller.
8.1 What a microcontroller is
A microcontroller packs a CPU, flash for the program, RAM for data, and peripherals onto one chip. Unlike a desktop processor it is built for control, with pins and timers rather than raw compute, and it runs one dedicated program.
8.2 Digital and analog I/O
General-purpose pins read logic levels and drive outputs; the on-chip ADC reads analog sensors as numbers; and output pins or DACs command actuators. This I/O is the boundary between the program and the physical system.
8.3 Timers and PWM
A timer counts clock pulses through a prescaler up to a value TOP, then resets. That gives a PWM frequency of fclk/(prescaler × (TOP + 1)) and a duty set by a compare value. Timers generate exact waveforms without the CPU's attention.
| Setting | Effect | In the formula |
|---|---|---|
| Prescaler | divides the clock | larger slows the timer |
| TOP | counts per cycle | uses TOP + 1 |
| Compare value | sets duty | fraction of TOP |
The prescaler and TOP together set the PWM frequency; the compare value sets the duty within it.
8.4 Interrupts
An interrupt suspends the main program to run a short handler when an event occurs, a pin change, a timer overflow, an arriving byte. It replaces wasteful polling and keeps the system responsive to the moment an event happens.
8.5 Embedded program structure
Firmware is typically an initialisation followed by an endless loop that reads, computes, and writes, with time-critical work moved into interrupts and timers. Avoiding long blocking delays keeps the control loop running at a steady rate.
Engineering connection: a motor controller uses one timer to generate the drive PWM and an interrupt to count encoder edges, so the CPU is free to run the speed loop.
Worked example 1: setting a PWM frequency
A timer runs from a 16 MHz clock with a prescaler of 8 and a TOP of 9999 (so it counts 10000 steps). Find the PWM frequency.
- ProblemFind the PWM frequency for the timer in Figure 1.
- Given / findfclk = 16 MHz, prescaler = 8, TOP = 9999. Find the PWM frequency.
- AssumptionsTimer counts 0 to TOP inclusive, then resets (so TOP + 1 steps).
- ModelPWM freq = fclk/(prescaler × (TOP + 1)).
- EquationsTOP + 1 = 10000f = 16×106/(8 × 10000)
- Solvef = 16×106/80000 = 200 Hz.
- CheckThe prescaled clock is 2 MHz; 2×106/10000 = 200 Hz, matching.
- ConclusionThe two divisions turn a 16 MHz clock into a clean 200 Hz PWM, suitable for a motor drive.
Worked example 2: a timer delay
A 16 MHz microcontroller uses a timer with a prescaler of 64. How many timer ticks make a 1 ms delay?
- ProblemFind the tick count for a 1 ms delay for the timer in Figure 2.
- Given / findfclk = 16 MHz, prescaler = 64, delay = 1 ms. Find the number of ticks.
- AssumptionsTimer increments once per prescaled clock pulse.
- Modeltick period = prescaler/fclk; ticks = delay/tick period.
- Equationstick = 64/16×106 = 4 µsticks = 10−3/4×10−6
- Solveticks = 10−3/(4×10−6) = 250 ticks.
- Check250 ticks × 4 µs = 1000 µs = 1 ms, as required.
- ConclusionLoading a compare value of 250 gives an exact 1 ms interrupt, the basis of a steady control loop rate.
Misconceptions and diagnostics
| Mistake | Symptom | Diagnostic question | Correction |
|---|---|---|---|
| Using TOP not TOP + 1 | Frequency slightly high | "Does the count include zero?" | The period spans TOP + 1 counts. |
| Prescaler multiplied into frequency | Frequency far too high | "Does the prescaler slow the timer?" | The prescaler divides the clock. |
| Blocking delay in the loop | Control loop stutters | "Am I busy-waiting?" | Use a timer or interrupt, not a blocking delay. |
| Polling instead of interrupts | Missed fast events | "Can this be an interrupt?" | Handle time-critical events by interrupt. |
Practice ladder
A timer on an 8 MHz clock has a prescaler of 1 and counts 1000 steps per cycle. Find the PWM frequency.
Show answer
f = 8×106/(1 × 1000) = 8000 Hz.
A 16 MHz timer has a prescaler of 8. Find the tick period, then the ticks for a 100 µs delay.
Show answer
Tick = 8/16×106 = 0.5 µs; ticks = 100/0.5 = 200 ticks.
You want a 50 Hz servo PWM from a 16 MHz clock with a prescaler of 8. What TOP is needed?
Show answer
TOP + 1 = fclk/(prescaler × f) = 16×106/(8 × 50) = 40000, so TOP = 39999.
Configure a timer to blink an LED at exactly 2 Hz from a 16 MHz clock. Choose a prescaler and a compare count for the half-period.
What good work looks like
A 2 Hz blink toggles every 250 ms; with a prescaler of 256 the tick is 16 µs, so 250 ms is 15625 ticks, within a 16-bit timer; state prescaler 256 and a compare of 15625.
Working with AI, and proving it yourself
Use AI as an examiner, not a solver
Portfolio task
Configure a timer for one real need: a PWM frequency or a loop-rate interrupt, and show the prescaler and count you chose.
Retrieval and spaced review
Closed notes. Answer out loud, then reveal.
1. Write the PWM frequency.
fclk/(prescaler × (TOP + 1)).
2. Write the tick period.
Prescaler divided by the clock frequency.
3. Why TOP + 1?
The counter spans 0 to TOP inclusive, which is TOP + 1 steps.
4. What is an interrupt for?
To handle an event immediately instead of polling for it.
5. Why avoid blocking delays?
They stall the control loop; use timers and interrupts instead.
Textbook mapping
This module follows William Bolton, Mechatronics, 6th edition. Use these references to read further.
| Topic in this module | Where to read more |
|---|---|
| Microcontroller architecture | Bolton, Chapter 15, Microprocessors and microcontrollers |
| Input and output systems | Bolton, Chapter 18, Input/output systems |
| Embedded programming | Bolton, Chapter 17, C language |
Chapter numbers refer to Bolton's Mechatronics, 6th edition. Any edition with the same chapter titles is equivalent for study.