Title: System
1System 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
2Introduction
- 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 choicetoo
good to be true. - Introduce Structure programming to assembly
language programming - Flowcharts
- Pseudo code
- Assembly language
3Flowcharts
Decision block
Off-page connector
Process box
Predefined process (subroutine)
Program flow arrow
Input/Output block
Program terminator
4Pseudo 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
5Advantages 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
6Advantages 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
7The Three Structures
- Statements
- count 0
- PRINT_STRING(Select Option)
- Loops (iteration)
- WHILE/DO
- REPEAT/UNTIL
- Choice
- IF/THEN/ELSE
- CASE
8The WHILE/DO Statement
- WHILE condition DO
- statement
- (statement might not be performed at all!)
Enter
WHILE c 1 DO statement
Condition True?
No
ENTER JNC EXIT STATEMENT (statement) JMP ENT
ER EXIT (continue)
Yes
Statement
Exit
9WHILE/DO SUM Subroutine
sum (A) 0 WHILE length (R7) gt 0 DO
BEGIN sum sum _at_pointer (R0) increment
pointer decrement length END
8051 code (closely structured 13 bytes) SUM
CLR A LOOP CJNZ R7,0,STAM JMP EXIT STAM
ADD A,_at_R0 INC R0 DEC R7 JMP LOOP EXIT RET
(loosely structured 9 bytes) SUM CLR A
INC R7 MORE DJNZ R7,SKIP RET SKIP
ADD A,_at_R0 INC R0 SJMP MORE
10WHILE/DO
Pseudo code WHILE ACC ! CR AND R7 ! 0
DO statement
Enter
8051 code ENTER CJNE A,0DH,SKIP JMP EXIT SKIP
CJNE R7,0,STAM JMP EXIT STAM (one or more
statements) . . JMP ENTER EXIT (continue)
ACC ! ltCRgt?
No
Yes
R7 ! 0?
No
Yes
Statement
Exit
11The REPEAT/UNTIL Statement
- REPEAT statement
- UNTIL condition
- (statement performed at least once)
Enter
REPEAT statement UNTIL c 1
Statement
ENTER (statement) JNC ENTER EXIT (continue)
Condition True?
No
Yes
Exit
12The REPEAT/UNTIL Statement
- REPEAT ACC _at_pointer
- increment pointer
- UNTIL ACC Z or ACC 0
Enter
8051 code STAM MOV A,_at_R0 INC R0 JZ EXIT CJNE
A,Z,STAM EXIT RET
Get a char
Inc pointer
No
Char Z?
Char 0?
No
Yes
Yes
Exit
13The IF/THEN/ELSE Statement
- IF condition
- THEN statement 1
- ELSE statement 2
14The IF/THEN/ELSE Statement
- input character
- IF input character graphic
- THEN echo character
- ELSE echo .
8051 code (closely structured 14
bytes) ENTER ACALL INCH ACALL ISGRPH JNC STM
ENT2 STMENT1 ACALL OUTCH JMP EXIT STMENT2 MOV
A,. ACALL OUTCH EXIT (continue)
Enter
Input character
Graphic char?
Yes
No
(loosely structured 10 bytes) ACALL INCH ACAL
L ISGRPH JC SKIP MOV A,. SKIP ACALL OUTCH
(continue)
Echo char
Echo .
Exit
15The CASE Statement
- CASE expression OF
- 0 statement 0
- 1 statement 1
- 2 statement 2
- .
- .
- N statement 0
- default
- END_CASE
Enter
expression 1?
expression 2?
expression n?
No
No
No
No
Yes
Yes
Yes
Yes
Statement 1
Statement 2
Statement n
default
Exit
16The 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
char 1?
char 2?
char 3?
No
No
No
No
Yes
Yes
Yes
Yes
Action 1
Action 2
Action 3
Exit
17The CASE Statement
8051 code (closely structured)
ACALL INCH CJNE A,0,SKIP1 ACT0 . . JMP EXI
T SKIP1 CJNE A,1,SKIP2 ACT1 . . JMP EXIT SK
IP2 CJNE A,2,SKIP3 ACT2 . . JMP EXIT SKIP3
CJNE A,3,EXIT ACT3 . . EXIT (continue)
(loosely structured) ACALL INCH ANL A,3 reduc
e to 3 bits RL A MOV DPTR,TABLE JMP _at_ADPTR TA
BLE AJMP ACT0 AJMP ACT1 AJMP ACT2 ACT3 . . J
MP EXIT ACT0 . . JMP EXIT ACT1 . . JMP EXIT
ACT2 . . EXIT (continue)
18The 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.
19Pseudo 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 (_at_) for indirect
addressing
20Suggested 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)
21Suggested Pseudo Code Syntax
- Relational operators
- true if values equal to each other
- ! true if values not equal to each other
- lt true if first value less than second
- lt true if first value lt second
- gt true if first value gt second
- gt true if first value gt second
- true if both values are true
- true if either value is true
- Bitwise Logical operators
- logical AND
- logical OR
- logical XOR
- logical NOT (ones complement)
- gtgt logical shift right
- ltlt logical shift left
22Suggested Pseudo Code Syntax
- Assignment operators
- set equal to
- op assign operator shorthand
- where op is one of
- - / ltlt gtgt
- Precedence operators
- ( )
- Indirect addressing
- _at_
23Suggested Pseudo Code Syntax
- Operator Precedence
- ( )
- _at_
- /
- -
- ltlt gtgt
- lt lt gt gt
- !
-
-
-
-
-
- - / etc
24Suggested Pseudo Code Syntax
- Structures
- Statement
- do something
- Statement block
- BEGIN
- statement
- statement
- END
- WHILE/DO
- WHILE condition DO
- statement
25Suggested Pseudo Code Syntax
- REPEAT/UNTIL
- REPEAT
- statement
- UNTIL condition
- IF/THEN/ELSE
- IF condition
- THEN statement 1
- (ELSE statement 2)
CASE/OF CASE expression OF 1 statement
1 2 statement 2 3 statement
3 . . n statement n default END
26Assembly Language Programming
- Labels
- Use labels that are descriptive of the
destination they represent - Comments
- Use comments wherever possible
- Comment conditional jump instructions using a
question similar to the flowchart - Comment Blocks
- At the beginning of each subroutine
- name of the sub, operations, entry conditions,
exit conditions, name of other subroutines used,
name of registers affected, etc.
27INLINE SUBROUTINE EXAMPLE
INLINE
INPUT LINE OF CHARACTERS LINE MUST END WITH
ltCRgt MAXIMUM LENGTH 31 CHARACTERS INCLUDE
ltCRgt ENTER NO CONDITIONS EXIT ASCII CODES
IN INTERNAL DATA RAM 0 STORED AT END OF
LINE USES INCHAR, OUTCHR
INLINE PUSH 00H SAVE R0 ON
STACK PUSH 07H SAVE R7 ON STACK PUSH ACC S
AVE ACCUMULATOR MOV R0,60H SET UP BUFFER AT
60H MOV R7,31 MAX LENGTH OF
LINE STMENT ACALL INCHAR INPUT A
CHARACTER ACALL OUTCHR ECHO TO
CONSOLE MOV _at_R0,A STORE IN BUFFER
28INLINE SUBROUTINE EXAMPLE
INLINE PUSH 00H SAVE R0 ON
STACK PUSH 07H SAVE R7 PUSH ACC SAVE
ACCUMULATOR MOV R0,60H SET UP BUFFER AT
60H MOV R7,31 MAX LENGTH OF
LINE STMENT ACALL INCHAR INPUT A
CHARACTER ACALL OUTCHR ECHO TO
CONSOLE MOV _at_R0,A STORE IN BUFFER INC R0 I
NC BUFFER POINTER DEC R7 DEC LENGTH
COUNTER CJNE A,0DH, SKIP IS CHAR
ltCRgt? SJMP EXIT YES, EXIT SKIP CJNE R7,0,ST
MENT NO, GET ANOTHER CHAR EXIT MOV _at_R0,0 END
WITH NULL CHAR POP ACC RESTORE REGISTERS
FROM POP 07H STACK POP 00H RET
29INLINE SUBROUTINE EXAMPLE
INCHR - INput CHaRacter from serial port
enter no condition exit ASCII code in ACC.0
to ACC.6 ACC.7 cleared ctrl-C aborts to
prompt
INCHR JB X13_BIT,IN1 if x13 installed, use
interrupt flag RI JNB r_flag, wait for
receive_flag to be set CLR ET1 begin critical
section CLR r_flag gtgtgt clear receive_flag
and MOV A,r_buff gtgtgt read receive_buffer SETB
ET1 end critical section SJMP IN2 if
x13 is not installed, test RI flag IN1 JNB RI,
wait for receive interrupt RI CLR RI clear RI
flag MOV A,SBUF done! IN2 CLR ACC.7 clear
parity bit (o error checking) CJNE A,ETX,
IN3 if ctrl-C, JMP GETCMD warm start IN3 RET
30INLINE SUBROUTINE EXAMPLE
OUTCHR - OUTput CHaRacter to serial port with odd
parity added in ACC.7 enter ASCII code in
ACC exit. Character written to SBUF ltCRgt
sent as ltCRgtltLFgt all registers
intact
RSEG EPROM OUTCHR PUSH A NL MOV C,P if
x13 installed, use interrupt flag RI CPL C add
odd parity MOV ACC.7,C JB X13_BIT,OUT1 if
x13 installed, use interrupt JNB t_flag, wait
for transmitter ready CLR ET1 begin critical
section CLR t_flag gtgtgt clear flag and
MOV t_buff,A gtgtgt load data to
transmit SETB ET1 end critical section
SJMP OUT2
31INLINE SUBROUTINE EXAMPLE
RSEG EPROM OUTCHR PUSH A NL MOV C,P CPL C
add odd parity MOV ACC.7,C JB X13_BIT,OUT1
if x13 installed, use interrupt JNB t_flag, wa
it for transmitter ready CLR ET1 begin
critical section CLR t_flag gtgtgt clear flag
and MOV t_buff,A gtgtgt load data to
transmit SETB ET1 end critical section
SJMP OUT2 if x13 is not installed, test
TI flag OUT1 JNB TI, wait for transmitter
ready CLR TI clear TI flag MOV SBUF,A done!
OUT2 CLR ACC.7 remove parity bit CJNE A,CR,
OUT3 if ltCRgt? MOV A,LF yes, add ltLFgt and
send it JMP NL warm start OUT3 POP A no
restore A and JNB p_bit,OUT4 check if send to
print CALL PCHAR yes, send to printer
OUT4 RET no, done and return
32Assembly Language Programming
- Saving Registers on the Stack
- Especially for nested subroutine calls, and
building complex programs using subroutine
building blocks. Avoid changing the working
registers when returns. - The Use of Equates
- Defining constants with equates makes programs
easier o read and maintain. - The Use of Subroutines
- Divide and Conquer subdivide large and complex
operations into small and simple operations.
These small and simple operations are programmed
as subroutines and used as building blocks of the
large complex program.
33OUTSTR SUBROUTINE EXAMPLE
OUTCHR
Enter
OUTSTR
Enter
Add odd parity To character
Get a char From string
TX buffer empty?
No
Char ! 0?
Yes
Clear flag
No
Yes
Exit
OUTCHR
Write character To transmitter
Clear parity bit
Inc pointer
Exit
34OUTSTR SUBROUTINE EXAMPLE
Pseudo code for OUTCHR OUTCHR (char) put odd
parity in bit 7 REPEAT test transmit
buffer UNTIL buffer empty clear transmit
buffer empty flag move char to transmit
buffer clear parity bit RETURN () Pseudo
code for OUTSTR OUTSTR (pointer) WHILE (char
_at_pointer) ! 0 BEGIN OUTCHR(char) increment
pointer END RETURN()
35OUTSTR SUBROUTINE EXAMPLE
OUTCHR O
UTPUT A CHAR IN ACC W. ODD PARITY VIA SERIAL
PORT ENTER NO CONDITION, ASCII CHAR IN
ACC EXIT ASCII CODE W. ODD PARITY SENT OUT
ACC.70
OUT
CHR MOV C,P PUT PARITY BIT IN C
FLAG CPL C CHANGE TO ODD PARITY MOV ACC.7,C
ADD TO CHAR AGAIN JNB TI,AGAIN TX
EMPTY? CLR TI YES, CLEAR FLAG
AND MOV SBUF,A SEND OUT CHARACTER CLR ACC.7
STRIP OFF PARITY BIT AND RET RETURN
USES OUTCHR O
UTSTR MOV A,_at_DPTR GET CHARACTER JZ EXIT IF
0, DONE AND EXIT CALL OUTCHR OTHERWISE SEND
IT INC DPTR INC POINTER SJMP OUTSTR EXIT R
ET
36Assembly Language Programming
- Program Organization
- Equates
- Initialization instructions
- Main body of program
- Subroutines
- Data constant definitions (DB and DW)
- RAM data locations defined using the DS directive
37Tools Techniques for Program Development
The Development Cycle
38Specifying Software
- User interface how the user will interact with
and control the system - Detail system operations below the user level
independent of user interface - Modularized system function with inter-module
communication - Interrupt driven, ISR, time-critical subroutine
39Designing Software
Editing and Translation
- Assemble time errors checking syntax errors only
40Preliminary Testing
- Run time errors will not appear until the program
is executed by a simulator or in the target
system. - Debugger a system program that executes a user
program for the purpose of finding run-time
errors. - Debugger can set breakpoints, single-stepping,
examine and modify registers, memories, etc.
during program execution
41Hardware Development
- Specifying hardware assign quantitative data to
system functions, physical size/weight, CPU
speed, memory, I/O ports, optional features, etc. - Designing Hardware
- breadboarding,
- wire-wrapping,
- PCB layout
- practice makes perfect
42Hardware Development
- Preliminary Testing
- Visual checks before power applied
- Continuity checks ohmmeter check each
connecting wire, IC pin to IC pin - DC measurements no ICs, DC voltages varied
- AC measurements IC installed, verify clock
signals, and so on - Functionality Testing drive RESET w a low f (1
kHz) square wave, write test software or monitor
program to debug each function of the hardware
board
43Detailed Steps in the Development Cycle
44Integration and Verification
- Hardware Software Integration test
- Software Simulation simulator
- Hardware Emulation hardware emulator or
in-circuit emulator (ICE) - Execution from RAM effective simple testing
for software in the target system
45Intel Hexadecimal Format
Field Bytes Description
Record mark 1 indicates start-of-record
Record length 2 Number of data bytes in record
Load address 4 Start address for data bytes
Record type 2 00 data record 01 end record
Data bytes 0-16 data
Checksum 2 Sum of all bytes in record checksum 0
46Intel Hexadecimal Format
47Integration and Verification
- Executing from EPROM firmware burnt in EPROM
using EPROM writer - Executing from on-chip flash (8952), EPROM
(8752), EEPROM, (OTP, MTP) using Universal
Programmer/Writer - The Factory Mask ROM for mass production
- Executing from SRAM downloaded from host into the
SBC-51
48The Development Environment