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.

01

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.
0 or 1 weak itemsContinue with this module.
2 weak itemsRevisit program structure in Programming and Computation.
3 or more weak itemsRevisit data acquisition in Module 4.
02

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 / prescaler

The 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 skill works when: you divide the clock by the prescaler and by TOP plus one, counting the extra one.
The skill breaks down when: TOP is used instead of TOP plus one, or the prescaler is multiplied into the frequency instead of dividing it.
The concept. One chip holds the processor, memory, and peripherals. Timers divide the clock into exact PWM and delays while the CPU runs the control loop.
03

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.

SettingEffectIn the formula
Prescalerdivides the clocklarger slows the timer
TOPcounts per cycleuses TOP + 1
Compare valuesets dutyfraction 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.

04

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.

Figure 1. The timer counts 10000 prescaled steps per cycle. With the clock divided by 8 and by 10000, the PWM repeats 200 times per second.
  1. ProblemFind the PWM frequency for the timer in Figure 1.
  2. Given / findfclk = 16 MHz, prescaler = 8, TOP = 9999. Find the PWM frequency.
  3. AssumptionsTimer counts 0 to TOP inclusive, then resets (so TOP + 1 steps).
  4. ModelPWM freq = fclk/(prescaler × (TOP + 1)).
  5. EquationsTOP + 1 = 10000f = 16×106/(8 × 10000)
  6. Solvef = 16×106/80000 = 200 Hz.
  7. CheckThe prescaled clock is 2 MHz; 2×106/10000 = 200 Hz, matching.
  8. ConclusionThe two divisions turn a 16 MHz clock into a clean 200 Hz PWM, suitable for a motor drive.
Result. PWM frequency 200 Hz.
05

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?

Figure 2. With a prescaler of 64 each tick lasts 4 microseconds, so a 1 millisecond delay is 250 ticks.
  1. ProblemFind the tick count for a 1 ms delay for the timer in Figure 2.
  2. Given / findfclk = 16 MHz, prescaler = 64, delay = 1 ms. Find the number of ticks.
  3. AssumptionsTimer increments once per prescaled clock pulse.
  4. Modeltick period = prescaler/fclk; ticks = delay/tick period.
  5. Equationstick = 64/16×106 = 4 µsticks = 10−3/4×10−6
  6. Solveticks = 10−3/(4×10−6) = 250 ticks.
  7. Check250 ticks × 4 µs = 1000 µs = 1 ms, as required.
  8. ConclusionLoading a compare value of 250 gives an exact 1 ms interrupt, the basis of a steady control loop rate.
Result. 250 timer ticks for 1 ms.
06

Misconceptions and diagnostics

MistakeSymptomDiagnostic questionCorrection
Using TOP not TOP + 1Frequency slightly high"Does the count include zero?"The period spans TOP + 1 counts.
Prescaler multiplied into frequencyFrequency far too high"Does the prescaler slow the timer?"The prescaler divides the clock.
Blocking delay in the loopControl loop stutters"Am I busy-waiting?"Use a timer or interrupt, not a blocking delay.
Polling instead of interruptsMissed fast events"Can this be an interrupt?"Handle time-critical events by interrupt.
07

Practice ladder

Level 1 · Direct skill

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.

Level 2 · Mixed concept

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.

Level 3 · Independent problem

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.

Transfer task | Real engineering

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.

08

Working with AI, and proving it yourself

Use AI as an examiner, not a solver

"Check that I used TOP + 1 and divided by the prescaler in this PWM frequency."
"Give me three timer settings; I will compute each frequency or delay."
"Write my firmware." Deriving the timer values is the skill.
"What prescaler should I use?" Solve it from the target frequency.

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.

Must include: a prescaler, a TOP or compare value, and the resulting frequency or delay checked.
09

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.

TodayFinish this quiz and Levels 1 and 2 of the ladder.
+1 dayRe-derive a PWM frequency and a delay count.
+3 daysConfigure one timer for a real task.
+7 daysMove on to PLCs and ladder logic in Module 9.
+30 daysReuse the clock, prescaler, TOP + 1 chain on any timer.
10

Textbook mapping

This module follows William Bolton, Mechatronics, 6th edition. Use these references to read further.

Topic in this moduleWhere to read more
Microcontroller architectureBolton, Chapter 15, Microprocessors and microcontrollers
Input and output systemsBolton, Chapter 18, Input/output systems
Embedded programmingBolton, Chapter 17, C language

Chapter numbers refer to Bolton's Mechatronics, 6th edition. Any edition with the same chapter titles is equivalent for study.