Discover millions of ebooks, audiobooks, and so much more with a free trial

Only $11.99/month after trial. Cancel anytime.

Interfacing PIC Microcontrollers: Embedded Design by Interactive Simulation
Interfacing PIC Microcontrollers: Embedded Design by Interactive Simulation
Interfacing PIC Microcontrollers: Embedded Design by Interactive Simulation
Ebook676 pages11 hours

Interfacing PIC Microcontrollers: Embedded Design by Interactive Simulation

Rating: 0 out of 5 stars

()

Read preview

About this ebook

Interfacing PIC Microcontrollers, 2nd Edition is a great introductory text for those starting out in this field and as a source reference for more experienced engineers. Martin Bates has drawn upon 20 years of experience of teaching microprocessor systems to produce a book containing an excellent balance of theory and practice with numerous working examples throughout.

It provides comprehensive coverage of basic microcontroller system interfacing using the latest interactive software, Proteus VSM, which allows real-time simulation of microcontroller based designs and supports the development of new applications from initial concept to final testing and deployment.

  • Comprehensive introduction to interfacing 8-bit PIC microcontrollers
  • Designs updated for current software versions MPLAB v8 & Proteus VSM v8
  • Additional applications in wireless communications, intelligent sensors and more
LanguageEnglish
Release dateSep 18, 2013
ISBN9780080993720
Interfacing PIC Microcontrollers: Embedded Design by Interactive Simulation
Author

Martin P. Bates

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 from Martin P. Bates

Related to Interfacing PIC Microcontrollers

Related ebooks

Electrical Engineering & Electronics For You

View More

Related articles

Reviews for Interfacing PIC Microcontrollers

Rating: 0 out of 5 stars
0 ratings

0 ratings0 reviews

What did you think?

Tap to rate

Review must be at least 10 words

    Book preview

    Interfacing PIC Microcontrollers - Martin P. Bates

    1

    PIC Microcontroller

    Outline

    Chapter 1 PIC Hardware

    Chapter 2 PIC Programming

    Chapter 3 PIC Design

    Chapter 1

    PIC Hardware

    The PIC microcontroller contains a processor, memory and input/output ports, and the program is stored in flash ROM memory in numbered locations. The P16F877A stores a maximum of 8096 14-bit instructions in flash ROM, has 368 bytes of RAM and five ports (33 I/O pins). The P16FXXX family uses only 35 instructions, and the program is executed in sequence, unless there is a jump instruction. The program counter tracks the current instruction address. A configuration word is loaded initially to select the clock type and other chip options. The program source code (.ASM) is assembled into machine code (.HEX), downloaded to the chip and the application hardware tested.

    Keywords

    PIC microcontroller; microcontroller unit (MCU); 16F877A; Read Only Memory (ROM); Read And write Memory (RAM)

    Summary

    • The microcontroller contains a processor, memory and input/output devices

    • The program is stored in flash ROM memory in numbered locations

    • The P16F877A family uses only 35 instructions

    • The P16F877A stores a maximum 8k × 14 instructions in flash ROM

    • The P16F877A has 368 bytes of RAM and 5 ports (33 I/O pins)

    • The program is executed in sequence, unless there is a jump instruction

    • The program counter tracks the current instruction address

    • A configuration word is need to select the clock type and other chip options

    • The program source code (.ASM) is assembled into machine code (.HEX)

    • Machine code is downloaded to the chip and the application hardware tested

    The microcontroller is a complete computer on a chip. When introduced, it was one of the most significant developments in electronics since the invention of the microprocessor itself and is essential in the operation of such devices as mobile phones, DVD players, video cameras, and most self-contained electronic systems. Working sometimes with other chips, but often on its own, the microcontroller unit (MCU) provides the key element in the vast range of small, programmed devices that are now commonplace.

    Although small, microcontrollers are complex, and we have to look carefully at the way the hardware and firmware (control program) work together to understand the processes at work. This book will then show how to connect the popular PIC range of microcontrollers to the outside world and put them to work. To keep things simple, we will concentrate on one device, the PIC 16F877A, which has a good range of features that allows most of the essential techniques to be explained. It has a set of serial ports built in that are used to transfer data to and from other devices, as well as analogue inputs, which allow measurement of inputs such as temperature. All microcontrollers work in a similar way, so analysis of the PIC MCU will go a long way to understanding all such devices.

    This chip has been around some time and is no longer the best choice for new designs. The 16F887 is a more recent equivalent and should be used as a pin compatible replacement in new designs. The reason for continuing to use the ‘877A’ in this edition of the book is that it is still available as a part of a low-cost simulation package that is suitable for students and hobbyists on a budget and has all the main interfaces that are still used in current chips. In any event, when designing a new application, a chip should always be selected from the available range which most closely matches the design requirements at minimum cost, so prototyping with a chip with surplus capabilities is a useful approach. It can be replaced at a later design stage with a chip that matches more closely with the system requirements.

    A big advantage of the PIC is that the programming language is relatively simple, as compared with microprocessors such as the Intel series used in the PC. These have a powerful, but complex, instruction set to support a wide range of multimedia applications. The supporting documentation for the PIC MCU range is also clear and well laid out, and a development system, for writing and testing programs, can also be downloaded free from the Microchip website (www.microchip.com).

    1.1 Processor System

    The microcontroller contains the same main elements as any computer system, namely:

    • Central Processor Unit (CPU)

    • Data storage (memory) devices

    • Input and output ports

    In a PC, these features are generally implemented as separate chips, linked together through bus connections on a complex printed circuit board, under the control of the microprocessor. A bus is a set of lines which carry data in parallel form which are shared by the peripheral devices. This type of system can be tailored to suit a particular application, with the type of CPU, size of memory, and selection of input and output (I/O) devices matched to the system requirements. However, even in real-time applications, it has been common for some time to use a standard board based on the Intel processor system running a generic operating system (often Windows and its derivatives). The basic microprocessor system is conveniently summarised in a block diagram (Figure 1.1) which shows the data flow between the main elements using shared bus lines.

    Figure 1.1 Block diagram of a basic microprocessor system.

    In the microcontroller, all these basic elements are on one chip. This means that the MCU for a particular application must be chosen from the available range to suit its operational requirements. In any given circuit, the microcontroller also tends to have a single, specific function. This type of product is frequently referred to as an embedded application.

    1.1.1 Processor

    In a microprocessor system or a microcontroller, a single processor block is in charge of all input, output, calculations and control. It cannot operate without a program, a list of instructions that is held in memory. The program consists of a sequence of binary codes that are fetched from memory by the CPU and executed in turn (Figure 1.2). The process is driven by a crystal clock circuit producing a fixed frequency that determines the speed of the system.

    Figure 1.2 Microprocessor program execution.

    The instructions are stored in numbered memory locations and copied to an instruction register in the CPU, via a data bus. Here, the instruction controls the selection of the required operation within the control unit of the processor. The program codes are found in memory by the processor by outputting the address number of the instruction on an address bus. The address is generated in the program counter, a register which starts at zero and is incremented or modified during each instruction cycle. The busses are parallel connections which transfer the address or data word in one operation. A set of control lines from the CPU is also needed to assist with this process; these are set up according to the requirements of the current instruction and trigger the data transfer circuits to output and receive the data at the appropriate time. In the conventional microprocessor system, the bus connections consist of parallel tracks on a motherboard but are internal in the microcontroller chip.

    Decoding the instruction is a hardware process, using a block of logic gates to set up the control lines of the processor unit, and to fetch the instruction ‘operands’. The operands are data to be operated on (or information about where to find it) which follow most instructions. Typically, a calculation or logical operation is carried out on the operands, and a result stored back in memory, or an I/O action set-up. Each complete instruction may be one, two or more bytes long, which includes the operation (instruction) code itself (op-code) and the operand/s (one byte=8 bits).

    For example, compare a word processor and games application. In the word processor, keystrokes are read in via the input keyboard port, stored as character codes in memory and sent to a screen output port for display. In a computer game, input signals from the control pad are processed and used to modify the screen graphics. The graphics are basically generated by mapping a memory block to the screen where the colour of one pixel is controlled by a particular data word. The word processor needs far less memory, and the graphics memory has to be large and fast.

    1.1.2 Memory

    There are two main types of microprocessor memory, volatile and non-volatile. ROM (Read Only Memory) is non-volatile and retains its data when switched off. In the PC, the main working memory is volatile RAM (Read and Write Memory), normally implemented as plug-in DIMM (Dual In-line Memory Module) modules, which carry a set of dynamic RAM chips. These are used by the CPU to store current working application files and data. RAM originally meant Random Access Memory, referring to the data read-and-write mechanism, but ROM is accessed in exactly the same way, using row and column addresses to identify each storage cell.

    In a traditional PC design, a small ROM chip is used to get the system started when it is switched on; it contains the BIOS (Basic Input Output System) program. However, the main operating system (OS), e.g. Windows™, and application program, e.g. Word™, have to be loaded into RAM from hard disk drive (HDD), which takes some time, as you may have noticed! So why not put the OS in ROM, where it would be instantly available? Well, RAM is faster, cheaper and more compact, and the OS can be changed or upgraded on disk. In addition, an OS such as Windows is large compared with the size of RAM generally installed, so elements are only loaded into RAM as needed. Numerous applications can also be stored on disk and loaded only as required.

    The ideal memory is non-volatile, read and write, fast, large and cheap. Unfortunately, it does not exist! Therefore, we have a range of memory technologies as shown in Table 1.1, which have different advantages. These are used in combination in the PC to provide the optimum overall performance for a given cost. This also depends on the application being run at any one time. The main trade-off is cost, size and speed of access.

    Table 1.1

    Memory and Data Storage Technologies.

    *1 byte=8 bits

    *1kb=1 kilobyte=1024 bytes

    *1Mb=1 megabyte=1024kb

    *1Gb=1 gigabyte=1024Mb

    Flash ROM, as used in memory sticks and MP3 players, is closest to the ideal, having the advantages of being non-volatile and rewritable. This is why it is used as program memory in microcontrollers which need to be reprogrammable, such as the PIC 16F877A. The microcontroller uses flash program memory because it is usually dedicated to a particular control task that does not need a large amount of working data storage. The working data registers in the PIC can therefore be implemented as a relatively small block of static RAM (SRAM). As flash ROM technology has improved, conventional one-time programmable ROM has now generally been rendered superfluous but is included for completeness in the comparison.

    1.1.3 Input and Output

    Without some means of getting information and signals in and out, a data processing or digital control system would not be very useful. Input and output ports generally contain a port data register and a set of control registers that allow data to pass in and out. Serial ports often use standard protocol (method of communication) to format the data.

    In a PC, the keyboard, screen and mouse interfaces are the main I/O channels, supported by network, USB, SD Card and disk interfaces. The DIMM memory module has a parallel connector (typically 64 bits), so it attaches directly to the processor busses. This means that access is fast, because complete data blocks can be transferred at one time. USB on the other hand is a serial bus, so data transfer can only occur one bit at a time, which is inherently slower.

    Microcontroller ports are generally more basic, especially in the smaller MCUs which cannot accommodate the complex hardware needed for, say, a network port. The basic MCU port consists of a group of 8 bits that can operate as a parallel port, but whose individual pins have alternate functions, often several. The basic parallel operation is straightforward, with an 8-bit data register that holds the I/O data, and an 8-bit data direction register whose individual bits control the data direction, in or out. In the PIC, 0=output and 1=input. Alternate functions are selected in the MCU control registers at set-up.

    In principle, the parallel port is faster that the serial port but uses more pins to transfer the data. The serial interface must organise the data in groups of bits for transmission. Dedicated registers are used to organise the data stream and control the timing of the data transfer. The serial port is based on a shift register that converts between parallel data on the internal data bus and serial data on the peripheral line. The general principles of parallel and serial data transfer are shown in Figure 1.3. The block arrows represent the 8-bit internal data bus.

    Figure 1.3 (a) Parallel and (b) serial data ports.

    In the parallel port operating in output mode, the data byte is loaded from the internal data bus under the control of a read/write pulse from the CPU. The data can then be seen on the output pins by the peripheral line. For testing, a logic probe, logic analyser or just a simple LED indicator can be used. In input mode, data presented at the input pins from a set of switches or other data source is latched into the register when the port is read and is then available on the data bus for collection by the CPU. One of the functions of the port is to separate the internal data bus from the external hardware, and another is to temporarily store the data. The data can then be transferred to memory, or otherwise processed, as determined by the CPU program.

    The serial port register also loads data from the internal bus in parallel but then sends it out one bit at a time, operating as a shift register. If an asynchronous serial format is used, such as RS232, start and stop bits are added so that bytes can be separated at the receiving end. An error check bit is also available, to allow the receiver to detect corrupt data. In receive mode, the register waits for a start bit and then shifts in the data at the same speed as it is sent. This means the clock rate for the send and receive port must be the same. The USART (Universal Synchronous/Asynchronous Receive/Transmit) port, which provides RS232, will be described in more detail later.

    A USB or network port is a more sophisticated version of the basic serial port and arranges the data bytes in packets of, perhaps, 1k bytes. These are sent in a form which is self-clocking, meaning that there is a transition within each bit (1 or 0), which triggers the bit read into the receiving register. This is synchronous data transmission. An error correction code follows the data, which allows mistakes to be corrected, rather than just detected. This reduces the need for retransmission of incorrectly received data, as required by simple error detection. Addressing information preceding the data allows multiple receivers to be used.

    The PIC 16F877A does not have USB or network interfaces built in, so we can avoid detailed consideration of these complex protocols. It does nevertheless have a range of other interfaces that will be discussed in detail and sample programs are provided. If further explanation of the basics of microcontroller operation is required, the reader is invited to refer to the introductory text ‘PIC Microcontrollers’ by the author.

    1.2 PIC Architecture

    Microcontrollers contain all the components required for a processor system in one chip: CPU, memory and I/O. A complete system can therefore be built using one MCU chip and a few I/O devices such as a keypad, display and other interfacing circuits. We will now see how this is done in practice in our typical microcontroller.

    1.2.1 PIC 16F877A Pin Out

    Let us first consider the pins that are seen on the IC package, and then we can discover how they relate to the internal architecture. The chip can be obtained in different packages, such as conventional 40-pin PDIP (Plastic Dual In-Line Package), square surface mount or socket format. The PDIP version, shown in Figure 1.4, is easier for prototyping.

    Figure 1.4 PIC 16F877A pin out.

    The I/O pins are arranged as 5 ports: A (5), B (8), C (8), D (8) and E (3), giving a total of 32 I/O pins. These can all operate as simple digital I/O pins but mostly have more than one function, and the mode of operation of each is selected by initialising various control registers within the chip. Note, in particular, that Ports A and E become ANALOGUE INPUTS by default (on power-up or reset), so they have to set up for digital I/O if required. The ports which have fewer than 8 bits have corresponding unused bits in their 8-bit data and control registers.

    Port B is used for downloading the program to the chip flash ROM, with RB7 receiving the program code in serial form and RB6 receiving a clock which strobes each data bit into the serial register on RB7. MCLR is taken to about 14V to initiate programming mode and supply the programming voltage. RB3 can optionally be used in low-voltage programming mode which does not require the 14V supply. Changes on RB0 and RB4–RB7 can generate an interrupt, which forces a change in the program sequence. Port C gives access to timers and serial ports, while Port D can be used as a slave port, with Port E providing the control pins for this function. All these options will be explained in detail

    Enjoying the preview?
    Page 1 of 1