introduction to mcs-51fivedots.coe.psu.ac.th/~cj/asm/slides/mcs51/slide-mcs51-v2.pdf ·...

71
Introduction To MCS-51 By Charoen Vongchumyen Department of Computer Engineering Faculty of Engineering • KMIT Ladkrabang

Upload: dinhkhue

Post on 30-Mar-2018

227 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: Introduction To MCS-51fivedots.coe.psu.ac.th/~cj/asm/slides/mcs51/slide-mcs51-v2.pdf · Introduction To MCS-51 By Charoen Vongchumyen • Department of Computer Engineering • Faculty

Introduction To MCS-51By

Charoen Vongchumyen

• Department of Computer Engineering

• Faculty of Engineering

• KMIT�Ladkrabang

Page 2: Introduction To MCS-51fivedots.coe.psu.ac.th/~cj/asm/slides/mcs51/slide-mcs51-v2.pdf · Introduction To MCS-51 By Charoen Vongchumyen • Department of Computer Engineering • Faculty

Tong+Tong+02/18/200002/18/2000 8051 Hardware Basic8051 Hardware Basic

8051 Hardware Basic8051 Hardware Basic

ContentContent

•• Overview Overview•• Architechture Architechture•• Memory map Memory map•• Register Register•• Interrupt Interrupt•• Timer/Counter Timer/Counter•• Serial communication Serial communication

Page 3: Introduction To MCS-51fivedots.coe.psu.ac.th/~cj/asm/slides/mcs51/slide-mcs51-v2.pdf · Introduction To MCS-51 By Charoen Vongchumyen • Department of Computer Engineering • Faculty

Tong+Tong+02/18/200002/18/2000 8051 Hardware Basic8051 Hardware Basic

8051 Overview8051 Overview

What is 8051?What is 8051?•• 8051 is a famous 8051 is a famous microcontroller microcontroller series from Intel series from Intel•• 8 bit architecture and instruction set 8 bit architecture and instruction set•• Easy to use and learning Easy to use and learning•• Widely use in many application Widely use in many application•• Suitable for Embedded and Controller application Suitable for Embedded and Controller application

Page 4: Introduction To MCS-51fivedots.coe.psu.ac.th/~cj/asm/slides/mcs51/slide-mcs51-v2.pdf · Introduction To MCS-51 By Charoen Vongchumyen • Department of Computer Engineering • Faculty

Tong+Tong+02/18/200002/18/2000 8051 Hardware Basic8051 Hardware Basic

8051 Feature8051 Feature

•• 8 Bit architecture 8 Bit architecture••Up to 64KB of external code and dataUp to 64KB of external code and data•• 4KB of internal code, EPROM (8751), ROM (8051), Flash (8951) 4KB of internal code, EPROM (8751), ROM (8051), Flash (8951) •• 128 Bytes of internal RAM 128 Bytes of internal RAM•• 32 bits 32 bits inport inport/output port/output port•• 2 Timer/Counter 2 Timer/Counter••1 Serial communication (Full Duplex)1 Serial communication (Full Duplex)•• 6 Sources of interrupt 6 Sources of interrupt

Page 5: Introduction To MCS-51fivedots.coe.psu.ac.th/~cj/asm/slides/mcs51/slide-mcs51-v2.pdf · Introduction To MCS-51 By Charoen Vongchumyen • Department of Computer Engineering • Faculty

Tong+Tong+02/18/200002/18/2000 8051 Hardware Basic8051 Hardware Basic

8051 Series8051 Series

Page 6: Introduction To MCS-51fivedots.coe.psu.ac.th/~cj/asm/slides/mcs51/slide-mcs51-v2.pdf · Introduction To MCS-51 By Charoen Vongchumyen • Department of Computer Engineering • Faculty

Tong+Tong+02/18/200002/18/2000 8051 Hardware Basic8051 Hardware Basic

8051 Series8051 Series

Page 7: Introduction To MCS-51fivedots.coe.psu.ac.th/~cj/asm/slides/mcs51/slide-mcs51-v2.pdf · Introduction To MCS-51 By Charoen Vongchumyen • Department of Computer Engineering • Faculty

Tong+Tong+02/18/200002/18/2000 8051 Hardware Basic8051 Hardware Basic

8051 Pin8051 Pin

Port 0 Port 0 ��Data and Address bus / PortData and Address bus / PortPort 1 Port 1 �� Bidirectional Bidirectional port portPort 2 Port 2 ��Address bus / PortAddress bus / PortPort 3 Port 3 ��Alternate Function / PortAlternate Function / PortALE ALE �� Address latch enable Address latch enableEA\ EA\ �� External memory access External memory accessRST RST �� Reset ResetPSEN\ PSEN\ �� Program strobe enable Program strobe enableXTAL1,2 XTAL1,2 �� Crystal in/out Crystal in/out

Page 8: Introduction To MCS-51fivedots.coe.psu.ac.th/~cj/asm/slides/mcs51/slide-mcs51-v2.pdf · Introduction To MCS-51 By Charoen Vongchumyen • Department of Computer Engineering • Faculty

Tong+Tong+02/18/200002/18/2000 8051 Hardware Basic8051 Hardware Basic

8051 Architecture8051 Architecture

Page 9: Introduction To MCS-51fivedots.coe.psu.ac.th/~cj/asm/slides/mcs51/slide-mcs51-v2.pdf · Introduction To MCS-51 By Charoen Vongchumyen • Department of Computer Engineering • Faculty

Tong+Tong+02/18/200002/18/2000 8051 Hardware Basic8051 Hardware Basic

8051 Ports8051 Ports

Page 10: Introduction To MCS-51fivedots.coe.psu.ac.th/~cj/asm/slides/mcs51/slide-mcs51-v2.pdf · Introduction To MCS-51 By Charoen Vongchumyen • Department of Computer Engineering • Faculty

Tong+Tong+02/18/200002/18/2000 8051 Hardware Basic8051 Hardware Basic

8051 Ports8051 Ports

Page 11: Introduction To MCS-51fivedots.coe.psu.ac.th/~cj/asm/slides/mcs51/slide-mcs51-v2.pdf · Introduction To MCS-51 By Charoen Vongchumyen • Department of Computer Engineering • Faculty

Tong+Tong+02/18/200002/18/2000 8051 Hardware Basic8051 Hardware Basic

8051 Ports8051 Ports

Page 12: Introduction To MCS-51fivedots.coe.psu.ac.th/~cj/asm/slides/mcs51/slide-mcs51-v2.pdf · Introduction To MCS-51 By Charoen Vongchumyen • Department of Computer Engineering • Faculty

Tong+Tong+02/18/200002/18/2000 8051 Hardware Basic8051 Hardware Basic

8051 Ports8051 Ports

Page 13: Introduction To MCS-51fivedots.coe.psu.ac.th/~cj/asm/slides/mcs51/slide-mcs51-v2.pdf · Introduction To MCS-51 By Charoen Vongchumyen • Department of Computer Engineering • Faculty

Tong+Tong+02/18/200002/18/2000 8051 Hardware Basic8051 Hardware Basic

8051 Machine Cycles8051 Machine Cycles

ClockClock

State 1State 1S1S1

State 2State 2S2S2

State 3State 3S3S3

State 4State 4S4S4

State 5State 5S5S5

State 6State 6S6S6

P1P1 P2P2 P1P1 P2P2 P1P1 P2P2 P1P1 P2P2 P1P1 P2P2 P1P1 P2P2

ALEALE

S1S1 S2S2 S3S3 S4S4 S5S5 S6S6

FetchFetch Opcode Opcode FetchFetch Opcode Opcode

Page 14: Introduction To MCS-51fivedots.coe.psu.ac.th/~cj/asm/slides/mcs51/slide-mcs51-v2.pdf · Introduction To MCS-51 By Charoen Vongchumyen • Department of Computer Engineering • Faculty

Tong+Tong+02/18/200002/18/2000 8051 Hardware Basic8051 Hardware Basic

80518051 Machince Machince Cycles Cycles ((ConCon��tt))

T = ( C * 12 ) / CFT = ( C * 12 ) / CFC = Machine Cycle ofC = Machine Cycle of instrution instrutionCF = Crystal FrequencyCF = Crystal Frequency

Such asSuch asADD A,R1 ADD A,R1 -> use 1 Machine Cycle-> use 1 Machine CycleXTAL 12 MHz XTAL 12 MHz -> use 1-> use 1 uSec uSecXTAL 16 MHzXTAL 16 MHz -> use 0.75-> use 0.75 uSec uSec

Note* Sometime we use XTAL 11.0592Note* Sometime we use XTAL 11.0592 of ofserial communicationserial communication

Page 15: Introduction To MCS-51fivedots.coe.psu.ac.th/~cj/asm/slides/mcs51/slide-mcs51-v2.pdf · Introduction To MCS-51 By Charoen Vongchumyen • Department of Computer Engineering • Faculty

Tong+Tong+02/18/200002/18/2000 8051 Hardware Basic8051 Hardware Basic

8051 Memory8051 MemoryCode MemoryCode Memory•• External Code memory External Code memory 0000 -0000 - FFFFh FFFFh•• Internal Code memory Internal Code memory 0000 - 0FFFh0000 - 0FFFh

Data MemoryData Memory•• External Data memory External Data memory 0000 -0000 - FFFFh FFFFh•• Internal Data memory Internal Data memory

?? Register Bank Register Bank 00 - 1Fh00 - 1Fh?? Bit addressable Bit addressable 20h - 2Fh20h - 2Fh?? Byte addressable Byte addressable 30h - 7Fh30h - 7Fh?? Special function register Special function register 80h - 7Fh80h - 7Fh

Page 16: Introduction To MCS-51fivedots.coe.psu.ac.th/~cj/asm/slides/mcs51/slide-mcs51-v2.pdf · Introduction To MCS-51 By Charoen Vongchumyen • Department of Computer Engineering • Faculty

Tong+Tong+02/18/200002/18/2000 8051 Hardware Basic8051 Hardware Basic

8051 Code Memory8051 Code Memory

External code memoryExternal code memory

Internal codeInternal codeEA\ = 1EA\ = 1

External codeExternal codeEA\ = 0EA\ = 0

0000h

0FFFh1000h

FFFFh

Page 17: Introduction To MCS-51fivedots.coe.psu.ac.th/~cj/asm/slides/mcs51/slide-mcs51-v2.pdf · Introduction To MCS-51 By Charoen Vongchumyen • Department of Computer Engineering • Faculty

Tong+Tong+02/18/200002/18/2000 8051 Hardware Basic8051 Hardware Basic

8051 External Code Fetches8051 External Code Fetches

Page 18: Introduction To MCS-51fivedots.coe.psu.ac.th/~cj/asm/slides/mcs51/slide-mcs51-v2.pdf · Introduction To MCS-51 By Charoen Vongchumyen • Department of Computer Engineering • Faculty

Tong+Tong+02/18/200002/18/2000 8051 Hardware Basic8051 Hardware Basic

8051 Data Memory8051 Data Memory

External DataExternal Data Memory Memory

Internal DataInternal Data RAM RAM

Special FunctionSpecial FunctionRegisterRegister

OverlabOverlabMemoryMemory

0

7Fh80h

FFh

0000

FFFFh

Page 19: Introduction To MCS-51fivedots.coe.psu.ac.th/~cj/asm/slides/mcs51/slide-mcs51-v2.pdf · Introduction To MCS-51 By Charoen Vongchumyen • Department of Computer Engineering • Faculty

Tong+Tong+02/18/200002/18/2000 8051 Hardware Basic8051 Hardware Basic

8051 Internal Data Memory8051 Internal Data Memory

Bank 0Bank 0Bank 1Bank 1Bank 2Bank 2Bank 3Bank 3

Bit AddressableBit AddressableRAMRAM

Byte AddressableByte Addressable RAM RAM

008h8h

10h10h18h18h20h20h

20h20h Special FunctionSpecial FunctionRegisterRegister

80h80h

FFhFFh7Fh7Fh

Extend RAMExtend RAM

Access by Access by Indirect addressingIndirect addressing

onlyonly

FFhFFh

80h80h

Page 20: Introduction To MCS-51fivedots.coe.psu.ac.th/~cj/asm/slides/mcs51/slide-mcs51-v2.pdf · Introduction To MCS-51 By Charoen Vongchumyen • Department of Computer Engineering • Faculty

Tong+Tong+02/18/200002/18/2000 8051 Hardware Basic8051 Hardware Basic

8051 Register Bank8051 Register Bank

R0R0 R1R1 R2R2 R3R3 R4R4 R5R5 R6R6 R7R7

Bank 0Bank 0

Bank 1Bank 1

Bank 2Bank 2Bank 3Bank 3

08h - 0Fh08h - 0Fh

10h - 17h10h - 17h18h - 1Fh18h - 1Fh

00h - 07h00h - 07h

Bank 0 -> RS0, 1 = 00Bank 0 -> RS0, 1 = 00Bank 1 -> RS0, 1 = 01Bank 1 -> RS0, 1 = 01Bank 2 -> RS0, 1 = 10Bank 2 -> RS0, 1 = 10Bank 3 -> RS0, 1 = 11Bank 3 -> RS0, 1 = 11All bank has same name of registerAll bank has same name of register

R0R0 R1R1 R2R2 R3R3 R4R4 R5R5 R6R6 R7R7R0R0 R1R1 R2R2 R3R3 R4R4 R5R5 R6R6 R7R7R0R0 R1R1 R2R2 R3R3 R4R4 R5R5 R6R6 R7R7

Page 21: Introduction To MCS-51fivedots.coe.psu.ac.th/~cj/asm/slides/mcs51/slide-mcs51-v2.pdf · Introduction To MCS-51 By Charoen Vongchumyen • Department of Computer Engineering • Faculty

Tong+Tong+02/18/200002/18/2000 8051 Hardware Basic8051 Hardware Basic

8051 Bit Addressable RAM8051 Bit Addressable RAM

00 01 02 03 04 05 06 07 00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F08 09 0A 0B 0C 0D 0E 0F10 11 12 13 14 15 16 1710 11 12 13 14 15 16 17

20 21 22 23 24 25 26 27 20 21 22 23 24 25 26 27

30 31 32 33 34 35 36 37 30 31 32 33 34 35 36 37

18 19 1A 1B 1C 1D 1E 1F18 19 1A 1B 1C 1D 1E 1F

28 29 2A 2B 2C 2D 2E 2F28 29 2A 2B 2C 2D 2E 2F

38 39 3A 3B 3C 3D 3E 3F38 39 3A 3B 3C 3D 3E 3F

40 41 42 43 44 45 46 47 40 41 42 43 44 45 46 47 48 49 4A 4B 4C 4D 4E 4F48 49 4A 4B 4C 4D 4E 4F50 51 52 53 54 55 56 5750 51 52 53 54 55 56 57

60 61 62 63 64 65 66 67 60 61 62 63 64 65 66 67

70 71 72 73 74 75 76 77 70 71 72 73 74 75 76 77

58 59 5A 5B 5C 5D 5E 5F58 59 5A 5B 5C 5D 5E 5F

68 69 6A 6B 66 6D 6E 6F68 69 6A 6B 66 6D 6E 6F

78 79 7A 7B 7C 7D 7E 7F78 79 7A 7B 7C 7D 7E 7F

21h21h23h23h25h25h27h27h29h29h2Bh2Bh2Dh2Dh2Fh2Fh

20h20h22h22h24h24h26h26h28h28h2Ah2Ah2Ch2Ch2Eh2Eh

Bit addressableBit addressableByte addressableByte addressable

Page 22: Introduction To MCS-51fivedots.coe.psu.ac.th/~cj/asm/slides/mcs51/slide-mcs51-v2.pdf · Introduction To MCS-51 By Charoen Vongchumyen • Department of Computer Engineering • Faculty

Tong+Tong+02/18/200002/18/2000 8051 Hardware Basic8051 Hardware Basic

80518051 SFR SFR

(� ) In 8052 Only

Page 23: Introduction To MCS-51fivedots.coe.psu.ac.th/~cj/asm/slides/mcs51/slide-mcs51-v2.pdf · Introduction To MCS-51 By Charoen Vongchumyen • Department of Computer Engineering • Faculty

Tong+Tong+02/18/200002/18/2000 8051 Hardware Basic8051 Hardware Basic

80518051 SFR SFR ((concon��tt))

•• P0 (Port 0, Address 80h, Bit-Addressable) P0 (Port 0, Address 80h, Bit-Addressable) •• P1 (Port 1, Address 90h, Bit-Addressable) P1 (Port 1, Address 90h, Bit-Addressable) •• P2 (Port 2, Address A0h, Bit-Addressable) P2 (Port 2, Address A0h, Bit-Addressable) •• P3 (Port 3, Address B0h, Bit-Addressable) P3 (Port 3, Address B0h, Bit-Addressable) •• ACC (Accumulator, Addresses E0h, Bit-Addressable) ACC (Accumulator, Addresses E0h, Bit-Addressable) •• B (B Register, Addresses F0h, Bit-Addressable) B (B Register, Addresses F0h, Bit-Addressable) •• SP (Stack Pointer, Address 81h) SP (Stack Pointer, Address 81h) •• DPL/DPH (Data Pointer Low/High, Addresses 82h/83h) DPL/DPH (Data Pointer Low/High, Addresses 82h/83h) •• TL0/TH0 (Timer 0 Low/High, Addresses 8Ah/8Bh) TL0/TH0 (Timer 0 Low/High, Addresses 8Ah/8Bh)•• TL1/TH1 (Timer 1 Low/High, Addresses 8Ch/8Dh)TL1/TH1 (Timer 1 Low/High, Addresses 8Ch/8Dh)••SBUF (Serial Control, Addresses 99h)SBUF (Serial Control, Addresses 99h)

Page 24: Introduction To MCS-51fivedots.coe.psu.ac.th/~cj/asm/slides/mcs51/slide-mcs51-v2.pdf · Introduction To MCS-51 By Charoen Vongchumyen • Department of Computer Engineering • Faculty

Tong+Tong+02/18/200002/18/2000 8051 Hardware Basic8051 Hardware Basic

80518051 SFR SFR ((concon��tt))

•• PSW (Program Status Word, Addresses D0h, Bit-Addressable) PSW (Program Status Word, Addresses D0h, Bit-Addressable)•• PCON (Power Control, Addresses 87h)PCON (Power Control, Addresses 87h)•• TCON (Timer Control, Addresses 88h, Bit-Addressable)TCON (Timer Control, Addresses 88h, Bit-Addressable)•• TMOD (Timer Mode, Addresses 89h)TMOD (Timer Mode, Addresses 89h)•• SCON (Serial Control, Addresses 98h, Bit-Addressable)SCON (Serial Control, Addresses 98h, Bit-Addressable)•• IE (Interrupt Enable, Addresses A8h) IE (Interrupt Enable, Addresses A8h)•• IP (Interrupt Priority, Addresses B8h, Bit-Addressable)IP (Interrupt Priority, Addresses B8h, Bit-Addressable)

Page 25: Introduction To MCS-51fivedots.coe.psu.ac.th/~cj/asm/slides/mcs51/slide-mcs51-v2.pdf · Introduction To MCS-51 By Charoen Vongchumyen • Department of Computer Engineering • Faculty

Tong+Tong+02/18/200002/18/2000 8051 Hardware Basic8051 Hardware Basic

Program Status Word (PSW)Program Status Word (PSW)

Page 26: Introduction To MCS-51fivedots.coe.psu.ac.th/~cj/asm/slides/mcs51/slide-mcs51-v2.pdf · Introduction To MCS-51 By Charoen Vongchumyen • Department of Computer Engineering • Faculty

Tong+Tong+02/18/200002/18/2000 8051 Hardware Basic8051 Hardware Basic

Power Control (PCON)Power Control (PCON)

Page 27: Introduction To MCS-51fivedots.coe.psu.ac.th/~cj/asm/slides/mcs51/slide-mcs51-v2.pdf · Introduction To MCS-51 By Charoen Vongchumyen • Department of Computer Engineering • Faculty

Tong+Tong+02/18/200002/18/2000 8051 Hardware Basic8051 Hardware Basic

Timer Control (TCON)Timer Control (TCON)

Page 28: Introduction To MCS-51fivedots.coe.psu.ac.th/~cj/asm/slides/mcs51/slide-mcs51-v2.pdf · Introduction To MCS-51 By Charoen Vongchumyen • Department of Computer Engineering • Faculty

Tong+Tong+02/18/200002/18/2000 8051 Hardware Basic8051 Hardware Basic

Timer Mode (TMOD)Timer Mode (TMOD)

Page 29: Introduction To MCS-51fivedots.coe.psu.ac.th/~cj/asm/slides/mcs51/slide-mcs51-v2.pdf · Introduction To MCS-51 By Charoen Vongchumyen • Department of Computer Engineering • Faculty

Tong+Tong+02/18/200002/18/2000 8051 Hardware Basic8051 Hardware Basic

Serial Control (SCON)Serial Control (SCON)

Page 30: Introduction To MCS-51fivedots.coe.psu.ac.th/~cj/asm/slides/mcs51/slide-mcs51-v2.pdf · Introduction To MCS-51 By Charoen Vongchumyen • Department of Computer Engineering • Faculty

Tong+Tong+02/18/200002/18/2000 8051 Hardware Basic8051 Hardware Basic

Interrupt Enable (IE)Interrupt Enable (IE)

Page 31: Introduction To MCS-51fivedots.coe.psu.ac.th/~cj/asm/slides/mcs51/slide-mcs51-v2.pdf · Introduction To MCS-51 By Charoen Vongchumyen • Department of Computer Engineering • Faculty

Tong+Tong+02/18/200002/18/2000 8051 Hardware Basic8051 Hardware Basic

Interrupt Priority (IP)Interrupt Priority (IP)

Page 32: Introduction To MCS-51fivedots.coe.psu.ac.th/~cj/asm/slides/mcs51/slide-mcs51-v2.pdf · Introduction To MCS-51 By Charoen Vongchumyen • Department of Computer Engineering • Faculty

Tong+Tong+02/18/200002/18/2000 8051 Software Basic8051 Software Basic

8051 Software Basic8051 Software Basic

•• Register and memory Register and memory•• Addressing mode Addressing mode•• Instructionset InstructionsetQQ ArithmeticArithmeticQQ LogicalLogicalQQ BooleanBooleanQQ Data transferData transferQQ Program control flowProgram control flow

Page 33: Introduction To MCS-51fivedots.coe.psu.ac.th/~cj/asm/slides/mcs51/slide-mcs51-v2.pdf · Introduction To MCS-51 By Charoen Vongchumyen • Department of Computer Engineering • Faculty

Tong+Tong+02/18/200002/18/2000 8051 Software Basic8051 Software Basic

8051 Register & Memory8051 Register & Memory

RegisterRegister•• R0 R0 �� R7 R7•• A (Accumulator) A (Accumulator)•• B B •• DPTR (DPH, DPL) DPTR (DPH, DPL)

MemoryMemory•• General purpose register R0 - R7 (00h - 1Fh) General purpose register R0 - R7 (00h - 1Fh)•• Bit addressable RAM (20h - 2Fh) Bit addressable RAM (20h - 2Fh)•• General purpose memory (30 - 7Fh) General purpose memory (30 - 7Fh)

Page 34: Introduction To MCS-51fivedots.coe.psu.ac.th/~cj/asm/slides/mcs51/slide-mcs51-v2.pdf · Introduction To MCS-51 By Charoen Vongchumyen • Department of Computer Engineering • Faculty

Tong+Tong+02/18/200002/18/2000 8051 Software Basic8051 Software Basic

8051 Addressing Mode8051 Addressing Mode

•• Immediate addressing mode Immediate addressing modeMOV MOV A,#20hA,#20h

•• Direct addressing mode Direct addressing modeMOV MOV A,20hA,20h

•• Indirect addressing mode Indirect addressing mode MOV MOV A,@R0A,@R0

•• Register addressing mode Register addressing mode MOV MOV A,R0A,R0

Page 35: Introduction To MCS-51fivedots.coe.psu.ac.th/~cj/asm/slides/mcs51/slide-mcs51-v2.pdf · Introduction To MCS-51 By Charoen Vongchumyen • Department of Computer Engineering • Faculty

Tong+Tong+02/18/200002/18/2000 8051 Software Basic8051 Software Basic

8051 Instruction Set8051 Instruction Set

•• Arithmetic instruction set Arithmetic instruction set•• Logical instruction set Logical instruction set•• Boolean instruction set Boolean instruction set •• Data transfer instruction set Data transfer instruction set•• Program flow control (jump instruction set) Program flow control (jump instruction set)

Page 36: Introduction To MCS-51fivedots.coe.psu.ac.th/~cj/asm/slides/mcs51/slide-mcs51-v2.pdf · Introduction To MCS-51 By Charoen Vongchumyen • Department of Computer Engineering • Faculty

Tong+Tong+02/18/200002/18/2000 8051 Software Basic8051 Software Basic

Arithmetic Instruction SetArithmetic Instruction Set

Byte = register or one byte memoryByte = register or one byte memory

Page 37: Introduction To MCS-51fivedots.coe.psu.ac.th/~cj/asm/slides/mcs51/slide-mcs51-v2.pdf · Introduction To MCS-51 By Charoen Vongchumyen • Department of Computer Engineering • Faculty

Tong+Tong+02/18/200002/18/2000 8051 Software Basic8051 Software Basic

Logical Instruction SetLogical Instruction Set

Page 38: Introduction To MCS-51fivedots.coe.psu.ac.th/~cj/asm/slides/mcs51/slide-mcs51-v2.pdf · Introduction To MCS-51 By Charoen Vongchumyen • Department of Computer Engineering • Faculty

Tong+Tong+02/18/200002/18/2000 8051 Software Basic8051 Software Basic

BooleanBooleanInstruction SetInstruction Set

Page 39: Introduction To MCS-51fivedots.coe.psu.ac.th/~cj/asm/slides/mcs51/slide-mcs51-v2.pdf · Introduction To MCS-51 By Charoen Vongchumyen • Department of Computer Engineering • Faculty

Tong+Tong+02/18/200002/18/2000 8051 Software Basic8051 Software Basic

Data Transfer Instruction SetData Transfer Instruction Set

•• Accessing internal data memory Accessing internal data memoryx,x,xxxx

•• Accessing external data memory Accessing external data memoryx,x,xxxx

•• Lookup table read instruction Lookup table read instructionx,x,xxxx

Page 40: Introduction To MCS-51fivedots.coe.psu.ac.th/~cj/asm/slides/mcs51/slide-mcs51-v2.pdf · Introduction To MCS-51 By Charoen Vongchumyen • Department of Computer Engineering • Faculty

Tong+Tong+02/18/200002/18/2000 8051 Software Basic8051 Software Basic

Data Transfer Instruction Set (Data Transfer Instruction Set (concon��tt))

Accessing internal data memoryAccessing internal data memory

Page 41: Introduction To MCS-51fivedots.coe.psu.ac.th/~cj/asm/slides/mcs51/slide-mcs51-v2.pdf · Introduction To MCS-51 By Charoen Vongchumyen • Department of Computer Engineering • Faculty

Tong+Tong+02/18/200002/18/2000 8051 Software Basic8051 Software Basic

Data Transfer Instruction Set (Data Transfer Instruction Set (concon��tt))Accessing external data memoryAccessing external data memory

Page 42: Introduction To MCS-51fivedots.coe.psu.ac.th/~cj/asm/slides/mcs51/slide-mcs51-v2.pdf · Introduction To MCS-51 By Charoen Vongchumyen • Department of Computer Engineering • Faculty

Tong+Tong+02/18/200002/18/2000 8051 Software Basic8051 Software Basic

Data Transfer Instruction Set (Data Transfer Instruction Set (concon��tt))

Table Lookup read instruction Table Lookup read instruction

Page 43: Introduction To MCS-51fivedots.coe.psu.ac.th/~cj/asm/slides/mcs51/slide-mcs51-v2.pdf · Introduction To MCS-51 By Charoen Vongchumyen • Department of Computer Engineering • Faculty

Tong+Tong+02/18/200002/18/2000 8051 Software Basic8051 Software Basic

Program Flow ControlProgram Flow Control

•• Uncondition Uncondition jump jumpjmpjmp, call, ret, call, ret

•• Condition jump Condition jumpjzjz,, jnz jnz,, djnz djnz,, cjne cjne

•• Bit condition jump Bit condition jumpjcjc,, jnc jnc,, jb jb,, jnb jnb,, jbc jbc

Page 44: Introduction To MCS-51fivedots.coe.psu.ac.th/~cj/asm/slides/mcs51/slide-mcs51-v2.pdf · Introduction To MCS-51 By Charoen Vongchumyen • Department of Computer Engineering • Faculty

Tong+Tong+02/18/200002/18/2000 8051 Software Basic8051 Software Basic

Program Flow Control (Program Flow Control (concon��tt))

UnconditionUncondition jump jump

Page 45: Introduction To MCS-51fivedots.coe.psu.ac.th/~cj/asm/slides/mcs51/slide-mcs51-v2.pdf · Introduction To MCS-51 By Charoen Vongchumyen • Department of Computer Engineering • Faculty

Tong+Tong+02/18/200002/18/2000 8051 Software Basic8051 Software Basic

Program Flow Control (Program Flow Control (concon��tt))

Condition jumpCondition jump

Page 46: Introduction To MCS-51fivedots.coe.psu.ac.th/~cj/asm/slides/mcs51/slide-mcs51-v2.pdf · Introduction To MCS-51 By Charoen Vongchumyen • Department of Computer Engineering • Faculty

Tong+Tong+02/18/200002/18/2000 8051 Software Basic8051 Software Basic

Program Flow Control (Program Flow Control (concon��tt))

Bit condition jumpBit condition jump

Page 47: Introduction To MCS-51fivedots.coe.psu.ac.th/~cj/asm/slides/mcs51/slide-mcs51-v2.pdf · Introduction To MCS-51 By Charoen Vongchumyen • Department of Computer Engineering • Faculty

Tong+Tong+02/18/200002/18/2000

8051 Assembler (SXA51)8051 Assembler (SXA51)

•• Defination Defination & Preprocessor & Preprocessor•• Lable Lable & Comment & Comment•• End statement & Predefine End statement & Predefine•• Assembly Assembly

8051 Assembler (SXA51)8051 Assembler (SXA51)

Page 48: Introduction To MCS-51fivedots.coe.psu.ac.th/~cj/asm/slides/mcs51/slide-mcs51-v2.pdf · Introduction To MCS-51 By Charoen Vongchumyen • Department of Computer Engineering • Faculty

Tong+Tong+02/18/200002/18/2000 8051 Assembler (SXA51)8051 Assembler (SXA51)

DefinationDefination & Preprocessor & Preprocessor

•• EQU EQU use to define variable oruse to define variable or contant contant value valueCounterCounter EQUEQU 20h 20h -> define value of Counter-> define value of CounterSettingSetting EQUEQU 00011010b -> define setting as 1Ah00011010b -> define setting as 1Ahmovmov -> use Counter as variable-> use Counter as variablemovmov -> use Counter as constant value-> use Counter as constant value

•• DW, DW, DB DB use to define constant value use to define constant valueDBDB ��HelloHello�� -> define string Hello in Code-> define string Hello in CodeDWDW -> define value in word-> define value in word

movmovmovmov

��llll get 48h ( get 48h (��HH��))

Page 49: Introduction To MCS-51fivedots.coe.psu.ac.th/~cj/asm/slides/mcs51/slide-mcs51-v2.pdf · Introduction To MCS-51 By Charoen Vongchumyen • Department of Computer Engineering • Faculty

Tong+Tong+02/18/200002/18/2000 8051 Assembler (SXA51)8051 Assembler (SXA51)

LableLable and Comment and Comment

•• Use Use ��::�� to make to make lable lable to to jmp jmp or call or call•• Use Use ��;;�� to make comment in code to make comment in code

Main:Main: movmov r0,#05hr0,#05h ;load r0 with 5;load r0 with 5djnzdjnz r0,$r0,$ ;decrement r0 and;decrement r0 and jmp jmp if = 0 if = 0cplcpl P1.4P1.4 ;complement P1.4;complement P1.4jmpjmp MainMain ;always jump to main;always jump to main

Page 50: Introduction To MCS-51fivedots.coe.psu.ac.th/~cj/asm/slides/mcs51/slide-mcs51-v2.pdf · Introduction To MCS-51 By Charoen Vongchumyen • Department of Computer Engineering • Faculty

Tong+Tong+02/18/200002/18/2000 8051 Assembler (SXA51)8051 Assembler (SXA51)

End Statement And PredefineEnd Statement And Predefine

•• Use END at end of program to indicate end for assembler Use END at end of program to indicate end for assembler•• Predefine Predefine

Register Bank Register Bank -> r0, r1-> r0, r1�� . R7. R7RegisterRegister -> A, B, DPTR, TH0-> A, B, DPTR, TH0 SFRSFR -> PCON, TMOD, PSW-> PCON, TMOD, PSWFlagFlag -> C, Z, P -> C, Z, P

Page 51: Introduction To MCS-51fivedots.coe.psu.ac.th/~cj/asm/slides/mcs51/slide-mcs51-v2.pdf · Introduction To MCS-51 By Charoen Vongchumyen • Department of Computer Engineering • Faculty

Tong+Tong+02/18/200002/18/2000 8051 Basic Code8051 Basic Code

8051 Basic Code8051 Basic Code Delay LoopDelay LoopDelay:Delay: pushpush 00 ;save register r0;save register r0

pushpush 11 ;save register r1;save register r1movmov r0,#00hr0,#00h ;r0 = 00;r0 = 00

Loop:Loop: movmov r1,#20hr1,#20h ;r1 = 20h;r1 = 20hdjnzdjnz r1,$r1,$ ;loop r1 32 times;loop r1 32 timesdjnzdjnz r0,Loopr0,Loop ;loop r0 256 times;loop r0 256 timespoppop 11 ;restore register r1;restore register r1poppop 00 ;restore register r0;restore register r0retret ;return;return

Page 52: Introduction To MCS-51fivedots.coe.psu.ac.th/~cj/asm/slides/mcs51/slide-mcs51-v2.pdf · Introduction To MCS-51 By Charoen Vongchumyen • Department of Computer Engineering • Faculty

Tong+Tong+02/18/200002/18/2000 8051 Basic Code8051 Basic Code

8051 Basic Code8051 Basic Code Delay Loop Cycle TimeDelay Loop Cycle TimeDelay:Delay: pushpush 00 ;2;2

pushpush 11 ;2;2movmov r0,#00hr0,#00h ;1;1

Loop:Loop: movmov r1,#20hr1,#20h ;1 X 256 = 256;1 X 256 = 256djnzdjnz r1,$r1,$ ;2 X 32 X 256 = 16,384;2 X 32 X 256 = 16,384djnzdjnz r0,Loopr0,Loop ;2 X 256 = 512;2 X 256 = 512poppop 11 ;2;2poppop 00 ;2;2retret ;2;2

Total time = 2 + 2 + 1 + 256 + 16,384 + 512 + 2 + 2 + 2 = 17,163Total time = 2 + 2 + 1 + 256 + 16,384 + 512 + 2 + 2 + 2 = 17,163 uS uS

Page 53: Introduction To MCS-51fivedots.coe.psu.ac.th/~cj/asm/slides/mcs51/slide-mcs51-v2.pdf · Introduction To MCS-51 By Charoen Vongchumyen • Department of Computer Engineering • Faculty

Tong+Tong+02/18/200002/18/2000 8051 Basic Code8051 Basic Code

8051 Basic Code8051 Basic Code Delay: Delay: pushpush 00

pushpush 11movmov r0,#00hr0,#00h

Loop:Loop: movmov r1,#20hr1,#20hdjnzdjnz r1,$r1,$djnzdjnz r0,Loopr0,Looppoppop 11poppop 00retret

Pulse generator Pulse generator

Main:Main: cplcpl p1.0p1.0 ;toggle p1.0;toggle p1.0callcall delaydelay ;delay;delay jmpjmp MainMain ;infinite loop;infinite loop

Duty cycle = 50%Duty cycle = 50%Pulse width = 17,163Pulse width = 17,163 uS uSFrequency = 58.26 HzFrequency = 58.26 HzCystalsCystals = 12 MHz = 12 MHz

Page 54: Introduction To MCS-51fivedots.coe.psu.ac.th/~cj/asm/slides/mcs51/slide-mcs51-v2.pdf · Introduction To MCS-51 By Charoen Vongchumyen • Department of Computer Engineering • Faculty

Tong+Tong+02/18/200002/18/2000 8051 Basic Code8051 Basic Code

8051 Basic Code8051 Basic Code Pulse generator use timer Pulse generator use timer

ORGORG 0000H0000Hjmpjmp MainMainORGORG 000BH000BHjmpjmp T0_ISRT0_ISR

Main:Main: setbsetb EAEAsetbsetb ET0ET0movmov TMOD,#00000010TMOD,#00000010movmov TH,#20hTH,#20hmovmov TL,#20hTL,#20hsetbsetb TR0TR0jmpjmp $$

T0_ISR:T0_ISR: pushpush pswpswcplcpl p1.0p1.0poppop pswpswretret

Duty cycle = 50%Duty cycle = 50%Pulse width = 32Pulse width = 32 uS uSFrequency = 31250 HzFrequency = 31250 HzCystalsCystals = 12 MHz = 12 MHz

Page 55: Introduction To MCS-51fivedots.coe.psu.ac.th/~cj/asm/slides/mcs51/slide-mcs51-v2.pdf · Introduction To MCS-51 By Charoen Vongchumyen • Department of Computer Engineering • Faculty

Tong+Tong+02/18/200002/18/2000 8051 Basic Code8051 Basic Code

8051 Basic Code8051 Basic CodeExternal InterruptExternal Interrupt

ORGORG 0000H0000Hjmpjmp MainMainORGORG 0003H0003Hjmpjmp INT0_ISRINT0_ISR

Main:Main: setbsetb EAEAsetbsetb EX0EX0setbsetb IT0IT0movmov P0,#0P0,#0jmpjmp $$

INT0_ISR:INT0_ISR: pushpush pswpswincinc P0P0poppop pswpswretireti

Count pulse on INT0 pin (externalCount pulse on INT0 pin (externalInterrupt 0) and display counter onInterrupt 0) and display counter on

Page 56: Introduction To MCS-51fivedots.coe.psu.ac.th/~cj/asm/slides/mcs51/slide-mcs51-v2.pdf · Introduction To MCS-51 By Charoen Vongchumyen • Department of Computer Engineering • Faculty

Tong+Tong+02/18/200002/18/2000 8051 Interface8051 Interface

8051 Interface8051 Interface

C1, C2 20 - 40C1, C2 20 - 40 pF pF

Page 57: Introduction To MCS-51fivedots.coe.psu.ac.th/~cj/asm/slides/mcs51/slide-mcs51-v2.pdf · Introduction To MCS-51 By Charoen Vongchumyen • Department of Computer Engineering • Faculty

Tong+Tong+02/18/200002/18/2000 8051 Interface8051 Interface

8051 Interface8051 Interface

Power On ResetPower On Reset

Page 58: Introduction To MCS-51fivedots.coe.psu.ac.th/~cj/asm/slides/mcs51/slide-mcs51-v2.pdf · Introduction To MCS-51 By Charoen Vongchumyen • Department of Computer Engineering • Faculty

Tong+Tong+02/18/200002/18/2000 8051 Interface8051 Interface

8051 Interface8051 Interface

Page 59: Introduction To MCS-51fivedots.coe.psu.ac.th/~cj/asm/slides/mcs51/slide-mcs51-v2.pdf · Introduction To MCS-51 By Charoen Vongchumyen • Department of Computer Engineering • Faculty

Tong+Tong+02/18/200002/18/2000 8051 Interface8051 Interface

8051 Interface8051 InterfaceExternal Program Memory Read CycleExternal Program Memory Read Cycle

Page 60: Introduction To MCS-51fivedots.coe.psu.ac.th/~cj/asm/slides/mcs51/slide-mcs51-v2.pdf · Introduction To MCS-51 By Charoen Vongchumyen • Department of Computer Engineering • Faculty

Tong+Tong+02/18/200002/18/2000 8051 Interface8051 Interface

8051 Interface8051 Interface

Page 61: Introduction To MCS-51fivedots.coe.psu.ac.th/~cj/asm/slides/mcs51/slide-mcs51-v2.pdf · Introduction To MCS-51 By Charoen Vongchumyen • Department of Computer Engineering • Faculty

Tong+Tong+02/18/200002/18/2000 8051 Interface8051 Interface

8051 Interface8051 InterfaceExteranlExteranl Data Memory Read Cycle Data Memory Read Cycle

Page 62: Introduction To MCS-51fivedots.coe.psu.ac.th/~cj/asm/slides/mcs51/slide-mcs51-v2.pdf · Introduction To MCS-51 By Charoen Vongchumyen • Department of Computer Engineering • Faculty

Tong+Tong+02/18/200002/18/2000 8051 Interface8051 Interface

8051 Interface8051 InterfaceExteranlExteranl Data Memory Write Cycle Data Memory Write Cycle

Page 63: Introduction To MCS-51fivedots.coe.psu.ac.th/~cj/asm/slides/mcs51/slide-mcs51-v2.pdf · Introduction To MCS-51 By Charoen Vongchumyen • Department of Computer Engineering • Faculty

Tong+Tong+02/18/200002/18/2000 8051 Interface8051 Interface

8051 Interface8051 Interface

80518051

P1-P3P1-P3

P0P0

VCCVCC

10K10KInput/Output Input/Output

MovMov a,P0a,P0MovMov P1,aP1,a

Page 64: Introduction To MCS-51fivedots.coe.psu.ac.th/~cj/asm/slides/mcs51/slide-mcs51-v2.pdf · Introduction To MCS-51 By Charoen Vongchumyen • Department of Computer Engineering • Faculty

Tong+Tong+02/18/200002/18/2000 8051 Interface8051 Interface

8051 Interface8051 Interface

80518051

P1-P3P1-P3

P0P0

VCCVCC

10K10KVCCVCC

LEDLED

300300

Input with switch, Output with LEDInput with switch, Output with LED

MovMov a,P0 a,P0MovMov P1,a P1,a

MovMov P1,#00 P1,#00MovMov P1,# P1,#FFhFFh

Page 65: Introduction To MCS-51fivedots.coe.psu.ac.th/~cj/asm/slides/mcs51/slide-mcs51-v2.pdf · Introduction To MCS-51 By Charoen Vongchumyen • Department of Computer Engineering • Faculty

Tong+Tong+02/18/200002/18/2000 8051 Interface8051 Interface

8051 Interface8051 Interface

80518051

P1.0, Row0P1.0, Row0P1.1, Row1P1.1, Row1P1.2, Row2P1.2, Row2P1.3, Col0P1.3, Col0P1.4, Col1P1.4, Col1P1.5, Col2P1.5, Col2

Matrix keyboard by scanningMatrix keyboard by scanning techinque techinqueMovMov P1,# P1,#FEhFEhJnbJnb P1.3,Key1 P1.3,Key1JnbJnb P1.4,Key2 P1.4,Key2JnbJnb P1.5,Key3 P1.5,Key3

MovMov P1,# P1,#FDhFDhJnbJnb P1.3,Key4 P1.3,Key4JnbJnb P1.4,Key5 P1.4,Key5JnbJnb P1.5,Key6 P1.5,Key6

MovMov P1,# P1,#FBhFBhJnbJnb P1.3,Key7 P1.3,Key7JnbJnb P1.4,Key8 P1.4,Key8JnbJnb P1.5,Key9 P1.5,Key9

11 22 3344 55 6677 88 99

Page 66: Introduction To MCS-51fivedots.coe.psu.ac.th/~cj/asm/slides/mcs51/slide-mcs51-v2.pdf · Introduction To MCS-51 By Charoen Vongchumyen • Department of Computer Engineering • Faculty

Tong+Tong+02/18/200002/18/2000 8051 Interface8051 Interface

8051 Interface8051 Interface

80518051

88P1P1

P2.0P2.0P2.1P2.1P2.2P2.2P2.3P2.3

7-Segment (common Anode)7-Segment (common Anode)

7-Segment Display by Scan7-Segment Display by Scan Tecnique Tecnique Loop:Loop: Mov Mov P1,#Num1 P1,#Num1MovMov P2,#00h P2,#00hCall DelayCall DelayMovMov P1,#Num2 P1,#Num2MovMov P2,#01h P2,#01hCall DelayCall DelayMovMov P1,#Num2 P1,#Num2MovMov P2,#01h P2,#01hCall DelayCall DelayMovMov P1,#Num2 P1,#Num2MovMov P2,#01h P2,#01hCall DelayCall DelayJmpJmp Loop Loop

Page 67: Introduction To MCS-51fivedots.coe.psu.ac.th/~cj/asm/slides/mcs51/slide-mcs51-v2.pdf · Introduction To MCS-51 By Charoen Vongchumyen • Department of Computer Engineering • Faculty

Tong+Tong+02/18/200002/18/2000 8051 Interface8051 Interface

8051 Interface8051 Interface

80518051

TXTXRXRX

GNDGNDPCPC

Use null modem connectorUse null modem connectorAnd XTAL = 11.0592MHzAnd XTAL = 11.0592MHz

Serial Communication Via RS-232C PortSerial Communication Via RS-232C Port

Page 68: Introduction To MCS-51fivedots.coe.psu.ac.th/~cj/asm/slides/mcs51/slide-mcs51-v2.pdf · Introduction To MCS-51 By Charoen Vongchumyen • Department of Computer Engineering • Faculty

Tong+Tong+02/18/200002/18/2000 8051 Interface8051 Interface

8051 Interface8051 Interface

Serial Communication Via RS-232C PortSerial Communication Via RS-232C Port

Page 69: Introduction To MCS-51fivedots.coe.psu.ac.th/~cj/asm/slides/mcs51/slide-mcs51-v2.pdf · Introduction To MCS-51 By Charoen Vongchumyen • Department of Computer Engineering • Faculty

ORG ORG 0000H0000HJmpJmp StartStartORGORG 0023H0023HJmpJmp Serial_ISRSerial_ISRORGORG 0040H0040H

Start:Start: ClrClr PCON.7 ;clear SMODPCON.7 ;clear SMODMovMov SCON,#52h ;set modeSCON,#52h ;set modeMovMov TMOD,#20h;timer mode 2TMOD,#20h;timer mode 2MovMov TH1,#TH1,#FDhFDh ;set reload value ;set reload valueSetbSetb EAEA ;enable all ;enable all Int Int..SetbSetb RENREN ;enable Receive ;enable ReceiveSetbSetb TR1TR1 ;start Timer ;start TimerMovMov DPTR,#0000hDPTR,#0000h

Tong+Tong+02/18/200002/18/2000 8051 Interface8051 Interface

8051 Interface8051 InterfaceSerial Communication Via RS-232C PortSerial Communication Via RS-232C Port

Serial_ISR: Serial_ISR: PushPush PSWPSWPushPush AccAccJnbJnb RI,TX_RI,TX_EvntEvntMovMov A,SBUFA,SBUFjnbjnb TI,$TI,$MovMov SBUF,ASBUF,AClrClr TITIJmpJmp End_ISREnd_ISR

TX_TX_EvntEvnt:: IncInc DPTRDPTREnd_ISR:End_ISR: PopPop AccAcc

PopPop PSWPSWRetiReti

Page 70: Introduction To MCS-51fivedots.coe.psu.ac.th/~cj/asm/slides/mcs51/slide-mcs51-v2.pdf · Introduction To MCS-51 By Charoen Vongchumyen • Department of Computer Engineering • Faculty

Tong+Tong+02/18/200002/18/2000 8051 Interface8051 Interface

8051 Interface8051 InterfaceBaud RateBaud Rate

Page 71: Introduction To MCS-51fivedots.coe.psu.ac.th/~cj/asm/slides/mcs51/slide-mcs51-v2.pdf · Introduction To MCS-51 By Charoen Vongchumyen • Department of Computer Engineering • Faculty

AssigmentAssigmentBubble SortBubble Sort 0000h = Counter, 0001h - 00xxh = External Data (< 256 bytes) 0000h = Counter, 0001h - 00xxh = External Data (< 256 bytes)

StartStart

N = Data[0] N = Data[0]Data = Data[1-N]Data = Data[1-N]

For I = 1 to N-1For I = 1 to N-1

For J = 1 to I-1For J = 1 to I-1

If I > J thenIf I > J then Swap Swap

EndEnd