单片机原理及应用--第4章 80C51单片机汇编语言程序设计.doc_第1页
单片机原理及应用--第4章 80C51单片机汇编语言程序设计.doc_第2页
单片机原理及应用--第4章 80C51单片机汇编语言程序设计.doc_第3页
单片机原理及应用--第4章 80C51单片机汇编语言程序设计.doc_第4页
单片机原理及应用--第4章 80C51单片机汇编语言程序设计.doc_第5页
免费预览已结束,剩余2页可下载查看

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

Chapter 4 Program Design of Assemble Language主讲教师:黄英Contents:4.1 Introduction of Assemble Language24.1.1 Features24.1.2 Format 24.2 Basic structures24.2.1 Sequential program24.2.2 Branching program34.2.2.1 Single branching program34.2.2.2. Multi-branching program111By using some CJNE instructions112By using “address table”133By using “jump instruction table”154By using stack operation184.2.3 Circular Structure234.3 Delay time Program244.3.1 Single-cycle delay time program244.3.2 Long delay time program (Multi-cycle)254.3.3 Adjusting delay time range254.3.4 Get different timing with basic delay program254.4 Pseudoinstruction254.5 How to edit and assemble the source program25第7页,共7页4.1 Introduction of Assemble Language4.1.1 Features1 Produces the optimized program.2 Programmer must be familiar with hardware.3 Manage and control hardware directly.4 Not universal, cant be transplanted. 4.1.2 Format (Detailed information: P77): ; 4.2 Basic structures3 types: Sequential Structure Branching Structure Circular Structure4.2.1 Sequential program For example: 3-byte unsigned constants additionAssumed that: 50H52H cells of internal RAM have three augends respectively (beginning with high bytes), 53H55H cells of internal RAM have three addends respectively(beginning with high bytes).Requirement: Design a program to put the addition result into 50H52H cells (beginning with high bytes), and the carry bit into 20H bit of bit addressable area.DECR0 (50H/51H/52H)= 11H/22H/33HDECR1 (53H/54H/55H)= 77H/88H/99H MOV A, R0ADDC A, R1 112233HMOVR0, A +778899H CLRAADDC A, #00H ? MOVR0, #20H ?MOVR0, A ?MOVR0, #52HMOVR1, #55HMOV A, R0ADDA, R1MOVR0, ADECR0DECR1 MOVA, R0ADDC A, R1MOVR0, A4.2.2 Branching program4.2.2.1 Single branching program Using jump instructions:P67-68: JZ, JNZ, CJNE, DJNZ(条件转移指令) P73-74: JC, JNC, JB, JNB, JBC(位控制转移指令)For example: Compare the valuesAssumed that: There are three continuous cells(ST1, ST2, ST3)in the external RAM where ST1 and ST2 have 8-bit unsigned binary constants respectively.Requirement: Design a program to find the bigger constant, and put it into ST3 cell.START:CLR C; clear the carry bitMOV DPTR, #ST1; set the data pointerMOVX A, DPTRMOV R2, A; put the 1st constant into R2INC DPTRMOVX A, DPTR; put the 2st constant into ASUBB A, R2; A = A R2= 2st - 1st JNC BIG1; if no carry, then the 2st constant is bigger.XCH A, R2; else 1st constant is bigger, and put into ABIG0:INC DPTR; let DPTR points to the result cell.MOVX DPTR, A; save the bigger constant. RETBIG1:MOVX A, DPTR; put the 2st constant into ASJMP BIG04.2.2.2. Multi-branching program Instruction Set has no multi-branching instruction. 4 methods to realize multi-branching: 1By using some CJNE instructions2By using “address table” 3By using “jump instruction table ” 4By using stack operation1By using some CJNE instructionsCJNE A, #data, rel; put the branching number into A.For example: temperature control systemAssumed that: There is a temperature control system, which current temperature value(Ta) is saved in A. The lower value limit (T54)of temperature control is saved in the internal RAM cell 54H, The upper value limit(T55)of temperature control is saved in the internal RAM cell 55H. Requirement: Design a program to control temperature under the conditions:If Ta T55, then executes the temperature-down processing subprogram(JW);If Ta T55, then jump to JW(降温)CJNE A, 54H, LOOP2; if TaT54(lower value limit),then jump to LOOP2AJMP FH; TaT54,jump to main program(FH) LOOP2:JC SW; if CY1,Ta T54, then jump to SW(升温) FH: RET2By using “offset address table” Step: (1)Set up a “offset address table”(2)Using instruction: JMP A+DPTRwhere: Offset table base address DPTR Offset A(offset value =BR0/BR1/BR2/BR3 - BRTAB)For example: Assumed that: There are four branching programs, which function is to fetch data from internal RAM, external RAM(lower 256B), external RAM(4KB), and external RAM 64KB respectively to A. R0 saves low 8-bit address, R1 saves high 8-bit address, R3 saves branching number. BRTAB denotes the base address of offset table. BR0_BRTABBR3_BRTAB denote offset.Requirement: Design a program to realize multi-branching.R3: 00, 01, 02, 03MOV A, R3; put the branching number into AMOV DPTR, # BRTAB; let DPTR point to base address of offset tableMOVC A, A+DPTR; fetch the offset value. JMP A+DPTR; jump to the corresponding subroutine. (A is offset value, DPTR is the address of BRTAB); PC = A+DPTRBRTAB:DB BR0_BRTAB; offset value of BR0Offset table (offset value=BR0/BR1/BR2/BR3 - BRTAB) DB BR1_BRTAB; offset value of BR1DB BR2_BRTAB; offset value of BR2DB BR3_BRTAB; offset value of BR3BR0:MOV A, R0; 00 ; fetch data from internal RAM to A(R0 saves low 8-bit address)SJMP DONE; stopBR1:MOVX A, R0; 01; fetch data from external RAM(lower 256B)to A (R0 saves low 8-bit address)SJMP DONE; stopBR2:MOV A, R1; 02; put high address into A (R1 saves high 8-bit address)ANL A, #0FH; fetch low 4-bit from high address, and clear high 4-bit.ANL P2, #0F0H; fetch high 4-bit from P2 port, and clear low 4-bit of P2ORL P2, A; not change high 4-bit of P2, and send low 4-bit of P2MOVX A, R0; fetch data from external RAM(4KB)to A (R0 saves low 8-bit address)SJMP DONE; stopBR3: MOV DPL, R0; 03; put low 8-bit address of external RAM to DPL MOV DPH, R1; put high 8-bit address of external RAM to DPHMOVX A, DPTR; fetch data from external RAM(64KB)to ADONE:SJMP $; stop3By using “jump instruction table” For example: Assumed that: R3 saves branching number. BRTAB denotes the base address of jump instruction table.Requirement: Design a program to realize multi-branching.R3: 00, 01, 02, 03, MOV A, R3; put the branching number into ARL A; A = branching number 2MOV DPTR, # BRTAB; let DPTR point to base address of offset tableJMP A+DPTR; jump to the corresponding subroutine, PC = A+DPTRBRTAB:AJMP ROUT0; jump to ROUT0Jump instruction tableAJMP ROUT1; jump to ROUT1AJMP ROUT2; jump to ROUT2AJMP ROUT3; jump to ROUT3AJMP ROUTn; jump to ROUTnWhy “RL A” ?When R3 = 00, need jump to BRTAB + 0, so A = 00,When R3 = 01, need jump to BRTAB + 2, so A = 02,When R3 = 02, need jump to BRTAB + 4, so A = 04,When R3 = 03, need jump to BRTAB + 6, so A = 06.4By using stack operationSteps: (1)Put the 16-bit address into stack (2)Using RET instruction to get PC value from stackFor example: Assumed that: R3 saves branching number. BRTAB denotes the base address of jump instruction table.Requirement: Design a program to realize multi-branching.R3: 00, 01, 02, 03MOV DPTR, # BRTAB; let DPTR point to base address of offset tableMOV A, R3; put the branching number into ARL A; A = branching number 2MOV R1, A; R1 = 2R3INC A; A = 2R3 + 1MOVC A, A+DPTR; fetch low 8-bit address to APUSH ACC; and put low 8-bit address into Stack.MOV A, R1MOVC A, A+DPTR; fetch high 8-bit address to APUSH ACC; and put it into Stack.RET; set PC value from stack. Get high 8-bit address first, and then low 8-bit address.BRTAB:DW BR0; 16-bit address of branching subroutine 0 16-bit address table. Pay attention to the operation of DW!DW BR1; 16-bit address of branching subroutine 1DWBR2; 16-bit address of branching subroutine 2DWBR3; 16-bit address of branching subroutine 3DWBRn ; 16-bit address of branching subroutine nNote:BRTAB = 3000H16-bitaddressR3R1RL AINC A RET: ; PC158 SP, SP SP 1 ; PC70 SP, SP SP 1 PUSH direct ; SP SP + 1, (SP)(direct) so need to PUSH low 8-bit address first, and then PUSH high 8-bit address at last. 3000High 8-bit addressBR00013001Low 8-bit address3002High 8-bit addressBR11233003Low 8-bit address3004High 8-bit addressBR22453005Low 8-bit address3006High 8-bit addressBR33673007Low 8-bit address4.2.3 Circular StructureMethod: Using conditional jump instructions.For example: Assumed that: There is a character string that adopts “Enter” symbol as the ending flag. This string is saved in the internal RAM beginning with 40H cell.Requirement: Design a program to get the length of this string.MOV R2, #0FFH; set the initial value of the length counter. MOV R0, #3FH; set the initial value of the string pointer.LOOP:INC R2; R2 = 00H, 01H,INC R0; R0 = 40H, 41H, CJNE R0, #0DH, LOOPRET4.3 Delay time Program4.3.1 Single-cycle delay time programMOV R5, #TIME; 1LOOP: NOP; 1 NOP; 1 DJNZ R5, LOOP; 2If fosc = 6MHz, then machine cycle TM:So total delay time: Tmax =1(112)2usTIME ; TIME = 00H-FFH, 0-255, cycles 265 times =1(112)2us256 = 12048 = 2049us4.3.2 Long delay time program (Multi-cycle)MOVR5, #TIME1; 1LOOP2:MOVR4, #TIME2; 1LOOP1:NOP; 1NOP; 1DJNZR4, LOOP1; 2DJNZR5, LOOP2; 2RET; 2If fosc = 6MHz, then total delay time Tmax : Tmax = 1(1(112)2562)256) 2 ) 2us = 1+ 525828 = 525829us4.3.3 Adjusting delay time

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论