Lesson 3: How it Works

Now that we know what the main parts of a microcontroller are lets have a look at how these all come together to run an actual program.

A useful analogy for a microcontroller is to imagine a human operator standing between two sets of shelves. The operator is essentially the ALU, while the shelves represent the separate data and program memory arrays. On the data memory shelf is a set of boxes (31 in our PIC12F508) that can each contain a piece of paper with a single data value written on it. On the program memory shelf is another set of boxes that each contain a simple instruction that the operator can understand and complete. Unfortunately, the operator is not very smart so we give them a notepad that they can uses to record a value temporarily (this is their working register) and we must also ensure that the instructions in the program memory boxes are kept extremely simple.

As the programmer we put together a sequence of these simple instructions and load them into the boxes on the program shelf.

When the 'computer' starts the operator goes to the first box on the program shelf and reads the instruction we put inside. After completing this they go to the second box and repeat the process, progressively working their way through all the boxes on the program shelf. Each instruction tells the operator how to manipulate the information stored in the boxes on the data shelf, and in this way useful calculations can be performed. An example of what a simple 'program' might look like this:

[Box 1] Write the number 10 on your notepad. [Box 2] Copy the number on your notepad into the first box on the data shelf. [Box 3] Write the number 20 on your notepad. [Box 4] Add the number inside the first box on the data shelf to the number on your notepad. [Box 5] Copy the number on your notepad into the second box on the data shelf.

After the program has run the second box on the data shelf will now contain the value 30. This is exactly how a real computer works. The ALU steps through the instructions stored in program memory and manipulates the data memory as requested by them. When we program in assembler we must manually arrange lots of these simple instructions in order to put a program together.

In the next section we will learn about the various instructions that the PIC12F508 understands.

Next Lesson >>