PIC Architecture & Assembly Language Programming Hsiao-Lung Chan Dept Electrical Engineering Chang Gung University, Taiwan chanhl@mail.cgu.edu.tw
ALU with working register (WREG) and literal value 2
MOVLW instruction MOVLW K ; move literal value K into WREG MOVLW 25H MOVLW 7F2H ; illegal 7F2H > 8 bits (FFH), become F2H Instruction format (16 bits) OP code Literal value 3
ADDLW instruction ADDLW K ; ADD literal value K to WREG MOVLW 25H ADDLW 34H Instruction format (16 bits) OP code Literal value 4
PIC18 memory access up to 2 MB of program memory Inside the c chip Program Memory Space (a portion of this space is on the c chip) 21-bit progam address 16-bit instruction bus Program counter (PC) PIC18 CPU 4096 registers 12-bit register address 8-bit data bus Data Memory Space (Special function registers and general purpose RAM) Figure 1.3 The PIC18 memory spaces 5
File register (data RAM) Special functional registers (SFRs) ALU status, timers, serial communication, I/O ports, etc General-purpose registers (GPRs) Data storage 6
SFRs in PIC18 7
File registers of PIC12, PIC16, and PIC18 8
PIC18 banks and access bank 9
PIC18 banks PIC18 banks 4096 registers are divided into 16 banks. Only one bank is active at a time. Bank switching When operating on a register in a different bank, bank switching is needed. 10
MOVWF instruction Copy WREG content to a file register Ex. 1 MOVLW 55H MOVWF PORTB ; copy WREG contents to port B Ex. 2 MOVLW 99H MOVWF 12H ; copy WREG contents to location 12h default 11
ADDWF instruction ADDWF filereg, d Add the contents of WREG and a file register Destination, d If d=0, result is placed in WREG If d=1, result is placed in file register Ex1 MOVLW 28H MOVWF 5H MOVLW 56H ADDWF 5H, 0 or ADDWF 5H, 1 (ADDWF 5H, W or ADDWF 5H, F) 12
WREG, filereg, and ALU in PIC18 13
ALU instructions using WREG and filereg 14
File register instructions 15
COMF instruction Complements (inverts) the content of file register Ex1 MOVLW 55H MOVWF PORTB COMF PORTB, F ; 55H (01010101) AAH (10101010) Write a program to toggle PORTB continuously forever Ans: MOVLW 55H MOVWF PORTB B1 COMF PORTB, F GOTO B1 16
DECF instruction Subtract one from the content of filereg Ex1 MOVLW 3 MOVWF 20H DECF 0x20, F ; loc 20H has 2 DECF 0x20, W ; loc 20H still has 2 but WREG = 1 17
MOVF instruction MOVF filereg, d Copy the content of filereg to WREG or itself Write a program to read data from PortB and send to PORTC continuously Ans: AGAIN MOVF MOVWF GOTO PORTB, W PORTC AGAIN 18
MOVFF instruction Move data directly among filereg locations 32-bit instruction 19
MOVFF instruction (cont.) Write a program to read data from PortB and send to PORTC continuously Ans: AGAIN MOVFF GOTO PORTB, PORTC AGAIN MOVFF (32-bit instruction) 20
GOTO instruction 32-bit instruction PIC16 has 2M of ROM space The LSB is 0, making sure even address 21
PIC18 status register (Carry from D7 bit during an ADD or SUB operation) (Carry from D3 to D4 bit) (Binary coded decimal (BCD) operation) (for signed number operation) (for signed number operation) 22
Status register example 23
Status register example (cont.) 24
Instruction that affect flag bits 25
Branch instruction using flag bits 1 1 N 1 1 26
Assembling and linking a PIC program 27
List file 28
von Neumann vs. Harvard acrchitecture 29
PIC on-chip program ROM address range 30
PIC18 Program ROM space Wake up at memory address 0000 when PIC is powered up 31
Reference M.A. Mazidi, R.D. Mckinlay, D Causey, PIC Microcontroller and Embedded Systems Using Assembly and C for PIC18, Pearson Education Inc., 2008. Han-Way Huang, PIC Microcontroller: An Introduction to Software and Hardware Interfacing, Thomson Delmar Learning, 2005. 32