Unit Iv
Unit Iv
PIC MICROCONTROLLER
APPLICATION
AGENDA
• Introduction to PIC
• Features
• Architecture
• Programming model(memory organization)
• Oscillator options
• BOD, power down modes
• Configuration bit settings
• Peripheral support
• Comparison of 10f,12f,16f and 18f PIC controller
  families
• Overview of instruction set
Introduction to PIC
• In 1989, Microchip technology introduced an 8 bit
  controller called PIC
• PIC: Peripheral Interface Controller.
• The PIC family:
  ▫ 10xxx, 12xxx, 14xxx, 16xxx,17xxx and 18xxx
• PIC 18xxx is the highest performing microcontroller
  from the PIC family
• It is available in 18-pin to 80-pin packages.
Features of PIC 18fxx
• High speed RISC Architecture
• Operating frequency for PIC18FXXX DC to 40 MHz
• General features include: Timers, Watch dog timers,
  ADC, Extended Instruction / Data memory , Serial
  communication , Capture/ Compare/ Pulse width
  modulation (CCP).
• PIC 18 can have 2MB of program ROM memory. ROM may
  be of different types such as (flash, OTP, UV-EPROM etc.)
  ▫ 21 bit PC available
  ▫ On chip ROM: Flash :F and OTP:C
• PIC can have maximum of 4KB of data RAM.
  ▫ The RAM consists of two components:
     GP RAM
     SFRs
• EEPROM 256 Bytes for storing critical data.(optional)
• It uses 16-bit wide instructions, 8-bit wide data path.
• 5 I/O ports available for PIC18f4520
  ▫ PORT A through PORT E
  ▫ Each port can be configured as input or output.-Bidirectional port
  ▫ Each port has some other functions Such as timer , ADC,
    interrupts and serial communication
Features: PIC 18Fxxx
• Power Management Features
• Flexible Oscillator Structure
• Wide Operating Voltage Range: 2.0V to 5.5V
• High-Current Sink/Source 25 mA/25 mA
• Many on chip peripherals available.
  ▫ Up to three Programmable External Interrupts
  ▫ Counter/Timer modules.
     Modules 0,2 (8-Bits)
     Modules 1,3 (16-Bits)
  ▫ Up to two CCP Modules/ ECCP module
     The CCP module is a peripheral which allows the user to time and
      control different events
  ▫ ADC 10-bits with 13 input multiplexer.
PIC 18Fxxx : Peripherals
 ▫ Master Synchronous Serial Port (MSSP) module
   supporting 3-Wire SPI (all 4 modes) and I2C Master
   and Slave modes
 ▫ Enhanced Addressable USART module
 ▫ Dual Analog Comparators with Input Multiplexing
 ▫ Programmable 16-Level High/Low-Voltage Detection
   (HLVD) module:
    Supports interrupt on High/Low-Voltage Detection
1    MCLR/Vpp/RE3                RB7/KBI3/PGD 40
2    RA0/AN0                     RB6/KBI2/PGC 39
3    RA1/AN1                     RB5/KBI1/PGM 38
4    RA2/AN2/Vref-/CVref    RB4/KBI0/AN11/CSSPP    37
5    RA3/AN3/Vref+         RB3/AN9/VPO/CCP2^ 36
6    RA4/T0CKI/C1out/RCV      RB2/INT2/AN8/VMO     35
7    RA5/AN4/SS       RB1/INT1/AN10/SCK/SCL 34
8    RE0/AN5/CK1SPP        RB0/INT0/AN12/SDI/SDA   33
9    RE1/AN6/CK2SPP                        VDD 32
10   RE2/AN7/OESPP                         VSS 31
11   VDD                         RD7/SPP7/P1D 30
12   VSS                         RD6/SPP6/P1C 29
13   OSC1/CLKI                   RD5/SPP5/P1B 28
14   OSC2/CLKO/RA6                    RD4/SPP4 27
15 RC0/T1OSO/T13CKI             RC7/RX/DT/SDO 26
16 RC1/T1OSI/UOE/CCP2^               RC6/TX/CK 25
17 RC2/CCP1/P1A                      RC5/D+/VP 24
18 VUSB                              RC4/D-/VM 23
19 RD0/SPP0                           RD3/SPP3 22
20 RD1/SPP1                           RD2/SPP2 21
Architecture of PIC 18fxxx
WREG(Working Register)
•   8 bit WREG in the PIC.
•   Similar to accumulator of other controller
•   WREG is used for all arithmetic and logic instructions.
•   Ex: MOVLW k ; move literal value k into WREG
        MOVLW 25h      ; move 25h into WREG
•      ADDLW K           ; add literal value k to WREG
         ADD LW 35h     ; add value 35h to W(W=W+35h)
• Moving a value larger than 255(FFh), into the WREG
  will truncate the upper byte and cause a warning in .err
  file.
The file register
• The data memory space in PIC is a read/write
  memory.
• The data memory is also called file registers.
• the file registers are used by the CPU for data
  storage, scratch pad and registers for internal use.
• The file register data RAM in PIC is divided into two
  sections:
  ▫ Special Function Registers(SFRs)
  ▫ General Purpose RAM(GP-RAM)
File Register and access bank in PIC18
• The PIC 18f can have maximum of 4KB (4096 bytes)
  of on chip RAM.
• With 4096 bytes ,the file register has an addresses of
  000-FFFh.
• The file register is divided into 256 byte memory
  banks.
• Therefore maximum 16 (0-F) banks .
• At least one bank for file register is there in every
  PIC. This bank is known as access bank.
Data Memory Organization
•   Data memory is upto 4KB
•   Divided into Banks of 256 bytes
•   4KB/256bytes= 16 Banks
•   Half of Bank 0 and half of virtual
    Bank 15 is known as access bank
• BSR: Bank Select Register (0 to F)
  ▫ 4-bit Register
  ▫ Provides upper 4-bits of 12-bit address of data memory
  ▫ Direct addressing
• C (Carry/Borrow Flag):
  ▫ set when an addition generates a carry and a subtraction generates a borrow
• DC (Digit Carry Flag):
  ▫ also called Half Carry flag; set when carry generated from Bit3 to Bit4 in an
     arithmetic operation
• Z (Zero Flag):
  ▫ set when result of an operation is zero
• OV (Overflow Flag):
  ▫ set when result of an operation of signed numbers goes beyond seven bits
• N (Negative Flag):
  ▫ set when bit B7 is one of the result of an arithmetic /logic operation
Stack
•   The stack operates as a 31-word by 21-bit RAM .
•   It is not part of either program or data space.
•   5-bit Pointer, STKPTR.
•   The Stack Pointer is readable and writable.
    ▫ The address on the top of the stack is readable and writable
      through the Top-of-Stack (TOS) Special Function Registers.
    ▫ Data can also be pushed to, or popped from the stack, using these
      registers.
• A CALL type instruction causes a push onto the stack.
    ▫ The Stack Pointer is first incremented and the location pointed to
      by the Stack Pointer is written with the contents of the PC
      (already pointing to the instruction following the CALL).
• A RETURN type instruction causes a pop from the stack.
    ▫ The contents of the location pointed to by the STKPTR are
      transferred to the PC and then the Stack Pointer is decremented
• For stack access, four registers are provided in the Special
  Function Register (SFR) bank. They are:
  ▫   TOSU
  ▫   TOSH
  ▫   TOSL
  ▫   STKPTR
• The Stack Pointer is initialized to ‘00000’ after all Resets.
• There is no RAM associated with the location
  corresponding to a Stack Pointer value of ‘00000’; this is
  only a Reset value.
• Status bits indicate if the stack is full, or has overflown or
  underflown.
• Only the top of the return address
  stack (TOS) is readable and
  writable.
• A set of three registers,
  TOSU:TOSH:TOSL, hold the
  contents of the stack location
  pointed to by the STKPTR
  register.
 Stack Pointer (STKPTR)
  D7           D6         D5        D4          D3    D2    D1    D0
• C (Carry/Borrow Flag):
  ▫ set when an addition generates a carry and a subtraction generates a borrow
• DC (Digit Carry Flag):
  ▫ also called Half Carry flag; set when carry generated from Bit3 to Bit4 in an arithmetic
    operation
• Z (Zero Flag):
  ▫ set when result of an operation is zero
• OV (Overflow Flag):
  ▫ set when result of an operation of signed numbers goes beyond seven bits
• N (Negative Flag):
  ▫ set when bit B7 is one of the result of an arithmetic /logic operation
File Select Registers (FSR)
• There are three registers:
     FSR0, FSR1, and FSR2
     Each register composed of two 8-bit
      registers (FSRH and FSRL)
• Used as pointers for data registers
• Holds 12-bit address of data register
Instruction set of PIC 18f4520
• The CPU can access data in various ways. The data
  could be in any register, memory location, or
  provided as an immediate value. These various ways
  of accessing data are called addressing modes.
• The addressing modes supported by a up/uc, are
  determined when it is designed and cannot be altered
  by the programmer.
• The addressing modes supported by PIC18f are:
  ▫   Immediate
  ▫   Direct
  ▫   Register Indirect
  ▫   Indexed-ROM
Addressing modes in brief
• Immediate:
 ▫ The operand is literal constant.
 ▫ Operand comes immediately after the opcode when an
   instruction is specified.
 ▫ This addressing mode is used to load information into
   WREG and selected registers but not in any file
   registers.
 ex: MOVLW 45H
     ADDLW D’62’
• Direct:
• The entire data RAM file register can be accessed
  using either direct or register indirect addressing
  mode.
• In direct addressing mode, the operand data is in a
  RAM memory location whose address is known and
  this address is given as part of the instruction.
ex: MOVWF 0X40
    MOVFF 0X40,0X50
• Register indirect:
• In this addressing mode, a register is used as a pointer to the
  data RAM location.
• In PIC18f, three registers are used for this purpose:
  ▫ FSR0, FSR1,FSR2 (FSR: File select register)
• The FSR is 12-bit register allowing access to entire 4096 bytes
  of data RAM.
• Use instruction LFSR (load FSR) to load the RAM location .
ex: LFSR 0,0X30 MOVWF INDF0
    LFSR 1,0X20
    LFSR 2,0X40
• The FSRs are split into 8 bit registers. As FSRxL and
  FSRxH(only lower 4 bits used).
• INDF(Indirect register) is associated with each FSR.
  ▫ INDF0,INDF1,INDF2
• The data pointed by FSRx is moved into INDFx register.
• Indexed ROM
• This addressing mode is widely used to access data
  from the program ROM space of PIC18f
• It is also known as table processing.
• There are a group of instructions for table
  processing(both read as well as write).
• TBLPTR(Table pointer) is an 21 bit SFR used to
  point byte to be fetched from code memory.
  ▫ TBLPTR is divided into three 8-bit parts viz.
    TBLPTRL, TBLPTRH, TBLPTRU.
• The other SFR register used for table processing is
  TABLAT.
  ▫ The TABLAT is used to keep the byte once it is fetched
    into the CPU.
The instruction set is also grouped as:
• Byte oriented
 15   OPCODE     9    d   8 a    7             f(FileReg)    0
• Bit oriented
 15   OPCODE     9    b   8 a    7             f(FileReg)    0
• Literal
 15   OPCODE                    8 7            k (Literal)   0
• Control
 15   OPCODE                    8 7            n (Literal)   0
0 1 1 0 1 1 1 a f f f f f f f f
fileReg (w)
                                    1 1 0 0 f f f f f f                             f f f f f f (src)
[fd]        [fs]                    1 1 1 1 f f f f f f                             f f f f f f (dest)
1 1 1 0 1 1 11 n n nn n nn n
1 1 1 1 n n nn n n nn n nn n
Call Instruction
• *CALL k,s ; Transfers control to subroutine.
• RCALL addr11; Transfers control to a subroutine
  within 1K space
• PUSH ;Push the PC onto the stack
• POP ; Pop from the stack.
• RETLW k ; The k value is placed in WREG and the
  top of the stack is placed in PC
• RETURN s; Return from subroutine
• RETFIE s; Return from Interrupt exit
                                                           Operation: (PC)+4       TOS,
                                                           n      PC<20:1>,
CALL k,s                                                   If s=1, (W)      (WS)
                                                                   (Status)    (STATUSs),
                                                                   (BSR)     (BSRs)
 1 1 1 0 1 1 0s n n nn n nn n
 1 1 1 1 n n nn n n nn n nn n
Machine Control Instruction
•   CLRWDT; Clears Watchdog Timer
•   SLEEP; Enter sleep mode
•   RESET; Reset by software
•   NOP; No operation
Bit oriented Instruction
• BCF f,b,a ; Clear bit of a FileReg
  ▫ BCF STATUS,C ; C=0
  ▫ BCF PORTB,5 ; clear PORTB.5(=0)
• BSF f,b,a ; Bit set FileReg
  ▫ BSF PORTC,4 ; Set bit PORTC.4
• BTFSC f,b,a ; Bit Test FileReg, Skip if Clear
  ▫ BTFSC PORTB.5; Skip the next instruction if bit 0
• BTFSS f,b,a; Bit test FileReg, skip if set
  ▫ BTFSS PORTB.5; Skip the next instruction if bit set
• BTG f,b,a ; Toggle bit
  ▫ BTG PORTB,0
• TSTFSZ f,a ; Test FileReg and skip if zero (Byte)
  ▫ TSTFSZ PORTB; Test PORTB for zero
TABLE Instructions
• TBLRD: Table Read
• Read the contents of Program memory addressed by
  the TBLPTR
• If TBLRD* = (P.M.(TBLPTR))      TABLAT;
 ▫ TBLPTR =No change.
• If TBLRD*+ = (P.M.(TBLPTR))      TABLAT;
 ▫ (TBLPTR)+1       (TBLPTR)
• If TBLRD*- = (P.M.(TBLPTR))      TABLAT;
 ▫ (TBLPTR)-1     (TBLPTR)
• TBLRD+* = (TBLPTR)+1         (TBLPTR)
 ▫ (P.M.(TBLPTR))     TABLAT
Summary (Table processing)
•   * no change
•   *+ post- increment
•   *- post decrement
•   +* pre-increment
•   Write:
•   TBLWT(*,*+,*-,+*)
•   (TABLAT)      Holding Register
•   Holding registers are used to program the contents of
    program memory.
Special Features
• 1,00,000 Erase/Write Cycle Enhanced Flash Program
  Memory Typical
• 1,000,000 Erase/Write Cycle Data EEPROM Memory
  Typical
• Flash/Data EEPROM Retention: 100 Years Typical
• Self-Programmable under Software Control
• Priority Levels for Interrupts
• 8 x 8 Single-Cycle Hardware Multiplier
• Programmable period from 4 ms to 131s
• Single-Supply 5V In-Circuit Serial Programming (ICSP) via
  Two Pins
• In-Circuit Debug (ICD) via Two Pins
Comparison