Professional Documents
Culture Documents
Chapter 3
Chapter 3
Chapter 3
Embedded Programs
Destaw M
1
Outlines
Introduction
PIC18F4520 instruction set
Fundamentals of assembly language and linking
Interrupt
2
Introduction
Instruction Set
Group of instruction computer can recognize
and execute
are represented in binary (machine code)
A program
is a sequence of instructions drawn from the
instruction set and combined to perform specific
operation
3
Introduction cont…
How to program
Machine code
Uses the binary equivalent of the instructions
Slow, tedious, and error‐prone
Example
00 0111 0001 0101
4
Introduction cont…
How to program
Assembly language
Each instruction is given a mnemonic
A program called Assembler converts to
machine code
slow and inefficient for large and complex
programs
Examples
ADDFW NUM, w
5
Introduction cont…
How to program
High‐level language
Use English‐like commands to program
Compiler converts to machine code
Easy !!
The program could be inefficient !
Example
for (i=0; i<10; i++) sum += a[i];
6
PIC18F4520 Instruction Set
Standard instruction set => 75
Extended instructions => 8
for optimization
Standard Instruction set
Use single program memory word => 16
bits
the16-bit word is divided into
an opcode, which specifies the instruction type
one or more operands, which further specify
7 the operation of the instruction.
PIC18F4520 Instruction Set cont…
Grouped as
1. Byte-oriented operations
2. Bit-oriented operations
3. Literal operations
4. Control operation
8
PIC18F4520 Instruction Set cont…
1. Byte oriented instructions
have three operands
1. The file register (specified by ‘f’)
specifies which file register is to be used by the instruction.
2. The destination of the result (specified by ‘d’)
specifies where the result of the operation is to be placed
If ‘d’ is zero, result on the WREG register.
If ‘d’ is one, result in the file register
3. The accessed memory (specified by ‘a’)
RAM access bit
a = 0: Access RAM (BSR register is ignored)
a = 1: BSR register
9
PIC18F4520 Instruction Set cont…
Example
ADDWF f, d, a
10
PIC18F4520 Instruction Set cont…
11
PIC18F4520 Instruction Set cont…
2. bit-oriented instructions
have three operands:
1. The file register (specified by ‘f’)
2. The bit in the file register (specified by
‘b’)
selects the number of the bit affected by the
operation
3. The accessed memory (specified by ‘a’)
12
PIC18F4520 Instruction Set cont…
Example
BSF f, bit, a
13
PIC18F4520 Instruction Set cont…
14
PIC18F4520 Instruction Set cont…
3. The literal instructions
Operands can be
A literal value to be loaded into a file register
specified by ‘k’
desired FSR register to load the literal value
specified by ‘f’
No operand required
specified by ‘—’)
Example MOVLW 7Fh
15
PIC18F4520 Instruction Set cont…
16
PIC18F4520 Instruction Set cont…
4.The control instructions
Operands can be:
A program memory address
specified by ‘n
mode of the CALL or RETURN instructions
specified by ‘s’
mode of the table read and table write instructions
specified by ‘m’
No operand required
specified by ‘—’
17
PIC18F4520 Instruction Set cont…
18
PIC18F4520 Instruction Set cont…
19
PIC18F4520 Instruction Set cont…
Reading assignment
PIC18F4520 Datasheet , Instruction set summary, pages
267 - 315
20
MPLAB Definition
is a command-line or GUI application
provides a platform for developing assembly language
code for Microchip's PIC1X
Download for free from Microchip
21
MPLAB Assembler
The MPASM assembler can be used in two ways:
generate absolute code that can be executed directly by
a microcontroller.
generate relocatable code that can be linked with other
separately assembled or compiled modules.
22
Generating Absolute code
all variables and routines must be defined within that
source file
a hex file will be generated, containing the executable
machine code
the default output from the MPASM assembler.
23
Generating relocatable code
Combine two or more code to create one final
executable code
very useful for creating reusable modules
24
Cont.…..
Compilers
translate programs written in some human-readable
language into an equivalent set of opcodes for a
particular processor
In that sense, an assembler is a light weight compilers
Linkers
The linker will join all object files resulted from the
compiler, assemblers and library files and produce a
single re-locatable files.
25
Source code
Each line of the source file may contain up to four
types of information:
1. Labels
2. Mnemonics, Directives and Macros
3. Operands
4. Comments
26
Project creation in MPLAB IDE
Open MPLAB
27
Project-> Project wizard
28
Click next and select the device family
29
Click next and select MPASM Assembler
30
Then, browse the location of your project in appropriate place
31
Then you get the following and click next
32
33
34
Then click build
35
Create Assembly code
Put the file under sample_project-> source Files
Write a program which will get constant from working
register and display the number on PORTB . Assume
your going to use PIC18F4520
36
37
PORTB initialization
More on Datasheet for PIC18F4520, Page 108 - 110
38
39
Sample code
40
Cont’d
41
LED flashing
42
For more on assembly programming
PIC programming for Beginners, Mark Spencer
43
Interrupt
What is it?
Consider student working in a lab and also expected to
answer phone call.
Normal / regular work -> the lab work
Interrupt -> phone call
Problem -> it interrupt from regular work
Overhead -> time it take to respond to the call
Short overhead or
Large overhead
o Call can be missed
Overall performance can be reduced
o Incase of multiple interrupts
44
Define Interrupt
An interrupt is an event that will stop processor from
current processing and switch to a different instruction
Event can be generated
Internally or
Externally
may or may not return to its original processing
Can be multiple
Source can be
Hardware or
software
45
Interrupt structure
46
Advantages of interrupt
modularization
background work where tasks are performed
while waiting for an interrupt and
foreground work where tasks are performed in
response to interrupts
Execution time
Advantageous for complex application
47
Disadvantage
Coding complexity for interrupt service
routines
Extra hardware needed
I/O device must generate an interrupt
request
48
Interrupt sources
Internal interrupts
generated by on-chip peripherals
serial and parallel ports
allow the priority level to be adjusted
Can be masked out or disabled altogether
External interrupts
Generated from external peripherals to the
processor
Through external pins that are connected to
peripherals
49
Types of interrupt
Two types :
1. Maskable interrupts
can be enabled/disabled by
setting/clearing some bits
2. Non‐maskable interrupts
can not be disabled
they always interrupt the processor
Higher priority
Example reset, power off
50
Interrupt detection
Edge triggered
clock edge is used to generate the interrupt
from a logical high to low or vice versa
Level triggered
dependent on the logic level
sampled on a regular basis then compare with
logic level
51
Steps in executing an interrupt
Upon activation of interrupt the microcontroller
Finishes executing the current instruction
Pushes the PC of next instruction in the stack
Jumps to the interrupt vector table to get the
address of ISR and jumps to it
Begin executing the ISR instructions to the last
instruction of ISR (RETFIE)
Executes RETFIE
Pops the PC from the stack
Starts to execute from the address of that PC
52
PIC18F4520 interrupts
have multiple interrupt sources
TIMER 0, 1, 2 , external source, counter , ……..
an interrupt priority feature
Low priority
High priority
Ten register to control it
RCON
INTCON
INTCON2
INTCON3
PIR1, PIR2
PIE1, PIE2
IPR1, IPR2
53
PIC18F4520 interrupts
Interrupt vector table
holds the address of ISRs
Power-on Reset 0000h
High priority interrupt
0008h
Low priority interrupt
0018h
54
PIC18F4520 interrupts
three bits to control their operation
Flag bit
to indicate that an interrupt event occurred
Enable bit
that allows program execution to branch to the
interrupt vector address when the flag bit is set
Priority bit
to select high priority or low priority
55
PIC18F4520 interrupt
Priority enable by IPEN bit (RCON<7>).
IPEN = 0 BY default
IPEN = 1 when to use priority
Global interrupt enable
GIEH bit (INTCON<7>)
enables all interrupts that have high priority
GIEL bit (INTCON<6>)
enables all interrupts that have low priority
56
57
RCON register
contains flag bits which are used to determine the
cause of the last Reset or wake-up from Idle or Sleep
modes.
contains the IPEN bit which enables interrupt priorities
58
59
INTCON Registers
contain various enable, priority and flag bits.
60
INTCON Registers
61
62
63
PIR (peripheral Interrupt Request) registers
Contain the individual flag bits for the peripheral
interrupts
There are two Peripheral Interrupt Request Flag
registers
PIR1 and PIR2
64
65
66
PIE(Peripheral Interrupt Enable ) registers
Contain the individual enable bits for the peripheral
interrupts.
There are two Peripheral Interrupt Enable registers
PIE1 and PIE2.
When IPEN = 0, the PEIE bit must be set to enable
any of these peripheral interrupts.
67
68
69
IPR(Peripheral Interrupt Priority ) registers
contain the individual priority bits for the peripheral
interrupts.
there are two Peripheral Interrupt Priority registers
IPR1 and IPR2.
70
71
72
Steps in enabling an interrupt
Set the GIE bit from INTCON REG
Set the IE bit for that interrupt
If the interrupt is one of the peripheral (timers 1,2 ,
serial, etc ) set PEIE bit from INTCON reg
73
Program organization in MPLAB X
74
75
What is difference between interrupt and function call?
Difference between RETFIE and RETURN?
76
Quiz (5 marks max)
Write an assembly code which will add two constants ,
let say 0xAA and 0x07 and send output through
PORTA. Assume you are going to program it on
PIC18F4520. Use datasheet for PORTA
Configurations.
77
78