Embedded Systems and Microcontrollers

CS 301 Lecture, Dr. Lawlor

Wristwatches, microwave ovens, keyboards, mice, and zillions of other gadgets often have little CPUs inside them.  These are called "embedded" processors, because they're stuck inside some other box; or "microcontrollers", because they're a microcomputer that controls some other system.  Most folks don't even think of them as computers, but these tiny machines often are fully programmable.

I've hooked up one of my Microchip(tm) PIC microcontrollers to NetRun.

PIC in C

I've included a few little utility functions in the NetRun PIC environment:
Note that embedded code is almost always an infinite loop--the controller's whole reason for existing is to do some task, and it's usually supposed to continually do that task.  In NetRun if your code ever returns, I light LED 0 (the bottom-left LED), to let you know something's wrong.

PIC Assembly

When you need to count clock cycles, or access special hardware features, assembly is quite useful.  Here's the (quite simple!) instruction set for PIC microcontrollers:
Instruction set for PIC microcontrollers

"w" is the only register the thing has.  "f" stands for a memory address (up to 128 bytes).  "k" stands for a program memory address (up to 2048 instructions).  "d" is the "direction bit"; it determines whether the memory location f or the register w receives the result.

Notice that:
You can read some software serial code I wrote for the PIC.  The "serial.c" file contains some carefully-timed assembly code to do serial communication.

Build One Yourself!

Here's the USB device programmer I used (with my own "usb_pickit" tool to upload the program).

Here's the underlying PIC hardware documentation.  (The table of instructions shown above is on page 72).

Here's how to build your own circuit boards.

Here's a cheap source of servos, and the servo communication protocol.