Microprocessing Systems - Chapter 3: Instruction Set. How to write a program - Lê Chí Thông
• 128 locations from address 00H to 7FH (256 locations for 8052)
• The content of one location is 8 bit.
• Register banks: address 00H to 1FH
• Bit Addressable RAM: address 20H to 2FH
• General purpose RAM: address from 30H to 7FH
t until R2=0 (10 times) MOV R5,A Lê Chí Thông 102 DJNZ – another example ĐH Bách Khoa TP.HCM Lê Chí Thông sites.google.com/site/chithong 52 Write a program to copy a block of 10 bytes from RAM location starting at 37h to RAM location starting at 59h. Lê Chí Thông 103 DJNZ – Your Turn! Write a program to copy a block of 10 bytes from RAM location starting at 37h to RAM location starting at 59h. Solution: MOV R0,#37h ; source pointer MOV R1,#59h ; dest pointer MOV R2,#10 ; counter L1: MOV A,@R0 MOV @R1,A INC R0 INC R1 DJNZ R2,L1 Lê Chí Thông 104 Solutions ĐH Bách Khoa TP.HCM Lê Chí Thông sites.google.com/site/chithong 53 Lê Chí Thông 105 Blinky Program P1.0 1 Delay P1.0 0 Delay schematic ORG 0 LOOP: SETB P1.0 ACALL DELAY CLR P1.0 ACALL DELAY SJMP LOOP DELAY: MOV R6,#200 DL1: MOV R7,#250 DJNZ R7,$ DJNZ R6,DL1 RET END (source) Waveform? Period? Frequency? Lê Chí Thông 106 Blinky Program P1.0 1 Delay P1.0 0 Delay ĐH Bách Khoa TP.HCM Lê Chí Thông sites.google.com/site/chithong 54 ORG 0 LOOP: SETB P1.0 ACALL DELAY CLR P1.0 ACALL DELAY SJMP LOOP DELAY: MOV R6,#200 DL1: MOV R7,#250 1MC x 1 DJNZ R7,$ 2 MC x 250 DJNZ R6,DL1 RET END Lê Chí Thông 107 Blinky Program 501 MC ORG 0 LOOP: SETB P1.0 ACALL DELAY CLR P1.0 ACALL DELAY SJMP LOOP DELAY: MOV R6,#200 1 MC x 1 DL1: MOV R7,#250 DJNZ R7,$ DJNZ R6,DL1 2 MC x 200 RET END Lê Chí Thông 108 Blinky Program 501 MC x 200 100,601 MC ĐH Bách Khoa TP.HCM Lê Chí Thông sites.google.com/site/chithong 55 ORG 0 LOOP: SETB P1.0 ACALL DELAY CLR P1.0 ACALL DELAY SJMP LOOP DELAY: MOV R6,#200 DL1: MOV R7,#250 DJNZ R7,$ DJNZ R6,DL1 RET 2 MC END Lê Chí Thông 109 Blinky Program 100,601 MC 100,603 MC ORG 0 LOOP: SETB P1.0 1 MC ACALL DELAY 100,063 + 2 MC CLR P1.0 1 MC ACALL DELAY 100,063 + 2 MC SJMP LOOP 2 MC DELAY: MOV R6,#200 DL1: MOV R7,#250 DJNZ R7,$ DJNZ R6,DL1 RET END tH = 100,063 + 2 + 1 = 100,066 MC tL = 100,063 + 2 + 2 + 1 = 100,068 MC Lê Chí Thông 110 Blinky Program ĐH Bách Khoa TP.HCM Lê Chí Thông sites.google.com/site/chithong 56 If using 12 MHz crystal, 1 MC = 1 μs tH = 100,066 MC = 100,066 μs tL = 100,068 MC = 100,068 μs T = tH + tL = 200,134 μs f = 1/T = 4.99 Hz Lê Chí Thông 111 Blinky Program tH tL T P1.0 1 Delay tH P1.0 0 Delay tL ORG 0 LOOP: SETB P1.0 ACALL DELAY CLR P1.0 ACALL DELAY SJMP LOOP DELAY: MOV R6,#200 DL1: MOV R7,#250 DJNZ R7,$ DJNZ R6,DL1 RET END tH = tL ≈ tDELAY ≈ 200 x 250 x 2 MC = 100,000 MC = 100,000 μs T ≈ 200,000 μs f ≈ 5 Hz Lê Chí Thông 112 Blinky Program - Estimating P1.0 1 Delay tH P1.0 0 Delay tL ĐH Bách Khoa TP.HCM Lê Chí Thông sites.google.com/site/chithong 57 ORG 0 LOOP: CPL P1.0 ACALL DELAY SJMP LOOP DELAY: MOV R6,#200 DL1: MOV R7,#250 DJNZ R7,$ DJNZ R6,DL1 RET END Lê Chí Thông 113 Blinky Program – Alternative Method P1.0 NOT (P1.0) Delay P1.0 1 Delay P1.0 0 Delay Write a program that creates a 10-KHz square wave at pin P1.3. Assume that crystal is 24 MHz. Lê Chí Thông 114 10-kHz square wave ĐH Bách Khoa TP.HCM Lê Chí Thông sites.google.com/site/chithong 58 Write a program that creates a 10-KHz square wave at pin P1.3. Assume that crystal is 24 MHz. Lê Chí Thông 115 10-kHz square wave tH tL T P1.3 1 Delay tH P1.3 0 Delay tL P1.3 NOT (P1.3) Delay If tH = tL Write a program that creates a 10-KHz square wave at pin P1.3. Assume that crystal is 24 MHz. ORG 0000H lap: CPL P1.3 ACALL delay SJMP lap delay: MOV R4,#50 DJNZ R4,$ RET END Lê Chí Thông 116 10-kHz square wave P1.3 NOT (P1.3) Delay ĐH Bách Khoa TP.HCM Lê Chí Thông sites.google.com/site/chithong 59 Write a program that creates a 10-KHz square wave with duty cycle 30% at pin P1.3. Assume that crystal is 24 MHz. Lê Chí Thông 117 10-kHz square wave, duty cycle 30% tH tL T P1.3 1 Delay tH P1.3 0 Delay tL Write a program that creates a 10-KHz square wave with duty cycle 30% at pin P1.3. Assume that crystal is 24 MHz. ORG 0000H lap: SETB P1.3 ACALL delay1 CLR P1.3 ACALL delay2 SJMP lap Delay1: MOV R4,#30 DJNZ R4,$ RET Delay2: MOV R4,#70 DJNZ R4,$ RET END Lê Chí Thông 118 10-kHz square wave, duty cycle 30% P1.3 1 Delay tH P1.3 0 Delay tL ĐH Bách Khoa TP.HCM Lê Chí Thông sites.google.com/site/chithong 60 Write a program that creates a 100-kHz square wave at pin P1.1. Assume that crystal is 12 MHz. Lê Chí Thông 119 100-kHz square wave Write a program that creates a 100-kHz square wave at pin P1.1. Assume that crystal is 12 MHz. ORG 0000H lap: CPL P1.1 NOP NOP SJMP lap END Lê Chí Thông 120 100-kHz square wave ĐH Bách Khoa TP.HCM Lê Chí Thông sites.google.com/site/chithong 61 Write a program that creates a 100-kHz square wave with duty cycle 40% at pin P1.2. Assume that crystal is 12 MHz. Lê Chí Thông 121 100-kHz square wave, duty cycle 40% Write a program that creates a 100-kHz square wave with duty cycle 40% at pin P1.2. Assume that crystal is 12 MHz. ORG 0000H lap: SETB P1.2 NOP NOP NOP CLR P1.2 NOP NOP NOP SJMP lap END Lê Chí Thông 122 100-kHz square wave, duty cycle 40% ĐH Bách Khoa TP.HCM Lê Chí Thông sites.google.com/site/chithong 62 CJNE A,#05H,Skip (Statement 1) Skip: (Continue) Lê Chí Thông 123 CJNE – Equal/Not Equal (1) A = 05H? N Y Statement 1 Ex: Write a program to write 40H to internal RAM from location 30H to location 36H. (source) ORG 0000H MOV R1,#30H;Address=30H Again: MOV @R1,#40H INC R1 CJNE R1,#37H,Again END Lê Chí Thông 124 An Example Addr 30H (Addr) 40H AddrAddr+1 Addr=37H? N Y ĐH Bách Khoa TP.HCM Lê Chí Thông sites.google.com/site/chithong 63 CJNE A,#05H,Not_Eq (Statement 1) SJMP Next Not_Eq: (Statement 2) Next: (Continue) Lê Chí Thông 125 CJNE – Equal/Not Equal (2) A = 05H? N Y Statement 1Statement 2 CJNE A,#05H,Next Next: JC LessThan (Statement 1) LessThan: (Continue) Lê Chí Thông 126 CJNE – Greater Than or Equal/Less Than (1) A ≥ 05H? N Y Statement 1 CJNE A,#05H,$+3 JC LessThan (Statement 1) LessThan: (Continue) ĐH Bách Khoa TP.HCM Lê Chí Thông sites.google.com/site/chithong 64 CJNE A,#05H,Next Next: JNC GT_Eq SJMP Continue GT_Eq: (Statement 1) Continue: (Continue) Lê Chí Thông 127 CJNE – Greater Than or Equal/Less Than (1) A ≥ 05H? N Y Statement 1 CJNE A,#05H,$+3 JNC GT_Eq SJMP Continue GT_Eq: (Statement 1) Continue: (Continue) Examine the content of A, if 5 ≤ A ≤ 10 then output A to Port 1; if not, output A to Port 2. ORG 0 CJNE A,#5,$+3 JC PORT2 CJNE A,#11,$+3 JNC PORT2 MOV P1,A SJMP DONE PORT2:MOV P2,A DONE: NOP END 128Lê Chí Thông An Example ĐH Bách Khoa TP.HCM Lê Chí Thông sites.google.com/site/chithong 65 CJNE A,#05H,$+3 JNC GT_Eq (Statement 2) SJMP Next GT_Eq: (Statement 1) Next: (Continue) Lê Chí Thông 129 CJNE – Greater Than or Equal/Less Than (2) A ≥ 05H? N Y Statement 1Statement 2 Given a 20-byte string in internal RAM, starting at address 40H. Write a program that output even numbers to Port 2. 130Lê Chí Thông Bit Testing ĐH Bách Khoa TP.HCM Lê Chí Thông sites.google.com/site/chithong 66 Given a 20-byte string in internal RAM, starting at address 40H. Write a program that output even numbers to Port 2. ORG 0 MOV R4,#20 ; Number of loops MOV R0,#40H ; Address pointer LOOP: MOV A,@R0 ; Read data from internal RAM to A JB ACC.0,NEXT ; skip if odd number MOV P2,A ; Output to Port 2 if even number NEXT: INC R0 DJNZ R4,LOOP END 131Lê Chí Thông Bit Testing Given a 20-byte string in external RAM, starting at address 4000H. Write a program that output odd numbers to Port 2. 132Lê Chí Thông Your Turn! ĐH Bách Khoa TP.HCM Lê Chí Thông sites.google.com/site/chithong 67 Given a 20-byte string in external RAM, starting at address 4000H. Write a program that output odd numbers to Port 2. ORG 0 MOV R4,#20 ; Number of loops MOV DPTR,#4000H ; Address of external RAM LOOP: MOVX A,@DPTR ; Read data from external RAM to A JNB ACC.0,NEXT; skip if even number MOV P2,A ; Output to Port 2 if odd number NEXT: INC DPTR DJNZ R4,LOOP END 133Lê Chí Thông Solutions Given a 100-byte unsigned number string in external RAM at address starting from 0100H. Write a program that sends positive numbers to Port 1 and negative numbers to Port 2. Hint: - A positive number has MSB = 0. - A negative number has MSB = 1. - Use JB / JNB instruction -- Lê Chí Thông Bit Testing 134 ĐH Bách Khoa TP.HCM Lê Chí Thông sites.google.com/site/chithong 68 Given a 100-byte unsigned number string in external RAM at address starting from 0100H. Write a program that sends positive numbers to Port 1 and negative numbers to Port 2. ORG 0000H MOV DPTR,#0100H MOV R4,#100 loop: MOVX A,@DPTR JNB ACC.7,positive MOV P2,A SJMP next positive: MOV P1,A next: INC DPTR DJNZ R4,loop END Lê Chí Thông Bit Testing 135 Lê Chí Thông Bit Testing LOOP: MOV C,P1.0 JNB P1.1,SKIP CPL C SKIP: MOV P1.2,C SJMP LOOP XOR P1.0 P1.1 P1.2 136 ĐH Bách Khoa TP.HCM Lê Chí Thông sites.google.com/site/chithong 69 Lê Chí Thông Example Problem A 4-bit DIP switch and a common-anode 7-segment LED are connected to an 8051 as shown in the following figure. Write a program that continually reads a 4-bit code from the DIP switch and updates the LEDs to display the appropriate hexadecimal character. For example, if the code 1100B is read, the hexadecimal character “C” should appear, thus, segments a through g respectively should be ON, OFF, OFF, ON, ON, ON, and OFF. Note that setting an 8051 port pin to “1” turns the corresponding segment “ON”. 137 138 References Lê Chí Thông • I. Scott MacKenzie , The 8051 Microcontroller, 2nd Edition, Prentice-Hall, 1995 • Kenneth J. Ayala, The 8051 Microcontroller: Architecture, Programming, and Applications, West Publishing Company • hsabaghianb@kashanu.ac.jr , Lecture notes
File đính kèm:
- microprocessing_systems_chapter_3_instruction_set_how_to_wri.pdf