chapter 4

Post on 23-Oct-2014

26 Views

Category:

Documents

0 Downloads

Preview:

Click to see full reader

TRANSCRIPT

1

CHAPTER 4: MICROCONTROLLER/MICROPROCESSOR

INSTRUCTION

SALTIHIE BIN ZENIsaltihiezen@pmu.edu.my

ELECTRICAL ENGINEERING DEPARTMENT

POLITEKNIK MUKAH SARAWAK

Micrprocessor and Microcontroller System - Saltihie Zeni

Micrprocessor and Microcontroller System - Saltihie Zeni

2

INTRODUCTION

• Instruction set – A list of instruction that can be executed by a microprocessor.

• Instruction are categorized according to basic operation performed:– Data transfer– Arithmetic– Logic– Shift and Rotates– Bit manipulation– BCD– Program control– System control

Instruction is written using mnemonic

(easy to remember code) because it is not practical to use

binary code for programming

Micrprocessor and Microcontroller System - Saltihie Zeni

3

BASIC INSTRUCTION SET

Micrprocessor and Microcontroller System - Saltihie Zeni

4

68000 REGISTER SET

Micrprocessor and Microcontroller System - Saltihie Zeni

5

68000 REGISTER

Micrprocessor and Microcontroller System - Saltihie Zeni

6

ADDRESSING MODE

Micrprocessor and Microcontroller System - Saltihie Zeni

7

1. REGISTER DIRECT ADDRESSING

Micrprocessor and Microcontroller System - Saltihie Zeni

8

2. IMMEDIATE ADDRESSING MODE

Micrprocessor and Microcontroller System - Saltihie Zeni

9

IMMEDIATE ADDRESSING MODE

Micrprocessor and Microcontroller System - Saltihie Zeni

10

3. ABSOLUTE ADDRESSING MODE

Micrprocessor and Microcontroller System - Saltihie Zeni

11

4. ADDRESS REGISTER INDIRECT ADDRESSING MODE

Micrprocessor and Microcontroller System - Saltihie Zeni

12

5. RELATIVE ADDRESSING MODE

Micrprocessor and Microcontroller System - Saltihie Zeni

13

6. INHERENT ADDRESSING MODE

Micrprocessor and Microcontroller System - Saltihie Zeni

14

Example 4.1

• State the addressing mode for the following instruction:

i. MOVE.W D2,D3

ii. JSR

iii. MOVE.B (A0), D3

iv. CLR.W $2003

v.MOVE.W #$1234,D0

vi.BRA DELAY

vii. SWAP D0,D1

viii. MOVE.B #%10010011,D3

Micrprocessor and Microcontroller System - Saltihie Zeni

15

BUILDING A PROGRAM

• Program design is a process to provide an executable program in microprocessor.

• A task that need to be done by microprocessor need a conversion to machine language that can be understood by microprocessor.

• 5 Steps to build a program:– Definition of Problem– Logic Design– Programming– Load, Test and Debug– Documentation

Micrprocessor and Microcontroller System - Saltihie Zeni

16

Problem analysis is a phase to convert a task that need to be done to an abstract shape and not particular to a microprocessor.This phase include a design using pseudo code or flow chart (will be discuss later).6 mini steps in problem analysis:– Clarify objectives and users– Clarify desired outputs– Clarify desired inputs– Clarify desired process– Double-check feasibility of implementing the program– Document the analysis.

1. DEFINITION OF PROBLEMS

Micrprocessor and Microcontroller System - Saltihie Zeni

17

2. LOGIC DESIGN

Micrprocessor and Microcontroller System - Saltihie Zeni

18

2. LOGIC DESIGN

• Algorithm can be represented in flow chart or pseudo code.

• Flow chart – A graphical representation of algorithm

• Pseudo – Artificial and informal language– Similar to everyday English

Micrprocessor and Microcontroller System - Saltihie Zeni

19

2. LOGIC DESIGNFlow chart – Basic blocks

Micrprocessor and Microcontroller System - Saltihie Zeni

20

• This phase is where a programmer write a task using editor(discussed in chapter 3).

• Editor is any software that can provide ASCII text like notepad or particular software like SciTE or programmers notepad.

• After source code provided, it then need to compile to produce a program that can be understood by microprocessor.

3. PROGRAMMING

Micrprocessor and Microcontroller System - Saltihie Zeni

21

• In program writing, there will be a mistake or technically called error.

• 2 types of error:– Syntax error– Semantic error

3. PROGRAMMING

Micrprocessor and Microcontroller System - Saltihie Zeni

22

• Syntax error– Mistake in program writing for example more or

less comma than needed. This error can be detect early and easy to do a correction.

• Semantic error– Logic mistake for example programmer done a

operation which is not appropriate and not suitable in the program.

– This error cannot detected by compiler and only can be seen when the program already execute.

3. PROGRAMMING

Micrprocessor and Microcontroller System - Saltihie Zeni

23

3. PROGRAMMING

Micrprocessor and Microcontroller System - Saltihie Zeni

24

4. LOAD, TESTING AND DEBUGGING

Micrprocessor and Microcontroller System - Saltihie Zeni

25

5. DOCUMENTATION

Micrprocessor and Microcontroller System - Saltihie Zeni

26

PROGRAM EXECUTION STRUCTURE

SEQUENCE SELECTION LOOPING

Micrprocessor and Microcontroller System - Saltihie Zeni

27

JUMP Instruction

JMP = JumpEffect : Program Counter Effective AddressSyntax:

JMP <ea>Example:

JMP START ;START = $1000

JMP $2000

JMP (A0) ; A0 = $1000Useful in selection structure especially to go to sub-routine or sub-program.

Micrprocessor and Microcontroller System - Saltihie Zeni

28

BRANCH ALWAYS Instruction

BRA = Branch AlwaysEffect : PC > PC +offsetSyntax:

BRA <ea>Example:

BRA LAST

Micrprocessor and Microcontroller System - Saltihie Zeni

29

if-then STRUCTURE

• Basic control structure – conditional statement• Syntax in high level language programming

if <condition> then <statement>

• In 68000, this structure is:

<condition> ; testing condition and set CCRBCC SKIP ; select subroutine<Statement> ;execute statement

SKIP ….. ;continue

Micrprocessor and Microcontroller System - Saltihie Zeni

30

CONDITIONAL BRANCH (Bcc)• Syntax:

Bcc <ea>• cc represent the condition that will be tested.• If condition TRUE = Subroutine executed• If condition FALSE = Subroutine not executed, program go to next instruction.

Micrprocessor and Microcontroller System - Saltihie Zeni

31

Example Using Conditional Branch

1. Clear data in D7 if operation D6-1 equal to zero.

2. Add 1 to D0 if addition of D1+D2 produce carry.

Micrprocessor and Microcontroller System - Saltihie Zeni

32

Compare Instruction

Compare = CMP• Function : Compare 2 data. Subtract data and set CCR. Destination must

data register. It look like subtract instruction but data register not change.

Micrprocessor and Microcontroller System - Saltihie Zeni

33

Variation of CMP Instruction• CMPI (Compare immediate)

– To compare immediate data– Operation size : B,W,L– Destination : Data or memory register

• CMPA(Compare Address)– To compare address register– Operation size : W,L . W will be extended to L before comparison– Destination : memory register

• CMPM(Compare Memory)– To compare data inside memory – Operation size : B,W,L– Destination : (An)+ only

Micrprocessor and Microcontroller System - Saltihie Zeni

34

Example of CMP and Bcc1. Subtract D1 from D0. Jump if result is 0.

2. Jump if value in D0 equal to 10.

CMPI #10, D0 ; Compare D0 with 10BEQ SAMA ; Jump if D0=10

Micrprocessor and Microcontroller System - Saltihie Zeni

35

if-then-else Structure

• Syntax in high level language:if <condition> then<statement 1> else <statement 2>

• In 68000, this structure is:

Micrprocessor and Microcontroller System - Saltihie Zeni

36

do-while Structure

• Syntax in high level programmingdo <statement>while <condition>

• In 68000, the structure is:

Micrprocessor and Microcontroller System - Saltihie Zeni

37

while Structure

• Syntax in high level programmingwhile <condition> do <statement>

• In 68000, the structure is:

Micrprocessor and Microcontroller System - Saltihie Zeni

38

SIMPLE PROGRAMMING EXAMPLE(ADDITION OF 2 NUMBERS)

1. Define the problemInput : 2 numbers (for example decimal 12 and 34) – save in data register D0 and D1Process : Addition of 2 numbersOutput : Add from Data register D0 and D1 and save result in D1.

2. Draw a flow chart or pseudo code.3. Using the assembly language, write the program.

Micrprocessor and Microcontroller System - Saltihie Zeni

39

Enter first number to data register D0

Enter second number to register

D1

Calculate:D1 = D0 +D1ADD D0, D1

START

END

Flow chart to add 2 number saved in

data register

Micrprocessor and Microcontroller System - Saltihie Zeni

40

PROGRAM

ORG $1000MOVE.B #12,D0MOVE.B #34,D1ADD.B D0,D1END

Write the following program in easy68k and observe the change in

register

Micrprocessor and Microcontroller System - Saltihie Zeni

41

Example 4.2

Case study

1. A robot will move forward when the sensor 1 (S1) enabled. When the sensor 2 (S2)

is activated,) it will make a round of U (right and stop for 20 seconds, then

continue to move forward. Until the sensor 3 (S3) is activated, then the robot will

stop. Draw a flow chart to illustrate the process.

2. In a classroom, when switch 1 is ON it will ON an air-conditioner and all light

respectively. After 3 minutes, LCD screen move down and 2 minutes after that LCD

projector also ON. When switch 1 is turn OFF, 5 minutes after that, air-conditioner,

light and LCD also turn OFF and LCD screen will move up. Draw a flow chart to

illustrate the process.

Micrprocessor and Microcontroller System - Saltihie Zeni

42

Example 4.3

Writing a program

Write a program for the flow chart given. Assume the starting address of your program

is $1000.

D0>5

D1=D0+7D0=D0-1

D7=D0/7

FALSETRUE

END

START

Micrprocessor and Microcontroller System - Saltihie Zeni

43

Note ….

top related