By
Randy Innerarity
Physics 475.001
Summer I, 2003
The original Word file and other documents can be found here:
http://observe.phy.sfasu.edu/courses/phy475/Innerarity2003/Phy475/
Our
modern world sometimes seems awash in remarkable new developments and
products. Most of them are interesting
but their utility can run the gamut from ridiculous to truly revolutionary. Occasionally, a product is so significant
that it reshapes an entire industry. The
digital electronics field has been experiencing such an event and it's due, in
part, by an amazing little device.
According to the popular WWW.howstuffworks.com web site1,
these devices are in a surprising number of ordinary products. They are in kitchen appliances, TVs, VCRs,
cameras, telephones, and many others.
Modern automobiles contain at least one, and perhaps as many as six of
them. What is this device that is
changing our world before our very eyes?
It's called a microcontroller.
In
the PIC Microcontroller Project Book2, author John Iovine defines a microcontroller as a single chip
computer. This definition refers to the
fact that they contain all of the functional sections (CPU, RAM, ROM, I/O,
ports and timers) of a traditionally defined computer on a single integrated
circuit. The WWW.howstuffworks.com web
site1 goes a bit further with their definition of a
microcontroller. They describe them as special
purpose computers with several qualifying distinctions that separate them
from other computers. Quoting directly
from the web site:
"If a computer matches a majority of these
characteristics, then you can call it a microcontroller:
Microcontrollers are "embedded" inside
some other device (often a consumer product) so that they can control the
features or actions of the product.
Another name for a microcontroller, therefore, is "embedded
controller."
Microcontrollers are dedicated to one task and
run one specific program. The program is
stored in ROM (read-only memory) and generally does not change.
Microcontrollers are often low-power devices. A
desktop computer is almost always plugged into a wall socket and might consume
50 watts of electricity. A battery-operated
microcontroller might consume 50 milliwatts.
A microcontroller has a dedicated input device
and often (but not always) has a small LED or LCD display for output. A microcontroller also takes input from the
device it is controlling and controls the device by sending signals to
different components in the device.
A microcontroller is often small and low cost.
The components are chosen to minimize size and to be as inexpensive as
possible.
A microcontroller is often, but not always, ruggedized in some way.
The microcontroller controlling a car's engine, for example, has to work
in temperature extremes that a normal computer generally cannot handle. A car's microcontroller in
Clearly, the distinction between a computer and a
microcontroller is sometimes blurred.
Applying these guidelines will, in most cases, clarify the role of a
particular device.
Figure 1.
Figure 2.
The example above is not an obscure case. The effects of this device are being felt in almost every facet of digital design. A sure method of determining the popularity of an electronic device is to note when they attain widespread use by hobbyists. The author of the PIC Microcontroller Project Book2 writes, “The future of electronics is here – and it’s microcontrollers…. It therefore becomes essential that the electronics engineer or hobbyist learn to program these microcontrollers to maintain a level of competence and to gain the advantages microcontrollers provide in his or her own circuit designs.” Certainly, if one were in the business of selling sequence counters it wouldn’t be difficult to decide which design to take to market!
The example in figure 2 uses the
PIC 16F84 microcontroller made by Microchip Technology, Inc of
In the introduction, some parallels were drawn between desktop PCs and microcontrollers. This cannot be done when speaking about architecture. Most desktop computers use von Neumann architecture, which means that programs and data share a single memory area that is accessed over a common bus. This scheme works well for general use computers but may not be of particular advantage for microcontrollers. This is one reason that the PIC microcontroller uses Harvard architecture. This design uses separate memory areas and buses for data and programs2. Harvard architecture allows the instruction bus to be a different width than the data bus. Since the instruction bus is custom tailored, an instruction can be fetched in a single clock cycle. According to Microchip4, the last instruction can execute while the next one is being fetched, all while data memory is simultaneously being accessed.
Figure 3. von Neumann
Architecture. Figure 4. Harvard Architecture.
Microchip
produces field programmable microcontrollers in three basic varieties, EPROM,
OTP, and Flash. The EPROM type can be
programmed electrically and erased, if needed, by a special ultraviolet
light. The device is made with a
transparent window in the center of the chip that allows the UV light to act on
the internal components. The OTP variety
can be programmed only once and is the least expensive of the three. In fact, the term OTP stands for “One Time
Programmable”. This type is used for the
mass production of mature
applications where the program is stable and bug-free. The Flash programmable device can be both
programmed and erased by electrical means.
The PIC 16F84 chip mentioned in the introduction is a flash programmable
device and will be the focus of more attention later in the report. Flash programmable devices are ideal choices
for microcontroller development systems because they can be reused hundreds of
times and retain their memory for over forty years with no power5.
The CPU, or central processing unit, of the PIC chip performs the same functions as the one in a desktop PC. Frequently, it's referred to as the brain of the system. The CPU in the PIC is a RISC chip. RISC is an acronym for Reduced Instruction Set Computer, which means that it has been designed to operate with fewer commands than other types. The PIC has only 35 opcodes in its instruction set compared with more than 256 for a DEC Vax system6. It is generally believed that this produces faster execution speeds and, without question, makes the chip easier to learn. The CPU is responsible for fetching, decoding and executing instructions, and serves as a traffic cop for the program and data buses. It also controls access to a special section of memory called the stack. The stack retains the return address for program execution in the event of a program branch. The CPU must also coordinate operations with the Arithmetic Logic Unit (ALU). This part of the chip performs the math and logic functions as well as bit shifting.
If the CPU is the brain of the
system then the oscillator, or clock, is the heartbeat. It provides the critical timing functions for
the rest of the chip. The original
version of the 16F84 can operate at up to 4-MHz while the newer chip has a
20-MHz upper limit. This section of the
PIC is extremely versatile. There are
three principal ways to clock a PIC
chip. The three clocking methods and their characteristics are listed below.
Figure 5.
The greatest timing accuracy is achieved with a crystal or ceramic resonator. For crystals of 2.0 to 10.0 MHz, the recommended capacitor values should be in the range of 15 to 33pF2. This clocking scheme will provide a 50 ppm accuracy.
Figure 6. RC Oscillator
For non-critical applications, the PIC can function with a resistor/capacitor
(R/C) network to set the oscillator frequency.
The manufacturer recommends an R- value of 5 -10 kW and
a capacitance of greater than 20 pF. There are no formulas for computing the RC
values. Microchip provides this
information the form of graphs given in the device data sheet2.
Figure 7. External clock
The PIC can also be clocked from an external source. The example circuit in figure 2 of the introduction uses this method. This method is useful when the chip must be synchronized with other components or an entire system must be operated from a single clock source2.
The program counter (PC) generates addresses for the "next" instruction to be fetched. It is this number that gets pushed onto the stack when an interrupt or program branch occurs4.
As stated earlier, the PIC 16F84's program memory is separate from its data memory. There is a total of 1K of program memory available across a 14 bit bus. Program memory resides at the addresses from 000h to 3FFh.
|
Figure 8. Register map and addresses. |
Data memory is composed of two main sections, the Special Function Registers (SFRs), and the General Purpose Registers (GPRs). The SFRs are used by the CPU and Peripheral modules to control the operation of the device. These registers are implemented as static ram. The GPRs are used for general data storage and scratch pad operations4. A complete register map with individual addresses is shown in figure eight2. It is helpful to think of the registers merely as a block of individual memory cells that contain control and general data. Learning to manipulate the control registers is the key to fully utilizing the PIC chip. This is a task that could easily make up an entire course. Fortunately, Microchip offers an abundance of free information on its web site at WWW.microchip.com. This report will focus entirely on manipulating the TRISB and PORTB registers.
The PIC 16F84 has two timers, the Timer0 (T0CK) real time clock/counter and the Watchdog Timer (WDT). The Timer0 clock is used for time measurement and event counting and can be incremented by an external or internal signal. Figure 8 shows the control register for Timer0 located at 01h, which can be read and written just like any other register. It can even be pre-scaled if needed.
The Watchdog Timer is a special timer that must be reset at a specific time or it will generate a processor reset. Under normal conditions, the program will reset the WDT at predetermined intervals. If the program happens to get trapped in an infinite loop, leap past the reset point, or lock up, the WDT will trigger a full processor reset. This is the PIC version of what happens when control-alt-delete is pressed on a desktop PC. The only difference is that it happens automatically. The WDT is also used to bring the 16F84 out of sleep mode, a special power saving capability of the chip.
Figure 9 Pin-out of the PIC 16F84.
Figure 10. Generating a hex file.
Programming the PIC chip is a very flexible multi-step process. First, the source code is entered in a common
text file and saved with a .asm
extension. In a process similar to
compiling a C or Fortran program, the assembler
software then transforms the source code file into a form that can be accepted
by
the programmer software. Microchip provides a free assembler called MPASM or one of several "Third Party" software packages can be used. The output file from the assembler will have a .hex extension. This phase of the process is shown graphically in figure ten.
As stated above, the hex file is in a form that can be understood by the programmer software. The programmer software controls the programmer hardware connected to the PC's parallel port. Finally, this hardware/software combination, sometimes referred to as the burner, physically programs the hex file into the PIC chip.
Figure 11. "Burning"
the program into the PIC chip.
Several third-party companies offer assemblers and compilers to generate hex files for the PIC. Most of them use the Basic or C languages but Microchip's free software, called MPASM, uses assembly language. Assembly language is generally considered to be a second-generation language situated between machine code and the higher level languages such as Basic or C. Assembly language uses mnemonic codes that correspond to machine language instructions. Many people don't like assembly language because it is machine dependent. This means that the assembly code for one microprocessor type may not be the same for another.
Even though assembly language is sometimes difficult to learn, it has several powerful advantages. It uses the less memory than any of the high level languages and results in the fastest execution speed. Assembly language also allows for very specialized data manipulation6 and is probably the main reason that Microchip selected it for use with the PIC.
The source code is the raw program to be assembled into a hex file. Source code for the PIC is entered in free form in much the same manner as a C program. Normally, assembler directives are entered first followed by variable declarations. There are a few conventions that must be followed for the actual body of the program. First, labels must start in column one followed in column two and beyond by mnemonics. Operands must follow the mnemonics with any comments after that. The source code is entered and saved into a common text file using a text editor. The example program in Section IV shows a source code listing, complete with comment lines.
The source code for a PIC program consists of three basic types of information: comment lines, assembler directives, and opcodes. It is always a good idea to include comments so that the code is properly documented. Before any opcodes are used in the program, a few instructions must be sent to the assembler so that it will know how to process the other program elements. This task is accomplished through the use of assembler directives. Assembler directives that appear in the source code do not get translated into instructions for the PIC; their only function is to control the assembler. The Microchip MPASM manual lists five types of assembler directives7:
• Control Directives – Control directives permit sections of conditionally
assembled code.
• Data Directives – Data Directives are those that control the allocation of
memory and provide a way to refer to data items symbolically, that is, by
meaningful names.
• Listing Directives – Listing Directives are those directives that control
the MPASM listing file format. They allow the specification of titles, pagination,
and other listing control.
• Macro Directives – These directives control the execution and data allocation
within macro body definitions.
• Object File Directives – These directives are used only when creating
an object file.
This report will only focus on the control and data directives that are used in the example program in Section IV. Full documentation of assembler directives and a wealth of other PIC information can be found at the WWW.microchip.com web site.
The example program shown in Section IV of this report uses six of the most common assembler directives. Five of these are control directives and one is a data directive. They are listed below along with their syntax and meaning.
Control directive (as used) Description
processor 16F84 Tells the assembler which processor will be used.
include <p16f84.inc> Tells assembler to use the header file for the PIC
16F84. Functions like a C language header file.
J equ H'1F' Creates the variable J and stores it at address 1F (hex)
org 0 Sets the beginning program address at zero.
end Marks the end of the program block for the assembler.
Data directive (as used) Description
__config RC_OSC & WDT_OFF &_PWRTE_ON Issues PIC setup instructions.
The config directive is very important. In the example, it tells the assembler to set up the PIC to use an RC oscillator, turn the watchdog timer off, and turn on the power-on reset timer. There are other ways to configure the chip. However, this is the best way because the directives are permanently written into the source code where they are recorded and can be documented.
The CPU, ALU, memory, and support sections of the PIC are collectively called the "core" by Microchip. They are simply the sections that make the chip operate. The instruction set is a collection of all of the opcodes available for use in PIC programs. Since the PIC is a RISC chip, there are only 35 opcodes in the entire instruction set. For the reasons mentioned earlier, Microchip prefers to use assembly language to program many of their microcontrollers. It is sometimes helpful to think of assembly language as the clever manipulation of a matrix of register values and the instruction set provides the tools to get that done. The names for opcodes are usually chosen as mnemonic references for the instructions supported by the architecture. For example, the PIC mnemonic that adds the values contained in register W and another register is ADDWF. The other register, F, is named as an argument to the mnemonic. Another, more powerful instruction is DECFSZ.