Bài giảng Vi xử lý - Chương 3: Vi điều khiển. Họ vi điều khiển 8051 (Phần 7)
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
eudo Code SyntaxOperator Precedence: ( ) ~ @ * / % + - > >= == != & ^ | && || = += -= *= /= etc2021/10/2423T. L. Jong, Dept. of E.E., NTHUSuggested Pseudo Code SyntaxStructures: Statement: [do something] Statement block: BEGIN [statement] [statement] END WHILE/DO: WHILE [condition] DO [statement]2021/10/2424T. L. Jong, Dept. of E.E., NTHUSuggested Pseudo Code SyntaxREPEAT/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] END2021/10/2425T. L. Jong, Dept. of E.E., NTHUAssembly Language ProgrammingLabels Use labels that are descriptive of the destination they representComments Use comments wherever possible Comment conditional jump instructions using a question similar to the flowchartComment 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.2021/10/2426T. L. Jong, Dept. of E.E., NTHU;***************************************************************************************;;INLINE INPUT LINE OF CHARACTERS; LINE MUST END WITH ; MAXIMUM LENGTH 31 CHARACTERS INCLUDE ;;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 LINESTMENT: ACALL INCHAR ;INPUT A CHARACTER ACALL OUTCHR ;ECHO TO CONSOLE MOV @R0,A ;STORE IN BUFFERINLINE SUBROUTINE EXAMPLE2021/10/2427T. L. Jong, Dept. of E.E., NTHUINLINE: 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 LINESTMENT: 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 = ? SJMP EXIT ;YES, EXITSKIP: CJNE R7,#0,STMENT ;NO, GET ANOTHER CHAREXIT: MOV @R0,#0 ;END WITH NULL CHAR POP ACC ;RESTORE REGISTERS FROM POP 07H ;STACK POP 00H RETINLINE SUBROUTINE EXAMPLE2021/10/2428T. L. Jong, Dept. of E.E., NTHU;********************************************************************************************; 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 ;>>> 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 flagIN1: 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 startIN3: RETINLINE SUBROUTINE EXAMPLE2021/10/2429T. L. Jong, Dept. of E.E., NTHU;********************************************************************************************; OUTCHR - OUTput CHaRacter to serial port with odd parity added in; ACC.7; enter: ASCII code in ACC; exit:. Character written to SBUF; sent as ; all ; registers intact;******************************************************************************************** RSEG EPROMOUTCHR: 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 SJMP OUT2 ;INLINE SUBROUTINE EXAMPLE2021/10/2430T. L. Jong, Dept. of E.E., NTHU RSEG EPROMOUTCHR: 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,$ ;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 SJMP OUT2 ; ;if x13 is not installed, test TI flagOUT1: 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 ? MOV A,#LF ;yes, add and send it JMP NL ;warm startOUT3: 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 returnINLINE SUBROUTINE EXAMPLE2021/10/2431T. L. Jong, Dept. of E.E., NTHUAssembly Language ProgrammingSaving 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.2021/10/2432T. L. Jong, Dept. of E.E., NTHUEnterChar !=0?Get a charFrom stringExitYesInc pointerNoOUTCHREnterTX bufferempty?Add odd parityTo characterExitYesWrite characterTo transmitterNoClear flagClear parity bitOUTSTROUTCHROUTSTR SUBROUTINE EXAMPLE2021/10/2433T. L. Jong, Dept. of E.E., NTHUPseudo code for OUTCHROUTCHR (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 OUTSTROUTSTR (pointer) WHILE [(char = @pointer) != 0] BEGIN OUTCHR(char) [increment pointer] ENDRETURN()OUTSTR SUBROUTINE EXAMPLE2021/10/2434T. L. Jong, Dept. of E.E., NTHU;****************************************************************************************;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 CHARAGAIN: 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 OUTSTREXIT: RETOUTSTR SUBROUTINE EXAMPLE2021/10/2435T. L. Jong, Dept. of E.E., NTHUAssembly Language ProgrammingProgram OrganizationEquatesInitialization instructionsMain body of programSubroutinesData constant definitions (DB and DW)RAM data locations defined using the DS directive2021/10/2436T. L. Jong, Dept. of E.E., NTHUTools & Techniques for Program DevelopmentThe Development Cycle2021/10/2437T. L. Jong, Dept. of E.E., NTHUSpecifying SoftwareUser interface: how the user will interact with and control the systemDetail system operations below the user level – independent of user interfaceModularized system function with inter-module communicationInterrupt driven, ISR, time-critical subroutine2021/10/2438T. L. Jong, Dept. of E.E., NTHUDesigning SoftwareFlowchartsPseudo codeEditing and TranslationAssemble time errors checking: syntax errors only2021/10/2439T. L. Jong, Dept. of E.E., NTHUPreliminary TestingRun 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 execution2021/10/2440T. L. Jong, Dept. of E.E., NTHUHardware DevelopmentSpecifying 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 layoutpractice makes perfect2021/10/2441T. L. Jong, Dept. of E.E., NTHUHardware DevelopmentPreliminary Testing: Visual checks – before power appliedContinuity checks – ohmmeter check each connecting wire, IC pin to IC pinDC measurements – no ICs, DC voltages variedAC measurements – IC installed, verify clock signals, and so onFunctionality Testing – drive RESET w a low f (1 kHz) square wave, write test software or monitor program to debug each function of the hardware board2021/10/2442T. L. Jong, Dept. of E.E., NTHUDetailed Steps in the Development Cycle2021/10/2443T. L. Jong, Dept. of E.E., NTHUIntegration and VerificationHardware Software Integration testSoftware Simulation – simulatorHardware Emulation – hardware emulator or in-circuit emulator (ICE)Execution from RAM – effective & simple testing for software in the target system2021/10/2444T. L. Jong, Dept. of E.E., NTHUIntel Hexadecimal FormatFieldBytesDescriptionRecord mark1“:” indicates start-of-recordRecord length2Number of data bytes in recordLoad address4Start address for data bytesRecord type200 = data record; 01 = end recordData bytes0-16dataChecksum2Sum of all bytes in record + checksum = 02021/10/2445T. L. Jong, Dept. of E.E., NTHUIntel Hexadecimal Format2021/10/2446T. L. Jong, Dept. of E.E., NTHUIntegration and VerificationExecuting from EPROM – firmware burnt in EPROM using EPROM writerExecuting from on-chip flash (8952), EPROM (8752), EEPROM, (OTP, MTP) using Universal Programmer/WriterThe Factory Mask ROM – for mass productionExecuting from SRAM downloaded from host into the SBC-512021/10/2447T. L. Jong, Dept. of E.E., NTHUThe Development Environment2021/10/2448T. L. Jong, Dept. of E.E., NTHU
File đính kèm:
- bai_giang_vi_xu_ly_chuong_3_vi_dieu_khien_ho_vi_dieu_khien_8.ppt
- VXL-Ch03-8051-3.9 Cau truc va TK chuong trinh.pdf