PIC
Lecture 03
PIC18 features
• RISC architecture
• On-chip program (code) – ROM
• Data RAM
• Data EEPROM
• Timers
• ADC
• USART and I/O ports
Simple view of PIC
Program ROM
• ROM is used to store programs (program or code ROM)
• PIC 18 program ROM
• Flash [letter F for this]
• OTP (One Time Programmable) [letter C for this]
• Masked (during fabrication process)
Data RAM and EEPROM
• RAM is for data storage.
• RAM Space = General Purpose Register (GPR) + Special Function
Registers (SFR)
• Microchip website gives only the GPR size (because SFRs are fixed)
• EEPROM:
• To store critical data that does not need to be changed very often
PIC microcontroller pheripherals
• ADC (10 bits)
• Timers
• USART (Universal Synchronous Asynchronous Receiver Transmitter)
PIC architecture and assembly
language programming
The WREG register
• The majority of PIC registers are 8 bits
• Therefore, only one data type: 8 bits
• Any data larger than 8 bits must be broken into 8-bits chunks.
• WREG  working register (only one)
• WREG  similar to accumulator in other microprocessors
• WREG  for all arithmetic and logic instructions
Understanding the WREG register
• MOVLW
• Move 8-bit data into WREG register
• Move a literal (L) value to WREG (W)
•
•
• MOVLW 25H ;move value 25H (25 in hex) into WREG
Understanding the WREG register
• ADDLW
• Add literal value K to WREG and put the result back to WREG
• WREG = WREG + k
Understanding the WREG register
Understanding the WREG register
PIC FILE Register
• File register  data memory space  read/ write (static RAM)
• File register data RAM = SFR + GPR
• SFR
• Dedicated to special functions
• ALU status
• Timers
• Serial communication
• I/O ports
• ADC
• ….Etc.
PIC FILE Register (Contd..)
• PIC SFRs are fixed and 8-bits
• More SFR registers while PIC has more timers, ADCs etc
• GPR (General purpose registers or RAM)
• For data storage and scratch pad
• Large GPR size means more difficulties in managing using assembly language
• C compiler need more registers to handle parameters and perform job faster
PIC FILE Register (Contd..)
PIC FILE Register (Contd..)
PIC FILE Register (Contd..)
PIC FILE Register (Contd..)
Simple instructions with the default access
bank
• MOVWF  (W: WREG, F: file register)
• This instruction tells to the CPU to move the source register of WREG to a
destination in the file register.
Special function registers
Simple instructions with the default access
bank GPR
Example
Example
More instructions
• ADDWF fileReg, D
SFR or GPR
Adding the content of
WREG and a file
register
Destination bit
D=0  destination is WREG
D=1  destination is file register
Example
Example - answer
Example
Example - answer
To make things less confusion
Destination = WREG
Destination = A file register
Try this..
Answer 1
Answer 2
WREG, File Registers and ALU
ALU instructions using both WREG and fileReg
File register instructions using fileReg or
WREG as destination
COMF instruction
How to write a program to toggle the SFR of Port B continuously forever?
COMF instruction
DECF
• Decrement (subtract one) the content of fileReg and place the result
in WREG or fileReg.
• Destination is fileReg
• Destination is WREG
MOVF
• This mnemonic is intended to perform MOVFW.
• MOVF fileReg, D
• If D=0  it copies the content of fileReg to WREG
• If D=1  fileReg is copied to itslef
MOVFF
• Copies data from One
location in fileReg 
another location in
fileReg
• Without going through
the WREG
PIC Status Register
• Flag register (status register)
PIC Status Register
• C (carry flag)
• This is set whenever there is a carry out from D7 bit (8th bit)
• This is affected after 8-bit addition and subtraction
• DC (digital carry flag) [Auxiliary Carry Flag]
• This bit is set whenever if there is a carry from D3 to D4 (during add and sub)
• This is used for BCD arithmetic
• Z (zero flag)
• If the result of arithmetic or logical operation is zero then z=1, otherwise (z=0)
for non-zero result.
PIC Status Register
• OV (overflow flag)
• This is used for signed number arithmetic
• N (Negative flag)
• This is also used for signed number arithmetic
• D7=0  N=0  result is positive
• D7=1  N=1  result is negative
• Not all instructions affect the flags
PIC Status Register
PIC Status Register
Flag bits and decision
• Status flags are also called conditional flags.
• Some instruction will make conditional jump (branch) based on the
status of the flag bits.
PIC data format representation
• Hex numbers
If the value started with hex digit (A-F), then it must
be preceded with zero.
PIC data format representation
• Binary numbers (1 way)
• Decimal numbers (2 ways)
• ASCII character
Assembler Directives
• Instructions  What to do using the CPU
• Directives (pseudo instructions)  Directions to the assembler
• Ex: EQU, SET, ORG and END
• EQU
• This is used to define a constant value or fixed address.
Assembler Directives
• SET
• This is used to define a constant value or fixed address.
• SET and EQU directives are identical.
• The value assigned by the SET directive may be reassigned later.
• ORG
• This is used to indicate the beginning of the address. (For both code and
data).
• The number that comes after ORG must be in hex.
Assembler Directives
• END
• This indicates to the assembler the end of source file (asm).
• Last line of the program
• LIST
• This indicates to the assembler the specific PIC chip
• We use LIST to state the target chip
Assembler Directives
• #include
• This tells the assembler to use the libraries associated with the specific PIC
• _config
• This tells the assembler the configurations bits for the target chip.
• Don’t use incorrect config:  it may meke the chip unusable
• radix
• This indicates whether the numbering system is hexadecimal or decimal.
• Default is hexadecimal
• For decimal
Rules for labels
• Meaningful
• Unique
• Label  consist of
• upper and lower case letters
• digits
• ?
• .
• @
• _
• $
• First character  alphabetic character
• Don’t use reserved words (check the assembler for more details)
Structure of assembly language
• Four fields
Optional fields
Line of the code by
name
Structure of assembly language (Example-
asm file)
Steps to create a program
Steps to create a program
• Sample of PIC err file
Steps to create a program
• Sample of List file
Program counter (PC)
• Another important register in the PIC microcontroller.
• The PC is used by the CPU to point the address of the next instruction
to be executed.
• The PC is incremented automatically.
• The wider the PC  CPU can access more memory locations
• 14-bit PC  214 (16K) code  (from 0000 to 3FFFH)
• 16-bit  216 (64K) code  (0000-FFFFH)
• 21-bit  221 (2M)  (000000-1FFFFFH)
Program counter (PC)
• PIC18 on-chip ROM Size and address space
Program counter (PC)
Program counter (PC)
Executing a program byte by byte
Two bytes
instructions
Four bytes instruction
Executing a program byte by byte
• ROM contents
Why PIC use Harvard architecture?
Instruction size of PIC 18
• MOLW (2 byte instruction)
Opcode (8 bits) Literal
value
Instruction size of PIC 18
• ADDLW
• MOVWF
Instruction size of PIC 18
• MOVFF (4 bytes)
• GOTO (4 bytes)
Because,
instructions are
either 2 bytes or 4
bytes
Ways to increase performance
• There are three ways available to microprocessor designers to
increase the processing power of CPU
• Increase clock frequency  more power and heat dissipation
• Use Harvard architecture  very expensive and unrealistic for x86
architecture
• Use RISC architecture
Microchip used all three methods
Features of RISC
• Feature 1: RISC processors have fixed instruction size.
• Feature 2: Use large number of registers (at least 32 registers).
• Feature 3: RISC processors have a small instruction set.
• Feature 4: more than 95% of instructions are executed with only one
clock cycle.
• Feature 5: RISC processors have separate buses for data and code.
• Feature 6: due to the small set of instructions, they are implemented
using the hardwire method. (no more than 10% of transistors)
• Feature 7: RISC uses load/store architecture. (no direct access to
external memory for arithmetic operations, only via registers)
• Shall we try MPLAB IDE?????
Summary

Lecture 03 basics of pic

  • 1.
  • 2.
    PIC18 features • RISCarchitecture • On-chip program (code) – ROM • Data RAM • Data EEPROM • Timers • ADC • USART and I/O ports
  • 3.
  • 5.
    Program ROM • ROMis used to store programs (program or code ROM) • PIC 18 program ROM • Flash [letter F for this] • OTP (One Time Programmable) [letter C for this] • Masked (during fabrication process)
  • 6.
    Data RAM andEEPROM • RAM is for data storage. • RAM Space = General Purpose Register (GPR) + Special Function Registers (SFR) • Microchip website gives only the GPR size (because SFRs are fixed) • EEPROM: • To store critical data that does not need to be changed very often
  • 7.
    PIC microcontroller pheripherals •ADC (10 bits) • Timers • USART (Universal Synchronous Asynchronous Receiver Transmitter)
  • 8.
    PIC architecture andassembly language programming
  • 9.
    The WREG register •The majority of PIC registers are 8 bits • Therefore, only one data type: 8 bits • Any data larger than 8 bits must be broken into 8-bits chunks. • WREG  working register (only one) • WREG  similar to accumulator in other microprocessors • WREG  for all arithmetic and logic instructions
  • 10.
    Understanding the WREGregister • MOVLW • Move 8-bit data into WREG register • Move a literal (L) value to WREG (W) • • • MOVLW 25H ;move value 25H (25 in hex) into WREG
  • 11.
    Understanding the WREGregister • ADDLW • Add literal value K to WREG and put the result back to WREG • WREG = WREG + k
  • 12.
  • 13.
  • 14.
    PIC FILE Register •File register  data memory space  read/ write (static RAM) • File register data RAM = SFR + GPR • SFR • Dedicated to special functions • ALU status • Timers • Serial communication • I/O ports • ADC • ….Etc.
  • 15.
    PIC FILE Register(Contd..) • PIC SFRs are fixed and 8-bits • More SFR registers while PIC has more timers, ADCs etc • GPR (General purpose registers or RAM) • For data storage and scratch pad • Large GPR size means more difficulties in managing using assembly language • C compiler need more registers to handle parameters and perform job faster
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
    Simple instructions withthe default access bank • MOVWF  (W: WREG, F: file register) • This instruction tells to the CPU to move the source register of WREG to a destination in the file register. Special function registers
  • 21.
    Simple instructions withthe default access bank GPR
  • 22.
  • 23.
  • 24.
    More instructions • ADDWFfileReg, D SFR or GPR Adding the content of WREG and a file register Destination bit D=0  destination is WREG D=1  destination is file register
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
    To make thingsless confusion Destination = WREG Destination = A file register
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
    ALU instructions usingboth WREG and fileReg
  • 35.
    File register instructionsusing fileReg or WREG as destination
  • 36.
    COMF instruction How towrite a program to toggle the SFR of Port B continuously forever?
  • 37.
  • 38.
    DECF • Decrement (subtractone) the content of fileReg and place the result in WREG or fileReg. • Destination is fileReg • Destination is WREG
  • 39.
    MOVF • This mnemonicis intended to perform MOVFW. • MOVF fileReg, D • If D=0  it copies the content of fileReg to WREG • If D=1  fileReg is copied to itslef
  • 40.
    MOVFF • Copies datafrom One location in fileReg  another location in fileReg • Without going through the WREG
  • 41.
    PIC Status Register •Flag register (status register)
  • 42.
    PIC Status Register •C (carry flag) • This is set whenever there is a carry out from D7 bit (8th bit) • This is affected after 8-bit addition and subtraction • DC (digital carry flag) [Auxiliary Carry Flag] • This bit is set whenever if there is a carry from D3 to D4 (during add and sub) • This is used for BCD arithmetic • Z (zero flag) • If the result of arithmetic or logical operation is zero then z=1, otherwise (z=0) for non-zero result.
  • 43.
    PIC Status Register •OV (overflow flag) • This is used for signed number arithmetic • N (Negative flag) • This is also used for signed number arithmetic • D7=0  N=0  result is positive • D7=1  N=1  result is negative • Not all instructions affect the flags
  • 44.
  • 45.
  • 46.
    Flag bits anddecision • Status flags are also called conditional flags. • Some instruction will make conditional jump (branch) based on the status of the flag bits.
  • 47.
    PIC data formatrepresentation • Hex numbers If the value started with hex digit (A-F), then it must be preceded with zero.
  • 48.
    PIC data formatrepresentation • Binary numbers (1 way) • Decimal numbers (2 ways) • ASCII character
  • 49.
    Assembler Directives • Instructions What to do using the CPU • Directives (pseudo instructions)  Directions to the assembler • Ex: EQU, SET, ORG and END • EQU • This is used to define a constant value or fixed address.
  • 50.
    Assembler Directives • SET •This is used to define a constant value or fixed address. • SET and EQU directives are identical. • The value assigned by the SET directive may be reassigned later. • ORG • This is used to indicate the beginning of the address. (For both code and data). • The number that comes after ORG must be in hex.
  • 51.
    Assembler Directives • END •This indicates to the assembler the end of source file (asm). • Last line of the program • LIST • This indicates to the assembler the specific PIC chip • We use LIST to state the target chip
  • 52.
    Assembler Directives • #include •This tells the assembler to use the libraries associated with the specific PIC • _config • This tells the assembler the configurations bits for the target chip. • Don’t use incorrect config:  it may meke the chip unusable • radix • This indicates whether the numbering system is hexadecimal or decimal. • Default is hexadecimal • For decimal
  • 53.
    Rules for labels •Meaningful • Unique • Label  consist of • upper and lower case letters • digits • ? • . • @ • _ • $ • First character  alphabetic character • Don’t use reserved words (check the assembler for more details)
  • 54.
    Structure of assemblylanguage • Four fields Optional fields Line of the code by name
  • 55.
    Structure of assemblylanguage (Example- asm file)
  • 56.
    Steps to createa program
  • 57.
    Steps to createa program • Sample of PIC err file
  • 58.
    Steps to createa program • Sample of List file
  • 59.
    Program counter (PC) •Another important register in the PIC microcontroller. • The PC is used by the CPU to point the address of the next instruction to be executed. • The PC is incremented automatically. • The wider the PC  CPU can access more memory locations • 14-bit PC  214 (16K) code  (from 0000 to 3FFFH) • 16-bit  216 (64K) code  (0000-FFFFH) • 21-bit  221 (2M)  (000000-1FFFFFH)
  • 60.
    Program counter (PC) •PIC18 on-chip ROM Size and address space
  • 61.
  • 62.
  • 63.
    Executing a programbyte by byte Two bytes instructions Four bytes instruction
  • 64.
    Executing a programbyte by byte • ROM contents
  • 65.
    Why PIC useHarvard architecture?
  • 66.
    Instruction size ofPIC 18 • MOLW (2 byte instruction) Opcode (8 bits) Literal value
  • 67.
    Instruction size ofPIC 18 • ADDLW • MOVWF
  • 68.
    Instruction size ofPIC 18 • MOVFF (4 bytes) • GOTO (4 bytes) Because, instructions are either 2 bytes or 4 bytes
  • 69.
    Ways to increaseperformance • There are three ways available to microprocessor designers to increase the processing power of CPU • Increase clock frequency  more power and heat dissipation • Use Harvard architecture  very expensive and unrealistic for x86 architecture • Use RISC architecture Microchip used all three methods
  • 70.
    Features of RISC •Feature 1: RISC processors have fixed instruction size. • Feature 2: Use large number of registers (at least 32 registers). • Feature 3: RISC processors have a small instruction set. • Feature 4: more than 95% of instructions are executed with only one clock cycle. • Feature 5: RISC processors have separate buses for data and code. • Feature 6: due to the small set of instructions, they are implemented using the hardwire method. (no more than 10% of transistors) • Feature 7: RISC uses load/store architecture. (no direct access to external memory for arithmetic operations, only via registers)
  • 71.
    • Shall wetry MPLAB IDE?????
  • 72.