- •Table of Contents
- •Preface
- •Additional Material
- •Basic Electronics
- •1.0 The Atom
- •1.1 Isotopes and Ions
- •1.2 Static Electricity
- •1.3 Electrical Charge
- •1.4 Electrical Circuits
- •1.5 Circuit Elements
- •1.6 Semiconductors
- •Number Systems
- •2.0 Counting
- •2.1 The Origins of the Decimal System
- •2.2 Types of Numbers
- •2.3 Radix Representations
- •2.4 Number System Conversions
- •Data Types and Data Storage
- •3.0 Electronic-Digital Machines
- •3.1 Character Representations
- •3.2 Storage and Encoding of Integers
- •3.3 Encoding of Fractional Numbers
- •3.4 Binary-Coded Decimals (BCD)
- •Digital Logic, Arithmetic, and Conversions
- •4.0 Microcontroller Logic and Arithmetic
- •4.1 Logical Instructions
- •4.2 Microcontroller Arithmetic
- •4.3 Bit Manipulations and Auxiliary Operations
- •4.4 Unsigned Binary Arithmetic
- •4.5 Signed Binary Arithmetic
- •4.6 Data Format Conversions
- •Circuits and Logic Gates
- •5.0 Digital Circuits
- •5.1 The Diode Revisited
- •5.2 The Transistor
- •5.3 Logic Gates
- •5.4 Transistor-Transistor Logic
- •5.5 Other TTL Logic Families
- •5.6 CMOS Logic Gates
- •Circuit Components
- •6.0 Power Supplies
- •6.1 Clocked Logic and Flip-flops
- •6.2 Clocks
- •6.3 Frequency Dividers and Counters
- •6.4 Multiplexers and Demultiplexers
- •6.5 Input Devices
- •The Microchip PIC
- •7.0 The PICMicro Microcontroller
- •7.1 PIC Architecture
- •Mid-range PIC Architecture
- •8.0 Processor Architecture and Design
- •8.1 The Mid-range Core Features
- •8.2 Mid-Range CPU and Instruction Set
- •8.3 EEPROM Data Storage
- •8.4 Data Memory Organization
- •8.5 Mid-range I/O and Peripheral Modules
- •PIC Programming: Tools and Techniques
- •9.0 Microchip’s MPLAB
- •9.1 Integrated Development Environment
- •9.2 Simulators and Debuggers
- •9.3 Programmers
- •9.4 Engineering PIC Software
- •9.5 Pseudo Instructions
- •Programming Essentials: Input and Output
- •10.0 16F84A Programming Template
- •10.1 Introducing the 16F84A
- •10.2 Simple Circuits and Programs
- •10.3 Programming the Seven-segment LED
- •10.4 A Demonstration Board
- •Interrupts
- •11.0 Interrupts on the 16F84
- •11.1 Interrupt Sources
- •11.2 Interrupt Handlers
- •11.3 Interrupt Programming
- •11.4 Sample Programs
- •Timers and Counters
- •12.0 The 16F84 Timer0 Module
- •12.1 Delays Using Timer0
- •12.2 Timer0 as a Counter
- •12.3 Timer0 Programming
- •12.4 The Watchdog Timer
- •12.5 Sample Programs
- •LCD Interfacing and Programming
- •13.0 LCD Features and Architecture
- •13.1 Interfacing with the HD44780
- •13.2 HD44780 Instruction Set
- •13.3 LCD Programming
- •13.4 Sample Programs
- •Communications
- •14.0 PIC Communications Overview
- •14.1 Serial Data Transmission
- •14.2 Parallel Data Transmission
- •14.4 PIC Protocol-based Serial Programming
- •14.5 Sample Programs
- •Data EEPROM Programming
- •15.0 PIC Internal EEPROM Memory
- •15.1 EEPROM Devices and Interfaces
- •15.2 Sample Programs
- •Analog to Digital and Realtime Clocks
- •16.0 A/D Converters
- •16.1 A/D Integrated Circuits
- •16.2 PIC On-Board A/D Hardware
- •16.3 Realtime Clocks
- •16.4 Sample Programs
- •Index
Mid-range PIC Architecture |
149 |
•A/D Conversion Complete Interrupt
•LCD Interrupt
•Data EEPROM Write Complete Interrupt
•Timer Overflow Interrupt
•CCP Interrupt
•SSP Interrupt
Several SFRs are related to the interrupt systems. The INTCON register provides interrupt enabling and control and the PIE1, PIE2, PIR1, and PIR2 registers have specific device-related functions. Programming interrupts is discussed in the context of the corresponding operations later in this book.
8.2 Mid-Range CPU and Instruction Set
In a digital system, the central processing unit (CPU) is the component that executes the program instructions and processes data. It provides the fundamental functionality of a digital system and is responsible for its programmability. In the PIC architecture, the CPU is the part of the device which fetches and executes the instructions contained in a program.
The arithmetic-logic unit (ALU) is the CPU element that performs arithmetic, bitwise, and logical operations. It also controls the bits in the STATUS register as they are changed by the execution of the various program instructions. For example, if the result of executing an instruction is zero, the ALU sets the zero bit in the STATUS register.
8.2.1 Mid-Range Instruction Set
The mid-range PIC instruction set consists of 35 instructions, divided into three general groups:
1.Byte-oriented and byte-wise file register operations
2.Bit-oriented and bit-wise file register operations
3.Literal and control instructions
Table 8.1 lists and briefly describes each instruction in the mid-range set.
Table 8.1
Mid-range PIC Instruction Set
|
|
|
|
BITS |
MNEMONIC |
OPERAND |
DESCRIPTION |
CYCLES |
AFFECTED |
|
|
|
||
|
BYTE-ORIENTED OPERATIONS: |
|
||
ADDWF |
f,d |
Add w and f |
1 |
C,DC,Z |
ANDWF |
f,d |
AND w with f |
1 |
Z |
CLRF |
f |
Clear f |
1 |
Z |
CLRW |
- |
Clear w |
1 |
Z |
COMF |
f,d |
Complement f |
1 |
Z |
DECF |
f,d |
Decrement f |
1 |
Z |
(continues)
150 |
|
|
|
Chapter 8 |
|
|
Table 8.1 |
|
|
|
Mid-range PIC Instruction Set (continued) |
|
||
|
|
|
|
|
|
|
|
|
BITS |
MNEMONIC |
OPERAND |
DESCRIPTION |
CYCLES |
AFFECTED |
|
|
|
||
|
BYTE-ORIENTED OPERATIONS |
|
||
DECFSZ |
f,d |
Decrement, skip if 0 |
1(2) |
- |
INCF |
f,d |
Increment f |
1 |
Z |
INCFSZ |
f,d |
Increment, skip if 0 |
1(2) |
- |
IORWF |
f,d |
Inclusive OR w and f |
1 |
Z |
MOVF |
f,d |
Move f |
1 |
Z |
MOVWF |
f |
Move w to f |
1 |
- |
NOP |
- |
No operation |
1 |
- |
RLF |
f,d |
Rotate left |
1 |
C |
|
|
through carry |
|
|
RRF |
f,d |
Rotate right |
1 |
C |
|
|
through carry |
|
|
SUBWF |
f,d |
Subtract w from f |
1 |
C,DC,Z |
SWAPF |
f,d |
Swap nibbles in f |
1 |
- |
XORWF |
|
|
|
|
|
BIT-ORIENTED OPERATIONS |
|
|
|
BCF |
f,b |
Bit clear in f |
1 |
- |
BSF |
f,b |
Bit set in f |
1 |
- |
BTFSC |
f,b |
Bit test, skip |
1 |
- |
|
|
if clear |
|
|
BTFSS |
f,b |
Bit test, skip |
1 |
- |
|
|
if set |
|
|
|
|
|
||
|
LITERAL AND CONTROL OPERATIONS |
|
||
ADDLW |
k |
Add literal and w |
1 |
C,DC,Z |
ANDLW |
k |
AND literal and w |
1 |
Z |
CALL |
k |
Call procedure |
2 |
- |
CLRWDT |
- |
Clear watchdog timer |
1 |
TO,PD |
GOTO |
k |
Go to address |
2 |
- |
IORLW |
k |
Inclusive OR literal |
1 |
Z |
|
|
with w |
|
|
MOVLW |
k |
Move literal to w |
1 |
- |
RETFIE |
- |
Return from interrupt |
2 |
- |
RETLWk |
- |
Return literal in w |
2 |
- |
RETURN |
- |
Return from procedure |
2 |
- |
SLEEP |
- |
Go into SLEEP mode |
1 |
TO,PD |
SUBLW |
k |
Subtract literal and w |
1 |
C,DC,Z |
XORLW |
k |
Exclusive OR literal |
1 |
Z |
|
|
with w |
|
|
|
|
|
|
|
Legend: |
|
|
|
|
f = file register |
|
|
|
|
d = destination: |
0 = w register |
|
|
|
|
|
1 = file register |
|
|
b = bit position
k = 8-bit constant
Mid-range PIC Architecture |
151 |
8.2.2 STATUS and OPTION Registers
The STATUS register is one of the SFRs in the mid-range PICs. The bits in this register reflect the arithmetic status of the ALU, the RESET status, and the bits that select which memory bank is currently being accessed. Because the bank selection bits are in the STATUS register it must be present and at the same relative position in every bank. Figure 8-5 is a bitmap of the STATUS register.
bits: |
7 |
|
6 |
|
5 |
|
4 |
|
3 |
|
2 |
1 |
|
0 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
||
|
IRP |
RP-1 |
RP-0 |
|
TO |
|
PD |
|
Z |
DC |
|
C |
|
||
|
|
|
|
|
|
|
|
|
|
||||||
bit 7 IRP: Register Bank Select bit (used for indirect |
|||||||||||||||
|
|
addressing) |
|
|
|
|
|
|
|
|
|
||||
|
|
1 |
= |
Bank 2, 3 (0x100 - 0x1ff) |
|
|
|
|
|
||||||
|
|
0 |
= |
Bank 0, 1 (0x000 - 0xff) |
|
|
|
|
|
||||||
|
|
For |
devices with only Bank0 and Bank1 the |
|
|
||||||||||
|
|
IRP |
bit is reserved, always maintain this |
|
|
||||||||||
|
|
bit |
clear. |
|
|
|
|
|
|
|
|
|
|||
bit 6:5 RP1:RP0: |
|
|
|
|
|
|
|
|
|
|
|
||||
|
|
Register Bank Select bits (used for direct |
|
|
|||||||||||
|
|
addressing) |
|
|
|
|
|
|
|
|
|
||||
|
|
11 = Bank 3 (0x180 - 0x1ff) |
|
|
|
|
|
||||||||
|
|
10 = Bank 2 (0x100 - 0xx17f) |
|
|
|
|
|
||||||||
|
|
01 = Bank 1 (0x80 - 0xff) |
|
|
|
|
|
||||||||
|
|
00 = Bank 0 (0x00 - 0x7f) |
|
|
|
|
|
||||||||
|
|
Each bank is 128 bytes. For devices with only |
|||||||||||||
|
|
Bank0 and Bank1 the IRP bit is reserved, |
|
|
|||||||||||
|
|
always maintain this bit clear. |
|
|
|
|
|||||||||
bit 4 TO: |
Time-out bit |
|
|
|
|
|
|
|
|
|
|||||
|
|
1 |
= |
After power-up, CLRWDT instruction, or |
|
|
|||||||||
|
|
|
|
SLEEP instruction |
|
|
|
|
|
|
|
||||
|
|
0 |
= |
A WDT time-out occurred |
|
|
|
|
|
||||||
bit 3 PD: |
Power-down bit |
|
|
|
|
|
|
|
|
|
|||||
|
|
1 |
= |
After power-up or by the CLRWDT instruction |
|||||||||||
|
|
0 |
= |
By execution of the SLEEP instruction |
|
|
|||||||||
bit2 Z: |
Zero bit |
|
|
|
|
|
|
|
|
|
|||||
|
|
1 |
= |
The result of an operation is zero |
|
|
|||||||||
|
|
0 |
= |
The result of an operation is not zero |
|
|
|||||||||
bit 1 DC: |
Digit carry/borrow bit for ADDWF, ADDLW, SUBLW, |
||||||||||||||
|
|
and |
SUBWF instructions. For borrow the polarity |
||||||||||||
|
|
is reversed. |
|
|
|
|
|
|
|
|
|
||||
|
|
1 |
= |
A carry-out from the 4th bit of the result |
|||||||||||
|
|
0 |
= |
No carry-out from the 4th bit of the result |
|||||||||||
bit 0 C: |
Carry/borrow bit for ADDWF, ADDLW, SUBLW, and |
||||||||||||||
|
|
SUBWF instructions |
|
|
|
|
|
|
|
||||||
|
|
1 |
= |
A carry-out from the most significant bit |
|||||||||||
|
|
0 |
= |
No carry-out from the most significant bit |
Figure 8-5 STATUS Register Bitmap
152 |
Chapter 8 |
The STATUS register can be the destination for any instruction. If it is, and the Z, DC, or C bits are affected, then the write operation to these bits is disabled. In addition, the TO and PD bits are not writable.
Some instructions may have an unexpected action on the STATUS register bits, for example, the instruction
Clrf STATUS
clears the upper 3 bits, sets the Z bit, and leaves all other bits unchanged. For this reason, it is recommended that only instructions that do not change the Z, C, and DC bits be used to alter the STATUS register. The only ones that qualify are BCF, BSF, SWAPF, and MOVWF.
The OPTION register is actually named the OPTION_REG to avoid name clash with the option instruction. The OPTION_REG register contains several bits related to interrupts, the internal timers, and the watchdog timer. Figure 8-6 is a bitmap of the OPTION_REG register.
bits: |
7 |
|
6 |
5 |
4 |
3 |
2 |
1 |
|
0 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
RPBU |
INTEDG |
TOCS |
TOSE |
PSA |
PS2 |
PS1 |
|
PS0 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
bit 7 RBPU: |
PORTB Pull-up Enable bit |
|
|
|
|
||||||
|
|
|
1 |
= PORTB pull-ups are disabled |
|
|
|||||
|
|
|
0 |
= PORTB pull-ups are enabled by individual |
|||||||
|
|
|
|
port latch values |
|
|
|
|
|
||
bit 6 INTEDG: |
Interrupt Edge Select bit |
|
|
|
|
||||||
|
|
|
1 |
= Interrupt on rising edge of INT pin |
|||||||
|
|
|
0 |
= Interrupt on falling edge of INT pin |
|||||||
bit 5 T0CS: |
TMR0 Clock Source Select bit |
|
|
|
|
||||||
|
|
|
1 |
= Transition on T0CKI pin |
|
|
|
|
|||
|
|
|
0 |
= Internal instruction cycle clock |
|
|
|||||
|
|
|
|
(CLKOUT) |
|
|
|
|
|
|
|
bit 4 T0SE: |
TMR0 Source Edge Select bit |
|
|
|
|
1= Increment on high-to-low transition on T0CKI pin
0 = Increment on low-to-high transition on T0CKI pin
bit 3 PSA: Prescaler Assignment bit
1 = Prescaler is assigned to the WDT
0 = Prescaler is assigned to the Timer0 bit 2-0 PS2:PS0:
Prescaler Rate Select bits
Figure 8-6 Bitmap of the OPTION_REG Register