Pic Microcontroller Unit Iii: Mr. S. Vinod Assistant Professor Eee Department
Pic Microcontroller Unit Iii: Mr. S. Vinod Assistant Professor Eee Department
Pic Microcontroller Unit Iii: Mr. S. Vinod Assistant Professor Eee Department
UNIT III
Mr. S. VINOD
ASSISTANT PROFESSOR
EEE DEPARTMENT
Architecture
memory organization
addressing modes
instruction set
PIC programming in Assembly &
C
I/O port
Data Conversion
RAM & ROM Allocation
Timer programming
PIC18F Microcontrollers
Microcontroller Unit (MCU)
Microprocessor unit (MPU)
Harvard Architecture
Program memory for instructions
Data memory for data
I/O ports
Support devices such as timers
Microcontroller Unit
Microprocessor Unit
Includes Arithmetic Logic Unit (ALU),
Registers, and Control Unit
Arithmetic Logic Unit (ALU)
Instruction decoder
16-bit instructions
Microprocessor Unit
Registers
Program Counter (PC)
21-bit register that holds the Program Memory address
Control unit
Provides timing and control signals
Read and Write operations
7
Control signals
Read and Write
PIC18F452/4520 Memory
Program Memory: 32 K
Address range: 000000 to 007FFFH
Data Memory: 4 K
Address range: 000 to FFFH
Data EEPROM
Not part of the data memory space
Addressed through special function
registers
10
PIC18F452/4520 Memory
Program Memory
Data Memory
11
12
13
14
Data Transfer
15
Serial Communications
Master Synchronous Serial Port (MSSP)
Addressable USART
A/D converter
Parallel Slave Port (PSP)
Data EEPROM
16
17
Sleep mode
Watchdog timer (WDT)
Code protection
In-circuit serial programming
In-circuit debugger
330_02
18
PIC18F452 Programming
Model
330_02
20
dual in-linepackage
Quad FlatPackage
P ro gra m
memory
Organizatio
n
Addr Nam
ess e
explanation
FFFh
TOSU
FFEh
TOSH
Top-of-Stack HIGHER(15-8)
FFDh
TOSL
Top-of-Stack LOWER(7-0)
FFCh
STKPTR
FFBh
PCLATU
FFAh
PCLATH
FF9h
PCL
FF8h
TBLPTRU
FF7h
TBLPTRH
FF6h
TBLPTRL
FF5h
TABLAT
FF4h
PRODH
FF3h
PRODL
Addr Nam
ess e
explanation
FF2
INTCON
INTurrptCONtrol register
FF1
INTCON2
FF0
INTCON3
FEF
INDF0
FEE
POSTINC
0
FED
POSTDEC
0
FEC
PREINC0
FEB
PLUSW0
FEA
FSR0H
FE9
FSR0L
FE8
WREG
Working Register
FE7
INDF1
FSR
o
Addr Nam
ess e
explanation
FE5
POSTDEC
1
FE4
PREINC1
FE3
PLUSW1
FE2
FSR1H
FE1
FSR1L
FE0
BSR
FDF
INDF2
FDE
POSTINC
2
FDD
POSTDEC
2
FDC
PREINC2
FDB
PLUSW2
Addr Nam
ess e
FD8
STATUS
explanation
STATUS register
PIC18 Pipelining
The PIC18 Divide most of the instruction
execution into two stages: instruction fetch
and instruction execution.
Up to two instructions are overlapped in their
execution One instruction is in Up to two
instructions are overlapped in their execution.
One instruction is infetch stage while the
second instruction is in execution stage.
Because of pipelining, each instruction
appears to take one instruction cycle to
complete.
PIC18 Pipelining
8-bit
0 x F2
MOVWF
F, a
;Copy WREG in File (Data) Reg.
; If a = 0, F is in Access Bank
;If a = 1, Bank is specified by BSR
0x25, 0 ;Copy W in Data Reg.25H
MOVWF
MOVFF fs, fd
;Copy from one Data Reg. to
;another Data Reg.
MOVFF 0x20,0x30
;Copy Data Reg. 20 into Reg.30
Arithmetic Instructions
ADDLW 8-bit
ADDLW 0x32
(1 of 3)
Arithmetic Instructions
(2 of 3)
ADDWFC F, d, a
;Add WREG to File Reg. with
;Carry and save result in W or F
SUBLW
8-bit
;Subtract WREG from literal
SUBWF
F, d, a
;Subtract WREG from File Reg.
SUBWFB F, d, a
;Subtract WREG from File Reg.
;with Borrow
INCF F, d, a
;Increment File Reg.
DECF F, d, a
;Decrement File Reg.
COMF F, d, a
;Complement File Reg.
NEGF F, a
;Take 2s Complement-File Reg.
Arithmetic Instructions
MULLW
(3 of 3)
Logic Instructions
ANDLW
ANDWF
8-bit
;AND literal with WREG
F, d, a
;AND WREG with File Reg. and
;save result in WREG/ File Reg.
IORLW 8-bit
;Inclusive OR literal with WREG
IORWF F, d, a
;Inclusive OR WREG with File Reg.
;and save result in WREG/File Reg.
XORLW
XORWF
8-bit
;Exclusive OR literal with WREG
F, d, a
;Exclusive OR WREG with File Reg.
;and save result in WREG/File Reg.
Branch Instructions
BC
n
;Branch if C flag = 1 within + or 64 Words
BNC n ;Branch if C flag = 0 within + or 64 Words
BZ
n
;Branch if Z flag = 1 within + or 64 Words
BNZ n ;Branch if Z flag = 0 within + or 64 Words
BN
n ;Branch if N flag = 1 within + or 64 Words
BNN n ;Branch if N flag = 0 within + or 64 Words
BOV n ;Branch if OV flag = 1 within + or 64 Words
BNOV n ;Branch if OV flag = 0 within + or 64 Words
GOTO Address: Branch to 20-bit address unconditionally
Increment/Decrement
and Skip Next Instruction
Table Read/Write
Instructions (1 of 2)
TBLRD* ;Read Program Memory pointed by TBLPTR
;into TABLAT
TBLRD*+ ;Read Program Memory pointed by TBLPTR
;into TABLAT and increment TBLPTR
TBLRD*- ;Read Program Memory pointed by TBLPTR
;into TABLAT and decrement TBLPTR
TBLRD+* ; Increment TBLPTR and Read Program
; Memory pointed by TBLPTR into TABLAT
Table Read/Write
Instructions (2 of 2)
TBLWT* ;Write TABLAT into Program Memory
pointed
;by TBLPTR
TBLWT*+ ; Write TABLAT into Program Memory
pointed
;by TBLPTR and increment TBLPTR
TBLWT*- ; Write TABLAT into Program Memory
pointed
;by TBLPTR and decrement TBLPTR
TBLWT+* ; Increment TBLPTR and Write TABLAT
into
; Program Memory pointed by TBLPTR
Instruction Format
(1 of 3)
Execution of an Instruction
Instruction: MOVLW 0x37 ; Load
37H in W
Memory
Address
Hex
Code
000020
000021
37
0E
Mnemonics
MOVLW 0x37
I/O PORTS
Depending on the device selected and
features enabled, there are up to five ports
available. Some pins of the I/O ports are
multiplexed with an alternate function from the
peripheral features on the device. In general,
when a peripheral is enabled, that pin may not
be used as a general purpose I/O pin.
Each port has three registers for its operation.
These registers are:
TRIS register (data direction register)
PORT register (reads the levels on the pins of the
device)
LAT register (output latch)
INITIALIZING PORTA
PORT B
PORT B
Switch to Bank 1
Set the Port A pins
to output.
Switch back to Bank 0
decfsz COUNT1,1 ;
Subtract 1 from 255
goto Loop1 ;
If COUNT is zero, carry on.
decfsz COUNT2,1 ;
Subtract 1 from 255
goto Loop1 ;
Go back to the start of our
loop. This delay counts
down from 255 to
zero,
255 times
;****Delay finished, now turn the LED off****
movlw 00h ;
Turn the LED off by first putting
movwf PORTA ;
it into the w register and then on
the port
;****Add another delay****
Loop2 decfsz COUNT1,1 ; This second loop keeps the
goto Loop2 ;
LED turned off long enough for
decfsz COUNT2,1 ;
us to see it turned off
goto Loop2 ;
;****Now go back to the start of the program
goto Start ;
go back to Start and turn LED on again
;****End of the program****
end
If two operands of different type are used in an arithmetic operation, the lower
priority operand type is automatically converted into the higher priority operand
type.
If the highest priority operand is of typedouble, then types of all other operands
in the expression as well as the result are automatically converted into
typedouble.
If the highest priority operand is of typelong, then types of all other operands in
the expression as well as the result are automatically converted into typelong.
If the operands are oflongorchartype, then types of all other operands in the
expression as well as the result are automatically converted into typeint.
MPLAB IDE
What is IDE?
Integrated
Development
Environment (IDE)
Collection of
integrated
programs (tools) to
write assembly
programs,
assemble, execute,
and debug
programs.
Microchip IDE is
called MPLAB IDE
Project_name.mcp
Project_name.mcw
Project_name.mcs
code_listing.asm
New project
Code listing
SYMBOL TABLE
LABEL
BYTE1
BYTE2
REG0
REG1
REG2
SAVE
START
__18F452
Messages :
VALUE
000000F2
00000032
00000000
00000001
00000002
0000002E
00000020
00000001
0 reported,
0 suppressed
0 suppressed
SYMBOL TABLE
LABEL
BYTE1
BYTE2
REG0
REG1
REG2
SAVE
START
__18F452
Messages :
Sequential
000000F2 Line numbers
00000032
VALUE
00000000
00000001
00000002
0000002E
00000020
00000001
0 reported,
0 suppressed
opcode
0 suppressed
PROG
STRUCTURE
PROG MEMORY
CODE
EEPROM
WATCH