Programming 8-bit PIC Microcontrollers in C: with Interactive Hardware Simulation

Paperback (Print)
Buy Used
Buy Used from BN.com
$30.95
(Save 39%)
Item is in good condition but packaging may have signs of shelf wear/aging or torn packaging.
Condition: Used – Good details
Used and New from Other Sellers
Used and New from Other Sellers
from $13.58
Usually ships in 1-2 business days
(Save 73%)
Other sellers (Paperback)
  • All (11) from $13.58   
  • New (6) from $38.27   
  • Used (5) from $11.69   

Overview

PIC Microcontrollers are present in almost every new electronic application that is released from garage door openers to the iPhone. With the proliferation of this product more and more engineers and engineers-to-be (students) need to understand how to design, develop, and build with them. Martin Bates, best-selling author, has provided a step-by-step guide to programming these microcontrollers (MCUs) with the C programming language.

With no previous knowledge of C necessary to read this book, it is the perfect for entry into this world for engineers who have not worked with PICs, new professionals, students, and hobbyists. As MCUs become more complex C is the most popular language due to its ability to process advanced processes and multitasking. RTOSs, that is a need to know for engineers, is also discussed as more advanced MCUs require timing and organization of programming and implementation of multitasking. The book includes lots of source code, circuit schematics, and hardware block diagrams. Microchip's PICDEM Mechatronics board is used to detail the examples throughout the book.

• Focuses on the C programming language which is by far the most popular for microcontrollers (MCUs)
• Features Proteus VSMg the most complete microcontroller simulator on the market, along with CCS PCM C compiler, both are highly compatible with Microchip tools
• Extensive downloadable content including fully worked examples

Read More Show Less

Product Details

  • ISBN-13: 9780750689601
  • Publisher: Elsevier Science
  • Publication date: 7/29/2008
  • Edition description: New Edition
  • Pages: 304
  • Sales rank: 1,352,020
  • Product dimensions: 7.50 (w) x 9.20 (h) x 0.80 (d)

Meet the Author

Martin Bates is one of the leading authors specializing in introductory level texts on PIC microcontrollers for the academic, professional and hobby markets, with 20 years’ experience of teaching microprocessor systems.

Read More Show Less

Read an Excerpt

Programming 8-bit PIC Microcontrollers in C

with Interactive Hardware Simulation
By Martin P. Bates

Newnes

Copyright © 2008 Elsevier Ltd.
All right reserved.

ISBN: 978-0-08-056014-4


Chapter One

PART 1

PIC Microcontroller Systems

1.1 PIC16 Microcontrollers

• MCU features

• Program execution

• 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.

MCU Features

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.

Program Execution

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

• Low-voltage programming

• Code protection

• 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.

Clock Options

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.

Configuration Options

Apart from the clock options, several other hardware options must be selected.

Watchdog Timer

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.

(Continues...)



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.

Read More Show Less

Table of Contents

Part 1 PIC Microcontroller Systems
Part 2 C Programming Essentials
Part 3 C Peripheral Interfaces
Part 4 C Mechatronics Applications
Part 5 PIC16 C Applications and Systems
Appendix A Hardware Design Using ISIS Schematic Capture
Appendix B Software Design Using CCS C
Appendix C System Testing Using Proteus VSM
Appendix D C Compiler Comparison
Appendix E CCS C Programming Syntax Summary
Appendix F CCS C Programming Function Reference
Answers

Read More Show Less

Customer Reviews

Average Rating 5
( 1 )
Rating Distribution

5 Star

(1)

4 Star

(0)

3 Star

(0)

2 Star

(0)

1 Star

(0)

Your Rating:

Your Name: Create a Pen Name or

Barnes & Noble.com Review Rules

Our reader reviews allow you to share your comments on titles you liked, or didn't, with others. By submitting an online review, you are representing to Barnes & Noble.com that all information contained in your review is original and accurate in all respects, and that the submission of such content by you and the posting of such content by Barnes & Noble.com does not and will not violate the rights of any third party. Please follow the rules below to help ensure that your review can be posted.

Reviews by Our Customers Under the Age of 13

We highly value and respect everyone's opinion concerning the titles we offer. However, we cannot allow persons under the age of 13 to have accounts at BN.com or to post customer reviews. Please see our Terms of Use for more details.

What to exclude from your review:

Please do not write about reviews, commentary, or information posted on the product page. If you see any errors in the information on the product page, please send us an email.

Reviews should not contain any of the following:

  • - HTML tags, profanity, obscenities, vulgarities, or comments that defame anyone
  • - Time-sensitive information such as tour dates, signings, lectures, etc.
  • - Single-word reviews. Other people will read your review to discover why you liked or didn't like the title. Be descriptive.
  • - Comments focusing on the author or that may ruin the ending for others
  • - Phone numbers, addresses, URLs
  • - Pricing and availability information or alternative ordering information
  • - Advertisements or commercial solicitation

Reminder:

  • - By submitting a review, you grant to Barnes & Noble.com and its sublicensees the royalty-free, perpetual, irrevocable right and license to use the review in accordance with the Barnes & Noble.com Terms of Use.
  • - Barnes & Noble.com reserves the right not to post any review -- particularly those that do not follow the terms and conditions of these Rules. Barnes & Noble.com also reserves the right to remove any review at any time without notice.
  • - See Terms of Use for other conditions and disclaimers.
Search for Products You'd Like to Recommend

Recommend other products that relate to your review. Just search for them below and share!

Create a Pen Name

Your Pen Name is your unique identity on BN.com. It will appear on the reviews you write and other website activities. Your Pen Name cannot be edited, changed or deleted once submitted.

 
Your Pen Name can be any combination of alphanumeric characters (plus - and _), and must be at least two characters long.

Continue Anonymously
Sort by: Showing 1 Customer Reviews
  • Anonymous

    Posted March 27, 2014

    8 bits preview

    I decided to write another fanfic its a shorter one (i hope) and hopefully will make you laugh] A cloaked figure entered the market in ponyville stealthily obviously trying to avoid being seen] i said its a preview

    Was this review helpful? Yes  No   Report this review
Sort by: Showing 1 Customer Reviews

If you find inappropriate content, please report it to Barnes & Noble
Why is this product inappropriate?
Comments (optional)