Lesson 2: Architecture of a Microcontroller.

A microcontroller is really just a complete computer system integrated onto a single chip. The main parts are shown in the figure below:

Diagram showing parts of microcontroller.

Arithmetic Logic Unit (ALU) — This is the core 'digital machine' that steps through the program's instructions, interprets them, and updates the data memory and peripherals based on what those instructions tell it to do.

Working Register (W) — This is single byte storage location that the ALU uses to keep temporary data it is currently working on. To simplify the design of the ALU, most instructions require one of the operands to be present in this register so it is used extensively. Note that in microcontroller speak a 'register' is nothing more than a memory location. The value of the working register is shown in the Simulation Window of the simulator.

Data Memory — In the PIC12F508 there are two separate memory arrays (called 'spaces') for storing code and data. The first of these, the data memory, is what you would normally think of as memory in a high level language. It is essentially just an array of 31 bytes of RAM that you can read or write from in your code and use to store variables. As the chip is an 8-bit processor the size of each data memory location is a single byte, and you can view its contents in the simulator using the Data Memory Window on the left side of the screen.

Special Function Registers (SFRs) — The first six bytes in the data memory array are pre-defined variables reserved by the system. These are called the Special Function Registers and they allow your code to access things like the external interface pins and internal ALU status flags. In the simulator you can see these registers have already been given names that correspond to those shown on Page 20 of the datasheet. We will cover what each of the SFRs do in later lessons, but for now just remember that you can't use them in your code for storing your own data.

Program Memory — This second memory space stores the actual machine instructions that make up your code. Each location in the program memory array can store exactly one instruction, and the ALU can process one instruction every four clock cycles. Unlike the data memory, program memory is physically implemented with non-volatile FLASH which means that your code is not lost when the chip's power is turned off. The FLASH can be programmed using a special tool that must be physically connected to the chip's pins.

Each location in the program memory is 12-bits wide. This is because it is storing patterns of bits that encode the instructions, not data values. These patterns are called op-codes, and in the PIC12F508 12-bits is enough to encode all the instructions the designers wanted to include.

In the simulator you can see the program memory contents in the Program Memory Window. Rather than having to enter the 12-bit op-codes directly into this array, the simulator lets you use a set of mnemonics for them instead. These mnemonics are what we refer to as assembly language.

Program Counter — The program counter is nothing more then an index the ALU uses to keep track of which instruction in program memory it needs to fetch and process next. The program counter stores the address of this instruction and is automatically incremented by one after each instruction cycle. In this way the ALU steps its way through each location in program memory and runs your code. The ALU also understands special instructions that allow your code to modify the current program counter value and these can be used to implement branches and jumps. We will learn more about these in Lesson 11.

In the simulator the program memory location that is currently indexed by the program counter is highlighted yellow. This allows you to visually see the ALU stepping through your code as it runs.

Peripherals — These are blocks of logic circuitry that enhance the system with special features. On more advanced devices these might include an LCD driver or USB interface, but on the PIC12F508 there is only a very simple timer (that keeps track of the number of clock ticks) and the General Purpose Input Output (GPIO) module that allows the ALU to control the external interface pins. We will learn more about this in Lesson 12.

Next Lesson >>