Bài giảng Vi xử lý - Chương 3: Họ vi điều khiển 805 (Phần 7) - Hồ Trung Mỹ
System & Program
Developments of 8051
Program Structure and Design
Introduction
Advantages and Disadvantages of Structured
Programming
The Three Structures: statements, loops, choice
Pseudo Code Syntax
Assembly Language Programming
Tools & Techniques for Program
Development
The Development Cycle
Integration and Verification
Command and Environments
2011/12/7
T. L. Jong, Dept. of E.E., NTHU
1
Introduction
Structured Programming:
Organizing and coding programs that reduces
complexity, improves clarity, and facilitates
debugging and modifying
All programs may be written using only three
structures: statements, loops, and choice—too
good to be true.
Introduce Structure programming to
assembly language programming
Flowcharts
Pseudo code
Assembly language
2011/12/7
T. L. Jong, Dept. of E.E., NTHU
2
Flowcharts
Decision block
Off-page connector
Process box
Predefined process
(subroutine)
Program flow arrow
Input/Output block
Program terminator
2011/12/7
T. L. Jong, Dept. of E.E., NTHU
3
Pseudo Code
Strict adherence to structure in
combination with informal language
[get a character from the keyboard]
IF [condition is true]
THEN [do statement 1]
ELSE BEGIN
[do statement 2]
[do statement 3]
END
2011/12/7
T. L. Jong, Dept. of E.E., NTHU
4
Advantages & Disadvantages of
Structured Programming
Advantages:
Simple to trace, debug
Finite number of structures
Structures as building block
The set of structure is complete
Structures are self-documenting, easy
to read
Structures are easy to describe in
flowcharts, syntax diagrams, pseudo
code,..
Increased program productivity
2011/12/7
T. L. Jong, Dept. of E.E., NTHU
5
Advantages & Disadvantages of
Structured Programming
Disadvantages:
Only a few high-level languages (Pascal,
C, PL/M) accept the structures directly;
others require extra translation stage
Structured program may execute slower
and require more memory
Some problems (a minority) are more
difficult to solve using only the three
structures
Nested structures can be difficult to
follow
2011/12/7
T. L. Jong, Dept. of E.E., NTHU
6
The Three Structures
Statements
[count = 0]
PRINT_STRING(“Select Option:”)
Loops (iteration)
WHILE/DO
REPEAT/UNTIL
Choice
IF/THEN/ELSE
CASE
2011/12/7
T. L. Jong, Dept. of E.E., NTHU
7
The WHILE/DO Statement
WHILE [condition] DO
[statement]
(statement might not be performed at all!)
Enter
WHILE [c == 1] DO
[statement]
No
Condition
True?
ENTER:
JNC
EXIT
STATEMENT: (statement)
Yes
JMP
(continue)
ENTER
EXIT:
Exit
Statement
2011/12/7
T. L. Jong, Dept. of E.E., NTHU
8
WHILE/DO: SUM Subroutine
[sum (A) = 0]
WHILE [length (R7) > 0] DO BEGIN
[sum = sum + @pointer (R0)]
[increment pointer]
[decrement length]
END
8051 code (closely structured; 13 bytes)
SUM: CLR A
LOOP: CJNZR7,#0,STAM
JMP EXIT
STAM: ADD A,@R0
INC R0
(loosely structured; 9 bytes)
SUM: CLR
INC
MORE: DJNZ
RET
A
R7
R7,SKIP
SKIP: ADD
INC
A,@R0
R0
MORE
DEC R7
JMP LOOP:
SJMP
EXIT: RET
2011/12/7
T. L. Jong, Dept. of E.E., NTHU
9
WHILE/DO
Pseudo code:
WHILE [ACC != CR AND R7 != 0] DO
[statement]
Enter
8051 code:
ENTER:
CJNE A,#0DH,SKIP
JMP EXIT
CJNE R7,#0,STAM
JMP EXIT
No
No
ACC !=
<CR>?
SKIP:
Yes
R7 != 0?
Yes
Statement
STAM: (one or more statements)
.
.
JMP ENTER
EXIT:
(continue)
Exit
2011/12/7
T. L. Jong, Dept. of E.E., NTHU
10
The REPEAT/UNTIL Statement
REPEAT
[statement]
[condition]
UNTIL
(statement performed at least once)
Enter
REPEAT [statement]
UNTIL [c == 1]
Statement
ENTER: (statement)
JNC
ENTER
No
Condition
True?
EXIT: (continue)
Yes
Exit
2011/12/7
T. L. Jong, Dept. of E.E., NTHU
11
The REPEAT/UNTIL Statement
REPEAT [ACC = @pointer]
[increment pointer]
UNTIL
[ACC == ‘Z’ or ACC == 0]
Enter
8051 code:
Get a char
Inc pointer
STAM: MOV A,@R0
INC
JZ
R0
EXIT
CJNE A,#’Z’,STAM
No
EXIT: RET
No
Char =
“Z”?
Char =
0?
Yes
Exit
Yes
2011/12/7
T. L. Jong, Dept. of E.E., NTHU
12
The IF/THEN/ELSE Statement
IF [condition]
THEN [statement 1]
ELSE [statement 2]
Enter
No
Yes
condition
true?
Statement 2
Statement 1
Exit
2011/12/7
T. L. Jong, Dept. of E.E., NTHU
13
The IF/THEN/ELSE Statement
[input character]
IF [input character == graphic]
THEN [echo character]
8051 code:
ELSE [echo ‘.’]
(closely structured; 14 bytes)
ENTER:
ACALL INCH
Enter
ACALL ISGRPH
JNC
STMENT2
STMENT1:
STMENT2:
EXIT:
ACALL OUTCH
Input character
JMP
MOV
EXIT
A,#’.’
ACALL OUTCH
(continue)
No
Yes
Graphic
char?
(loosely structured; 10 bytes)
ACALL INCH
Echo “.”
Echo char
ACALL ISGRPH
JC
MOV
SKIP
A,#’.’
SKIP:
ACALL OUTCH
(continue)
Exit
2011/12/7
T. L. Jong, Dept. of E.E., NTHU
14
The CASE Statement
CASE [expression] OF
0: [statement 0]
1: [statement 1]
2: [statement 2]
.
.
N: [statement 0]
[default]
END_CASE
Enter
No
No
No
No
expression
0?
expression
1?
expression
2?
expression
n?
Yes
Yes
Yes
Yes
Statement 0
Statement 1
Statement 2
Statement n
default
Exit
2011/12/7
T. L. Jong, Dept. of E.E., NTHU
15
The CASE Statement
[input a character]
CASE [character] OF
‘0’: [statement 0]
‘1’: [statement 1]
‘2’: [statement 2]
‘3’: [statement 3]
END_CASE
Enter
Input
character
No
No
No
No
char
== “0”?
char
== “1”?
char
== “2”?
char
== “3”?
Yes
Yes
Action 1
Yes
Action 2
Yes
Action 0
Action 3
Exit
2011/12/7
T. L. Jong, Dept. of E.E., NTHU
16
The CASE Statement
8051 code:
(closely structured)
(loosely structured)
ACALL INCH
ACALL INCH
ANL
RL
A,#3
A
DPTR,#TABLE
@A+DPTR
ACT0
ACT1
ACT2
;reduce to 3 bits
CJNE
A,#’0’,SKIP1
ACT0:
.
MOV
.
JMP
TABLE: AJMP
AJMP
JMP
EXIT
A,#’1’,SKIP2
SKIP1: CJNE
ACT1:
.
.
AJMP
.
.
JMP
.
.
JMP
.
.
ACT3:
ACT0:
ACT1:
JMP
EXIT
A,#’2’,SKIP3
SKIP2: CJNE
EXIT
EXIT
EXIT
ACT2:
.
.
JMP
EXIT
A,#’3’,EXIT
SKIP3: CJNE
ACT3:
.
.
JMP
ACT2:
EXIT:
.
.
EXIT:
(continue)
(continue)
2011/12/7
T. L. Jong, Dept. of E.E., NTHU
17
The GOTO Statement
GOTO statement can always be avoided by
using the structures. Sometimes GOTO
statement provides an easy method of
terminating a structure when errors occur.
GOTO statements usually becomes
unconditional jump in assembly
implementation. Extreme caution is needed.
Never exit a subroutine using GOTO instead
of normal return for the return address will
be left on the stack and eventually stack
overflow will occur.
2011/12/7
T. L. Jong, Dept. of E.E., NTHU
18
Pseudo Code Syntax
Tips of using pseudo code:
Use descriptive language for statements
Avoid machine dependency in statements
Enclose conditions & statements in brackets: []
Begin all subroutines with their names followed
by a set of parameters: ()
End all subroutine with RETURN ()
Use lower text except reserved words &
subroutine names
Indent all statements from the structure entry
points and exit points
Use the commercial at sign (@) for indirect
addressing
2011/12/7
T. L. Jong, Dept. of E.E., NTHU
19
Suggested Pseudo Code Syntax
Reserved words:
BEGIN END
REPEAT UNTIL
WHILE DO
IF
THEN
ELSE
CASE OF
RETURN
Arithmetic operators:
+ addition
- subtraction
* multiplication
/ division
% modulus (remainder after division)
2011/12/7
T. L. Jong, Dept. of E.E., NTHU
20
Tải về để xem bản đầy đủ
Bạn đang xem 20 trang mẫu của tài liệu "Bài giảng Vi xử lý - Chương 3: Họ vi điều khiển 805 (Phần 7) - Hồ Trung Mỹ", để tải tài liệu gốc về máy hãy click vào nút Download ở trên
File đính kèm:
bai_giang_vi_xu_ly_chuong_3_ho_vi_dieu_khien_805_phan_7_ho_t.pdf

