Thus, a list of instructions in memory is executed in turn to carry out the
required process. In a word processor, for example, keystrokes are read in via
the keyboard port, stored as character codes, and sent to a screen output for
display. In a game, input from the switches on the control pad are processed and
used to modify the screen. In this case, speed of the system is a critical factor.
Memory
There are two types of memory: volatile and non-volatile. Volatile memory
loses its data when switched off, but can be written by the CPU to store current
data; this is RAM (Random Access Memory). ROM (Read Only Memory) is
non-volatile, and retains its data when switched off.
In a PC, a small ROM 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), for example, 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 if required. In addition, an OS such as Windows is very large, and
some elements are only loaded into RAM as needed. In addition, numerous
applications can 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 provide different advantages, which
Interfacing PIC Microcontrollers
6
Flash
ROM ROM RAM CD-ROM DVD-RW HDD
Description Chip Chip Chip Optical Optical Magnetic
disk disk disk
Sample size* 128 kb 128 Mb 512 Mb 650 Mb 4.7 Gb 30 Gb
Non-volatile x
multipin connector. The new standard, USB (Universal Serial Bus) is a serial
data system, sending only 1 bit at a time. Potentially, the parallel connection is
8 times faster, but USB operates at up to 480 megabits (Mb) per second, and
the printer is slow anyway, so there is no problem. One advantage of using
PIC Hardware
7
(a)
Parallel Port Register
Internal
Data
Bus
External data lines
Read/Write
Control
External
data line
Serial Port Register
Internal
Data
Bus
Read/Write
Control
(b)
Figure 1.3 Parallel and serial data ports: (a) parallel; (b) serial
Else_IPM-BATES_ch001.qxd 6/27/2006 10:04 PM Page 7
USB is that it provides a simple, robust connector and this outweighs the fact
that the interface protocol (driver software) is relatively complex, because this
is hidden from the user. USB also provides power to the peripheral, if required,
and the printer can be daisy-chained with other devices. USB also automati-
cally configures itself for different peripherals, such as scanners and cameras.
PIC 16F877 Architecture
Microcontrollers contain all the components required for a processor system in
one chip: a CPU, memory and I/O. A complete system can therefore be built
Interfacing PIC Microcontrollers
8
Else_IPM-BATES_ch001.qxd 6/27/2006 10:04 PM Page 8
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.
PIC 16F877 Pin Out
Let us first consider the pins that are seen on the IC package, and we can then
discover how they relate the internal architecture. The chip can be obtained in
different packages, such as conventional 40-pin DIP (Dual In-Line Package),
square surface mount or socket format. The DIP version is recommended for
prototyping, and is shown in Figure 1.4.
Most of the pins are for input and output, and 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 most 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.
Port B is used for downloading the program to the chip flash ROM (RB6 and
RB7), and RB0 and RB4–RB7 can generate an interrupt. 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 later.
PIC Hardware
9
Reset = 0, Run = 1 MCLR 140RB7 Port B, Bit 7 (Prog. Data, Interrupt)
Port A, Bit 0 (Analogue AN0)
Port E, Bit 1 (AN6, Slave control) RE1 932V
DD
+5V Power Supply
Port E, Bit 2 (AN7, Slave control) RE2 10 31 Vss 0V Power Supply
+5V Power Supply
V
DD
11 30
RD7
Port D, Bit 7 (Slave Port)
0V Power Supply
Vss
12 29
RD6
Port D, Bit 6 (Slave Port)
(CR clock) XTAL circuit
CLKIN
13 28
RD5
Port D, Bit 5 (Slave Port)
XTAL circuit
CLKOUT
14 27
RD4
Port D, Bit 4 (Slave Port)
Port C, Bit 0 (Timer 1) RC0 15 26 RC7 Port C, Bit 7 (Serial Ports)
Port C, Bit 1 (Timer 1) RC1 16 25 RC6 Port C, Bit 6 (Serial Ports)
Port C, Bit 2 (Timer 1) RC2 17 24 RC5 Port C, Bit 5 (Serial Ports)
Port C, Bit 3 (Serial Clocks)
RC3
(downloadable from www.microchip.com). A somewhat simplified version is
given in Figure 1.5, which emphasises the program execution mechanism.
The main program memory is flash ROM, which stores a list of 14-bits
instructions. These are fed to the execution unit, and used to modify the RAM
file registers. These include special control registers, the port registers and a set
of general purpose registers which can be used to store data temporarily. A sep-
arate working register (W) is used with the ALU (Arithmetic Logic Unit) to
process data. Various special peripheral modules provide a range of I/O options.
There are 512 RAM File Register addresses (0–1FFh), which are organised
in 4 banks (0–3), each bank containing 128 addresses. The default (selected
on power up) Bank 0 is numbered from 0 to 7Fh, Bank 1 from 80h to FFh and
so on. These contain both Special Function Registers (SFRs), which have a
dedicated purpose, and the General Purpose Registers (GPRs). The file regis-
ters are mapped in Figure 2-3 of the data sheet. The SFRs may be shown in
the block diagram as separate from the GPRs, but they are in fact in the same
logical block, and addressed in the same way. Deducting the SFRs from the
total number of RAM locations, and allowing for some registers which are re-
peated in more than one bank, leaves 368 bytes of GPR (data) registers.
Test Hardware
We need to define the hardware in which we will demonstrate PIC program
operation. Initially, a block diagram is used to outline the hardware design
(Figure 1.6). The schematic symbol for the MCU is also shown indicating the pins
to be used. For this test program, we simply need inputs which switch between 0
V and ϩ5 V, and a logic indication at the outputs. For simulation purposes, we will
see that the clock circuit does not have to be included in the schematic; instead, the
clock frequency must be input to the MCU properties dialogue. The power supply
pins are implicit – the simulated MCU operates at ϩ5 V by default. Unused pins
can be left open circuit, as long as they are programmed as inputs.
Interfacing PIC Microcontrollers
10
x 8
levels
RAM
File
Registers
368
x 8 bits
000-1FF
Data Bus
(8 bits)
File Select
Register (8)
Working
Register (8)
File Address (7)
Literal (8)
Op-
code
Instructions (14)
Address (13)
ALU
Status
Register (8)
Ports, Timers
ADC, Serial I/O
Status bits
Clock, Timing & Programming
Control
EEPROM
Clock Reset
LEDs
Input
Buttons
Active
Low
Port B
LEDs
RB0 – RB7
Port D
Buttons
RD0, RD1
RC Clock = CLKIN
MCLR = 1
Power
Supply
Pins
Implicit
Vdd = 5V
Vss = 0V
Figure 1.6 BINX hardware outline: (a) block diagram; (b) PIC 16F877 MCU connections
Else_IPM-BATES_ch001.qxd 6/27/2006 10:04 PM Page 12
PIC Hardware
13
The program listing includes the source code at the right, with source line
numbers, the hex machine code and the memory location where each instruction
is stored (0000–0004). Notice that some statements are assembler directives, not
instructions: PROCESSOR to specify the MCU type and END to terminate the
source code. These are not converted into machine code.
The ‘877 has 8k of program memory, that is, it can store a maximum of
1024 ϫ 8 ϭ 8192 14-bit instructions. By default, it is loaded, and starts
0004 2803 00008 GOTO again
00009
00010 END
Note: Lines 00001 and 00010 are assembler directives
Line
Number
Program 1.1 BIN1 list file
Else_IPM-BATES_ch001.qxd 6/27/2006 10:04 PM Page 13
as 11 0000 0101 1010. Since each hex digit represents four binary bits, the
leading two bits are zero, and the leading digit will only range from 0 to 3 for
a 14-bit number.
In the instruction set (data sheet, Table 13-2), it is seen that the first 4 bits
(11 00) are the instruction code, the next two are unused (xx, appearing as 00 in
the binary code) and the last 8 are the literal value (5A). The literal is represented
as ‘kkkk kkkk’ since it can have any value from 00000000 to 11111111 (00–FF).
The format of other instructions depends mainly on the number of bits
required for the operand (data to be processed). The number of op-code bits
can vary from 3 to all 14, depending on the number of bits needed for the
operand. This is different from a conventional processor, such as the Pentium,
where the op-code and operand are each created as a whole number of bytes.
The PIC instruction is more compact, as is the instruction set itself, for greater
speed of operation. This defines it as a RISC (Reduced Instruction Set
Computer) chip.
Program BIN4
The program BIN4 contains many of the basic program elements, and the list
file (Program 1.2) shows the source code, machine code, memory address and
list file line number as before. There are additional comments to aid program
analysis and debugging.
Note that two types of labels are used in program to represent numbers.
Label equates are used at the top of the program to declare labels for the file
00009 ; Slow output binary count is stopped, started
00010 ; and reset with push buttons.
00011 ;
00012 ; Processor: PIC 16F877
00013 ;
00014 ; Hardware: PIC Demo System
00015 ; Clock: RC = 40kHz
00016 ; Inputs: Port D: Push Buttons
00017 ; RD0, RD1 (active low)
00018 ; Outputs: Port B: LEDs (active high)
00019 ;
00020 ; WDTimer: Disabled
00021 ; PUTimer: Enabled
00022 ; Interrupts: Disabled
00023 ; Code Protect: Disabled
00024 ;
00025 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
00026
00027 PROCESSOR 16F877 ; Define MCU type
2007 3733 00028 __CONFIG 0x3733 ; Set config fuses
00029
00030 ; Register Label Equates
00031
00000006 00032 PORTB EQU 06 ; Port B Data Register
00000086 00033 TRISB EQU 86 ; Port B Direction Register
00000008 00034 PORTD EQU 08 ; Port D Data Register
00000020 00035 Timer EQU 20 ; GPR used as delay counter
00036
00037 ; Input Bit Label Equates
00038
00069
0010 0A86 00070 INCF PORTB ; Increment output at Port B
0011 30FF 00071 MOVLW 0FF ; Delay count literal
0012 2007 00072 CALL delay ; Jump to subroutine 'delay'
0013 280C 00073 GOTO start ; Repeat main loop always
00074
00075 END ; Terminate source code
Program 1.2 BIN4 list file
Else_IPM-BATES_ch001.qxd 6/27/2006 10:04 PM Page 15