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