SlideShare a Scribd company logo
Copyright Politeknik Kota Bharu Page 1
CHAPTER 2 – MICROCONTROLLER ARCHITECTURE & ASSEMBLY LANGUAGE PROGRAMMING
PIC18 Microcontroller families
 PIC is a family of modified Harvard architecture microcontrollers made by Microchip
Technology, derived from the PIC1650 originally developed by General Instrument's
Microelectronics Division. The name PIC initially referred to "Peripheral Interface
Controller".
 1989 – Microchip Technology Corporation introduced it’s first 8-bit microcontroller
PIC18 Architecture
 PIC microcontrollers are based on advanced RISC architecture.
 RISC stands for Reduced Instruction Set Computing. In this architecture, the instruction
set of hardware gets reduced which increases the execution rate (speed) of system.
 CPUs use many register to store data temporarily.
 To program in Assembly Language, we must understand the register and architecture
 PIC microcontrollers follow Harvard architecture for internal data transfer
 PIC microcontrollers are designed using the Harvard Architecture which includes:
 Microprocessor unit (MPU)
 Program memory for instructions
 Data memory for data
 I/O ports
 Support devices such as timers
Copyright Politeknik Kota Bharu Page 2
Copyright Politeknik Kota Bharu Page 3
The data RAM file Register
 CPUs use many registers to store data temporarily.
 To program in assembly language, we must understand the registers and architecture of
given CPU and the role they play in processing data.
WREG register
 Registers are used to store information temporarily
 The information could be a byte of data to be processed, or and address pointing to the
data to be fetched
 The WREG is the most widely used register in PIC microcontroller
Copyright Politeknik Kota Bharu Page 4
 The 8-bit WREG register is the most widely used register in the PIC micro controller.
 WREG stands for working register, as there is only one. The WREG register is the same
as the accumulator in other microprocessors.
 The WREG register is used for all arithmetic and logic instructions. To understand the
use of the WREG register, we will show it in the context of two simple instructions:
MOVLW and ADDWL.
MOVLW Instruction
Notice that in MOVLW, the letter L (literal) comes first and then the letter W (WREG),
which means "move a literal value to WREG, " the destination. The following
instruction loads the WREG register with a literal value of 25H (i.e., 25 in hex).
o MOVLW 25H ; move value 25H into WREG
o MOVLW 87H ; load 87H into WREG
ADDLW Instruction
The ADD instruction tells the CPU to add the literal value K to register WREG and put the
result back in the WREG register. Notice that in ADDLW, first comes the letter L (literal)
and then the letter W (WREG), which means "add a literal value to WREG," the
destination
MOVLW 25H ; load 25H into WREG
ADDLW 34H ; add value 34 to WREG
W = W + 34H Executing the above lines results in WREG = 59H (25H + 34H = 59H)
File register (SFRs and GPR)
 File Register (data RAM) is read/write memory used by CPU for data storage, scratch
pad and register for internal use and function
 Divided into two sections:
o SFRs – Special Function Registers
o GPR – General-Purpose Register
Copyright Politeknik Kota Bharu Page 5
Special Function Register (SFRs)
 Special-Function registers are RAM memory locations, their purpose is
predetermined during manufacturing process and cannot be changed.
 Dedicated to specific functions – ALU status, timers, serial communication, I/O ports
ADC etc.
 Fixed by CPU designer at the time of design
 8 bits register
Copyright Politeknik Kota Bharu Page 6
General Purpose Register (GPR)
 8-bit registers
 are a group of RAM locations in the file register that are used for data storage and
scratch pad.
 the space that is not allocated to the SFRs typically used for general-purpose registers
MOVWF Instruction
Notice that in MOVWF, the letter F stands for a location in the file register, while W
means WREG. The MOVWF instructions tells the CPU to move (in reality, copy) the
source register of WREG to a destination in the file register (F)
MOVLW 55H ; WREG = 55H
MOVWF PORTB ; Copy WREG to Port B (Port B = 55H)
MOVWF PORTC ; Copy WREG to Port C (Port C = 55H)
MOVWF PORTD ; Copy WREG to Port D (Port D = 55H)
Copyright Politeknik Kota Bharu Page 7
MOVF Instruction
The MOVF mnemonic is intended to perform MOVFW. It move the contents of file
register into WREG or to itself
MOVF PORTB,W ; move from file register of Port B to WREG
MOVF PORTB,0 ; move content of file register into WREG
MOVF PORTB,1 ; move content of file register into itself
Review Question
1. State five example of SFR
2. What is GPR
3. Write an instruction to move value of 58H into WREG
4. Write an instruction to move value of 58H into PORTB
5. What is the difference between the MOVWF and MOVF instruction
6. Explain each instruction below:
a. MOVLW 55H
b. MOVF PORTB,W
Status Register
 One of the most important register in PIC. Most of PIC has this register.
 8-Bit register. Sometime referred as flag register.
 Only 5 bits of it are used by the PIC18.
o called conditional flags
 The three unused bits are unimplemented and read as 0.
Copyright Politeknik Kota Bharu Page 8
 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 an arithmetic operatio.n
 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-if the results fall outside 127(0x7F)and -128(0x80).
 N(Negative Flag):set when bit B7 is one of the result of an arithmetic/logic operation.
Example
1. Show the status of the C, DC and Z flag after the addition of 38H and 2FH.
Solution:
38H 00111000
+ 2FH + 00101111
67H 01100111
C = 0 ; DC = 1; Z = 0
Copyright Politeknik Kota Bharu Page 9
2. Show the status of the C, DC and Z flags after the addition of 9CH and 64H in the
following instructions:
MOVLW 9CH
ADDLW 64H
3. Write the instruction for addition operation of 88H and 93H and show the status of the
C, DC and Z flags.
Solution:
MOVLW 88H
ADDLW 93H
Review Question
1. Determine the state of status register after the instruction below:
a. MOVLW 0F5H
ADDLW 0BH
b. MOVLW 9FH
ADDLW 61H
Copyright Politeknik Kota Bharu Page 10
2. Show the status of the C, DC and Z flags after the addition of 9CH and 64H in the
following instructions:
a. MOVLW 9CH
ADDLW 64H
b. MOVLW 67H
ADDLW 99H
c. MOVLW 87H
ADDLW 22H
Data format and directives
 PIC data type has only one data type. 8-bits. Size of register also 8-bits ( 00-FF, or 0-255)
 Data format representation.
o Hex,
o Binary,
o Decimal,
o ASCII
HEX number
 Use h, or H right after the number. Example MOVLW 12H
 Put 0x. Example MOVLW 0x12;
 Put nothing. Eg. MOVLW 12
 Put h in front of number. MOVLW h’12’
Binary number
 Only one way to represent binary numbers,
Example: MOVLW B’00010010’
1 2 in hex
Decimal number
 Two ways to represent. MOVLW D’12’ , (in hex 0x0C)
 Or MOVLW .12
Copyright Politeknik Kota Bharu Page 11
ASCII code
To represent ASCII data in PIC assemble we use the letter A as follows
MOVLW A’2’ ; WREG = 00110010 or 32 in hex ( see ASCII chart)
MOVLW A’c’ ; WREG = 01100011 or 63 in hex (see ASCII chart)
MOVLW ‘9’ ; WREG = 39H another way for ASCII
Copyright Politeknik Kota Bharu Page 12
ASSEMBLER Directive
 Directive give direction to he assembler..
 Example EQU, ORG, END, #INCLUDE, LIST, _CONFIG
EQU (equate)
 Used to define constant value or fixed address.
 Associates a constant number / address label
 Eg. COUNT EQU 0x25
MOVLW COUNT ; WREG = 25
ORG (Origin)
 Indicate the beginning of the address.
 Example ORG 0x00
END
 Important to indicate End of code, last line
LIST
 Indicates to the assembler the specific PIC chip for which the program should be
assembled. Eg. LIST P=18F458
#INLCUDE
 Tell the assembler to use the libraries associated with the specific chip
_CONFIG
 To configure bits for the targeted PIC. Read during the power-up, wrong configuration
makes PIC unusable.
Copyright Politeknik Kota Bharu Page 13
Example code
Instruction set of PIC18
 PIC18F2455/2550/4455/4550 devices incorporate the standard set of 75 PIC18 core
instructions.
 The instruction set is highly orthogonal and is grouped into four basic categories:
o Byte-oriented operations
o Bit-oriented operations
o Literal operations
o Control operations
Copyright Politeknik Kota Bharu Page 14
Copyright Politeknik Kota Bharu Page 15
Arithmetic Instruction and Operation
a. Addition and subtraction
MOVLW 0xF5 ; WREG = F5
ADDLW 0x0B ; WREG = F5 + 0B = 00 and C = 1
MOVLW 0x23 ; WREG = 23H
SUBLW 0x3F ; WREG = 3F - WREG
Copyright Politeknik Kota Bharu Page 16
b. Multiplication
The PIC supports byte-by-byte multiplication only. The byte is assumed to be unsigned
data. The syntax is as follows. After multiplication, the result is in the special function
registers PRODH and PRODL; the lower byte is in PRODL, and the upper byte is in
PRODH.
MULLW K ; W x K 16 bit is result in PRODH:PRODL
Copyright Politeknik Kota Bharu Page 17
c. Addition of BCD data
 BCD stands for binary coded decimal. BCD is needed because in everyday life we use
the digits 0 to 9 for number, not binary or hex number
 Binary representation of 0 to 9 is called BCD . In computer literature, one encounters
two terms for BCD numbers: (i) unpacked BCD, and (ii) packed BCD.
 In unpacked BCD, the lower 4 bits of the number represent the BCD number, and
the rest of the bits are O. Example: "0000 100 I" and "0000 0101" are unpacked BCD
for 9 and 5, respectively. Unpacked BCD requires I byte of memory, or an S-bit
register, to contain it.
 In packed BCD, a single byte has two BCD numbers in it: one in the lower 4 bits, and
one in the upper 4 bits. For example, "0101 1001" is packed BCD for 59H. Only I byte
of memory is needed to store the packed BCD operands. One reason to use packed
BCD is that it is twice as efficient in storing data.
 There is a problem with adding BCD numbers, which must be corrected. The
problem is that after adding packed BCD numbers, the result is no longerBCD. Look
at the following.
MOVLW 0x17
ADDLW 0x28
 Adding these two numbers gives 0011 IIII B (3FH), which is not BCD. A BCD number
can only have digits from 0000 to 1001 (or 0 to 9).
 The result above should have been 17 + 28 = 45 (01000101). To correct this problem,
the programmer must add 6 (0 II 0) to the low digit: 3F + 06 = 45H.
 The same problem could have happened in the upper digit (for example, in 52H +
87H = D9H). Again, 6 must be added to the upper digit (D9H + 60H = 139H) to ensure
that the result is BCD (52 + 87 = 139).
 This problem is so pervasive that most microprocessors such as the PIC 18 have an
instruction to deal with it. In the PIC 18 instruction "DAW" is designed to correct the
BCD addition problem.
 The DAW (decimal adjust WREG) instruction in the PIC 18 is provided to correct the
aforementioned problem associated with BCD addition.
 The mnemonic "DAW" works only with an operand in the WREG register. The DAW
instruction will add 6 to the lower nibble or higher nibble if needed; otherwise, it will
leave the result alone. The following example will clarify these points

Copyright Politeknik Kota Bharu Page 18
Logic Instruction and Bit Manipulation
 Apart from I/O and arithmetic instructions, logic instructions are some of most widely
used instructions.
 Boolean logic instructions such as AND, OR, Exclusive-OR (XOR), and complement.
 Logical operations are useful for looking for array elements with certain properties (e.g.,
divisible by power of 2) and manipulating I/O pin values (e.g., set certain pins to high,
clear a few pins, toggle a few signals, and so on).
 The logical instruction allow user to perform AND, OR, exclusive-OR and complementing
on 8-bit numbers.
AND
ANDLW K ; WREG = WREG AND K
Example : Show the result of the following
MOVLW 0x35h
ANDLW 0x0Fh
Copyright Politeknik Kota Bharu Page 19
OR
IORLW K ; WREG = WREG OR K
Example : Show the result of the following
MOVLW 0x04h
IORLW 0x30h
EX-OR
XORLW K ; WREG = WREG XOR K
Example : Show the result of the following
MOVLW 0x54h
XORLW 0x78h
Copyright Politeknik Kota Bharu Page 20
COMF (Compliment file register)
This instruction complements the contents of a file register. The complement action changes
the 0s to 1s and the 1s to 0s. This is also called 1’s complement
Bit Manipulation
 BCF – Bit Clear File register (set the bit: bit = 1)
 BSF – Bit Set File register (clear the bit: bit = 0)
 BTG – Bit Toggle File register (complement the bit)
 BTFSC – Bit Test File register, skip if clear (skip next instruction if bit = 0)
 BTFSS– Bit Test File register, skip if set (skip next instruction if bit = 1)
 RRNCF – Rotate right f (no carry)
 RLNCF – Rotate left f (no carry)
 RRCF – Rotate right f through carry
 RLCF – Rotate left f through carry
Example
Copyright Politeknik Kota Bharu Page 21
Review Question
1. Explain each of the register below:
a. WREG Register
b. File Register
c. Status Register
2. Include directive is used to ……………
3. EQU is used to ………………………………
4. Illustrate the result after the following code is executed

More Related Content

What's hot (20)

PPTX
State transition diagram 8085
ShivamSood22
 
PPTX
Programmable peripheral interface 8255
Marajulislam3
 
PPT
Cpu organisation
Er Sangita Vishwakarma
 
PPTX
3.programmable interrupt controller 8259
MdFazleRabbi18
 
PPTX
PROGRAMMABLE KEYBOARD AND DISPLAY INTERFACE(8279).pptx
SanjayV73
 
PPTX
Lecture 28 , 29 & 30(instruction set & addressing mode of 8086.pptx
VikasMahor3
 
PPTX
ARM Processors
Mathivanan Natarajan
 
PPS
Addressing modes of 8085
Syed Zaid Irshad
 
PPT
8255 presentaion.ppt
kamlesh deshmukh
 
PPTX
Analog to Digital converter in ARM
Aarav Soni
 
PPTX
Embedded c
Ami Prakash
 
PPT
Interfacing 8255
Anuja Bhakuni
 
PPT
Timing diagram 8085 microprocessor
Velalar College of Engineering and Technology
 
PPTX
4.programmable dma controller 8257
MdFazleRabbi18
 
PPT
Logical instruction of 8085
vishalgohel12195
 
PPT
Adc interfacing
Monica Gunjal
 
PDF
Chapter 6 - Introduction to 8085 Instructions
cmkandemir
 
PPT
Assembly language programming_fundamentals 8086
Shehrevar Davierwala
 
PPTX
5.programmable interval timer 8253
MdFazleRabbi18
 
PPTX
INTEL 80386 MICROPROCESSOR
Annies Minu
 
State transition diagram 8085
ShivamSood22
 
Programmable peripheral interface 8255
Marajulislam3
 
Cpu organisation
Er Sangita Vishwakarma
 
3.programmable interrupt controller 8259
MdFazleRabbi18
 
PROGRAMMABLE KEYBOARD AND DISPLAY INTERFACE(8279).pptx
SanjayV73
 
Lecture 28 , 29 & 30(instruction set & addressing mode of 8086.pptx
VikasMahor3
 
ARM Processors
Mathivanan Natarajan
 
Addressing modes of 8085
Syed Zaid Irshad
 
8255 presentaion.ppt
kamlesh deshmukh
 
Analog to Digital converter in ARM
Aarav Soni
 
Embedded c
Ami Prakash
 
Interfacing 8255
Anuja Bhakuni
 
Timing diagram 8085 microprocessor
Velalar College of Engineering and Technology
 
4.programmable dma controller 8257
MdFazleRabbi18
 
Logical instruction of 8085
vishalgohel12195
 
Adc interfacing
Monica Gunjal
 
Chapter 6 - Introduction to 8085 Instructions
cmkandemir
 
Assembly language programming_fundamentals 8086
Shehrevar Davierwala
 
5.programmable interval timer 8253
MdFazleRabbi18
 
INTEL 80386 MICROPROCESSOR
Annies Minu
 

Viewers also liked (19)

PDF
Lab 1 microcontroller
mkazree
 
DOC
3 organization of intel 8086
ELIMENG
 
PPT
Chp6 assembly language programming for pic copy
mkazree
 
PPT
Management of Learner Support Centres in Open and Distance Education
Ramesh C. Sharma
 
PPTX
Introduction of microcontroller
Engineer Maze
 
PPTX
Microprocessor 8086 instructions
Ravi Anand
 
PPT
Chp5 pic microcontroller instruction set copy
mkazree
 
PDF
PIC microcontroller review
Mohsen Sarakbi
 
PPT
Microcontroleur Pic16 F84
guest1e7b02
 
PDF
Embedded system (Chapter 1)
Ikhwan_Fakrudin
 
PDF
Pic microcontroller step by step your complete guide
Ashraf Said AlMadhoun - Educational Engineering Team
 
PPT
Chp4 introduction to the pic microcontroller copy
mkazree
 
PPTX
Architecture of 80286 microprocessor
Syed Ahmed Zaki
 
PDF
Programming with PIC microcontroller
Raghav Shetty
 
PPTX
PIC-MICROCONTROLLER TUTORIALS FOR BEGINNERS
VISHNU KP
 
PPT
Digital modulation
Muhd Iqwan Mustaffa
 
PPT
Digital electronics
Afghanistan civil aviation institute
 
PPTX
Latest Digital Electronic Projects
elprocus
 
PPTX
Embedded system (Chapter )
Ikhwan_Fakrudin
 
Lab 1 microcontroller
mkazree
 
3 organization of intel 8086
ELIMENG
 
Chp6 assembly language programming for pic copy
mkazree
 
Management of Learner Support Centres in Open and Distance Education
Ramesh C. Sharma
 
Introduction of microcontroller
Engineer Maze
 
Microprocessor 8086 instructions
Ravi Anand
 
Chp5 pic microcontroller instruction set copy
mkazree
 
PIC microcontroller review
Mohsen Sarakbi
 
Microcontroleur Pic16 F84
guest1e7b02
 
Embedded system (Chapter 1)
Ikhwan_Fakrudin
 
Pic microcontroller step by step your complete guide
Ashraf Said AlMadhoun - Educational Engineering Team
 
Chp4 introduction to the pic microcontroller copy
mkazree
 
Architecture of 80286 microprocessor
Syed Ahmed Zaki
 
Programming with PIC microcontroller
Raghav Shetty
 
PIC-MICROCONTROLLER TUTORIALS FOR BEGINNERS
VISHNU KP
 
Digital modulation
Muhd Iqwan Mustaffa
 
Latest Digital Electronic Projects
elprocus
 
Embedded system (Chapter )
Ikhwan_Fakrudin
 
Ad

Similar to Ch2 microcontroller architecture (20)

PDF
Embedded system (Chapter 2) part 2
Ikhwan_Fakrudin
 
PDF
Xcs 234 microprocessors
sweta suman
 
PPT
1. Instructionset.pptfor engineering student
ayushmishraaa09
 
PPT
My seminar new 28
rajeshkvdn
 
PPT
Picmico
loges91
 
PPT
Computer architecture 3
Dr.Umadevi V
 
DOC
Qb microprocessors
Neelam Kapoor
 
DOCX
Ecet 330 Enthusiastic Study / snaptutorial.com
Stephenson033
 
DOCX
Ecet 330 Success Begins / snaptutorial.com
WilliamsTaylorzm
 
DOCX
ECET 330 Massive Success--snaptutorial.com
santricksapiens71
 
DOCX
ECET 330 Technology levels--snaptutorial.com
sholingarjosh102
 
PDF
15CS44 MP & MC Module 1
RLJIT
 
PDF
Design of an Embedded Micro controller
Daniel Gomez-Prado
 
PPTX
07_ PIC16F778 Overview (families and standards ) .pptx
Belal90
 
PPTX
Instruction Set Architecture
Dilum Bandara
 
PPTX
PIC introduction + mapping
OsaMa Hasan
 
PDF
Unit 2 Instruction set.pdf
HimanshuPant41
 
PDF
Microcontroller pic 16f877 addressing modes instructions and programming
Nilesh Bhaskarrao Bahadure
 
PDF
Highridge ISA
Alec Selfridge
 
PPT
PICPICPICPICPICPICPICPICPICPICPICPIC .ppt
DrHamdyMMousa
 
Embedded system (Chapter 2) part 2
Ikhwan_Fakrudin
 
Xcs 234 microprocessors
sweta suman
 
1. Instructionset.pptfor engineering student
ayushmishraaa09
 
My seminar new 28
rajeshkvdn
 
Picmico
loges91
 
Computer architecture 3
Dr.Umadevi V
 
Qb microprocessors
Neelam Kapoor
 
Ecet 330 Enthusiastic Study / snaptutorial.com
Stephenson033
 
Ecet 330 Success Begins / snaptutorial.com
WilliamsTaylorzm
 
ECET 330 Massive Success--snaptutorial.com
santricksapiens71
 
ECET 330 Technology levels--snaptutorial.com
sholingarjosh102
 
15CS44 MP & MC Module 1
RLJIT
 
Design of an Embedded Micro controller
Daniel Gomez-Prado
 
07_ PIC16F778 Overview (families and standards ) .pptx
Belal90
 
Instruction Set Architecture
Dilum Bandara
 
PIC introduction + mapping
OsaMa Hasan
 
Unit 2 Instruction set.pdf
HimanshuPant41
 
Microcontroller pic 16f877 addressing modes instructions and programming
Nilesh Bhaskarrao Bahadure
 
Highridge ISA
Alec Selfridge
 
PICPICPICPICPICPICPICPICPICPICPICPIC .ppt
DrHamdyMMousa
 
Ad

Recently uploaded (20)

PPTX
Biosensors, BioDevices, Biomediccal.pptx
AsimovRiyaz
 
PPTX
template.pptxr4t5y67yrttttttttttttttttttttttttttttttttttt
SithamparanaathanPir
 
PDF
20ES1152 Programming for Problem Solving Lab Manual VRSEC.pdf
Ashutosh Satapathy
 
PDF
3rd International Conference on Machine Learning and IoT (MLIoT 2025)
ClaraZara1
 
PPTX
darshai cross section and river section analysis
muk7971
 
PDF
WD2(I)-RFQ-GW-1415_ Shifting and Filling of Sand in the Pond at the WD5 Area_...
ShahadathHossain23
 
PDF
PROGRAMMING REQUESTS/RESPONSES WITH GREATFREE IN THE CLOUD ENVIRONMENT
samueljackson3773
 
PPTX
Unit_I Functional Units, Instruction Sets.pptx
logaprakash9
 
PPTX
Diabetes diabetes diabetes diabetes jsnsmxndm
130SaniyaAbduNasir
 
PPTX
Engineering Quiz ShowEngineering Quiz Show
CalvinLabial
 
PPTX
OCS353 DATA SCIENCE FUNDAMENTALS- Unit 1 Introduction to Data Science
A R SIVANESH M.E., (Ph.D)
 
PPTX
Precooling and Refrigerated storage.pptx
ThongamSunita
 
PDF
13th International Conference of Security, Privacy and Trust Management (SPTM...
ijcisjournal
 
PPTX
Distribution reservoir and service storage pptx
dhanashree78
 
PDF
this idjfk sgfdhgdhgdbhgbgrbdrwhrgbbhtgdt
WaleedAziz7
 
PDF
NTPC PATRATU Summer internship report.pdf
hemant03701
 
PPTX
Seminar Description: YOLO v1 (You Only Look Once).pptx
abhijithpramod20002
 
PPTX
Fundamentals of Quantitative Design and Analysis.pptx
aliali240367
 
PPTX
Engineering Quiz ShowEngineering Quiz Show
CalvinLabial
 
PPTX
Introduction to File Transfer Protocol with commands in FTP
BeulahS2
 
Biosensors, BioDevices, Biomediccal.pptx
AsimovRiyaz
 
template.pptxr4t5y67yrttttttttttttttttttttttttttttttttttt
SithamparanaathanPir
 
20ES1152 Programming for Problem Solving Lab Manual VRSEC.pdf
Ashutosh Satapathy
 
3rd International Conference on Machine Learning and IoT (MLIoT 2025)
ClaraZara1
 
darshai cross section and river section analysis
muk7971
 
WD2(I)-RFQ-GW-1415_ Shifting and Filling of Sand in the Pond at the WD5 Area_...
ShahadathHossain23
 
PROGRAMMING REQUESTS/RESPONSES WITH GREATFREE IN THE CLOUD ENVIRONMENT
samueljackson3773
 
Unit_I Functional Units, Instruction Sets.pptx
logaprakash9
 
Diabetes diabetes diabetes diabetes jsnsmxndm
130SaniyaAbduNasir
 
Engineering Quiz ShowEngineering Quiz Show
CalvinLabial
 
OCS353 DATA SCIENCE FUNDAMENTALS- Unit 1 Introduction to Data Science
A R SIVANESH M.E., (Ph.D)
 
Precooling and Refrigerated storage.pptx
ThongamSunita
 
13th International Conference of Security, Privacy and Trust Management (SPTM...
ijcisjournal
 
Distribution reservoir and service storage pptx
dhanashree78
 
this idjfk sgfdhgdhgdbhgbgrbdrwhrgbbhtgdt
WaleedAziz7
 
NTPC PATRATU Summer internship report.pdf
hemant03701
 
Seminar Description: YOLO v1 (You Only Look Once).pptx
abhijithpramod20002
 
Fundamentals of Quantitative Design and Analysis.pptx
aliali240367
 
Engineering Quiz ShowEngineering Quiz Show
CalvinLabial
 
Introduction to File Transfer Protocol with commands in FTP
BeulahS2
 

Ch2 microcontroller architecture

  • 1. Copyright Politeknik Kota Bharu Page 1 CHAPTER 2 – MICROCONTROLLER ARCHITECTURE & ASSEMBLY LANGUAGE PROGRAMMING PIC18 Microcontroller families  PIC is a family of modified Harvard architecture microcontrollers made by Microchip Technology, derived from the PIC1650 originally developed by General Instrument's Microelectronics Division. The name PIC initially referred to "Peripheral Interface Controller".  1989 – Microchip Technology Corporation introduced it’s first 8-bit microcontroller PIC18 Architecture  PIC microcontrollers are based on advanced RISC architecture.  RISC stands for Reduced Instruction Set Computing. In this architecture, the instruction set of hardware gets reduced which increases the execution rate (speed) of system.  CPUs use many register to store data temporarily.  To program in Assembly Language, we must understand the register and architecture  PIC microcontrollers follow Harvard architecture for internal data transfer  PIC microcontrollers are designed using the Harvard Architecture which includes:  Microprocessor unit (MPU)  Program memory for instructions  Data memory for data  I/O ports  Support devices such as timers
  • 3. Copyright Politeknik Kota Bharu Page 3 The data RAM file Register  CPUs use many registers to store data temporarily.  To program in assembly language, we must understand the registers and architecture of given CPU and the role they play in processing data. WREG register  Registers are used to store information temporarily  The information could be a byte of data to be processed, or and address pointing to the data to be fetched  The WREG is the most widely used register in PIC microcontroller
  • 4. Copyright Politeknik Kota Bharu Page 4  The 8-bit WREG register is the most widely used register in the PIC micro controller.  WREG stands for working register, as there is only one. The WREG register is the same as the accumulator in other microprocessors.  The WREG register is used for all arithmetic and logic instructions. To understand the use of the WREG register, we will show it in the context of two simple instructions: MOVLW and ADDWL. MOVLW Instruction Notice that in MOVLW, the letter L (literal) comes first and then the letter W (WREG), which means "move a literal value to WREG, " the destination. The following instruction loads the WREG register with a literal value of 25H (i.e., 25 in hex). o MOVLW 25H ; move value 25H into WREG o MOVLW 87H ; load 87H into WREG ADDLW Instruction The ADD instruction tells the CPU to add the literal value K to register WREG and put the result back in the WREG register. Notice that in ADDLW, first comes the letter L (literal) and then the letter W (WREG), which means "add a literal value to WREG," the destination MOVLW 25H ; load 25H into WREG ADDLW 34H ; add value 34 to WREG W = W + 34H Executing the above lines results in WREG = 59H (25H + 34H = 59H) File register (SFRs and GPR)  File Register (data RAM) is read/write memory used by CPU for data storage, scratch pad and register for internal use and function  Divided into two sections: o SFRs – Special Function Registers o GPR – General-Purpose Register
  • 5. Copyright Politeknik Kota Bharu Page 5 Special Function Register (SFRs)  Special-Function registers are RAM memory locations, their purpose is predetermined during manufacturing process and cannot be changed.  Dedicated to specific functions – ALU status, timers, serial communication, I/O ports ADC etc.  Fixed by CPU designer at the time of design  8 bits register
  • 6. Copyright Politeknik Kota Bharu Page 6 General Purpose Register (GPR)  8-bit registers  are a group of RAM locations in the file register that are used for data storage and scratch pad.  the space that is not allocated to the SFRs typically used for general-purpose registers MOVWF Instruction Notice that in MOVWF, the letter F stands for a location in the file register, while W means WREG. The MOVWF instructions tells the CPU to move (in reality, copy) the source register of WREG to a destination in the file register (F) MOVLW 55H ; WREG = 55H MOVWF PORTB ; Copy WREG to Port B (Port B = 55H) MOVWF PORTC ; Copy WREG to Port C (Port C = 55H) MOVWF PORTD ; Copy WREG to Port D (Port D = 55H)
  • 7. Copyright Politeknik Kota Bharu Page 7 MOVF Instruction The MOVF mnemonic is intended to perform MOVFW. It move the contents of file register into WREG or to itself MOVF PORTB,W ; move from file register of Port B to WREG MOVF PORTB,0 ; move content of file register into WREG MOVF PORTB,1 ; move content of file register into itself Review Question 1. State five example of SFR 2. What is GPR 3. Write an instruction to move value of 58H into WREG 4. Write an instruction to move value of 58H into PORTB 5. What is the difference between the MOVWF and MOVF instruction 6. Explain each instruction below: a. MOVLW 55H b. MOVF PORTB,W Status Register  One of the most important register in PIC. Most of PIC has this register.  8-Bit register. Sometime referred as flag register.  Only 5 bits of it are used by the PIC18. o called conditional flags  The three unused bits are unimplemented and read as 0.
  • 8. Copyright Politeknik Kota Bharu Page 8  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 an arithmetic operatio.n  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-if the results fall outside 127(0x7F)and -128(0x80).  N(Negative Flag):set when bit B7 is one of the result of an arithmetic/logic operation. Example 1. Show the status of the C, DC and Z flag after the addition of 38H and 2FH. Solution: 38H 00111000 + 2FH + 00101111 67H 01100111 C = 0 ; DC = 1; Z = 0
  • 9. Copyright Politeknik Kota Bharu Page 9 2. Show the status of the C, DC and Z flags after the addition of 9CH and 64H in the following instructions: MOVLW 9CH ADDLW 64H 3. Write the instruction for addition operation of 88H and 93H and show the status of the C, DC and Z flags. Solution: MOVLW 88H ADDLW 93H Review Question 1. Determine the state of status register after the instruction below: a. MOVLW 0F5H ADDLW 0BH b. MOVLW 9FH ADDLW 61H
  • 10. Copyright Politeknik Kota Bharu Page 10 2. Show the status of the C, DC and Z flags after the addition of 9CH and 64H in the following instructions: a. MOVLW 9CH ADDLW 64H b. MOVLW 67H ADDLW 99H c. MOVLW 87H ADDLW 22H Data format and directives  PIC data type has only one data type. 8-bits. Size of register also 8-bits ( 00-FF, or 0-255)  Data format representation. o Hex, o Binary, o Decimal, o ASCII HEX number  Use h, or H right after the number. Example MOVLW 12H  Put 0x. Example MOVLW 0x12;  Put nothing. Eg. MOVLW 12  Put h in front of number. MOVLW h’12’ Binary number  Only one way to represent binary numbers, Example: MOVLW B’00010010’ 1 2 in hex Decimal number  Two ways to represent. MOVLW D’12’ , (in hex 0x0C)  Or MOVLW .12
  • 11. Copyright Politeknik Kota Bharu Page 11 ASCII code To represent ASCII data in PIC assemble we use the letter A as follows MOVLW A’2’ ; WREG = 00110010 or 32 in hex ( see ASCII chart) MOVLW A’c’ ; WREG = 01100011 or 63 in hex (see ASCII chart) MOVLW ‘9’ ; WREG = 39H another way for ASCII
  • 12. Copyright Politeknik Kota Bharu Page 12 ASSEMBLER Directive  Directive give direction to he assembler..  Example EQU, ORG, END, #INCLUDE, LIST, _CONFIG EQU (equate)  Used to define constant value or fixed address.  Associates a constant number / address label  Eg. COUNT EQU 0x25 MOVLW COUNT ; WREG = 25 ORG (Origin)  Indicate the beginning of the address.  Example ORG 0x00 END  Important to indicate End of code, last line LIST  Indicates to the assembler the specific PIC chip for which the program should be assembled. Eg. LIST P=18F458 #INLCUDE  Tell the assembler to use the libraries associated with the specific chip _CONFIG  To configure bits for the targeted PIC. Read during the power-up, wrong configuration makes PIC unusable.
  • 13. Copyright Politeknik Kota Bharu Page 13 Example code Instruction set of PIC18  PIC18F2455/2550/4455/4550 devices incorporate the standard set of 75 PIC18 core instructions.  The instruction set is highly orthogonal and is grouped into four basic categories: o Byte-oriented operations o Bit-oriented operations o Literal operations o Control operations
  • 14. Copyright Politeknik Kota Bharu Page 14
  • 15. Copyright Politeknik Kota Bharu Page 15 Arithmetic Instruction and Operation a. Addition and subtraction MOVLW 0xF5 ; WREG = F5 ADDLW 0x0B ; WREG = F5 + 0B = 00 and C = 1 MOVLW 0x23 ; WREG = 23H SUBLW 0x3F ; WREG = 3F - WREG
  • 16. Copyright Politeknik Kota Bharu Page 16 b. Multiplication The PIC supports byte-by-byte multiplication only. The byte is assumed to be unsigned data. The syntax is as follows. After multiplication, the result is in the special function registers PRODH and PRODL; the lower byte is in PRODL, and the upper byte is in PRODH. MULLW K ; W x K 16 bit is result in PRODH:PRODL
  • 17. Copyright Politeknik Kota Bharu Page 17 c. Addition of BCD data  BCD stands for binary coded decimal. BCD is needed because in everyday life we use the digits 0 to 9 for number, not binary or hex number  Binary representation of 0 to 9 is called BCD . In computer literature, one encounters two terms for BCD numbers: (i) unpacked BCD, and (ii) packed BCD.  In unpacked BCD, the lower 4 bits of the number represent the BCD number, and the rest of the bits are O. Example: "0000 100 I" and "0000 0101" are unpacked BCD for 9 and 5, respectively. Unpacked BCD requires I byte of memory, or an S-bit register, to contain it.  In packed BCD, a single byte has two BCD numbers in it: one in the lower 4 bits, and one in the upper 4 bits. For example, "0101 1001" is packed BCD for 59H. Only I byte of memory is needed to store the packed BCD operands. One reason to use packed BCD is that it is twice as efficient in storing data.  There is a problem with adding BCD numbers, which must be corrected. The problem is that after adding packed BCD numbers, the result is no longerBCD. Look at the following. MOVLW 0x17 ADDLW 0x28  Adding these two numbers gives 0011 IIII B (3FH), which is not BCD. A BCD number can only have digits from 0000 to 1001 (or 0 to 9).  The result above should have been 17 + 28 = 45 (01000101). To correct this problem, the programmer must add 6 (0 II 0) to the low digit: 3F + 06 = 45H.  The same problem could have happened in the upper digit (for example, in 52H + 87H = D9H). Again, 6 must be added to the upper digit (D9H + 60H = 139H) to ensure that the result is BCD (52 + 87 = 139).  This problem is so pervasive that most microprocessors such as the PIC 18 have an instruction to deal with it. In the PIC 18 instruction "DAW" is designed to correct the BCD addition problem.  The DAW (decimal adjust WREG) instruction in the PIC 18 is provided to correct the aforementioned problem associated with BCD addition.  The mnemonic "DAW" works only with an operand in the WREG register. The DAW instruction will add 6 to the lower nibble or higher nibble if needed; otherwise, it will leave the result alone. The following example will clarify these points 
  • 18. Copyright Politeknik Kota Bharu Page 18 Logic Instruction and Bit Manipulation  Apart from I/O and arithmetic instructions, logic instructions are some of most widely used instructions.  Boolean logic instructions such as AND, OR, Exclusive-OR (XOR), and complement.  Logical operations are useful for looking for array elements with certain properties (e.g., divisible by power of 2) and manipulating I/O pin values (e.g., set certain pins to high, clear a few pins, toggle a few signals, and so on).  The logical instruction allow user to perform AND, OR, exclusive-OR and complementing on 8-bit numbers. AND ANDLW K ; WREG = WREG AND K Example : Show the result of the following MOVLW 0x35h ANDLW 0x0Fh
  • 19. Copyright Politeknik Kota Bharu Page 19 OR IORLW K ; WREG = WREG OR K Example : Show the result of the following MOVLW 0x04h IORLW 0x30h EX-OR XORLW K ; WREG = WREG XOR K Example : Show the result of the following MOVLW 0x54h XORLW 0x78h
  • 20. Copyright Politeknik Kota Bharu Page 20 COMF (Compliment file register) This instruction complements the contents of a file register. The complement action changes the 0s to 1s and the 1s to 0s. This is also called 1’s complement Bit Manipulation  BCF – Bit Clear File register (set the bit: bit = 1)  BSF – Bit Set File register (clear the bit: bit = 0)  BTG – Bit Toggle File register (complement the bit)  BTFSC – Bit Test File register, skip if clear (skip next instruction if bit = 0)  BTFSS– Bit Test File register, skip if set (skip next instruction if bit = 1)  RRNCF – Rotate right f (no carry)  RLNCF – Rotate left f (no carry)  RRCF – Rotate right f through carry  RLCF – Rotate left f through carry Example
  • 21. Copyright Politeknik Kota Bharu Page 21 Review Question 1. Explain each of the register below: a. WREG Register b. File Register c. Status Register 2. Include directive is used to …………… 3. EQU is used to ……………………………… 4. Illustrate the result after the following code is executed