Read an Excerpt
Programming 8-bit PIC Microcontrollers in Cwith Interactive Hardware Simulation
By Martin P. Bates
NewnesCopyright © 2008 Elsevier Ltd.
All right reserved.
Chapter OnePART 1
PIC Microcontroller Systems
1.1 PIC16 Microcontrollers
RAM file registers
Other PIC chips
The microcontroller unit (MCU) is now big, or rather small, in electronics. It is one of the most significant developments in the continuing miniaturization of electronic hardware. Now, even trivial products, such as a musical birthday card or electronic price tag, can include an MCU. They are an important factor in the digitization of analog systems, such as sound systems or television. In addition, they provide an essential component of larger systems, such as automobiles, robots, and industrial systems. There is no escape from microcontrollers, so it is pretty useful to know how they work.
The computer or digital controller has three main elements: input and output devices, which communicate with the outside world; a processor, to make calculations and handle data operations; and memory, to store programs and data. Figure 1.1 shows these in a little more detail. Unlike the conventional microprocessor system (such as a PC), which has separate chips on a printed circuit board, the microcontroller contains all these elements in one chip. The MCU is essentially a computer on a chip; however, it still needs input and output devices, such as a keypad and display, to form a working system.
The microcontroller stores its program in ROM (read only memory). In the past, UV (ultraviolet) erasable programmable ROM (EPROM) was used for prototyping or small batch production, and one-time programmable ROM for longer product runs. Programmable ROM chips are programmed in the final stages of manufacture, while EPROM could be programmed by the user.
Flash ROM is now normally used for prototyping and low-volume production. This can be programmed in circuit by the user after the circuit has been built. The prototyping cycle is faster, and software variations are easier to accommodate. We are all now familiar with flash ROM as used in USB memory sticks, digital camera memory, and so on, with Gb (109 byte) capacities commonplace.
The range of microcontrollers available is expanding rapidly. The first to be widely used, the Intel 8051, was developed alongside the early Intel PC processors, such as the 8086. This device dominated the field for some time; others emerged only slowly, mainly in the form of complex processors for applications such as engine management systems. These devices were relatively expensive, so they were justified only in high-value products. The potential of microcontrollers seems to have been realized only slowly.
The development of flash ROM helped open up the market, and Microchip was among the first to take advantage. The cheap and reprogrammable PIC16F84 became the most widely known, rapidly becoming the number one device for students and hobbyists. On the back of this success, the Microchip product range rapidly developed and diversified. The supporting development system, MPLAB, was distributed free, which helped the PIC to dominate the low-end market.
Flash ROM is one of the technical developments that made learning about microsystems easier and more interesting. Interactive circuit design software is another. The whole design process is now much more transparent, so that working systems are more quickly achievable by the beginner. Low-cost in-circuit debugging is another technique that helps get the final hardware up and running quickly, with only a modest expenditure on development tools.
The range of microcontrollers now available developed because the features of the MCU used in any particular circuit must be as closely matched as possible to the actual needs of the application. Some of the main features to consider are
Number of inputs and outputs.
Program memory size.
Data RAM size.
Nonvolatile data memory.
Maximum clock speed.
Range of interfaces.
Development system support.
Cost and availability.
The PIC16F877A is useful as a reference device because it has a minimal instruction set but a full range of peripheral features. The general approach to microcontroller application design followed here is to develop a design using a chip that has spare capacity, then later select a related device that has the set of features most closely matching the application requirements. If necessary, we can drop down to a lower range (PIC10/12 series), or if it becomes clear that more power is needed, we can move up to a higher specification chip (PIC18/24 series). This is possible as all devices have the same core architecture and compatible instructions sets.
The most significant variation among PIC chips is the instruction size, which can be 12, 14, or 16 bits. The A suffix indicates that the chip has a maximum clock speed of 20MHz, the main upgrade from the original 16F877 device. These chips can otherwise be regarded as identical, the suffix being optional for most purposes. The 16F877A pin-out is seen in Figure 1.2 and the internal architecture in Figure 1.3. The latter is a somewhat simplified version of the definitive block diagram in the data sheet.
The chip has 8k (8096 14 bits) of flash ROM program memory, which has to be programmed via the serial programming pins PGM, PGC, and PGD. The fixed-length instructions contain both the operation code and operand (immediate data, register address, or jump address). The mid-range PIC has a limited number of instructions (35) and is therefore classified as a RISC (reduced instruction set computer) processor.
Looking at the internal architecture, we can identify the blocks involved in program execution. The program memory ROM contains the machine code, in locations numbered from 0000h to 1FFFh (8k). The program counter holds the address of the current instruction and is incremented or modified after each step. On reset or power up, it is reset to zero and the first instruction at address 0000 is loaded into the instruction register, decoded, and executed. The program then proceeds in sequence, operating on the contents of the file registers (0001FFh), executing data movement instructions to transfer data between ports and file registers or arithmetic and logic instructions to process it. The CPU has one main working register (W), through which all the data must pass.
If a branch instruction (conditional jump) is decoded, a bit test is carried out; and if the result is true, the destination address included in the instruction is loaded into the program counter to force the jump. If the result is false, the execution sequence continues unchanged. In assembly language, when CALL and RETURN are used to implement subroutines, a similar process occurs. The stack is used to store return addresses, so that the program can return automatically to the original program position. However, this mechanism is not used by the CCS C compiler, as it limits the number of levels of subroutine (or C functions) to eight, which is the depth of the stack. Instead, a simple GOTO instruction is used for function calls and returns, with the return address computed by the compiler.
RAM File Registers
The main RAM block (Table 1.1) is a set of 368 8-bit file registers, including the special function registers (SFRs), which have a dedicated function, and the general purpose registers (GPRs). When variables are created in C, they are stored in the GPRs, starting at address 0020h. The file registers are divided into four blocks, register banks 0 to 3. The SFRs are located at the low addresses in each RAM bank.
Some registers are addressable across the bank boundaries; for example, the status register can be accessed in all blocks at the corresponding address in each bank. Others are addressable in only a specific page, for example, Port A data register. Some register addresses are not physically implemented. Since some registers are accessible in multiple banks, bank switching can be minimized by the compiler when assembling the machine code, thus saving program code space and execution time. For full details of the file register set, see the MCU data sheet.
The program counter uses two 8-bit registers to store a 13-bit program memory address. Only the low byte at address 002h is directly addressable. The status register 003h records results from ALU (arithmetic and logic unit) operations, such as zero and carry/ borrow. The indirect and file select registers are used for indexed addressing of the GPRs. Timer0 is the timer/counter register available in all PIC MCUs, while Timer1 and Timer2 registers are in the peripheral block. The port registers are located in Bank 0 at addresses 05h (Port A) to 09h (Port E) with the data direction register for each at the corresponding location in bank 1. We can see that a total of 80 + 16 + 80 + 96 + 96 = 368 GPRs are available for use as data RAM. Note that the number of registers used for each C variable depends on the variable type and can range from 1 to 32 bits (1-4 GPRs).
Other PIC Chips
In any embedded design, the features of the MCU need to be matched to the application requirements. The manufacturer needs to make sure that, as applications become more demanding, a more powerful device of a familiar type is available. We can see this process at work where Microchip started out producing basic chips such as the 16C84, then developed the product range to meet the growing market. PIC microcontrollers are currently available in distinct groups, designated the 10, 12, 16, 18, and 24 series. Their general characteristics are outlined in Table 1.2.
The original 16 series CMOS devices were designated as 16CXX. When flash memory was introduced, they became 16FXXX. Currently, a limited number of devices are available in the low pin count (LPC) ranges (10/12 series), while the power ranges are expanding rapidly. In addition are those listed in the 24HXXXX range, which runs at 40 MIPS, and the dsPIC (digital signal processor) high-specification range.
1.2 PIC16 MCU Configuration
Clock oscillator types
Watchdog, power-up, brown-out timers
In-circuit debug mode
When programming the PIC microcontroller, certain operational modes must be set prior to the main program download. These are controlled by individual bits in a special configuration register separated from the main memory block. The main options are as follows.
The '877 chip has two main clock modes, CR and XT. The CR mode needs a simple capacitor and resistor circuit attached to CLKIN, whose time constant (C x R) determines the clock period. R should be between 3k and 100k, and C greater than 20pF. For example, if R = 10kΩ and C = 10nF, the clock period will be around 2 x CR 200 µs (calculated from the CR rise/fall time) and the frequency about 5kHz. This option is acceptable when the program timing is not critical.
The XT mode is the one most commonly used, since the extra component cost is small compared with the cost of the chip itself and accurate timing is often a necessity. An external crystal and two capacitors are fitted to CLKIN and CLKOUT pins. The crystal frequency in this mode can be from 200kHz to 4MHz and is typically accurate to better than 50ppm (parts per million) or 0.005%. A convenient value is 4Mz, as this is the maximum frequency possible with a standard crystal and gives an instruction execution time of 1.000 µs (1 million instructions per second, or 1Mip).
A low-speed crystal can be used to reduce power consumption, which is proportional to clock speed in CMOS devices. The LP (low-power) mode supports the clock frequency range 32200kHz. To achieve the maximum clock speed of 20MHz, a high-speed (HS) crystal is needed, with a corresponding increase in power consumption.
The MCU configuration fuses must be set to the required clock mode when the chip is programmed. Many PIC chips now have an internal oscillator, which needs no external components. It is more accurate than the RC clock but less accurate than a crystal. It typically runs at 8MHz and can be calibrated in the chip configuration phase to provide a more accurate timing source.
Apart from the clock options, several other hardware options must be selected.
When enabled, the watchdog timer (WDT) automatically resets the processor after a given period (default 18ms). This allows, for example, an application to escape from an endless loop caused by a program bug or run-time condition not anticipated by the software designer. To maintain normal operation, the WDT must be disabled or reset within the program loop before the set time-out period has expired. It is therefore important to set the MCU configuration bits to disable the WDT if it is not intended to use this feature. Otherwise, the program is liable to misbehave, due to random resetting of the MCU.
Excerpted from Programming 8-bit PIC Microcontrollers in C by Martin P. Bates Copyright © 2008 by Elsevier Ltd.. Excerpted by permission of Newnes. All rights reserved. No part of this excerpt may be reproduced or reprinted without permission in writing from the publisher.
Excerpts are provided by Dial-A-Book Inc. solely for the personal use of visitors to this web site.