Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống
1
/ 138 trang
THÔNG TIN TÀI LIỆU
Thông tin cơ bản
Định dạng
Số trang
138
Dung lượng
3,27 MB
Nội dung
The 8051 Microcontroller Chapter Instruction Set How to write a program Lê Chí Thơng chithong@hcmut.edu.vn sites.google.com/site/chithong Ho Chi Minh City University of Technology Lê Chí Thơng CuuDuongThanCong.com https://fb.com/tailieudientucntt Instruction Set • 255 instructions 1-byte instructions: 139 2-byte instructions: 92 3-byte instructions: 24 • Summary instruction set (pdf) • Full instruction set (pdf) Lê Chí Thơng CuuDuongThanCong.com https://fb.com/tailieudientucntt The First Program First_program.asm ORG 0000H MOV P1,#0FH SETB P1.7 CLR P1.0 END First_program.hex Byte count Load address Type First_program.lst LOC OBJ LINE 0000 0000 75900F 0003 D297 0005 C290 Checksum = 2’s complement of all data Opcode :0700000075900FD297C2902A :00000001FF CuuDuongThanCong.com 0000H 75H 0001H 90H 0002H 0FH 0003H D2H 0004H 97H 0005H C2H Lê Chí Thông end of file record type SOURCE ORG 0000H MOV P1,#0FH SETB P1.7 CLR P1.0 END 0006H 90H https://fb.com/tailieudientucntt Code Memory Registers and Data Transfer Instructions • Register o Register A = accumulator o Register B o Register R0 – R7 • Data Transfer Instruction o MOV destination, source Ex: MOV A,R0 MOV R1,B MOV R1,R3 A R0 R1 B CuuDuongThanCong.com Lê Chí Thơng https://fb.com/tailieudientucntt Internal RAM • • • • • 128 locations from address 00H to 7FH (256 locations for 8052) The content of one location is bit Register banks: address 00H to 1FH Bit Addressable RAM: address 20H to 2FH General purpose RAM: address from 30H to 7FH Lê Chí Thơng CuuDuongThanCong.com https://fb.com/tailieudientucntt General purpose RAM (30H-7FH) Special function registers (SFRs) (80H-FFH) Bitaddressable RAM (20H-2FH) Register banks (00H-1FH) Lê Chí Thơng CuuDuongThanCong.com https://fb.com/tailieudientucntt General Purpose RAM • Address from 30H to 7FH • The content of one location is bit Ex: MOV A,30H This instruction moves (reads) the content of location 30H (NOT data 30H) to register A A 30H Ex: MOV 31H,R4 This instruction moves (writes) the content of register R4 to location 31H 31H R4 Lê Chí Thơng CuuDuongThanCong.com https://fb.com/tailieudientucntt General purpose RAM (30H-7FH) Special function registers (SFRs) (80H-FFH) Bitaddressable RAM (20H-2FH) Register banks (00H-1FH) Lê Chí Thơng CuuDuongThanCong.com https://fb.com/tailieudientucntt Bit Addressable RAM • Address from 20H to 2FH • The content of one location is bit • Can read/write a byte or a bit Ex: MOV 20H, A ; writes the content of register A to location 20H 20H Ex: SETB 20H.0 or SETB 00H A 20H Not affected Ex: MOV C,31H or MOV C, 26H.1 CY 26H Lê Chí Thơng CuuDuongThanCong.com https://fb.com/tailieudientucntt General purpose RAM (30H-7FH) Special function registers (SFRs) (80H-FFH) Bitaddressable RAM (20H-2FH) Register banks (00H-1FH) Lê Chí Thơng CuuDuongThanCong.com 10 https://fb.com/tailieudientucntt An Example Ex: Write a program to write 40H to internal RAM from location 30H to location 36H (source) ORG 0000H Addr 30H MOV R1,#30H;Address=30H Again: MOV @R1,#40H INC R1 (Addr) 40H CJNE R1,#37H,Again END AddrAddr+1 N Addr=37H? Y Lê Chí Thơng CuuDuongThanCong.com 124 https://fb.com/tailieudientucntt CJNE – Equal/Not Equal (2) N A = 05H? Y Statement Statement CuuDuongThanCong.com CJNE A,#05H,Not_Eq (Statement 1) SJMP Next Not_Eq: (Statement 2) Next: (Continue) Lê Chí Thơng https://fb.com/tailieudientucntt 125 CJNE – Greater Than or Equal/Less Than (1) N A ≥ 05H? Y CJNE A,#05H,Next Next: JC LessThan (Statement 1) LessThan: (Continue) Statement CJNE A,#05H,$+3 JC LessThan (Statement 1) LessThan: (Continue) CuuDuongThanCong.com Lê Chí Thơng https://fb.com/tailieudientucntt 126 CJNE – Greater Than or Equal/Less Than (1) N A ≥ 05H? Y Statement CJNE A,#05H,Next Next: JNC GT_Eq SJMP Continue GT_Eq: (Statement 1) Continue: (Continue) CJNE A,#05H,$+3 JNC GT_Eq SJMP Continue GT_Eq: (Statement 1) Continue: (Continue) CuuDuongThanCong.com Lê Chí Thơng https://fb.com/tailieudientucntt 127 An Example Examine the content of A, if ≤ A ≤ 10 then output A to Port 1; if not, output A to Port ORG CJNE A,#5,$+3 JC PORT2 CJNE A,#11,$+3 JNC PORT2 MOV P1,A SJMP DONE PORT2:MOV P2,A DONE: NOP END Lê Chí Thơng CuuDuongThanCong.com 128 https://fb.com/tailieudientucntt CJNE – Greater Than or Equal/Less Than (2) N A ≥ 05H? Y Statement Statement CuuDuongThanCong.com GT_Eq: Next: Lê Chí Thơng CJNE A,#05H,$+3 JNC GT_Eq (Statement 2) SJMP Next (Statement 1) (Continue) https://fb.com/tailieudientucntt 129 Bit Testing Given a 20-byte string in internal RAM, starting at address 40H Write a program that output even numbers to Port Lê Chí Thơng CuuDuongThanCong.com 130 https://fb.com/tailieudientucntt Bit Testing Given a 20-byte string in internal RAM, starting at address 40H Write a program that output even numbers to Port ORG MOV R4,#20 MOV R0,#40H LOOP: MOV A,@R0 JB ACC.0,NEXT MOV P2,A NEXT: INC R0 DJNZ R4,LOOP END CuuDuongThanCong.com ; Number of loops ; Address pointer ; Read data from internal RAM to A ; skip if odd number ; Output to Port if even number Lê Chí Thông 131 https://fb.com/tailieudientucntt Your Turn! Given a 20-byte string in external RAM, starting at address 4000H Write a program that output odd numbers to Port Lê Chí Thơng CuuDuongThanCong.com 132 https://fb.com/tailieudientucntt Solutions Given a 20-byte string in external RAM, starting at address 4000H Write a program that output odd numbers to Port ORG 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 if odd number NEXT: INC DPTR DJNZ R4,LOOP END Lê Chí Thơng CuuDuongThanCong.com https://fb.com/tailieudientucntt 133 Bit Testing Given a 100-byte unsigned number string in external RAM at address starting from 0100H Write a program that sends positive numbers to Port and negative numbers to Port Hint: - A positive number has MSB = - A negative number has MSB = - Use JB / JNB instruction Lê Chí Thơng CuuDuongThanCong.com 134 https://fb.com/tailieudientucntt Bit Testing Given a 100-byte unsigned number string in external RAM at address starting from 0100H Write a program that sends positive numbers to Port and negative numbers to Port 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 CuuDuongThanCong.com 135 https://fb.com/tailieudientucntt Bit Testing P1.0 P1.1 LOOP: SKIP: MOV JNB CPL MOV SJMP XOR P1.2 C,P1.0 P1.1,SKIP C P1.2,C LOOP Lê Chí Thơng CuuDuongThanCong.com 136 https://fb.com/tailieudientucntt 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” Lê Chí Thơng CuuDuongThanCong.com 137 https://fb.com/tailieudientucntt References • 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 Lê Chí Thơng CuuDuongThanCong.com 138 https://fb.com/tailieudientucntt .. .Instruction Set • 255 instructions 1-byte instructions: 139 2-byte instructions: 92 3-byte instructions: 24 • Summary instruction set (pdf) • Full instruction set (pdf) Lê Chí Thơng CuuDuongThanCong. com. .. location 00H Lê Chí Thơng CuuDuongThanCong. com 13 https://fb .com/ tailieudientucntt Register PSW (Program Status Word) Lê Chí Thơng CuuDuongThanCong. com 14 https://fb .com/ tailieudientucntt Selecting... Indexed Addressing Lê Chí Thơng CuuDuongThanCong. com 18 https://fb .com/ tailieudientucntt Coding format- Addressing modes Lê Chí Thơng CuuDuongThanCong. com 19 https://fb .com/ tailieudientucntt Immediate