EuraStudy
Notes/Electronics/Microcontrollers
Notes · ElectronicsUK · A-Levels

Microcontrollers

A microcontroller replaces hard-wired logic with a small programmable computer on a chip. This chapter develops the internal structure of the PIC microcontroller, the input-process-output systems view expressed as flowcharts, the essentials of assembler programming and port control, and the reasoned choice between a programmable and a hard-wired solution.

4 sections·~16 min reading time·3 competencies·Level Standard 2 · Advanced 2

T·121212 / 16
Exam profile
AO1 · Describe the internal structure of a PIC microcontroller and the function of its blocks.AO2 · Construct and interpret flowcharts and simple assembler programs that read inputs and drive outputs.AO3 · Evaluate a programmable solution against a hard-wired one for a given application.
Operators:describeinterpretconstructanalyseevaluatejustify

basic level

At AS the focus is the idea of a programmable device and the input-process-output flowchart.

higher level

The full A-Level develops the PIC architecture, assembler and port control, and the evaluation of programmable versus hard-wired designs.

Depth

Reading depth: In depth

Text

Text size: Standard

Contents · 4 sections▾
  1. Microcontrollers
    • 01Inside the PIC◐
    • 02The systems view and flowcharts◐
    • 03Assembler and ports●
    • 04Programmable versus hard-wired●
§ 01

Inside the PIC#

●●○StandardLPWJEC/Eduqas A Level Electronics — Unit 4 Digital and Communication (microcontrollers)

PIC microcontroller architecture

PIC architectureGraph, Clock oscillator → CPU (ALU + W), Program memory (flash) → CPU (ALU + W), CPU (ALU + W) → Data memory (RAM), Data memory (RAM) → CPU (ALU + W), CPU (ALU + W) → I/O ports, I/O ports → CPU (ALU + W), CPU (ALU + W) → Timers / countersClockoscillatorCPU (ALU + W)Programmemory (flas…Data memory(RAM)I/O portsTimers /countersinstructionsdataI/O
Fig. 1The PIC's blocks around the CPU: program memory (flash), data memory (RAM), I/O ports and timers, paced by the clock.

Key points

A microcontroller is a complete small computer on a single chip: it contains a processor, memory, input and output ports and timers, all built to control a piece of equipment. The PIC family is a common example. Its central processing unit (CPU) fetches instructions one at a time from program memory, decodes them, and executes them using an arithmetic and logic unit (ALU) and a working register (called W). Everything the device does is the result of this fetch-decode-execute cycle running through a stored program.
The PIC keeps its program and its data in separate memories — a Harvard architecture. Program memory is non-volatile flash memory that holds the sequence of instructions and retains it when the power is off; data memory is volatile RAM that holds the variables and the special-function registers the program works with while it runs. Understanding that the program lives in flash and the working data in RAM explains why a microcontroller remembers its program but forgets its variables at power-down.
The connection to the outside world is through input/output ports, groups of pins that can each be configured as an input or an output under program control. A direction register (called TRIS on the PIC) sets whether each pin reads a signal in or drives a signal out, and a data register (PORT) holds the value read from, or written to, those pins. Sensors and switches connect to input pins; LEDs, transistors and other actuators connect to output pins. The ports are where the input-process-output structure meets the physical circuit.
A clock oscillator sets the pace at which instructions are executed, and on-chip timers and counters measure time and count events independently of the main program — used for generating delays, timing pulses and counting inputs. Together with the ports, the timers make the microcontroller a self-contained control system: it can sense inputs, keep time, make decisions and drive outputs, all without external logic. This integration is what makes a microcontroller so powerful and compact.
Seeing the PIC as a set of cooperating blocks — CPU, program memory, data memory, ports and timers, linked by internal buses — is the systems view that makes it comprehensible. Each block has one job, and a program orchestrates them: read a port, process the value in the CPU using data memory, write to another port. This is precisely the input-process-output model of the whole course, now realised in software running on one chip rather than in wired-up gates.
fetch→decode→execute\text{fetch} \to \text{decode} \to \text{execute}fetch→decode→execute

Instruction cycle

The CPU repeatedly fetches, decodes and executes stored instructions.

Worked example

Where the program and data live

A PIC controls a battery-powered timer. Explain what happens to its program and its stored count when the battery is briefly removed and replaced.

  1. 01The program

    The program is held in non-volatile flash program memory, so it survives the loss of power and is still there when the battery returns.

  2. 02The variables

    The running count is held in volatile RAM (data memory), which loses its contents when power is removed, so the count is lost.

  3. 03Consequence

    On power-up the program runs again from the start, but the count restarts from its initial value unless it was saved to non-volatile memory (EEPROM).

Result: The program survives (flash) but the count is lost (RAM); persistent data would need to be stored in non-volatile EEPROM.

Exam focus

  • Label the blocks of a PIC (CPU/ALU, program memory, data memory, ports, timers, clock) and state the function of each.
  • Explain the difference between program (flash) and data (RAM) memory and the role of the TRIS and PORT registers.

Typical mistakes

  • Saying a microcontroller loses its program at power-down — the program is in non-volatile flash; only the RAM variables are lost.
  • Confusing the direction register (TRIS) with the data register (PORT).

Active revision

State which PIC memory holds the program and which holds the variables, and explain what the TRIS and PORT registers do for an I/O pin connected to a switch.

Active recall

Recall the key points — then reveal.

Sources: WJEC/Eduqas GCE Electronics specification (WJEC / Eduqas)

§ 02

The systems view and flowcharts#

●●○StandardLPWJEC/Eduqas A Level Electronics — Unit 4 Digital and Communication (flowcharts)

Flowchart: switch-controlled LED

Switch-controlled LED flowchartGraph, Start → Read switch input, Read switch input → Switch pressed?, Switch pressed? → LED on, Switch pressed? → LED off, LED on → Read switch input, LED off → Read switch inputStartRead switchinputSwitchpressed?LED onLED offyesnolooploop
Fig. 2A switch-controlled LED: read the input, decide (diamond), drive the output, and loop back — the input-process-output cycle repeated forever.

Key points

A microcontroller program is designed before it is coded, and the standard design tool is the flowchart. A flowchart shows the sequence of operations and the decisions the program makes, using a small set of standard symbols: a rounded box (terminator) for start and stop, a rectangle for a process step, a parallelogram for input or output, and a diamond for a decision that branches the flow. Arrows connect the symbols to show the order in which the steps run.
The flowchart makes the input-process-output structure of the program explicit and visible. A typical control program reads an input (a parallelogram), processes or tests it (a rectangle or a diamond), and drives an output (a parallelogram), then loops back to read the input again. Designing the flowchart first — before writing a single line of code — separates the logic of the solution from the details of the programming language, and is exactly the structured approach the specification expects.
Decisions are the heart of a control program, and they are shown by the diamond. A decision asks a yes/no question — is the switch pressed? has the count reached ten? — and the flow branches one way or the other depending on the answer. Loops are formed by arrows that return the flow to an earlier point, so that the program repeats a sequence continuously (a control system usually loops forever) or until a condition is met. Reading and constructing these branch-and-loop structures is a core skill.
Structured programming builds a program from three basic constructs, all expressible in a flowchart: sequence (steps one after another), selection (a decision that chooses between alternatives) and iteration (a loop that repeats). Any control task, however complex, can be built from these three, which is why the flowchart is such a powerful design tool — it forces the designer to think clearly about the order of operations, the decisions and the repetitions before worrying about syntax.
The flowchart is the bridge between the systems block diagram and the actual code. Where the block diagram shows the hardware sub-systems, the flowchart shows the software behaviour that ties them together, and each flowchart step translates fairly directly into one or a few assembler instructions. Being able to move between a word description of a task, its flowchart, and the assembler that implements it is the central competence of the microcontroller topic.
sequence⋅selection⋅iteration\text{sequence} \cdot \text{selection} \cdot \text{iteration}sequence⋅selection⋅iteration

Structured programming

The three constructs from which any program is built.

Worked example

Interpreting a control flowchart

A flowchart reads a switch, and if it is pressed turns an LED on, otherwise turns it off, then loops back to read the switch. Describe the behaviour and identify the three structured-programming constructs used.

  1. 01Behaviour

    The program continuously reads the switch and drives the LED to match: on while pressed, off while released — a real-time responsive control.

  2. 02Selection

    The decision 'switch pressed?' is a selection: it chooses between the 'LED on' and 'LED off' branches.

  3. 03Sequence and iteration

    Within each branch the steps run in sequence; the loop-back arrow gives iteration, so the whole thing repeats forever.

Result: The LED tracks the switch in real time; the flowchart uses sequence (steps in order), selection (the decision) and iteration (the loop).

Exam focus

  • Construct a flowchart for a stated control task using the correct symbols for terminators, processes, inputs/outputs and decisions.
  • Interpret a given flowchart and describe the behaviour of the program it represents.

Typical mistakes

  • Using the wrong symbol — a decision must be a diamond with two exits, not a rectangle.
  • Forgetting the loop-back arrow, so the program runs once instead of continuously.

Active revision

Draw a flowchart for a program that turns an LED on while a switch is pressed and off when it is released, repeating continuously, using the correct flowchart symbols.

Active recall

Recall the key points — then reveal.

Sources: WJEC/Eduqas GCE Electronics specification (WJEC / Eduqas)

§ 03

Assembler and ports#

●●●AdvancedLPWJEC/Eduqas A Level Electronics — Unit 4 Digital and Communication (assembler)

A short PIC assembler routine

PIC assembler (poll a switch, drive an LED)Table with 2 columns and 6 rows, Data: Instruction · What it does; MOVLW b'00000001' · load W with the bit pattern 00000001; MOVWF TRISB · RB0 becomes an input (switch), RB1-7 outputs; loop: BTFSS PORTB,0 · test switch RB0; skip next if it is 1; BCF PORTB,1 · switch not pressed: LED (RB1) off; BSF PORTB,1 · switch pressed: LED (RB1) on; GOTO loop · repeat the test foreverINSTRUCTIONWHAT IT DOESMOVLW b'00000001'load W with the bit pattern00000001MOVWF TRISBRB0 becomes an input(switch), RB1-7 outputsloop: BTFSS PORTB,0test switch RB0; skip nextif it is 1BCF PORTB,1switch not pressed: LED(RB1) offBSF PORTB,1switch pressed: LED (RB1) onGOTO looprepeat the test forever
Fig. 3A polling routine: TRIS sets the pin directions, then BTFSS tests the switch bit and BSF/BCF drive the LED, with GOTO looping.

Key points

Assembler (assembly language) is the low-level language in which each instruction corresponds to one operation the CPU can perform directly. A PIC program is a sequence of these short mnemonics — such as MOVLW (move a literal value into the working register W), MOVWF (move W into a file register), BSF and BCF (set or clear a single bit), and BTFSS (test a bit and skip the next instruction if it is set). GOTO and CALL change the flow, jumping to a label or a subroutine. Each mnemonic assembles into one machine-code instruction.
Controlling an input/output pin is a two-register job. First the direction register TRIS is written to set each pin as an input or an output — a 1 in a TRIS bit makes the corresponding pin an input, a 0 makes it an output. Then the data register PORT is used: writing to a PORT bit sets an output pin high or low, and reading a PORT bit returns the level on an input pin. Setting up TRIS correctly before using PORT is essential, and getting it wrong is a common bug.
Reading an input is usually done by polling: the program repeatedly tests the relevant PORT bit in a loop and acts on its value. The instruction BTFSS PORTB,0, for example, tests bit 0 of PORTB and skips the next instruction if that bit is 1, which lets the program branch on the state of a switch. Driving an output is done with BSF and BCF on the PORT register, setting or clearing the pin that a transistor or LED is connected to. This poll-and-drive pattern implements the flowchart's read-decide-output loop.
A short program shows how the pieces fit. To make an LED follow a switch, the program sets one pin as an input (the switch) and another as an output (the LED) via TRIS, then enters a loop that tests the switch bit and sets or clears the LED bit accordingly before looping back. Each flowchart box — read, decide, output, loop — becomes one or two assembler instructions, so the translation from design to code is direct and traceable.
Working in assembler gives complete, precise control over the hardware and produces compact, fast code, which is why it is used for the tight timing and direct pin control of small control tasks — and why the specification expects you to read and write short assembler routines. Higher-level languages are easier for large programs, but understanding assembler and the TRIS/PORT mechanism reveals exactly how a microcontroller reads its inputs and drives its outputs, which is the point of studying it here.
TRIS bit=1⇒input,0⇒output\text{TRIS bit} = 1 \Rightarrow \text{input}, \quad 0 \Rightarrow \text{output}TRIS bit=1⇒input,0⇒output

Port direction

The TRIS register sets each pin as input or output before PORT is used.

Worked example

Tracing a polling routine

In the routine above, describe what happens on each pass of the loop when the switch (RB0) is pressed (reads 1) and when it is released (reads 0).

  1. 01Switch pressed (RB0 = 1)

    BTFSS tests RB0; because it is 1 the next instruction (BCF, LED off) is skipped, so BSF runs and the LED is turned on.

  2. 02Switch released (RB0 = 0)

    BTFSS tests RB0; because it is 0 the next instruction is not skipped, so BCF runs and turns the LED off (the following BSF would then turn it on, so a correct program would branch past it).

  3. 03Loop

    GOTO loop returns to the test, so the LED is updated continuously to follow the switch.

Result: When pressed the skip causes the LED to be set on; when released the LED is cleared off; the GOTO makes it repeat, so the LED tracks the switch.

Exam focus

  • Interpret a short PIC assembler routine that configures a port and polls an input to drive an output.
  • Explain the roles of the TRIS and PORT registers and of the BSF, BCF and BTFSS instructions.

Typical mistakes

  • Forgetting to set the pin direction in TRIS before using PORT, so an output pin never drives.
  • Misreading BTFSS — it skips the next instruction when the tested bit is set (1), not when it is clear.

Active revision

Write, in words or as assembler, the steps to configure PORTB bit 0 as an input (a switch) and bit 1 as an output (an LED), then continuously make the LED match the switch.

Active recall

Recall the key points — then reveal.

Sources: WJEC/Eduqas GCE Electronics specification (WJEC / Eduqas)

§ 04

Programmable versus hard-wired#

●●●AdvancedLPWJEC/Eduqas A Level Electronics — Unit 4 Digital and Communication (design choice)

Programmable versus hard-wired

Programmable versus hard-wiredTable with 3 columns and 5 rows, Data: Aspect · Microcontroller · Hard-wired logic; Changing function · reprogram software · redesign and rewire; Component count · one chip · many gates; Speed · slower (fetch-execute) · faster (dedicated); Development · flexible, quick to change · fixed once built; Best for · complex, changeable tasks · simple, fast, fixed tasksASPECTMICROCONTROLLERHARD-WIRED LOGICChanging functionreprogram softwareredesign and rewireComponent countone chipmany gatesSpeedslower (fetch-execute)faster (dedicated)Developmentflexible, quick to changefixed once builtBest forcomplex, changeable taskssimple, fast, fixed tasks
Fig. 4The trade-offs: the microcontroller offers flexibility and few components; hard-wired logic offers raw speed for simple, fixed tasks.

Key points

A control task can be built either from hard-wired logic — gates, flip-flops and counters connected to do one fixed job — or from a programmable microcontroller running software. The two approaches lead to very different designs, and choosing between them is a genuine engineering decision that the specification asks you to make and to justify. The right choice depends on the task's complexity, how often it might change, the required speed, the production volume and the cost.
The great advantage of the microcontroller is flexibility. Because its behaviour is set by software, the same chip can perform completely different tasks, and its function can be changed or corrected simply by reprogramming it — no rewiring, no new circuit board. A hard-wired design, by contrast, is fixed once built: changing what it does means redesigning and rebuilding the circuit. For any product whose requirements might evolve, or that must be updated in the field, the programmable route is compelling.
The microcontroller also replaces many gates with one chip, which usually means fewer components, a smaller board and lower cost for any task of moderate complexity — a job needing a dozen gates, several flip-flops and a counter often fits in one cheap microcontroller. It also brings timers, memory and arithmetic 'for free', so complex behaviour (counting, timing, sequencing, calculation) that would need a lot of hard-wired logic is just a few lines of code.
Hard-wired logic still wins in some cases. Dedicated gates respond in nanoseconds, with no fetch-execute overhead, so for the very fastest, simplest tasks — a high-speed decoder or a simple combinational function — hard-wired logic can be faster than a microcontroller stepping through instructions. For extremely high production volumes a custom logic chip can be cheaper per unit, and a purely combinational safety interlock may be preferred for its transparency and predictability. The choice is not automatic.
In practice the microcontroller has become the default for all but the simplest or fastest control tasks, precisely because its flexibility, low component count and rich built-in features outweigh its slight speed penalty. A good exam answer weighs the specific demands of the stated application — complexity, changeability, speed, volume, cost — and reaches a justified recommendation, rather than asserting that one approach is always better. This reasoned evaluation is the culmination of the microcontroller topic and a frequent extended-response question.
flexibility vs speed and simplicity\text{flexibility} \ \text{vs} \ \text{speed and simplicity}flexibility vs speed and simplicity

The core trade-off

Programmable gives flexibility and low part count; hard-wired gives raw speed for simple fixed tasks.

Worked example

Choosing the right approach

Recommend a solution for (a) a washing machine with several selectable wash programmes and (b) a simple two-input priority encoder needing the fastest possible response.

  1. 01Washing machine

    The task is complex (timing, sequencing, several programmes) and may need updating, so a microcontroller is ideal: the programmes are software, changeable without rewiring, and one chip replaces much logic.

  2. 02Priority encoder

    The task is trivial and purely combinational but must be as fast as possible; hard-wired gates respond in nanoseconds with no instruction overhead, so hard-wired logic is the better choice.

  3. 03Justification

    The decision follows the demands: complexity and changeability favour the microcontroller; raw speed and simplicity favour hard-wired logic.

Result: Use a microcontroller for the washing machine (complex, changeable) and hard-wired logic for the priority encoder (simple, fastest).

Exam focus

  • Compare a programmable and a hard-wired solution on flexibility, component count, speed and cost.
  • Justify a recommendation for a stated application, weighing its complexity, changeability, speed and volume.

Typical mistakes

  • Claiming a microcontroller is always better — hard-wired logic can be faster for simple, high-speed tasks.
  • Ignoring the specific demands of the application when justifying a choice.

Active revision

Recommend, with reasons, whether to use a microcontroller or hard-wired logic for (a) a washing-machine controller with several programmes, and (b) a simple, very fast two-input priority encoder.

Active recall

Recall the key points — then reveal.

Sources: WJEC/Eduqas GCE Electronics specification (WJEC / Eduqas)

Contents

Section -- / 04

    • 01Inside the PIC◐
    • 02The systems view and flowcharts◐
    • 03Assembler and ports●
    • 04Programmable versus hard-wired●

0/4 Read

From notes into training

Microcontrollers

Reinforce this topic with matching tasks from the question bank.

~16
min
3
Competencies
Practise

References & sources

Sources

WJEC / Eduqas

  • WJEC/Eduqas GCE Electronics specification

Previous topic

Audio systems

Next topic

Communications systems

EuraStudy·Notes T·12·MMXXVI

Carry on to the next topic — your learning path is kept.