SlideShare ist ein Scribd-Unternehmen logo
1 von 22
Chapter 5
Building Assembler Programs
The aims of this chapter are to introduce:
• how to visualise a program, and represent it diagrammatically;
• how to use program branching and subroutines;
• how to implement delays;
• how to use look-up tables;
• logical instructions;
• how to simplify and optimise Assembler programming;
• more advanced features of software simulators.
Designing Embedded Systems
with PIC Microcontrollers:
Principles and Applications
2nd Edition. Tim Wilmshurst
Instructors using Designing Embedded Systems with PIC Microcontrollers are welcome to use or change
these slides as they see fit. Feedback, to t.j.wilmshurst@derby.ac.uk, is welcomed.
The copyright to all diagrams is held by Microchip Technology, or T. Wilmshurst, unless otherwise stated
1
Read Actual
Temperature TA
Read Demand
Temperature TD
TA > TD ?
Activate
Compressor
Switch off
Compressor
TA >> TD ?
Activate
Alarm
Yes
Yes
No
No
Initial Program Design
- Flow Diagrams -
Read Actual
Temperature TA
Read Demand
Temperature TD
TA > TD ?
Activate
Compressor
Switch off
Compressor
TA >> TD ?
Activate
Alarm
Yes
Yes
No
No
2
Initial Program Design – Flow Diagrams
It is easy in Assembler to produce “spaghetti code”, ie code
which has no structure, with branches going anywhere, and
which is incomprehensible to any but the programmer, and
incomprehensible even to him/her after a week.
Therefore it is essential to plan a programme structure. While it
is now viewed as quite an old technique, flow diagramming is
still an appropriate tool to use.
Flow diagrams emphasise the linear nature of a programme, and
they can be drawn so that they can be readily translated into a
programme, BUT they do not in themselves exert good
programming discipline.
3
An Alternative Programme Visualisation - State Diagrams
This is an alternative to the flow diagram.
In a State Diagram we recognise that the embedded system
proceeds through a number of distinct states, and may stay in
each state for a considerable period of time. Transition from one
state to the next is determined by certain conditions satisfied.
The state diagram is useful for visualising certain types of
programme, but not all. It doesn’t translate directly into code.
A simple example of a state machine programme is a washing
machine controller, which will move from pre-wash, to wash to
rinse states.
4
Fill
water
Heat
water
Wash
Rinse
Spin Fault
Ready
User initiates
Full level
detected
Timeout
Required
temperature
reached
Out of
balance
Out of
balance
Motor
failure
Motor
failure
Function
complete
Function
complete
Function
complete
Fault
cleared
Start!
Door closed
Timeout
Example of a
State Diagram:
the Washing
Machine
5
Conditional Branching SubtractAfromB
Result=0?
Add C to D
No
Yes
Features of PIC program:
- The ability to make “decisions”, i.e.
to act differently according to the
state of logical variables.
- have a number of instructions allow
them to test a particular bit, and either
continue program execution if the
condition is not met, or branch to
another part of the program if it is.
- Often these variables are bit values in
the Status Registers.
6
Conditional Branching SubtractAfromB
Result=0?
Add C to D
No
Yes
The PIC 16 Series microcontrollers have
four conditional “skip” instructions.
These test for a certain condition, and
skip just one instruction if the condition
is met, and continue normal program
execution if it is not.
The most versatile and general-purpose
of these are:
btfsc f, b; Bit Test f, Skip if Clear
btfss f, b; Bit Test f, Skip if Set
7
;The “main” program starts here
movlw 00h
movwf porta ;clear all bits in port A
movwf portb ;clear all bits in port B
loop
bcf portb, 3 ;preclear port B, bit 3
btfss porta, 3 ;Bit Test f, Skip if set
bsf portb, 3 ;but set it if button pressed
bcf portb, 4 ;preclear port B, bit 4
btfss porta, 4 ;Bit Test f, Skip if set
bsf portb, 4 ;but set it if button pressed
goto loop
end
Testing and Manipulating Single Bits
8
Do this
Do that
Do som ething else
Call SR1
Call SR2
Call SR1
Main Program
SR1
Return
SR2
Subroutine 1 Subroutine 2
Return
.......
.......
.......
.......
.......
.......
.......
.......
.......
.......
.......
.......
.......
.......
Do that
Subroutines and the Stack
The subroutine is a program section structured in such a way
that it can be called from anywhere in the program. Once it has
been executed the program continues to execute from wherever
it was before.
9
Do this
Do that
Do som ething else
Call SR1
Call SR2
Call SR1
Main Program
SR1
Return
SR2
Subroutine 1 Subroutine 2
Return
.......
.......
.......
.......
.......
.......
.......
.......
.......
.......
.......
.......
.......
.......
Do that
Subroutines and the Stack
The PIC 16 Series subroutine call and return instructions are
call and return. A special return instruction, retlw, is also
available. Subroutine Call and Return instructions must always
work in pairs.
10
;Delay of 5ms approx. Instruction cycle time is 5us.
delay5 movlw D'200‘ ;200 cycles called, 5x5=25us each
;200*25us = 5000us (5ms)
movwf delcntr1
del1 nop ;1 inst. cycle
nop ;1 inst. cycle
decfsz delcntr1,1 ;1 inst. cycle, Decrement f, Skip if 0
goto del1 ;2 inst. cycles
return
A Delay Subroutine
;500ms delay (approx) ;100 calls to delay5
Delay500 movlw D'100'
movwf delcntr2
del2 call delay5 ;Delay of 5ms
decfsz delcntr2,1 ;1 cycle, Dcrmnt f, Skip if 0
goto del2 ;2 inst. cycles
return
Nested Subroutines for Greater Delay
11
More Use of the MPLAB Simulator: Breakpoints
Once programs become long, it becomes increasingly tedious to step through them when
simulating. We need a means of getting them to run through the code that we may not be
interested in, but stopping where we need to take a closer look at what is happening. Breakpoints
let this happen.
In their simplest form, breakpoints allow you to run a program up to a specified instruction.
Program execution then stops, and memory and register values can be inspected.
12
More Use of the MPLAB Simulator: Stopwatch
A weakness of the software simulator is that it does not run in real time, yet in embedded
systems we have a strong desire to understand the timing behaviour of our programs. The
Stopwatch facility of the simulator allows accurate time measurements to be simulated. It
simply requires that the simulator “knows” what the oscillator frequency is. As it can
record the number of instruction cycles executed, it can then calculate time taken.
13
Try
Programming
Exercise 5.8
More Use of the MPLAB Simulator: Trace
The various windows available in MPSIM give a good picture of the state of the processor
status and memory locations at any time, but they do not tell us the history of program
execution. Even if program execution has halted at a breakpoint, there may have been a
number of program paths for it to go down to reach that point. The Trace function is there
to give a record of the recent past of the program execution.
14
15
Flowcode Software
Introducing Logical Instructions
We have seen a good selection of the 16 series instructions, but
have yet to see any logical ones. These instructions, like andwf,
andlw, iorwf, or xorwf, perform logical operations between the
contents of the W register, and either a literal value, or a value
held in a memory location. They do it on a bitwise basis.
;Changes led pattern, using OR and rrf (Rotate Right f through Carry)
;pattern_IR btfsc flags, 0
goto pat_IR1 ;here if first visit to SR
bsf flags,0
clrf portb ;set up initial output pattern
return
pat_IR1 rrf portb,0 ;here if 2nd or later visit to SR
iorlw B'10000000‘ ;(OR with W)add 1 bit to pattern
btfsc status,c ;has pattern reached carry flag?
clrw ;if yes, clear W
movwf portb
return 16
The instruction movlw allows us to introduce within the program a byte
of constant data. We have already seen this in previous programs, for
example with the instruction combination:
movlw D'100'
movwf delcntr2
This is fine for introducing single bytes of data into a program, or just a
few. But suppose we want to place in the program a whole list of
numbers. Suppose also that we want to be able to record where we are in
the list, with some sort of marker. The movlw instruction is then not
really up to the job, and we need to apply a way of setting up and
accessing a block of data. This is called a look-up table.
The PIC 16 Series approach to look-up tables is shown in the next slide.
The table is formed as a subroutine. Every byte of data in the table is
accompanied by a special instruction, retlw. This instruction is another
“return from subroutine”, but with a difference - it requires an 8-bit literal
operand. As it implements the subroutine return, it picks up its operand
and puts it into the W register. The table is essentially a list of retlw
instructions, each with its byte of data.
Look-up Tables
17
Ia
m
th
eW
re
g
is
te
r.
Ia
m
c
a
rry
in
gac
o
d
e
fro
m
th
em
a
in
p
ro
g
ra
m
,w
h
ic
hw
ill
s
h
o
ww
h
ic
hn
u
m
b
e
rI
m
u
s
tc
o
m
eb
a
c
kw
ith
N
o
wI’m
b
rin
g
in
g
b
a
c
kth
ec
h
o
s
e
n
n
u
m
b
e
rfro
m
th
e
s
u
b
ro
u
tin
e
.
ta
b
le a
d
d
w
fp
c
l
re
tlw 2
3
re
tlw3
f
re
tlw4
7
re
tlw7
f
re
tlw0
a
2
re
tlw1
f
re
tlw0
3
re
tlw6
7
re
tlw0
c
5
re
tlw3
2
....
....
....
m
o
v
fs
a
m
p
le
_
n
o
,0
c
a
llta
b
le
m
o
v
w
fp
o
rtb
....
....
....
....
....
M
a
inP
ro
g
ra
m L
o
o
k
-u
pT
a
b
le
,in
fo
rm
o
fS
u
b
ro
u
tin
e
0
5
1
f
Fetching Data from a Look-up Table
18
Wait
Initialise
Score
left-to-right right-to-left
successful
return hit
rule
violation
left paddle
pressed
right paddle
pressed
rule
violation
on
completion
successful
return hit
on
completion
Start!
Rule Violation:
1. Attempting to hit the ball when
it is not at your end
2. Not hitting ball when it is at your end
Scoring
You score when your
opponent violates a rule.
Successful Return Hit
Hitting the ball when
it is at your end.
Note: Ball is at “your end”
when led nearest you is lit.
The Ping-pong Program Visualised as a State Diagram
19
D efine ball start position
O utput new ball position
B all at start?
A ny rule
violation?
N o
Y es
Y es
B all at end?
A ny rule
violation?
N o
Y es
Y es
R eset loop counter
(here if ball not at
r_to_l
rtl_0
rtl_1
rtl_2
N o
N o
Flow Diagram of Right-to-Left/Left-to-Right States
20
violation?
A ny rule
violation?
D elay
D ecrem ent loop counter
L oop C ounter
zero?
Y es
S uccessful
return hit?
(here if ball not at
beginning or end)
Y es
C hange State
(to “ l_to_r” )
C hange State
(to “ Score” )
Y es
D erive new ball position
A ny rule
violation?
Y es
N o
rtl_3
rtlen d
N o
N o
N o
21
End of Chapter 5
22

Weitere ähnliche Inhalte

Ähnlich wie ES-CH5.ppt

Advanced Techniques for Exploiting ILP
Advanced Techniques for Exploiting ILPAdvanced Techniques for Exploiting ILP
Advanced Techniques for Exploiting ILPA B Shinde
 
ECET 360 help A Guide to career/Snaptutorial
ECET 360 help A Guide to career/SnaptutorialECET 360 help A Guide to career/Snaptutorial
ECET 360 help A Guide to career/Snaptutorialpinck200
 
ECET 360 help A Guide to career/Snaptutorial
ECET 360 help A Guide to career/SnaptutorialECET 360 help A Guide to career/Snaptutorial
ECET 360 help A Guide to career/Snaptutorialpinck2380
 
Dynamic debugging in 8085 microprocessor
Dynamic debugging in 8085 microprocessorDynamic debugging in 8085 microprocessor
Dynamic debugging in 8085 microprocessorRamaPrabha24
 
EEP301: Ca06 sample
EEP301: Ca06 sampleEEP301: Ca06 sample
EEP301: Ca06 sampleUmang Gupta
 
system prgramming - loaders-linkers.pdf
system prgramming - loaders-linkers.pdfsystem prgramming - loaders-linkers.pdf
system prgramming - loaders-linkers.pdfSATHYABAMAMADHANKUMA
 
Ecet 360 Enthusiastic Study / snaptutorial.com
Ecet 360 Enthusiastic Study / snaptutorial.comEcet 360 Enthusiastic Study / snaptutorial.com
Ecet 360 Enthusiastic Study / snaptutorial.comStephenson34
 
Ecet 360 Success Begins / snaptutorial.com
Ecet 360  Success Begins / snaptutorial.comEcet 360  Success Begins / snaptutorial.com
Ecet 360 Success Begins / snaptutorial.comWilliamsTaylorzl
 
Ecet 360 Massive Success / snaptutorial.com
Ecet 360 Massive Success / snaptutorial.comEcet 360 Massive Success / snaptutorial.com
Ecet 360 Massive Success / snaptutorial.comHarrisGeorgx
 
Implementing True Zero Cycle Branching in Scalar and Superscalar Pipelined Pr...
Implementing True Zero Cycle Branching in Scalar and Superscalar Pipelined Pr...Implementing True Zero Cycle Branching in Scalar and Superscalar Pipelined Pr...
Implementing True Zero Cycle Branching in Scalar and Superscalar Pipelined Pr...IDES Editor
 
Introduction to Embedded C for 8051 and Implementation of Timer and Interrupt...
Introduction to Embedded C for 8051 and Implementation of Timer and Interrupt...Introduction to Embedded C for 8051 and Implementation of Timer and Interrupt...
Introduction to Embedded C for 8051 and Implementation of Timer and Interrupt...Sivaranjan Goswami
 
Model Execution and System Simulation
Model Execution and System SimulationModel Execution and System Simulation
Model Execution and System SimulationObeo
 
[Capella Day 2019] Model execution and system simulation in Capella
[Capella Day 2019] Model execution and system simulation in Capella[Capella Day 2019] Model execution and system simulation in Capella
[Capella Day 2019] Model execution and system simulation in CapellaObeo
 
Design the implementation of CDEx Robust DC Motor.
Design the implementation of CDEx Robust DC Motor.Design the implementation of CDEx Robust DC Motor.
Design the implementation of CDEx Robust DC Motor.Ankita Tiwari
 

Ähnlich wie ES-CH5.ppt (20)

Advanced Techniques for Exploiting ILP
Advanced Techniques for Exploiting ILPAdvanced Techniques for Exploiting ILP
Advanced Techniques for Exploiting ILP
 
ECET 360 help A Guide to career/Snaptutorial
ECET 360 help A Guide to career/SnaptutorialECET 360 help A Guide to career/Snaptutorial
ECET 360 help A Guide to career/Snaptutorial
 
ECET 360 help A Guide to career/Snaptutorial
ECET 360 help A Guide to career/SnaptutorialECET 360 help A Guide to career/Snaptutorial
ECET 360 help A Guide to career/Snaptutorial
 
Electrician Training for USAF
Electrician Training for USAFElectrician Training for USAF
Electrician Training for USAF
 
Lect 3-4 Zaheer Abbas
Lect 3-4 Zaheer AbbasLect 3-4 Zaheer Abbas
Lect 3-4 Zaheer Abbas
 
Dynamic debugging in 8085 microprocessor
Dynamic debugging in 8085 microprocessorDynamic debugging in 8085 microprocessor
Dynamic debugging in 8085 microprocessor
 
finalpaper.doc
finalpaper.docfinalpaper.doc
finalpaper.doc
 
EEP301: Ca06 sample
EEP301: Ca06 sampleEEP301: Ca06 sample
EEP301: Ca06 sample
 
system prgramming - loaders-linkers.pdf
system prgramming - loaders-linkers.pdfsystem prgramming - loaders-linkers.pdf
system prgramming - loaders-linkers.pdf
 
Adsa u1 ver 1.0
Adsa u1 ver 1.0Adsa u1 ver 1.0
Adsa u1 ver 1.0
 
Ecet 360 Enthusiastic Study / snaptutorial.com
Ecet 360 Enthusiastic Study / snaptutorial.comEcet 360 Enthusiastic Study / snaptutorial.com
Ecet 360 Enthusiastic Study / snaptutorial.com
 
Ecet 360 Success Begins / snaptutorial.com
Ecet 360  Success Begins / snaptutorial.comEcet 360  Success Begins / snaptutorial.com
Ecet 360 Success Begins / snaptutorial.com
 
Ecet 360 Massive Success / snaptutorial.com
Ecet 360 Massive Success / snaptutorial.comEcet 360 Massive Success / snaptutorial.com
Ecet 360 Massive Success / snaptutorial.com
 
Implementing True Zero Cycle Branching in Scalar and Superscalar Pipelined Pr...
Implementing True Zero Cycle Branching in Scalar and Superscalar Pipelined Pr...Implementing True Zero Cycle Branching in Scalar and Superscalar Pipelined Pr...
Implementing True Zero Cycle Branching in Scalar and Superscalar Pipelined Pr...
 
Introduction to Embedded C for 8051 and Implementation of Timer and Interrupt...
Introduction to Embedded C for 8051 and Implementation of Timer and Interrupt...Introduction to Embedded C for 8051 and Implementation of Timer and Interrupt...
Introduction to Embedded C for 8051 and Implementation of Timer and Interrupt...
 
Intro-Soft-Engg-2.pptx
Intro-Soft-Engg-2.pptxIntro-Soft-Engg-2.pptx
Intro-Soft-Engg-2.pptx
 
Csd01
Csd01Csd01
Csd01
 
Model Execution and System Simulation
Model Execution and System SimulationModel Execution and System Simulation
Model Execution and System Simulation
 
[Capella Day 2019] Model execution and system simulation in Capella
[Capella Day 2019] Model execution and system simulation in Capella[Capella Day 2019] Model execution and system simulation in Capella
[Capella Day 2019] Model execution and system simulation in Capella
 
Design the implementation of CDEx Robust DC Motor.
Design the implementation of CDEx Robust DC Motor.Design the implementation of CDEx Robust DC Motor.
Design the implementation of CDEx Robust DC Motor.
 

Mehr von alaakaraja1

Mehr von alaakaraja1 (6)

13.pptx
13.pptx13.pptx
13.pptx
 
1.pptx
1.pptx1.pptx
1.pptx
 
7.pptx
7.pptx7.pptx
7.pptx
 
ES-CH6.ppt
ES-CH6.pptES-CH6.ppt
ES-CH6.ppt
 
ES-CH1.ppt
ES-CH1.pptES-CH1.ppt
ES-CH1.ppt
 
Embedded System Basics - Introduction.ppt
Embedded System Basics - Introduction.pptEmbedded System Basics - Introduction.ppt
Embedded System Basics - Introduction.ppt
 

Kürzlich hochgeladen

+97470301568>> buy weed in qatar,buy thc oil qatar,buy weed and vape oil in d...
+97470301568>> buy weed in qatar,buy thc oil qatar,buy weed and vape oil in d...+97470301568>> buy weed in qatar,buy thc oil qatar,buy weed and vape oil in d...
+97470301568>> buy weed in qatar,buy thc oil qatar,buy weed and vape oil in d...Health
 
Tamil Call Girls Bhayandar WhatsApp +91-9930687706, Best Service
Tamil Call Girls Bhayandar WhatsApp +91-9930687706, Best ServiceTamil Call Girls Bhayandar WhatsApp +91-9930687706, Best Service
Tamil Call Girls Bhayandar WhatsApp +91-9930687706, Best Servicemeghakumariji156
 
AIRCANVAS[1].pdf mini project for btech students
AIRCANVAS[1].pdf mini project for btech studentsAIRCANVAS[1].pdf mini project for btech students
AIRCANVAS[1].pdf mini project for btech studentsvanyagupta248
 
Standard vs Custom Battery Packs - Decoding the Power Play
Standard vs Custom Battery Packs - Decoding the Power PlayStandard vs Custom Battery Packs - Decoding the Power Play
Standard vs Custom Battery Packs - Decoding the Power PlayEpec Engineered Technologies
 
Work-Permit-Receiver-in-Saudi-Aramco.pptx
Work-Permit-Receiver-in-Saudi-Aramco.pptxWork-Permit-Receiver-in-Saudi-Aramco.pptx
Work-Permit-Receiver-in-Saudi-Aramco.pptxJuliansyahHarahap1
 
Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...
Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...
Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...Arindam Chakraborty, Ph.D., P.E. (CA, TX)
 
Bridge Jacking Design Sample Calculation.pptx
Bridge Jacking Design Sample Calculation.pptxBridge Jacking Design Sample Calculation.pptx
Bridge Jacking Design Sample Calculation.pptxnuruddin69
 
Employee leave management system project.
Employee leave management system project.Employee leave management system project.
Employee leave management system project.Kamal Acharya
 
Hostel management system project report..pdf
Hostel management system project report..pdfHostel management system project report..pdf
Hostel management system project report..pdfKamal Acharya
 
Block diagram reduction techniques in control systems.ppt
Block diagram reduction techniques in control systems.pptBlock diagram reduction techniques in control systems.ppt
Block diagram reduction techniques in control systems.pptNANDHAKUMARA10
 
COST-EFFETIVE and Energy Efficient BUILDINGS ptx
COST-EFFETIVE  and Energy Efficient BUILDINGS ptxCOST-EFFETIVE  and Energy Efficient BUILDINGS ptx
COST-EFFETIVE and Energy Efficient BUILDINGS ptxJIT KUMAR GUPTA
 
Online food ordering system project report.pdf
Online food ordering system project report.pdfOnline food ordering system project report.pdf
Online food ordering system project report.pdfKamal Acharya
 
Generative AI or GenAI technology based PPT
Generative AI or GenAI technology based PPTGenerative AI or GenAI technology based PPT
Generative AI or GenAI technology based PPTbhaskargani46
 
Bhubaneswar🌹Call Girls Bhubaneswar ❤Komal 9777949614 💟 Full Trusted CALL GIRL...
Bhubaneswar🌹Call Girls Bhubaneswar ❤Komal 9777949614 💟 Full Trusted CALL GIRL...Bhubaneswar🌹Call Girls Bhubaneswar ❤Komal 9777949614 💟 Full Trusted CALL GIRL...
Bhubaneswar🌹Call Girls Bhubaneswar ❤Komal 9777949614 💟 Full Trusted CALL GIRL...Call Girls Mumbai
 
S1S2 B.Arch MGU - HOA1&2 Module 3 -Temple Architecture of Kerala.pptx
S1S2 B.Arch MGU - HOA1&2 Module 3 -Temple Architecture of Kerala.pptxS1S2 B.Arch MGU - HOA1&2 Module 3 -Temple Architecture of Kerala.pptx
S1S2 B.Arch MGU - HOA1&2 Module 3 -Temple Architecture of Kerala.pptxSCMS School of Architecture
 
A CASE STUDY ON CERAMIC INDUSTRY OF BANGLADESH.pptx
A CASE STUDY ON CERAMIC INDUSTRY OF BANGLADESH.pptxA CASE STUDY ON CERAMIC INDUSTRY OF BANGLADESH.pptx
A CASE STUDY ON CERAMIC INDUSTRY OF BANGLADESH.pptxmaisarahman1
 
DeepFakes presentation : brief idea of DeepFakes
DeepFakes presentation : brief idea of DeepFakesDeepFakes presentation : brief idea of DeepFakes
DeepFakes presentation : brief idea of DeepFakesMayuraD1
 
School management system project Report.pdf
School management system project Report.pdfSchool management system project Report.pdf
School management system project Report.pdfKamal Acharya
 
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXssuser89054b
 

Kürzlich hochgeladen (20)

+97470301568>> buy weed in qatar,buy thc oil qatar,buy weed and vape oil in d...
+97470301568>> buy weed in qatar,buy thc oil qatar,buy weed and vape oil in d...+97470301568>> buy weed in qatar,buy thc oil qatar,buy weed and vape oil in d...
+97470301568>> buy weed in qatar,buy thc oil qatar,buy weed and vape oil in d...
 
Tamil Call Girls Bhayandar WhatsApp +91-9930687706, Best Service
Tamil Call Girls Bhayandar WhatsApp +91-9930687706, Best ServiceTamil Call Girls Bhayandar WhatsApp +91-9930687706, Best Service
Tamil Call Girls Bhayandar WhatsApp +91-9930687706, Best Service
 
AIRCANVAS[1].pdf mini project for btech students
AIRCANVAS[1].pdf mini project for btech studentsAIRCANVAS[1].pdf mini project for btech students
AIRCANVAS[1].pdf mini project for btech students
 
Standard vs Custom Battery Packs - Decoding the Power Play
Standard vs Custom Battery Packs - Decoding the Power PlayStandard vs Custom Battery Packs - Decoding the Power Play
Standard vs Custom Battery Packs - Decoding the Power Play
 
Work-Permit-Receiver-in-Saudi-Aramco.pptx
Work-Permit-Receiver-in-Saudi-Aramco.pptxWork-Permit-Receiver-in-Saudi-Aramco.pptx
Work-Permit-Receiver-in-Saudi-Aramco.pptx
 
Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...
Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...
Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...
 
Bridge Jacking Design Sample Calculation.pptx
Bridge Jacking Design Sample Calculation.pptxBridge Jacking Design Sample Calculation.pptx
Bridge Jacking Design Sample Calculation.pptx
 
Employee leave management system project.
Employee leave management system project.Employee leave management system project.
Employee leave management system project.
 
Hostel management system project report..pdf
Hostel management system project report..pdfHostel management system project report..pdf
Hostel management system project report..pdf
 
Block diagram reduction techniques in control systems.ppt
Block diagram reduction techniques in control systems.pptBlock diagram reduction techniques in control systems.ppt
Block diagram reduction techniques in control systems.ppt
 
COST-EFFETIVE and Energy Efficient BUILDINGS ptx
COST-EFFETIVE  and Energy Efficient BUILDINGS ptxCOST-EFFETIVE  and Energy Efficient BUILDINGS ptx
COST-EFFETIVE and Energy Efficient BUILDINGS ptx
 
Online food ordering system project report.pdf
Online food ordering system project report.pdfOnline food ordering system project report.pdf
Online food ordering system project report.pdf
 
Generative AI or GenAI technology based PPT
Generative AI or GenAI technology based PPTGenerative AI or GenAI technology based PPT
Generative AI or GenAI technology based PPT
 
Bhubaneswar🌹Call Girls Bhubaneswar ❤Komal 9777949614 💟 Full Trusted CALL GIRL...
Bhubaneswar🌹Call Girls Bhubaneswar ❤Komal 9777949614 💟 Full Trusted CALL GIRL...Bhubaneswar🌹Call Girls Bhubaneswar ❤Komal 9777949614 💟 Full Trusted CALL GIRL...
Bhubaneswar🌹Call Girls Bhubaneswar ❤Komal 9777949614 💟 Full Trusted CALL GIRL...
 
S1S2 B.Arch MGU - HOA1&2 Module 3 -Temple Architecture of Kerala.pptx
S1S2 B.Arch MGU - HOA1&2 Module 3 -Temple Architecture of Kerala.pptxS1S2 B.Arch MGU - HOA1&2 Module 3 -Temple Architecture of Kerala.pptx
S1S2 B.Arch MGU - HOA1&2 Module 3 -Temple Architecture of Kerala.pptx
 
A CASE STUDY ON CERAMIC INDUSTRY OF BANGLADESH.pptx
A CASE STUDY ON CERAMIC INDUSTRY OF BANGLADESH.pptxA CASE STUDY ON CERAMIC INDUSTRY OF BANGLADESH.pptx
A CASE STUDY ON CERAMIC INDUSTRY OF BANGLADESH.pptx
 
Call Girls in South Ex (delhi) call me [🔝9953056974🔝] escort service 24X7
Call Girls in South Ex (delhi) call me [🔝9953056974🔝] escort service 24X7Call Girls in South Ex (delhi) call me [🔝9953056974🔝] escort service 24X7
Call Girls in South Ex (delhi) call me [🔝9953056974🔝] escort service 24X7
 
DeepFakes presentation : brief idea of DeepFakes
DeepFakes presentation : brief idea of DeepFakesDeepFakes presentation : brief idea of DeepFakes
DeepFakes presentation : brief idea of DeepFakes
 
School management system project Report.pdf
School management system project Report.pdfSchool management system project Report.pdf
School management system project Report.pdf
 
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
 

ES-CH5.ppt

  • 1. Chapter 5 Building Assembler Programs The aims of this chapter are to introduce: • how to visualise a program, and represent it diagrammatically; • how to use program branching and subroutines; • how to implement delays; • how to use look-up tables; • logical instructions; • how to simplify and optimise Assembler programming; • more advanced features of software simulators. Designing Embedded Systems with PIC Microcontrollers: Principles and Applications 2nd Edition. Tim Wilmshurst Instructors using Designing Embedded Systems with PIC Microcontrollers are welcome to use or change these slides as they see fit. Feedback, to t.j.wilmshurst@derby.ac.uk, is welcomed. The copyright to all diagrams is held by Microchip Technology, or T. Wilmshurst, unless otherwise stated 1
  • 2. Read Actual Temperature TA Read Demand Temperature TD TA > TD ? Activate Compressor Switch off Compressor TA >> TD ? Activate Alarm Yes Yes No No Initial Program Design - Flow Diagrams - Read Actual Temperature TA Read Demand Temperature TD TA > TD ? Activate Compressor Switch off Compressor TA >> TD ? Activate Alarm Yes Yes No No 2
  • 3. Initial Program Design – Flow Diagrams It is easy in Assembler to produce “spaghetti code”, ie code which has no structure, with branches going anywhere, and which is incomprehensible to any but the programmer, and incomprehensible even to him/her after a week. Therefore it is essential to plan a programme structure. While it is now viewed as quite an old technique, flow diagramming is still an appropriate tool to use. Flow diagrams emphasise the linear nature of a programme, and they can be drawn so that they can be readily translated into a programme, BUT they do not in themselves exert good programming discipline. 3
  • 4. An Alternative Programme Visualisation - State Diagrams This is an alternative to the flow diagram. In a State Diagram we recognise that the embedded system proceeds through a number of distinct states, and may stay in each state for a considerable period of time. Transition from one state to the next is determined by certain conditions satisfied. The state diagram is useful for visualising certain types of programme, but not all. It doesn’t translate directly into code. A simple example of a state machine programme is a washing machine controller, which will move from pre-wash, to wash to rinse states. 4
  • 5. Fill water Heat water Wash Rinse Spin Fault Ready User initiates Full level detected Timeout Required temperature reached Out of balance Out of balance Motor failure Motor failure Function complete Function complete Function complete Fault cleared Start! Door closed Timeout Example of a State Diagram: the Washing Machine 5
  • 6. Conditional Branching SubtractAfromB Result=0? Add C to D No Yes Features of PIC program: - The ability to make “decisions”, i.e. to act differently according to the state of logical variables. - have a number of instructions allow them to test a particular bit, and either continue program execution if the condition is not met, or branch to another part of the program if it is. - Often these variables are bit values in the Status Registers. 6
  • 7. Conditional Branching SubtractAfromB Result=0? Add C to D No Yes The PIC 16 Series microcontrollers have four conditional “skip” instructions. These test for a certain condition, and skip just one instruction if the condition is met, and continue normal program execution if it is not. The most versatile and general-purpose of these are: btfsc f, b; Bit Test f, Skip if Clear btfss f, b; Bit Test f, Skip if Set 7
  • 8. ;The “main” program starts here movlw 00h movwf porta ;clear all bits in port A movwf portb ;clear all bits in port B loop bcf portb, 3 ;preclear port B, bit 3 btfss porta, 3 ;Bit Test f, Skip if set bsf portb, 3 ;but set it if button pressed bcf portb, 4 ;preclear port B, bit 4 btfss porta, 4 ;Bit Test f, Skip if set bsf portb, 4 ;but set it if button pressed goto loop end Testing and Manipulating Single Bits 8
  • 9. Do this Do that Do som ething else Call SR1 Call SR2 Call SR1 Main Program SR1 Return SR2 Subroutine 1 Subroutine 2 Return ....... ....... ....... ....... ....... ....... ....... ....... ....... ....... ....... ....... ....... ....... Do that Subroutines and the Stack The subroutine is a program section structured in such a way that it can be called from anywhere in the program. Once it has been executed the program continues to execute from wherever it was before. 9
  • 10. Do this Do that Do som ething else Call SR1 Call SR2 Call SR1 Main Program SR1 Return SR2 Subroutine 1 Subroutine 2 Return ....... ....... ....... ....... ....... ....... ....... ....... ....... ....... ....... ....... ....... ....... Do that Subroutines and the Stack The PIC 16 Series subroutine call and return instructions are call and return. A special return instruction, retlw, is also available. Subroutine Call and Return instructions must always work in pairs. 10
  • 11. ;Delay of 5ms approx. Instruction cycle time is 5us. delay5 movlw D'200‘ ;200 cycles called, 5x5=25us each ;200*25us = 5000us (5ms) movwf delcntr1 del1 nop ;1 inst. cycle nop ;1 inst. cycle decfsz delcntr1,1 ;1 inst. cycle, Decrement f, Skip if 0 goto del1 ;2 inst. cycles return A Delay Subroutine ;500ms delay (approx) ;100 calls to delay5 Delay500 movlw D'100' movwf delcntr2 del2 call delay5 ;Delay of 5ms decfsz delcntr2,1 ;1 cycle, Dcrmnt f, Skip if 0 goto del2 ;2 inst. cycles return Nested Subroutines for Greater Delay 11
  • 12. More Use of the MPLAB Simulator: Breakpoints Once programs become long, it becomes increasingly tedious to step through them when simulating. We need a means of getting them to run through the code that we may not be interested in, but stopping where we need to take a closer look at what is happening. Breakpoints let this happen. In their simplest form, breakpoints allow you to run a program up to a specified instruction. Program execution then stops, and memory and register values can be inspected. 12
  • 13. More Use of the MPLAB Simulator: Stopwatch A weakness of the software simulator is that it does not run in real time, yet in embedded systems we have a strong desire to understand the timing behaviour of our programs. The Stopwatch facility of the simulator allows accurate time measurements to be simulated. It simply requires that the simulator “knows” what the oscillator frequency is. As it can record the number of instruction cycles executed, it can then calculate time taken. 13
  • 14. Try Programming Exercise 5.8 More Use of the MPLAB Simulator: Trace The various windows available in MPSIM give a good picture of the state of the processor status and memory locations at any time, but they do not tell us the history of program execution. Even if program execution has halted at a breakpoint, there may have been a number of program paths for it to go down to reach that point. The Trace function is there to give a record of the recent past of the program execution. 14
  • 16. Introducing Logical Instructions We have seen a good selection of the 16 series instructions, but have yet to see any logical ones. These instructions, like andwf, andlw, iorwf, or xorwf, perform logical operations between the contents of the W register, and either a literal value, or a value held in a memory location. They do it on a bitwise basis. ;Changes led pattern, using OR and rrf (Rotate Right f through Carry) ;pattern_IR btfsc flags, 0 goto pat_IR1 ;here if first visit to SR bsf flags,0 clrf portb ;set up initial output pattern return pat_IR1 rrf portb,0 ;here if 2nd or later visit to SR iorlw B'10000000‘ ;(OR with W)add 1 bit to pattern btfsc status,c ;has pattern reached carry flag? clrw ;if yes, clear W movwf portb return 16
  • 17. The instruction movlw allows us to introduce within the program a byte of constant data. We have already seen this in previous programs, for example with the instruction combination: movlw D'100' movwf delcntr2 This is fine for introducing single bytes of data into a program, or just a few. But suppose we want to place in the program a whole list of numbers. Suppose also that we want to be able to record where we are in the list, with some sort of marker. The movlw instruction is then not really up to the job, and we need to apply a way of setting up and accessing a block of data. This is called a look-up table. The PIC 16 Series approach to look-up tables is shown in the next slide. The table is formed as a subroutine. Every byte of data in the table is accompanied by a special instruction, retlw. This instruction is another “return from subroutine”, but with a difference - it requires an 8-bit literal operand. As it implements the subroutine return, it picks up its operand and puts it into the W register. The table is essentially a list of retlw instructions, each with its byte of data. Look-up Tables 17
  • 19. Wait Initialise Score left-to-right right-to-left successful return hit rule violation left paddle pressed right paddle pressed rule violation on completion successful return hit on completion Start! Rule Violation: 1. Attempting to hit the ball when it is not at your end 2. Not hitting ball when it is at your end Scoring You score when your opponent violates a rule. Successful Return Hit Hitting the ball when it is at your end. Note: Ball is at “your end” when led nearest you is lit. The Ping-pong Program Visualised as a State Diagram 19
  • 20. D efine ball start position O utput new ball position B all at start? A ny rule violation? N o Y es Y es B all at end? A ny rule violation? N o Y es Y es R eset loop counter (here if ball not at r_to_l rtl_0 rtl_1 rtl_2 N o N o Flow Diagram of Right-to-Left/Left-to-Right States 20
  • 21. violation? A ny rule violation? D elay D ecrem ent loop counter L oop C ounter zero? Y es S uccessful return hit? (here if ball not at beginning or end) Y es C hange State (to “ l_to_r” ) C hange State (to “ Score” ) Y es D erive new ball position A ny rule violation? Y es N o rtl_3 rtlen d N o N o N o 21