Anda di halaman 1dari 6

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.
Clock Freq. [MHz] Resolution of A/D Converter 8/16 bit Timers

Family

ROM [Kbytes]

RAM [bytes]

Pins

A/D Inputs

Comparators

Serial Comm.

PWM Outputs

Others

Base-Line 8 - bit architecture, 12-bit Instruction Word Length PIC10FXXX PIC12FXXX PIC16FXXX PIC16HVXXX 0.375 0.75 0.75 1.5 0.75 - 3 1.5 16 - 24 25 - 38 25 134 25 68 8 14 44 18 20 4-8 4-8 20 20 0-2 0-3 0-3 8 8 8 0-1 0-1 0-2 1x8 1x8 1x8 1x8 EEPROM EEPROM Vdd = 15V

Mid-Range 8 - bit architecture, 14-bit Instruction World Length

PIC12FXXX

1.75 3.5

64 128

20

0-4

10

1-2x 81x 16 1-2x 81x 16 1-2x 81x 16 2x81 x 16

0-1

EEPROM

PIC12HVXXX

1.75

64

20

0-4

10

0-1

PIC16FXXX

1.75 - 14 1.75 3.5

64 368 64 128

14 64 14 20

20

0 - 13

8 or 10

0-2

USART I2C SPI USART I2C SPI

0-3

PIC16HVXXX

20

0 - 12

10

High-End 8 - bit architecture, 16-bit Instruction Word Length 256 3936 18 80 32 48 0-2x 82-3 x 16 0-2x 82-3 x 16 1x83 x 16 USB2.0 CAN2.0 USART I2C SPI USB2.0 USART Ethernet I2C SPI USART I2C SPI

PIC18FXXX

4 - 128

4 - 16

10 or 12

0-3

0-5

PIC18FXXJXX

8 - 128

1024 3936 768 3936

28 100 28 44

40 48

10 - 16

10

2-5

PIC18FXXKXX

8 - 64

64

10 - 13

10

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 8bit 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 1S for an instruction to be executed. The instruction execution time for jump instructions is 2S. Instruction set of 14-bit program words PIC microcontrollers:

INSTRUCTION

DESCRIPTION Data Transfer Instructions

OPERATION

FLAG

CLK

MOVLW k MOVWF f MOVF f,d CLRW CLRF f SWAPF f,d

Move constant to W Move W to f Move f to d Clear W Clear f Swap nibbles in f

k -> w W -> f f -> d 0 -> W 0 -> f f(7:4),(3:0) -> f(3:0),(7:4) Z Z Z

1 1 1 1 1 1 2 1, 2 1, 2

Arithmetic-logic Instructions ADDLW k ADDWF f,d Add W and constant Add W and f W+k -> W W+f -> d C, DC, Z C, DC 1 1 1, 2

,Z SUBLW k SUBWF f,d ANDLW k ANDWF f,d IORLW k IORWF f,d XORWF f,d XORLW k INCF f,d DECF f,d RLF f,d RRF f,d COMF f,d Subtract W from constant Subtract W from f Logical AND with W with constant Logical AND with W with f Logical OR with W with constant Logical OR with W with f Logical exclusive OR with W with constant Logical exclusive OR with W with f Increment f by 1 Decrement f by 1 Rotate left f through CARRY bit Rotate right f through CARRY bit Complement f Bit-oriented Instructions BCF f,b BSF f,b Clear bit b in f Clear bit b in f Program Control Instructions BTFSC f,b BTFSS f,b DECFSZ f,d INCFSZ f,d Test bit b of f. Skip the following instruction if clear. Test bit b of f. Skip the following instruction if set. Decrement f. Skip the following instruction if clear. Increment f. Skip the following instruction if set. Skip if f(b) = 0 Skip if f(b) = 1 f-1 -> d skip if Z = 1 f+1 -> d skip if Z = 0 1 (2) 1 (2) 1 (2) 1 (2) 3 3 1, 2, 3 1, 2, 0 -> f(b) 1 -> f(b) 1 1 1, 2 1, 2 f -> d k-W -> W f-W -> d W AND k -> W W AND f -> d W OR k -> W W OR f -> d W XOR k -> W W XOR f -> d f+1 -> f f-1 -> f C, DC, Z C, DC, Z Z Z Z Z Z Z Z Z C C Z 1 1 1 1 1 1 1 1 1 1 1 1 1 1, 2 1, 2 1, 2 1, 2 1, 2 1, 2 1, 2 1, 2 1, 2

3 GOTO k CALL k RETURN RETLW k RETFIE Go to address Call subroutine Return from subroutine Return with constant in W Return from interrupt Other instructions NOP CLRWDT SLEEP No operation Clear watchdog timer Go into sleep mode TOS -> PC, 1 -> GIE 0 -> WDT, 1 -> TO, 1 -> PD 0 -> WDT, 1 -> TO, 0 -> PD TO, PD TO, PD 1 1 1 k -> PC PC -> TOS, k -> PC TOS -> PC k -> W, TOS -> PC TOS -> PC, 1 -> GIE 2 2 2 2 2

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