37
Programming in Assembly Chapter 09

Programming in Assembly€¦ · Lesson 03 Programming Approach for Main and Interrupt Service Routines in 8051

Embed Size (px)

Citation preview

Page 1: Programming in Assembly€¦ · Lesson 03 Programming Approach for Main and Interrupt Service Routines in 8051

Programming in Assembly

Chapter 09

Page 2: Programming in Assembly€¦ · Lesson 03 Programming Approach for Main and Interrupt Service Routines in 8051

Lesson 03

Programming Approach for Main

and Interrupt Service Routines in

8051

Page 3: Programming in Assembly€¦ · Lesson 03 Programming Approach for Main and Interrupt Service Routines in 8051

2011 Microcontrollers-... 2nd Ed. Raj Kamal

Pearson Education3

Program Approach for programming

Main Program Instructions

Page 4: Programming in Assembly€¦ · Lesson 03 Programming Approach for Main and Interrupt Service Routines in 8051

2011 Microcontrollers-... 2nd Ed. Raj Kamal

Pearson Education4

Main program initial instructions

• Instruction to define the register bank being

used

• Instruction to define stack top

• Remember default Settings for the A, B, PSW

and DPTR on Reset and change if required

Page 5: Programming in Assembly€¦ · Lesson 03 Programming Approach for Main and Interrupt Service Routines in 8051

2011 Microcontrollers-... 2nd Ed. Raj Kamal

Pearson Education5

Instruction to write the control bits and

mode bits

• Instruction to write into the required SFRs

• Keep default settings in view

• Write into TCON, TMOD, TH0, TL0, TH1,

TL0, TH2, TL2, RCAP2H, RCAP2L if timers

being used

• Write into SCON if serial interface is being

used.

Page 6: Programming in Assembly€¦ · Lesson 03 Programming Approach for Main and Interrupt Service Routines in 8051

2011 Microcontrollers-... 2nd Ed. Raj Kamal

Pearson Education6

Setting Interrupt priorities

• Instructions to define the Interrupt priorities

• This is to minimize the worst case interrupt

latencies of the possible sources of interrupts

in the system

• Set or reset the bits in the IP SFR

Page 7: Programming in Assembly€¦ · Lesson 03 Programming Approach for Main and Interrupt Service Routines in 8051

2011 Microcontrollers-... 2nd Ed. Raj Kamal

Pearson Education7

Setting Interrupt priorities for Latency

control

• Latency is the waiting period in the response to an interrupt

• Latency when multiple devices in the MCU used

• Interrupts from each takes place at the different instances

• Some ISRs have lower latency (shorter code) and some high (longer code)

Page 8: Programming in Assembly€¦ · Lesson 03 Programming Approach for Main and Interrupt Service Routines in 8051

2011 Microcontrollers-... 2nd Ed. Raj Kamal

Pearson Education8

Latency Control

• Required because some interrupts have to be

responded by executing ISR fast compared to

other

Page 9: Programming in Assembly€¦ · Lesson 03 Programming Approach for Main and Interrupt Service Routines in 8051

2011 Microcontrollers-... 2nd Ed. Raj Kamal

Pearson Education9

Enabling (un-mask) selected sources of

the interrupts

• Instruction to do this is the set or reset bits in

the IE SFR

• First set or reset the EA (enable all) bit in the

IE SFR

• SETB 0AFH is the instruction

• Then the corresponding source interrupt

enabled (unmasked)

Page 10: Programming in Assembly€¦ · Lesson 03 Programming Approach for Main and Interrupt Service Routines in 8051

2011 Microcontrollers-... 2nd Ed. Raj Kamal

Pearson Education10

Use of the Timer, Serial Port and

INT pin ISRs

• The interrupt service routines written for the

timers, serial port and external interrupts

Page 11: Programming in Assembly€¦ · Lesson 03 Programming Approach for Main and Interrupt Service Routines in 8051

2011 Microcontrollers-... 2nd Ed. Raj Kamal

Pearson Education11

Program Approach for programming

ISR Instructions

Page 12: Programming in Assembly€¦ · Lesson 03 Programming Approach for Main and Interrupt Service Routines in 8051

2011 Microcontrollers-... 2nd Ed. Raj Kamal

Pearson Education12

Execution Interval of an ISR

• Multiple interrupts from same source (for

example timer overflow)

• There can be interrupts from different sources

at different instances

• ISR execution time must be less than the

interval of next interrupt from the same source

Page 13: Programming in Assembly€¦ · Lesson 03 Programming Approach for Main and Interrupt Service Routines in 8051

2011 Microcontrollers-... 2nd Ed. Raj Kamal

Pearson Education13

Disabling and Enabling of an

interrupt

• When one routine or ISR runs there might be need to disable all or selected source of interrupts

• Disabling of a source of interrupt for a give interval increases the latency of that interrupt

• Programming approach such that the latency of higher priority interrupt doe not increase

• significantly and no source of interrupt misses the deadline for the service

Page 14: Programming in Assembly€¦ · Lesson 03 Programming Approach for Main and Interrupt Service Routines in 8051

2011 Microcontrollers-... 2nd Ed. Raj Kamal

Pearson Education14

Resetting of Interrupt flags if not

done automatically by hardware

• Some flags such as TI and RI don’t reset

automatically.

• Use instructions to reset those in case present

service routine for service of those interrupts

Page 15: Programming in Assembly€¦ · Lesson 03 Programming Approach for Main and Interrupt Service Routines in 8051

2011 Microcontrollers-... 2nd Ed. Raj Kamal

Pearson Education15

Polling

• Write instructions to check occurrences of an

interrupt in a interrupt sources group if the ISR

is for servicing more than one interrupt source

.

Page 16: Programming in Assembly€¦ · Lesson 03 Programming Approach for Main and Interrupt Service Routines in 8051

2011 Microcontrollers-... 2nd Ed. Raj Kamal

Pearson Education16

Push Instruction

• A routine on interrupt must use separate set of

registers in another register bank in case the

currently use set of registers in bank of 8

registers is required to be protected in the

interrupted main or routine program

• Therefore PSW needs to be pushed onto stack

if another register bank is required in an ISR

Page 17: Programming in Assembly€¦ · Lesson 03 Programming Approach for Main and Interrupt Service Routines in 8051

2011 Microcontrollers-... 2nd Ed. Raj Kamal

Pearson Education17

Push PSW Instruction

• Write the instructions, if feasible, without the need for using the PSW

• If required then Push the PSW at the beginning of the ISR

• This done when the ISR uses any of the flags CY, AC, OV or P and the currently set flags are required to be protected in the interrupted main program or routine.

• PUSH 0D0H [D0H is address of PSW]

Page 18: Programming in Assembly€¦ · Lesson 03 Programming Approach for Main and Interrupt Service Routines in 8051

2011 Microcontrollers-... 2nd Ed. Raj Kamal

Pearson Education18

Push Instruction

• Write the instructions, if feasible, without the

need for using the A register. If not feasible,

then PUSH A also

• PUSH 0E0H

• For example, use the logic operations between

direct and data in the ISR in place of logic

operations on A.

Page 19: Programming in Assembly€¦ · Lesson 03 Programming Approach for Main and Interrupt Service Routines in 8051

2011 Microcontrollers-... 2nd Ed. Raj Kamal

Pearson Education19

Push Instruction

• Write the instructions, if feasible, without the

need for using those SFRs and internal

memory addresses which are required to be

saved in the main program or routine

• If not feasible, then PUSH those SFRs or

internal memory RAM addresses

• Use PUSH direct

Page 20: Programming in Assembly€¦ · Lesson 03 Programming Approach for Main and Interrupt Service Routines in 8051

2011 Microcontrollers-... 2nd Ed. Raj Kamal

Pearson Education20

Use of Pointers R0 and R1 and bank

registers

• Use pointer registers and registers in the

register bank as much as possible, because

instructions using registers shorter and taking

smaller number of instruction cycles

• For example, use MOV using @Ri; CJNE Rn,

#data8, Rel; and CJNE @Ri, #data8 , Rel;

Page 21: Programming in Assembly€¦ · Lesson 03 Programming Approach for Main and Interrupt Service Routines in 8051

2011 Microcontrollers-... 2nd Ed. Raj Kamal

Pearson Education21

Use of Combined Instructions

• Use combined instruction

• DJNZ Rn, Rel;

Page 22: Programming in Assembly€¦ · Lesson 03 Programming Approach for Main and Interrupt Service Routines in 8051

2011 Microcontrollers-... 2nd Ed. Raj Kamal

Pearson Education22

POP Instructions

• POP all registers and memory addresses before

the return from the ISR that were pushed at the

beginning of ISR

• POPs must be in last in first out way

• For example, if PSW first and then A were

pushed then POP A first then PSW

Page 23: Programming in Assembly€¦ · Lesson 03 Programming Approach for Main and Interrupt Service Routines in 8051

2011 Microcontrollers-... 2nd Ed. Raj Kamal

Pearson Education23

Interrupts Enable

• Enable all or selected interrupts, in case they

were disabled at the beginning of the ISR

• Use in 8051 the instruction for SETB 0x0AFH.

[IE^7= 1; for enabling interrupts handling of

un-masked interrupts.]

Page 24: Programming in Assembly€¦ · Lesson 03 Programming Approach for Main and Interrupt Service Routines in 8051

2011 Microcontrollers-... 2nd Ed. Raj Kamal

Pearson Education24

Stack

Page 25: Programming in Assembly€¦ · Lesson 03 Programming Approach for Main and Interrupt Service Routines in 8051

2011 Microcontrollers-... 2nd Ed. Raj Kamal

Pearson Education25

Push Stacked Stacked

Processor Status Processor Status

and CPU and CPU

registers and registers and

variablesvariables

Codes

block

E

Used in LIFO mode

Pop

Page 26: Programming in Assembly€¦ · Lesson 03 Programming Approach for Main and Interrupt Service Routines in 8051

2011 Microcontrollers-... 2nd Ed. Raj Kamal

Pearson Education26

Calling a routine from other

routine

Page 27: Programming in Assembly€¦ · Lesson 03 Programming Approach for Main and Interrupt Service Routines in 8051

2011 Microcontrollers-... 2nd Ed. Raj Kamal

Pearson Education27

Codes

block B

Call

for B

Stacked Local variables for A

Stacked Local

Variables for B

Codes block

A

Values passed at the argument

of the called function D

Page 28: Programming in Assembly€¦ · Lesson 03 Programming Approach for Main and Interrupt Service Routines in 8051

2011 Microcontrollers-... 2nd Ed. Raj Kamal

Pearson Education28

Codes for DCall for D

Shared or global variables at C and D New

assignment of Variables at

D

Codes

for

C

Values passed by address references at the argument of the called function D

Page 29: Programming in Assembly€¦ · Lesson 03 Programming Approach for Main and Interrupt Service Routines in 8051

2011 Microcontrollers-... 2nd Ed. Raj Kamal

Pearson Education29

Codes for H

Passing by reference

Return using

pointer X and offset

Codes

for

G

Use of pointer X and offset

values of Global values of Global variablesvariables

offsetoffsetoffsetoffset

Page 30: Programming in Assembly€¦ · Lesson 03 Programming Approach for Main and Interrupt Service Routines in 8051

2011 Microcontrollers-... 2nd Ed. Raj Kamal

Pearson Education30

Codes for H

Passing using Registers or

Internal RAM

Return using

address or

register name

Codes

for

G

Use of Register name or address

values of values of Temporary Temporary variablesvariables

Page 31: Programming in Assembly€¦ · Lesson 03 Programming Approach for Main and Interrupt Service Routines in 8051

2011 Microcontrollers-... 2nd Ed. Raj Kamal

Pearson Education31

Reentrant Function

Page 32: Programming in Assembly€¦ · Lesson 03 Programming Approach for Main and Interrupt Service Routines in 8051

2011 Microcontrollers-... 2nd Ed. Raj Kamal

Pearson Education32

Reentrant Function

� When interrupted in-between, it

Enters into identical state of variables and CPU registers on return from another

Page 33: Programming in Assembly€¦ · Lesson 03 Programming Approach for Main and Interrupt Service Routines in 8051

2011 Microcontrollers-... 2nd Ed. Raj Kamal

Pearson Education33

Reentrant Function

• No shared data problem

• Always gets the values on pass by

values, not by reference

• Always call reentrant function only

• Always uses local variables only

Page 34: Programming in Assembly€¦ · Lesson 03 Programming Approach for Main and Interrupt Service Routines in 8051

2011 Microcontrollers-... 2nd Ed. Raj Kamal

Pearson Education34

Summary

Page 35: Programming in Assembly€¦ · Lesson 03 Programming Approach for Main and Interrupt Service Routines in 8051

2011 Microcontrollers-... 2nd Ed. Raj Kamal

Pearson Education35

We learnt

• Program Initial Instructions in Main

• Program Instructions in ISR

• Use Pointers

• Use Register Banks

• Use combine Instructions

Page 36: Programming in Assembly€¦ · Lesson 03 Programming Approach for Main and Interrupt Service Routines in 8051

2011 Microcontrollers-... 2nd Ed. Raj Kamal

Pearson Education36

We learnt

• Status and CPU registers saved on

stack

• Local variables stacked and passed to

called routine by values

• Shared variables passed to called

routine by reference to address

Page 37: Programming in Assembly€¦ · Lesson 03 Programming Approach for Main and Interrupt Service Routines in 8051

End of Lesson 03 on

Programming Approach for

Main and Interrupt Service

Routines in 8051