Top Banner
PIC MICROCONTROLLER 1
24

Pic microcontroller kh -

Jan 18, 2017

Download

Engineering

Ken Ken
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Pic microcontroller kh -

1

PIC MICROCONTROLLER

Page 2: Pic microcontroller kh -

2

Overview

• Microcontroller and Architecture Basics• Variants among families• ISA• Programming overview • Hardware interface/Developmental boards

Page 3: Pic microcontroller kh -

3

Description

• PIC – “Peripheral Interface Controller”• Made by Microchip Technology• Most popular by industry developers and

hobbyists– Low cost (cents to dollars)– Availability– Extensive application notes– Serial programming

Page 4: Pic microcontroller kh -

4

vhttp://www.microchip.com/stellent/images/mchpsiteimages/en537986.jpg

Page 5: Pic microcontroller kh -

5

8-bit Core Architecture• Harvard (separate code

and data space)• RISC• One accumulator

– W Register• Small addressable data

space (256 bytes)– Banking

• RAM– PC, special purpose

registers

http://www.microchip.com/_images/BaselineArch_large.jpg

Page 6: Pic microcontroller kh -

6

Advantages & Limitations to 8-bit Architecture

• Small ISA to learn• Built in oscillator with selectable speeds• Development made easy• Inexpensive• Device variants• Wide range of interfaces (I2C, SPI, etc.)

• One accumulator• Bank switching• Hardware call stack unaddressable (no multi-tasking)

Page 7: Pic microcontroller kh -

7

16-bit and 32-bit Architecture

• More working registers• No bank switching• Assignable interrupt vector table• More flash memory• Cache (32-bit architecture)

Page 8: Pic microcontroller kh -

8

http://www.microchip.com/TechDoc.aspx?type=appnotes

Application Notes• Very extensive

– 16758 items• Description of

documentation• PDF documenation• Source code

http://www.microchip.com/TechDoc.aspx?type=appnotes

Page 9: Pic microcontroller kh -

8-bit architecture

Baseline Architecture

Mid-Range Architecture

Enhanced Mid-Range Architecture

PIC18 Architecture

Pin Count 6-40 8-64 8-64 18-100

Interrupts No Single interrupt capability

Single interrupt capability with hardware context save

Multiple interrupt capability with hardware context save

Performance 5 MIPS 5 MIPS 8 MIPS Up to 16 MIPS

Instructions 33, 12-bit 35, 14-bit 49, 14-bit 83, 16-bit

Program Memory

Up to 3 KB Up to 14 KB Up to 28 KB Up to 128 KB

Data Memory Up to 138 Bytes Up to 368 Bytes Up to 1.5 KB Up to 4 KB

Features • Comparator• 8-bit ADC• Data Memory• Internal

Oscillator

In addition to Baseline:• SPI/I²C™• UART• PWMs• LCD• 10-bit ADC• Op Amp

In addition toMid-Range:• Multiple

Communication Peripherals

• Linear Programming Space

• PWMs with Independent Time Base

In addition toEnhanced Mid-Range:• 8x8 Hardware Multiplier• CAN• CTMU• USB• Ethernet• 12-bit ADC

Families PIC10, PIC12, PIC16 PIC12, PIC16 PIC12FXXX, PIC16F1XX PIC18

Page 10: Pic microcontroller kh -

INSTRUCTION DESCRIPTION OPERATION

Data Transfer Instructions

MOVLW k Move constant to W k -> w

MOVWF f Move W to f W -> f

MOVF f,d Move f to d f -> d

CLRW Clear W 0 -> W

CLRF f Clear f 0 -> f

SWAPF f,d Swap nibbles in f f(7:4),(3:0) -> f(3:0),(7:4)

Arithmetic-logic Instructions

ADDLW k Add W and constant W+k -> W

ADDWF f,d Add W and f W+f -> d

SUBLW k Subtract W from constant k-W -> W

SUBWF f,d Subtract W from f f-W -> d

ANDLW k Logical AND with W with constant W AND k -> W

ANDWF f,d Logical AND with W with f W AND f -> d

ANDWF f,d Logical AND with W with f W AND f -> d

IORLW k Logical OR with W with constant W OR k -> W

IORWF f,d Logical OR with W with f W OR f -> d

XORWF f,d Logical exclusive OR with W with constant W XOR k -> W

XORLW k Logical exclusive OR with W with f W XOR f -> d

INCF f,d Increment f by 1 f+1 -> f

DECF f,d Decrement f by 1 f-1 -> f

RLF f,d Rotate left f through CARRY bit

RRF f,d Rotate right f through CARRY bit

COMF f,d Complement f f -> d

Bit-oriented InstructionsBCF f,b Clear bit b in f 0 -> f(b)

BSF f,b Clear bit b in f 1 -> f(b)

Program Control Instructions

BTFSC f,b Test bit b of f. Skip the following instruction if clear. Skip if f(b) = 0

BTFSS f,b Test bit b of f. Skip the following instruction if set. Skip if f(b) = 1

DECFSZ f,d Decrement f. Skip the following instruction if clear. f-1 -> d skip if Z = 1

INCFSZ f,d Increment f. Skip the following instruction if set. f+1 -> d skip if Z = 0

GOTO k Go to address k -> PC

CALL k Call subroutine PC -> TOS, k -> PC

RETURN Return from subroutine TOS -> PC

RETLW k Return with constant in W k -> W, TOS -> PC

RETFIE Return from interrupt TOS -> PC, 1 -> GIE

Other instructions

NOP No operation TOS -> PC, 1 -> GIE

CLRWDT Clear watchdog timer 0 -> WDT, 1 -> TO, 1 -> PD

SLEEP Go into sleep mode 0 -> WDT, 1 -> TO, 0 -> PD

Data Transfer Instructions

INSTRUCTION DESCRIPTION OPERATION

MOVLW k Move constant to W k -> w

MOVWF f Move W to f W -> f

MOVF f,d Move f to d f -> d

CLRW Clear W 0 -> W

CLRF f Clear f 0 -> f

SWAPF f,d Swap nibbles in f f(7:4),(3:0) -> f(3:0),(7:4)

PIC1

6 IS

A

PIC16 ISA:35 Instructions, 14-bit

W: Working register(Accumulator)

Registers: Memory locations

Page 11: Pic microcontroller kh -

INSTRUCTION DESCRIPTION OPERATION

Data Transfer Instructions

MOVLW k Move constant to W k -> w

MOVWF f Move W to f W -> f

MOVF f,d Move f to d f -> d

CLRW Clear W 0 -> W

CLRF f Clear f 0 -> f

SWAPF f,d Swap nibbles in f f(7:4),(3:0) -> f(3:0),(7:4)

Arithmetic-logic Instructions

ADDLW k Add W and constant W+k -> W

ADDWF f,d Add W and f W+f -> d

SUBLW k Subtract W from constant k-W -> W

SUBWF f,d Subtract W from f f-W -> d

ANDLW k Logical AND with W with constant W AND k -> W

ANDWF f,d Logical AND with W with f W AND f -> d

ANDWF f,d Logical AND with W with f W AND f -> d

IORLW k Logical OR with W with constant W OR k -> W

IORWF f,d Logical OR with W with f W OR f -> d

XORWF f,d Logical exclusive OR with W with constant W XOR k -> W

XORLW k Logical exclusive OR with W with f W XOR f -> d

INCF f,d Increment f by 1 f+1 -> f

DECF f,d Decrement f by 1 f-1 -> f

RLF f,d Rotate left f through CARRY bit

RRF f,d Rotate right f through CARRY bit

COMF f,d Complement f f -> d

Bit-oriented InstructionsBCF f,b Clear bit b in f 0 -> f(b)

BSF f,b Clear bit b in f 1 -> f(b)

Program Control Instructions

BTFSC f,b Test bit b of f. Skip the following instruction if clear. Skip if f(b) = 0

BTFSS f,b Test bit b of f. Skip the following instruction if set. Skip if f(b) = 1

DECFSZ f,d Decrement f. Skip the following instruction if clear. f-1 -> d skip if Z = 1

INCFSZ f,d Increment f. Skip the following instruction if set. f+1 -> d skip if Z = 0

GOTO k Go to address k -> PC

CALL k Call subroutine PC -> TOS, k -> PC

RETURN Return from subroutine TOS -> PC

RETLW k Return with constant in W k -> W, TOS -> PC

RETFIE Return from interrupt TOS -> PC, 1 -> GIE

Other instructions

NOP No operation TOS -> PC, 1 -> GIE

CLRWDT Clear watchdog timer 0 -> WDT, 1 -> TO, 1 -> PD

SLEEP Go into sleep mode 0 -> WDT, 1 -> TO, 0 -> PD

Arithmetic-logic Instructions (partial)ADDLW k Add W and

constant W+k -> W

ADDWF f,d Add W and f W+f -> d

INCF f,d Increment f by 1 f+1 -> f

DECF f,d Decrement f by 1 f-1 -> f

RLF f,d Rotate left f through CARRY bit

RRF f,d Rotate right f through CARRY bit

COMF f,d Complement f f -> d

SUBLW, SUBWFANDLW, ANDWFIORLW, IORWFXORLW, XORWF

PIC1

6 IS

A

Page 12: Pic microcontroller kh -

INSTRUCTION DESCRIPTION OPERATION

Data Transfer Instructions

MOVLW k Move constant to W k -> w

MOVWF f Move W to f W -> f

MOVF f,d Move f to d f -> d

CLRW Clear W 0 -> W

CLRF f Clear f 0 -> f

SWAPF f,d Swap nibbles in f f(7:4),(3:0) -> f(3:0),(7:4)

Arithmetic-logic Instructions

ADDLW k Add W and constant W+k -> W

ADDWF f,d Add W and f W+f -> d

SUBLW k Subtract W from constant k-W -> W

SUBWF f,d Subtract W from f f-W -> d

ANDLW k Logical AND with W with constant W AND k -> W

ANDWF f,d Logical AND with W with f W AND f -> d

ANDWF f,d Logical AND with W with f W AND f -> d

IORLW k Logical OR with W with constant W OR k -> W

IORWF f,d Logical OR with W with f W OR f -> d

XORWF f,d Logical exclusive OR with W with constant W XOR k -> W

XORLW k Logical exclusive OR with W with f W XOR f -> d

INCF f,d Increment f by 1 f+1 -> f

DECF f,d Decrement f by 1 f-1 -> f

RLF f,d Rotate left f through CARRY bit

RRF f,d Rotate right f through CARRY bit

COMF f,d Complement f f -> d

Bit-oriented InstructionsBCF f,b Clear bit b in f 0 -> f(b)

BSF f,b Clear bit b in f 1 -> f(b)

Program Control Instructions

BTFSC f,b Test bit b of f. Skip the following instruction if clear. Skip if f(b) = 0

BTFSS f,b Test bit b of f. Skip the following instruction if set. Skip if f(b) = 1

DECFSZ f,d Decrement f. Skip the following instruction if clear. f-1 -> d skip if Z = 1

INCFSZ f,d Increment f. Skip the following instruction if set. f+1 -> d skip if Z = 0

GOTO k Go to address k -> PC

CALL k Call subroutine PC -> TOS, k -> PC

RETURN Return from subroutine TOS -> PC

RETLW k Return with constant in W k -> W, TOS -> PC

RETFIE Return from interrupt TOS -> PC, 1 -> GIE

Other instructions

NOP No operation TOS -> PC, 1 -> GIE

CLRWDT Clear watchdog timer 0 -> WDT, 1 -> TO, 1 -> PD

SLEEP Go into sleep mode 0 -> WDT, 1 -> TO, 0 -> PD

Program Control Instructions

BTFSC f,bTest bit b of f. Skip the following instruction if clear.

Skip if f(b) = 0

BTFSS f,bTest bit b of f. Skip the following instruction if set.

Skip if f(b) = 1

DECFSZ f,dDecrement f. Skip the following instruction if clear.

f-1 -> d skip if Z = 1

INCFSZ f,dIncrement f. Skip the following instruction if set.

f+1 -> d skip if Z = 0

GOTO k Go to address k -> PC

CALL k Call subroutine PC -> TOS, k -> PC

RETURN Return from subroutine TOS -> PC

RETLW k Return with constant in W k -> W, TOS -> PC

RETFIE Return from interrupt

TOS -> PC, 1 -> GIE

PIC1

6 IS

A

Page 13: Pic microcontroller kh -

INSTRUCTION DESCRIPTION OPERATION

Data Transfer Instructions

MOVLW k Move constant to W k -> w

MOVWF f Move W to f W -> f

MOVF f,d Move f to d f -> d

CLRW Clear W 0 -> W

CLRF f Clear f 0 -> f

SWAPF f,d Swap nibbles in f f(7:4),(3:0) -> f(3:0),(7:4)

Arithmetic-logic Instructions

ADDLW k Add W and constant W+k -> W

ADDWF f,d Add W and f W+f -> d

SUBLW k Subtract W from constant k-W -> W

SUBWF f,d Subtract W from f f-W -> d

ANDLW k Logical AND with W with constant W AND k -> W

ANDWF f,d Logical AND with W with f W AND f -> d

ANDWF f,d Logical AND with W with f W AND f -> d

IORLW k Logical OR with W with constant W OR k -> W

IORWF f,d Logical OR with W with f W OR f -> d

XORWF f,d Logical exclusive OR with W with constant W XOR k -> W

XORLW k Logical exclusive OR with W with f W XOR f -> d

INCF f,d Increment f by 1 f+1 -> f

DECF f,d Decrement f by 1 f-1 -> f

RLF f,d Rotate left f through CARRY bit

RRF f,d Rotate right f through CARRY bit

COMF f,d Complement f f -> d

Bit-oriented InstructionsBCF f,b Clear bit b in f 0 -> f(b)

BSF f,b Clear bit b in f 1 -> f(b)

Program Control Instructions

BTFSC f,b Test bit b of f. Skip the following instruction if clear. Skip if f(b) = 0

BTFSS f,b Test bit b of f. Skip the following instruction if set. Skip if f(b) = 1

DECFSZ f,d Decrement f. Skip the following instruction if clear. f-1 -> d skip if Z = 1

INCFSZ f,d Increment f. Skip the following instruction if set. f+1 -> d skip if Z = 0

GOTO k Go to address k -> PC

CALL k Call subroutine PC -> TOS, k -> PC

RETURN Return from subroutine TOS -> PC

RETLW k Return with constant in W k -> W, TOS -> PC

RETFIE Return from interrupt TOS -> PC, 1 -> GIE

Other instructions

NOP No operation TOS -> PC, 1 -> GIE

CLRWDT Clear watchdog timer 0 -> WDT, 1 -> TO, 1 -> PD

SLEEP Go into sleep mode 0 -> WDT, 1 -> TO, 0 -> PDPI

C16

ISA

Bit-oriented InstructionsBCF f,b Clear bit b in f 0 -> f(b)

BSF f,b Set bit b in f 1 -> f(b)

Other instructionsNOP No operation TOS -> PC, 1 ->

GIE

CLRWDT Clear watchdog timer

0 -> WDT, 1 -> TO, 1 -> PD

SLEEP Go into sleep mode

0 -> WDT, 1 -> TO, 0 -> PD

Page 14: Pic microcontroller kh -

14

PIC 16F887 layoutAnalog I/O

UART

I2C

PORTA

http://www.mikroe.com/eng/chapters/view/74/pic-basic-book-chapter-1-world-of-microcontrollers/

Page 15: Pic microcontroller kh -

15

Programming a PIC

• Microchip provides the free MPLAB:– Assembler and linker– Application development– Hardware emulation– Debugging– C or assembly compatible

• Compiler – Can be C-based or Basic– A free one is the CCS C Compiler for PIC12/24/26/18 (not

compatible with all PICS) or the HI-TECH PICC-Lit

http://www.microchip.com/pagehandler/en-us/family/mplabx/

Page 16: Pic microcontroller kh -

16

Programming Tools• Basic based environments

are available, but don’t offer the functionality of C

• Third party IDE’s might be preferred due to an enhanced software library or debugging tools

http://www.microchip.com/stellent/idcplg?IdcService=SS_GET_PAGE&nodeId=1406&dDocName=en019469&part=SW007002

Page 17: Pic microcontroller kh -

17

Device Programmer• Need device to store machine code

into PIC’s memory (EEPROM or Flash)• Can be external device, but ICSP is

easier:– Don’t have to remove chip from its

circuit– Provides interface between computer

(USB) and PIC– Specific to circuit (due to interconnect

scheme and surrounding circuit)– Communication protocol requires 5

signalshttp://en.wikipedia.org/wiki/PIC_microcontroller

Page 18: Pic microcontroller kh -

18

Device Programmer

Five Signals:• Vpp (programming voltage)• Vdd (power)• Vss (ground)• IC SPCLK (clock)• IC SPDAT (data)

http://en.wikipedia.org/wiki/PIC_microcontroller

Page 19: Pic microcontroller kh -

19

Necessary Connections (PIC16F877A)

• PIC can be bread-boarded, with the following important connections:– Power– Ground– Reset signal– Crystal (oscillator)

http://www.mikroe.com/eng/chapters/view/74/pic-basic-book-chapter-1-world-of-microcontrollers/

Page 20: Pic microcontroller kh -

20

Development Boards• Prepackaged boards come with a multitude of

peripherals for development and debugging:– Programmer– User I/O: buttons, port pinouts, LED’s– Displays: LCD and seven segment– Power– Serial connection interface

Page 21: Pic microcontroller kh -

21

Easy PIC v7 from MikroElectronika

PIC

Programmer

Power Supply

USB Port

Port PinoutsLEDs and buttons

http://www.mikroe.com/eng/chapters/view/74/pic-basic-book-chapter-1-world-of-microcontrollers/

Page 22: Pic microcontroller kh -

22

Additional Peripherals

Touch Screen

ADC converter

Page 23: Pic microcontroller kh -

23

Questions?

Page 24: Pic microcontroller kh -

24

References• http://www.microchip.com/• http://en.wikipedia.org/wiki/PIC_microcontroller• http://www.mikroe.com/eng/chapters/view/10/chapter-9

-instruction-set/• http://www.mikroe.com/eng/chapters/view/74/pic-basic-

book-chapter-1-world-of-microcontrollers/• http://www.slideshare.net/element14/microchips-16bit-a

nd-32bit-pic-mcus-7267006• http://www.ladyada.net/library/picvsavr.html• http://www.mikroe.com/eng/chapters/view/74/pic-basic-

book-chapter-1-world-of-microcontrollers/• http://www.slideshare.net/element14/microchips-16bit-a

nd-32bit-pic-mcus-7267006• http://en.wikipedia.org/wiki/PIC_microcontroller• http://www.ladyada.net/library/picvsavr.html