Professional Documents
Culture Documents
development
Presented by developerWorks, your source for great tutorials
ibm.com/developerWorks
Table of Contents
If you're viewing this document online, you can click any of the topics below to link directly to that section.
1. Introduction.............................................................. 2
2. Microcontroller basics ................................................. 5
3. Development tools ..................................................... 8
4. Circuit design ........................................................... 13
5. Summary & resources................................................. 16
Section 1. Introduction
A dedicated device refers to equipment built for a single, functional purpose, rather
than a design around a general-purpose computer. The dedicated device employs just
enough computational horsepower to get the job done in a cost-effective manner. The
"brain" of these dedicated devices is often a microcontroller. A classic wireless
application involving a microcontroller-enabled device is illustrated by a technician
performing field diagnostics on a device such as a fire sensor or piece of networking
equipment. This application is made possible by the inherent logic and communications
capabilities of the device itself. Without the myriad of microcontroller-based appliances
and equipment in the field, an entire classification of wireless applications would be lost
to the modern world. This tutorial begins to explore this "other side of wireless" by
examining the fundamentals of microcontroller application design and development.
Roadmap
This tutorial presents an introduction to microcontroller application design and
development, and includes both hardware and software components in the design of a
functional sample application. The sample application illuminates a series of
light-emitting diodes based on the application's low-level instructions. This design is
meant to be a starting point for someone seeking to learn about embedded application
development. The tutorial includes circuit explanation, circuit layout, application source
code, a parts list, required tools, and all of the information needed to build and test the
sample application.
Full source code and diagrams are available for download from the Resources on page 16
section. These include:
Helpful terms
The following terms are used throughout this tutorial:
Motivation
Technology as a whole is often regarded in a bipolar manner, divided between
hardware and software. Hardware represents anything with mass -- something taking
up space. Common examples of hardware include computers and printers, or discrete
components such as integrated circuits, resistors, capacitors, memory chips, quartz
clocking crystals, etc. Software, on the other hand, is non-physical. Examples of
software include an operating system, a word processor, or a game played on a
personal computer. Beyond these physical characteristics, hardware and software are
also separated by the skills commonly needed to construct them. Traditionally,
hardware design and software development are divided between specialists in each
respective field. Microcontrollers present an exciting opportunity for both groups.
design engineers enjoy flexible and rapid design iterations without the need to redesign
and construct discrete logic chips. The final design may utilize a microcontroller, or be
built from discrete components for faster operation. In either case, the microcontroller
aids in the design process, resulting in time- and cost-saving opportunities.
For the software engineer, microcontrollers offer at least two exciting opportunities. The
first is liberation. No longer is the end product relegated to a display monitor, but now
an application has a physical embodiment. The other, more important aspect of
microcontroller development is a fine-tuning of software programming skills.
Programming applications for limited-resource devices require more discipline and
precision than developing applications for the resource-rich and forgiving desktop and
server arenas.
A common phrase of our time is, "computers are everywhere." Any modern office
building contains as many computers as desks, if not more. These computers are
personal computers, midrange computers, and beyond. However, other industries
employ computers as well, although they are not all "personal computers." One of the
largest consumers of computer technology is the automotive industry. Each vehicle
rolling off the assembly line contains one or more computers. They govern fuel
injection, environmental control, global positioning, and more. These are not personal
computers, but rather application-specific computers controlling vital vehicle functions.
These devices are more cost-effective and reliable than personal computers for their
specialized applications.
The home is another place where a variety of microcontrollers might be found. Any
device that is "programmable," such as a microwave oven or a VCR, likely contains a
microcontroller class computer. As homes and devices get "smarter," microcontroller
deployment opportunities increase. Modern home automation designs call for
PC-based applications directing various embedded devices throughout the dwelling.
These embedded devices require limited sensory, communications, and
decision-making capabilities. The microcontroller is the perfect fit.
Popular architectures
The core design of microcontrollers varies by manufacturer. Some devices employ the
Von-Neumann (or Princeton) architecture; others, the Harvard architecture. The
Von-Neumann architecture employs a single memory space for both program code and
working data storage (variables). The Harvard architecture maintains one distinct
memory space for program code and another for variable storage. The Harvard
architecture offers a high degree of "parallelism" in its ability to fetch instructions while
simultaneously carrying out previously fetched instructions on variable or register data.
The Von Neumann design allows for flexible operations not possible in the dual
memory architecture of the Harvard design. Both designs have strong points, and
should be considered on a project-by-project basis.
No matter the embedded design, there is likely a microcontroller properly sized and
suited to do the job. For more information on selecting an appropriate part for a project,
please see the Resources on page 16 section.
The Microchip line of microcontrollers is known as the PIC family. There are five basic
lines to the PIC family:
Each of these device families is source level-compatible, and the product line continues
to mature.
The 'F' in the part number stands for flash. A flash part is capable of being configured
via a device known as a programmer. Flash parts can be reprogrammed hundreds of
times. The advent of flash technology and its significance to the embedded design
industry is remarkable; it is a topic unto itself. Suffice it to say for the purpose of this
tutorial, flash technology aids in the edit, compile, and debug cycles by allowing the
same part to be efficiently programmed numerous times. Another common name for a
programmer is an EEPROM burner. Not all programmers are alike. Programmers and
burners are differentiated by the size and shape of the devices they can accept, their
programming speed, and the variety of supported chips.
Examples and instruction supplied in this tutorial are based on the 16F84 part and
programmer available in the PICStart kit. The sample application can be ported to other
Microchip parts with little effort.
These tools are freely available from Microchip's Web site. See the Resources on page 16
section for details on obtaining these tools.
MPLAB
Perform the default installation of the MPLAB software and then start MPLAB. Take a
brief look through the various menu options to get a feel for the available choices.
MPLAB is broken into the following menu choices:
The PIC is organized into a series of file registers, each 8 bits wide. Instructions can
access a register directly, indirectly, or use a literal value. All PIC microcontrollers have
a special register known as the 'W' register. This is a working register, used as the
implied source and optional destination for many operations. Here are some examples:
movlw 0x01 ; moves the literal value 0x01 into the w register.
movwf R ; moves the value in the w register into the register, R
addwf R ; add the contents of the w register to R, leaving results in R
incf R ; increment value in R by 1, leaving result in R
incrsz R ; increment value in R by 1, if R becomes zero,
; skip the next instruction
There are also bit-oriented instructions used to set, clear, or test a bit in a register or
port. Here are some examples:
INCLUDE "p16f84.inc"
org 0x00 ; set program memory base at vector reset
reset
bsf STATUS, RP0 ; Goto Bank 1 to set Port Direction
movlw 0x00 ; set all of portb to output
This program begins by including information specific to the 16F84 processor with the
INCLUDE directive.
The PIC architecture "multiplexes" access to various registers by organizing them into
register banks. Setting the RP0 bit in the STATUS register provides access to the
TRISB register. This register controls the input/output state of PORTB. Once the
TRISB register has been updated, the register bank is restored.
The main loop of the program simply sets and clears various bits in PORTB. These bits
control pins RB0, RB1, RB2, and RB3. The pins control the current flow in the circuit.
When the value is cleared, or low, current flows through an LED causing it to illuminate.
The output of an MPLAB project is a HEX file. The HEX file contains the low-level
information required by the PIC device.
Select the Project | Edit Project menu choice. This displays a view of the ibmdw project
in MPLAB.
The target of the project is the HEX file mentioned above. The target filename is
ibmdw.hex . The project contains a single node, namely: ibmdw.asm . If the project
contained multiple source files, each would appear as a node beneath the ibmdw[hex]
line in the Project Files window.
* Run: F9
* Reset: F6
* Halt: F5
* Step: F7
* Step-Over: F8
Under the window menu choice, select Watch Windows | New Watch Window.
Select PORTB from the available choices in the list to the left. Click on the Add button.
As each line of code is executing during the debugging process, the Watch Window's
contents update to reflect current register values. The window can contain variables,
I/O ports, and registers. Debugging the tutorial's sample application is not very exciting,
but verifying the installation, including the ability to debug, is an important step in
setting up a productive development environment.
Connect the programmer to the PC via an available serial port. Place the 16F84 part
into the programmer, aligning it in the top-most pin slots. The 16F84 (and most
integrated circuits) have a semi-circle on the end near pin number 1. Place this end
toward the top of the programmer. Raise the silver handle to secure the part.
Next, select the PICStart Plus | Enable Programmer menu choice. This causes
MPLAB to find the PICStart programmer attached to one of the PC's serial ports. When
the programmer is found, MPLAB displays a small dialog window containing important
settings for the programmer. Click on the Configuration Bits button. Another, smaller
window is displayed. Make the following selections:
* Oscillator: XT
* Watchdog Timer: Off
* PowerUp Timer: On
* Code Protect: Off
Click on the Program button on the PICStart window in MPLAB. Another window
appears displaying the status of the programming process. The result should be
Success. The status window closes a moment after the programming is complete.
* Power: The chip needs a 5-volt source to operate with sufficient amperage to run
the PIC and accompanying peripherals.
* Clocking: The clock provides timing for the instructions of the microcontroller to
operate. Without a clock signal, the PIC sits dormant, without a heartbeat.
A PIC operates quite happily with power and a clock source, although it doesn't do
anyone much good running all by itself. A useful PIC circuit needs additional
capabilities to provide value. Switches, sensors, serial interfaces, feedback
mechanisms such as LEDs, relays, buzzers, and the like provide the necessary glue
for a microcontroller application.
In addition to the overtly useful support components mentioned above, there are
discrete components used to keep the circuit healthy. The most common examples of
this in introductory circuit design include current limiting resistors to protect LEDs, and
power line filtering capacitors to stabilize the power source for the microcontroller.
* A quartz crystal is connected across pins 15 and 16, providing a clocking source.
PIC circuit clocking ranges from a simple Resistor/Capacitor (RC) circuit to 4, 7,
12, and even 20 MHz crystals.
* A capacitor filters the power source.
* The MCLR pin is "pulled high," allowing the part to run and stay out of the reset
condition.
* Pins 14 and 5 provide power and ground respectively.
* The main loop of the program clears bits in PORTB, attached to pins 6, 7, 8, and
9. This creates a potential across the LEDs, allowing current to flow, and thus
illuminating them.
* Current limiting resistors are employed to protect the LEDs and the PIC.
Bill Of material
The parts list for this project is very modest:
Item Quantity
5 volt power supply 1
Quartz Crystal (4 or 7 MHz is fine). A simple RC Oscillator is sufficient 1
as well
.1 micro-Farad Capacitor to filter power supply 1
10K ohm resistor to "pull up" MCLR pin 1
LED array 1
Or single LED 4
220 ohm resistor (one per LED) 4
Jumper wires handful
PIC16F84 1
UMPS
There is an additional tool available for prototyping circuit designs warranting mention.
The Universal Microprocessor Program Simulator (UMPS) is a Windows-based tool
that allows rapid prototyping of microcontroller applications and circuit designs. It
models components outside of the processor itself, such as switches, serial interfaces,
latches, and more. This ability to simulate external components takes the development
process to the next level by affording the designer the luxury of simulating a design
prior to purchasing parts and undergoing the task of circuit layout.
Summary
This tutorial has presented the basics of microcontroller application design for the
beginner. From this introductory framework, more complex applications can be built.
Next steps for the aspiring embedded developer call for enhancements to the sample
application and circuit. These enhancements might include capturing user input via a
keypad or interacting with another device by way of a serial connection. The only
limitation to embedded development is the imagination.
Resources
* You can find sample source for this tutorial at http://www.cfgsolutions.com. Follow
the IBM developerWorks link on the left.
* The developerWorksPalm OS Data Collection Series discusses a potential
embedded application. The article and tutorials may be found at
http://www-106.ibm.com/developerworks/wireless/library/wi-edpick.html .
* Myke Predko has written many quality books on embedded development. Check
out his Web site at http://www.myke.com.
* Find the PicList mailing list at http://www.piclist.com.
* Microchip offers free development tools. Download MPLAB at
http://www.microchip.com/11110/pline/tools/index.htm.
* Selecting the proper microcontroller is a crucial step in the success of a project.
This guide enumerates all of the current Microchip parts:
http://www.microchip.com/11110/suppdoc/index.htm.
* Find available C compilers for Microchip parts at
http://www.microchip.com/11110/pline/tools/picmicro/code/third/index.htm.
* Parallax markets the Basic Stamp line of microcontrollers. Learn more at
http://www.parallax.com.
* Jan Axelson has contributed many fine volumes to PC and embedded design.
Visit her site at http://www.lvr.com.
* Microchip offers many Application Notes providing helpful ideas on PIC-based
projects. Visit the Microchip site at
http://www.microchip.com/11110/suppdoc/appnote/index.htm.
* You can find UMPS at http://www.vmdesign.com.
* Darrick Addison's developerWorks article on Robotics provides some insight into
the use of microcontrollers in automation:
http://www-106.ibm.com/developerworks/library/l-rob.html .
* For a glimpse at assembly language on the Intel X86 platform, see this
developerWorks article: http://www-106.ibm.com/developerworks/library/l-ia.html .
Feedback
Please send us your feedback on this tutorial. We look forward to hearing from you!
Colophon
This tutorial was written entirely in XML, using the developerWorks Toot-O-Matic tutorial
generator. The open source Toot-O-Matic tool is an XSLT stylesheet and several XSLT
extension functions that convert an XML file into a number of HTML pages, a zip file, JPEG
heading graphics, and two PDF files. Our ability to generate multiple text and binary formats
from a single source file illustrates the power and flexibility of XML. (It also saves our
production team a great deal of time and effort.)