1.4 PIC Microcontrollers
The original name of the PIC microcontroller is PICmicro (Peripheral Interface Controller), but it is better known as a PIC. Its ancestor, called the PIC1650, was designed in 1975 by General Instruments. It was intended for totally different purposes. About ten years later, this circuit evolved into a real PIC microcontroller by adding EEPROM memory.
If you want to learn more about it, just keep on reading.
The main purpose of this book is to provide the user with necessary information he/she needs to know in order to be able to use microcontrollers in practice. In order to avoid tedious explanations and endless stories about the useful features of different microcontrollers, this book deals with the operation of one particular model belonging to the ‘high middle class’. It is the PIC16F887 microcontroller - powerful enough to be worth attention and simple enough to be easily learned by everybody. So, even though the following chapters describe this microcontroller in detail, they actually refer to the whole PIC family.
All PIC microcontrollers implement the Harvard architecture, which means that their program memory is connected to the CPU via more than 8 lines. Depending on the bus width, there are 12-, 14- and 16-bit microcontrollers. Table below shows the main features of these three categories of PIC microcontrollers.
Family |
ROM [Kbytes] |
RAM [bytes] |
Pins |
Clock Freq. [MHz] |
A/D Inputs |
Resolution of A/D Converter |
Compar- ators |
8/16 – bit Timers |
Serial Comm. |
PWM Outputs |
Others |
Base-Line 8 - bit architecture, 12-bit Instruction Word Length |
PIC10FXXX |
0.375 - 0.75 |
16 - 24 |
6 - 8 |
4 - 8 |
0 - 2 |
8 |
0 - 1 |
1 x 8 |
- |
- |
- |
PIC12FXXX |
0.75 - 1.5 |
25 - 38 |
8 |
4 - 8 |
0 - 3 |
8 |
0 - 1 |
1 x 8 |
- |
- |
EEPROM |
PIC16FXXX |
0.75 - 3 |
25 - 134 |
14 - 44 |
20 |
0 - 3 |
8 |
0 - 2 |
1 x 8 |
- |
- |
EEPROM |
PIC16HVXXX |
1.5 |
25 |
18 - 20 |
20 |
- |
- |
- |
1 x 8 |
- |
- |
Vdd = 15V |
Mid-Range 8 - bit architecture, 14-bit Instruction World Length |
PIC12FXXX |
1.75 - 3.5 |
64 - 128 |
8 |
20 |
0 - 4 |
10 |
1 |
1 - 2 x 8 1 x 16 |
- |
0 - 1 |
EEPROM |
PIC12HVXXX |
1.75 |
64 |
8 |
20 |
0 - 4 |
10 |
1 |
1 - 2 x 8 1 x 16 |
- |
0 - 1 |
- |
PIC16FXXX |
1.75 - 14 |
64 - 368 |
14 - 64 |
20 |
0 - 13 |
8 or 10 |
0 - 2 |
1 - 2 x 8 1 x 16 |
USART I2C SPI |
0 - 3 |
- |
PIC16HVXXX |
1.75 - 3.5 |
64 - 128 |
14 - 20 |
20 |
0 - 12 |
10 |
2 |
2 x 8 1 x 16 |
USART I2C SPI |
- |
- |
High-End 8 - bit architecture, 16-bit Instruction Word Length |
PIC18FXXX |
4 - 128 |
256 - 3936 |
18 - 80 |
32 - 48 |
4 - 16 |
10 or 12 |
0 - 3 |
0 - 2 x 8 2 - 3 x 16 |
USB2.0 CAN2.0 USART I2C SPI |
0 - 5 |
- |
PIC18FXXJXX |
8 - 128 |
1024 - 3936 |
28 - 100 |
40 - 48 |
10 - 16 |
10 |
2 |
0 - 2 x 8 2 - 3 x 16 |
USB2.0 USART Ethernet I2C SPI |
2 - 5 |
- |
PIC18FXXKXX |
8 - 64 |
768 - 3936 |
28 - 44 |
64 |
10 - 13 |
10 |
2 |
1 x 8 3 x 16 |
USART I2C SPI |
2 |
- |
All PIC microcontrollers use Harvard architecture, which means that their program memory is connected to the CPU over more than 8 lines. Depending on the bus width, there are 12-, 14- and 16-bit microcontrollers. Table above shows the main features of these three categories.
As can be seen in the table on the previous page, if we disregard ‘16-bit monsters’- PIC 24FXXX and PIC 24HXXX for a moment- all PIC microcontrollers have 8-bit Harvard architecture and belong to one out of three large groups. Accordingly, depending on the size of the program word there are the first, second and third microcontroller category, i.e. 12-, 14- or 16-bit microcontrollers. Having similar 8-bit core, all of them use the same instruction set and the basic hardware ‘skeleton’ connected to more or less peripheral units.
PIC microcontrollers with 14-bit program words are most likely the best choice for beginners. Here is why...
INSTRUCTION SET
The instruction set for the 16F8XX includes 35 instructions in total. The reason for such a small number of instructions lies in the RISC architecture. Instructions are well optimized from the operating speed aspect, simplicity in architecture and code compactness. The drawback to the RISC architecture is that the user is expected to cope with these instructions. Of course, this is relevant only if you use assembly language for programming. This book deals with programming in a high-level programming language called Basic, which means that most work has been already done by somebody else. You just have to use relatively simple instructions.
INSTRUCTION EXECUTION TIME
All instructions for PIC microcontrollers are single-cycle instructions. The only exception are conditional branch instructions (if condition is met) and instructions performed upon the program counter. In both cases, two cycles are required for instruction execution, while the second cycle is executed as an NOP (
No Operation). Single-cycle instructions consist of four clock cycles. It means that if a 4MHz oscillator is used, it will take 1µS for an instruction to be executed. The instruction execution time for jump instructions is 2µS.
Instruction set of 14-bit program words PIC microcontrollers:
INSTRUCTION |
DESCRIPTION |
OPERATION |
FLAG |
CLK |
* |
Data Transfer Instructions |
MOVLW k |
Move constant to W |
k -> w |
|
1 |
|
MOVWF f |
Move W to f |
W -> f |
|
1 |
|
MOVF f,d |
Move f to d |
f -> d |
Z |
1 |
1, 2 |
CLRW |
Clear W |
0 -> W |
Z |
1 |
|
CLRF f |
Clear f |
0 -> f |
Z |
1 |
2 |
SWAPF f,d |
Swap nibbles in f |
f(7:4),(3:0) -> f(3:0),(7:4) |
|
1 |
1, 2 |
Arithmetic-logic Instructions |
ADDLW k |
Add W and constant |
W+k -> W |
C, DC, Z |
1 |
|
ADDWF f,d |
Add W and f |
W+f -> d |
C, DC ,Z |
1 |
1, 2 |
SUBLW k |
Subtract W from constant |
k-W -> W |
C, DC, Z |
1 |
|
SUBWF f,d |
Subtract W from f |
f-W -> d |
C, DC, Z |
1 |
1, 2 |
ANDLW k |
Logical AND with W with constant |
W AND k -> W |
Z |
1 |
|
ANDWF f,d |
Logical AND with W with f |
W AND f -> d |
Z |
1 |
1, 2 |
IORLW k |
Logical OR with W with constant |
W OR k -> W |
Z |
1 |
|
IORWF f,d |
Logical OR with W with f |
W OR f -> d |
Z |
1 |
1, 2 |
XORWF f,d |
Logical exclusive OR with W with constant |
W XOR k -> W |
Z |
1 |
1, 2 |
XORLW k |
Logical exclusive OR with W with f |
W XOR f -> d |
Z |
1 |
|
INCF f,d |
Increment f by 1 |
f+1 -> f |
Z |
1 |
1, 2 |
DECF f,d |
Decrement f by 1 |
f-1 -> f |
Z |
1 |
1, 2 |
RLF f,d |
Rotate left f through CARRY bit |
|
C |
1 |
1, 2 |
RRF f,d |
Rotate right f through CARRY bit |
|
C |
1 |
1, 2 |
COMF f,d |
Complement f |
f -> d |
Z |
1 |
1, 2 |
Bit-oriented Instructions |
BCF f,b |
Clear bit b in f |
0 -> f(b) |
|
1 |
1, 2 |
BSF f,b |
Clear bit b in f |
1 -> f(b) |
|
1 |
1, 2 |
Program Control Instructions |
BTFSC f,b |
Test bit b of f. Skip the following instruction if clear. |
Skip if f(b) = 0 |
|
1 (2) |
3 |
BTFSS f,b |
Test bit b of f. Skip the following instruction if set. |
Skip if f(b) = 1 |
|
1 (2) |
3 |
DECFSZ f,d |
Decrement f. Skip the following instruction if clear. |
f-1 -> d skip if Z = 1 |
|
1 (2) |
1, 2, 3 |
INCFSZ f,d |
Increment f. Skip the following instruction if set. |
f+1 -> d skip if Z = 0 |
|
1 (2) |
1, 2, 3 |
GOTO k |
Go to address |
k -> PC |
|
2 |
|
CALL k |
Call subroutine |
PC -> TOS, k -> PC |
|
2 |
|
RETURN |
Return from subroutine |
TOS -> PC |
|
2 |
|
RETLW k |
Return with constant in W |
k -> W, TOS -> PC |
|
2 |
|
RETFIE |
Return from interrupt |
TOS -> PC, 1 -> GIE |
|
2 |
|
Other instructions |
NOP |
No operation |
TOS -> PC, 1 -> GIE |
|
1 |
|
CLRWDT |
Clear watchdog timer |
0 -> WDT, 1 -> TO, 1 -> PD |
TO, PD |
1 |
|
SLEEP |
Go into sleep mode |
0 -> WDT, 1 -> TO, 0 -> PD |
TO, PD |
1 |
|
*1 When an I/O register is modified, the value used will be that value present on the pins themselves.
*2 If the instruction is executed upon the TMR register and if d=1, the prescaler will be cleared.
*3 If the PC is modified or test result is a logic one (1), the instruction requires two cycles. The second cycle is executed as an NOP.
The architecture of 8-bit PIC microcontrollers. Which of these modules are to be built into the microcontroller depends on the type thereof.