Hardware Summary of 8051 2011/12/7 T L Jong, Dept of E E , NTHU 1 System & Program System & Program Developments of 8051Developments of 8051 Program Structure and Design Introduction Advantages and Di[.]
Trang 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
Trang 2 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
Trang 3Program flow arrow
Trang 4Pseudo Code
combination with informal language
Trang 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
Trang 6Advantages & Disadvantages of
Structured Programming
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
Trang 7The Three Structures
Trang 8The WHILE/DO Statement
JMP ENTER EXIT: (continue)
Trang 9WHILE/DO: SUM Subroutine
[sum (A) = 0]
WHILE [length (R7) > 0] DO BEGIN
[sum = sum + @pointer (R0)]
[increment pointer]
[decrement length]
END
[sum (A) = 0]
WHILE [length (R7) > 0] DO BEGIN
[sum = sum + @pointer (R0) ] [increment pointer]
DEC R7 JMP LOOP:
Trang 11The REPEAT/UNTIL Statement
Statement
Exit
Yes No
REPEAT [statement]
ENTER: (statement)
JNC ENTER EXIT: (continue)
Trang 12The REPEAT/UNTIL Statement
REPEAT [ACC = @pointer]
Inc pointer
Char = 0?
Yes No
Trang 13The IF/THEN/ELSE Statement
Statement 1
Exit Statement 2
Yes No
Trang 14The IF/THEN/ELSE Statement
[input character]
IF [input character == graphic]
THEN [echo character]
ELSE [echo ‘.’]
Enter
Graphic char?
JMP EXIT STMENT2: MOV A,#’.’
ACALL OUTCH EXIT: (continue)
Echo “.”
Yes No
Input character
(loosely structured; 10 bytes)
ACALL INCH ACALL ISGRPH
JC SKIP MOV A,#’.’
SKIP: ACALL OUTCH
(continue)
Trang 15The CASE Statement
CASE [expression] OF 0: [statement 0]
1: [statement 1]
2: [statement 2]
N: [statement 0]
Statement 1
expression 2?
Statement 2
expression n?
Statement n default
No
Trang 16The CASE Statement
Trang 17The CASE Statement
8051 code:
(closely structured)
ACALL INCH CJNE A,#’0’,SKIP1 ACT0:
JMP EXIT SKIP1: CJNE A,#’1’,SKIP2
ACT1:
JMP EXIT SKIP2: CJNE A,#’2’,SKIP3
ACT2:
JMP EXIT SKIP3: CJNE A,#’3’,EXIT
RL A MOV DPTR,#TABLE JMP @A+DPTR TABLE: AJMP ACT0
AJMP ACT1 AJMP ACT2 ACT3:
JMP EXIT ACT0:
JMP EXIT ACT1:
JMP EXIT ACT2:
EXIT: (continue)
Trang 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.
Trang 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 (@) for indirect
addressing
Trang 20Suggested Pseudo Code Syntax
Trang 21Suggested Pseudo Code Syntax
Relational operators:
== true if values equal to each other
!= true if values not equal to each other
< true if first value less than second
<= true if first value <= second
> true if first value > second
>= true if first value >= second
&& true if both values are true
|| true if either value is true
Bitwise Logical operators:
& logical AND
| logical OR
^ logical XOR
~ logical NOT (one’s complement)
>> logical shift right
<< logical shift left
Trang 22Suggested Pseudo Code Syntax
Assignment operators:
= set equal to
op = assign operator shorthand
where “op” is one of + - * / % << >> & ^ | Precedence operators:
( )
Indirect addressing:
@
Trang 23Suggested Pseudo Code Syntax
Trang 24Suggested Pseudo Code Syntax
Trang 25Suggested Pseudo Code Syntax
END
Trang 26Assembly Language Programming
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.
Trang 27;
;INLINE INPUT LINE OF CHARACTERS
; LINE MUST END WITH <CR>
; MAXIMUM LENGTH 31 CHARACTERS INCLUDE <CR>
;
;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 ;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 @R0,A ;STORE IN BUFFER
INLINE SUBROUTINE EXAMPLE
Trang 28INLINE: 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 @R0,A ;STORE IN BUFFER INC R0 ;INC BUFFER POINTER DEC R7 ;DEC LENGTH COUNTER CJNE A,#0DH, SKIP ;IS CHAR = <CR>?
SJMP EXIT ;YES, EXIT SKIP: CJNE R7,#0,STMENT ;NO, GET ANOTHER CHAR EXIT: MOV @R0,#0 ;END WITH NULL CHAR
POP ACC ;RESTORE REGISTERS FROM POP 07H ;STACK
POP 00H RET
INLINE SUBROUTINE EXAMPLE
Trang 29JNB r_flag,$ ;wait for receive_flag to be set CLR ET1 ;begin “critical section”
CLR r_flag ;>>> clear receive_flag and…
MOV A,r_buff ;>>> 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
INLINE SUBROUTINE EXAMPLE
Trang 30*
; OUTCHR - OUTput CHaRacter to serial port with odd parity added in
; ACC.7
; enter: ASCII code in ACC
; exit: Character written to SBUF; <CR> sent as <CR><LF>; 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 ;>>> clear flag and … MOV t_buff,A ;>>> load data to transmit SETB ET1 ;end critical section
INLINE SUBROUTINE EXAMPLE
Trang 31RSEG EPROM OUTCHR: PUSH A
CLR t_flag ;>>> clear flag and … MOV t_buff,A ;>>> load data to transmit SETB ET1 ;end critical section
;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 <CR>?
MOV A,#LF ;yes, add <LF> 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
INLINE SUBROUTINE EXAMPLE
Trang 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.
Trang 33Char !=
0?
Get a char From string
Add odd parity
OUTSTR SUBROUTINE EXAMPLE
Trang 34Pseudo code for OUTCHR
OUTCHR (char)
[put odd parity in bit 7]
REPEAT [test transmit buffer]
[clear transmit buffer empty flag]
[move char to transmit buffer]
[clear parity bit]
END RETURN()
OUTSTR SUBROUTINE EXAMPLE
Trang 35;OUTCHR: OUTPUT 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.7=0
;*************************************************************************************** OUTCHR: 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
;
OUTSTR: MOV A,@DPTR ;GET CHARACTER
JZ EXIT ;IF 0, DONE AND EXIT CALL OUTCHR ;OTHERWISE SEND IT INC DPTR ;INC POINTER
SJMP OUTSTR
OUTSTR SUBROUTINE EXAMPLE
Trang 36Assembly Language Programming
Data constant definitions (DB and DW)
RAM data locations defined using the DS
directive
Trang 37Tools & Techniques for
Program Development
The Development Cycle
Trang 38Specifying Software
interact with and control the system
user level – independent of user
Trang 39Designing Software
Editing and Translation
syntax errors only
Trang 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.
single-stepping , examine and modify
registers, memories, etc during
program execution
Trang 41Hardware Development
Specifying hardware: assign
quantitative data to system
functions, physical size/weight, CPU speed, memory, I/O ports, optional
Trang 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
Trang 43Detailed Steps in the Development Cycle
Trang 44Integration and Verification
emulator or in-circuit emulator (ICE)
simple testing for software in the
target system
Trang 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
Trang 46Intel Hexadecimal Format
Trang 47Integration and Verification
burnt in EPROM using EPROM writer
EPROM (8752), EEPROM, (OTP, MTP) using Universal Programmer/Writer
production
from host into the SBC-51
Trang 48The Development Environment