mcs-51 microcontroller - ecpe.nu.ac.th 01.pdf · p89v51rd2 64 k internal program memory ......

24
MCS-51 MICROCONTROLLER 1

Upload: phungcong

Post on 27-Jun-2018

236 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: MCS-51 MICROCONTROLLER - ecpe.nu.ac.th 01.pdf · P89V51RD2 64 k internal program memory ... Internal Organization of MCS-51 4 Memory Structure. Memory Structure

MCS-51 MICROCONTROLLER

1

Page 2: MCS-51 MICROCONTROLLER - ecpe.nu.ac.th 01.pdf · P89V51RD2 64 k internal program memory ... Internal Organization of MCS-51 4 Memory Structure. Memory Structure

The Features of The 8051 Core

8-bit CPU

Extensive Boolean processing (Single-bit logic) capabilities

64K Program Memory address space

64K Data Memory address space

128 bytes of on-chip Data RAM

32 bi-directional and individually addressable I/0 lines

Two 16-bit timer/counters

Full duplex UART

6-source/5-vector interrupt structure with two priority levels

On-chip clock oscillator

2

P89V51RD2 64 k internal program memory

12-clock or 6-clock machine cycle

4 interrupt priority levels

Compatible with both TTL and CMOS logic

Page 3: MCS-51 MICROCONTROLLER - ecpe.nu.ac.th 01.pdf · P89V51RD2 64 k internal program memory ... Internal Organization of MCS-51 4 Memory Structure. Memory Structure

External view 3

PDIP40 (Plastic dual in-line package) TQFP44 (Thin quad flat pack) PLCC44 (Plastic leaded chip carrier)

Page 4: MCS-51 MICROCONTROLLER - ecpe.nu.ac.th 01.pdf · P89V51RD2 64 k internal program memory ... Internal Organization of MCS-51 4 Memory Structure. Memory Structure

Internal Organization of MCS-51 4

Memory Structure

Page 5: MCS-51 MICROCONTROLLER - ecpe.nu.ac.th 01.pdf · P89V51RD2 64 k internal program memory ... Internal Organization of MCS-51 4 Memory Structure. Memory Structure

Memory Structure

Program memory Data memory 5

ต าแหนงของ ISR

(Interrupt Service

Routine)

MCS-51 ในแบบทม Program memory ภายใน 4K, 8K และ ไมมเลย

MCS-51 with 128 bytes Internal Data

memory

MCS-51 with 256 bytes Internal Data memory

FFFF

0000

64KBYTES

EXTERNAL

60KBYTES

EXTERNAL

4K BYTESINTERNAL

FFFF

0000

0FFF

1000

56KBYTES

EXTERNAL

8K BYTESINTERNAL

FFFF

0000

1FFF

2000

FFFF

0000

64KBYTES

EXTERNAL

SFRsDIRECTADDRESSINGONLY

DIRECT&INDIRECT

ADDRESSING

FF

00

7F80 +

FFFF

0000

64KBYTES

EXTERNAL

SFRsDIRECTADDRESSINGONLYDIRECT&INDIRECTADDRESSING

00

FF

7F80

FF

+80

INDIRECTADDRESSINGONLY

Page 6: MCS-51 MICROCONTROLLER - ecpe.nu.ac.th 01.pdf · P89V51RD2 64 k internal program memory ... Internal Organization of MCS-51 4 Memory Structure. Memory Structure

Internal Data Memory

แบงออกเปน สามสวนดวยกน คอ Lower 128, Upper 128 และ SFR

Lower 128 bytes of internal data memory

6

ACCESSIBLE

BY DIRECT

AND INDIRECT

ADDRESSING

ACCESSIBLE

BY DIRECT

ADDRESSING

SPECIAL

FUNCTION

REGISTERS

ACCESSIBLE

BY INDIRECT

ADDRESSING

ONLY

FFH

80H

FFH

80H7FH

00H

LOWER

128

UPPER

128

PORTS

STATUS AND

CONTROL BITS

TIMER

REGISTERS

STRACK POINTER

ACCUMULATOR

(ECT.)

4 BANKS OF

8 REGISTERS

R0-R7

7FH

1FH

2FH

17H

0FH

07H08H

10H

18H

20H

0

BIT-ADDRESSABLE SPACE

(BIT ADDRESSABLE 0-7F

RESET VALUE OF

STACK POINTER

BANK SELECT

BIT IN PSW

00

01

10

11

Upper 128 bytes of internal data memory

Upper 128 น จะมสองสวนทบซอนกนอยใน MCS-51 แบบทม Internal

data memory 256 ต าแหนงเทานน เชน 80C52, 83C154, 83C154D

การเขาถงขอมลทอยใน Upper 128 นจะตองท าโดยใชวธการของ Indirect

addressing เทานน

Page 7: MCS-51 MICROCONTROLLER - ecpe.nu.ac.th 01.pdf · P89V51RD2 64 k internal program memory ... Internal Organization of MCS-51 4 Memory Structure. Memory Structure

Special Function Registers 7

Page 8: MCS-51 MICROCONTROLLER - ecpe.nu.ac.th 01.pdf · P89V51RD2 64 k internal program memory ... Internal Organization of MCS-51 4 Memory Structure. Memory Structure

Special Function Registers

8

Accumulator 0E0H

เปนรจสเตอรทใชมาก ค าสงทจะอานหรอเกบขอมลจากภายนอกจะตองกระท าผานรจสเตอรตวนเทานน

B register 0F0H ใชในการคณและการหารเทานน

PSW (Program Status Word) 0D0H

Page 9: MCS-51 MICROCONTROLLER - ecpe.nu.ac.th 01.pdf · P89V51RD2 64 k internal program memory ... Internal Organization of MCS-51 4 Memory Structure. Memory Structure

Special Function Registers 9

SP (Stack Pointer) 081H

ใชชต าแหนงหนวยความจ ากอนการท างานค าสง CALL หรอใชเกบขอมลดวยค าสง PUSH ขอมลในรจสเตอรนจะมคา 07H เมอมการรเซท

DPTR (Data Pointer Register) 082H and 083H

มขนาด 16 บต ใชชต าแหนงในหนวยความจ า

P0, P1, P2, P3 (PORT 0, 1, 2, 3) 080H, 090H, 0A0H, 0B0H

ใชอานและเขยนขอมลตดตออปกรณภายนอก เมอเขยน ขอมลจะคางไว การอานขอมล ตองเขยน 11111111B ไปทพอรทนนๆกอน

Page 10: MCS-51 MICROCONTROLLER - ecpe.nu.ac.th 01.pdf · P89V51RD2 64 k internal program memory ... Internal Organization of MCS-51 4 Memory Structure. Memory Structure

Special Function Registers 10

SBUF (Serial Data Buffer) 099H แทจรงแลวมรจสเตอรสองตวทมชอเดยวกน ตวหนงส าหรบรบขอมล อกตวหนงส าหรบสงขอมล จงท าใหสามารถท างาน

แบบ Full Duplex ได

Control Registers

ไดแก PCON, SCON, TH0, TL0, TH1, TL1, TMOD, TCON, IE และ IP

Page 11: MCS-51 MICROCONTROLLER - ecpe.nu.ac.th 01.pdf · P89V51RD2 64 k internal program memory ... Internal Organization of MCS-51 4 Memory Structure. Memory Structure

Special Function Registers

PCON (Power Control Register) 087H

11

Page 12: MCS-51 MICROCONTROLLER - ecpe.nu.ac.th 01.pdf · P89V51RD2 64 k internal program memory ... Internal Organization of MCS-51 4 Memory Structure. Memory Structure

Power Down and Idle Mode

Power Down Mode

ใชกระแสนอยกวา 10 µA หลงจากค าสงทท าให PCON.1=1 แลว Oscillator จะหยดท างาน ดงนนจะไมมการท างานใดๆรวมทงขอมลในรจสเตอรตางๆไมปลยนแปลง การใชก าลงจะต ามาก การออกจาก Power

down mode มวธเดยวคอการ Reset แตจะท าใหขอมลใน SFR เปลยนไป

12

Page 13: MCS-51 MICROCONTROLLER - ecpe.nu.ac.th 01.pdf · P89V51RD2 64 k internal program memory ... Internal Organization of MCS-51 4 Memory Structure. Memory Structure

Power Down and Idle Mode 13

Idle Mode

การใชกระแสจะลดลงเหลอ 15 % ไมมสญญาณนาฬกาไป CPU ขอมลในรจสเตอรตางๆไม

เปลยนแปลง การออกจาก Idle mode มสองวธคอ

1. โดยการ Reset : การท างานจะท างานในค าสงทตอจากค าสงทท าให PCON.0=1 ไปอก 2-3 ค าสง กอนเขาสการ Reset ดงนนจงตองระวงวาค าสงเหลานนอาจท าใหการความผดพลาดในการปฏบตงานได

2. โดยการ Interrupt: การท างานจะกระโดดไปท างานยงต าแหนงของ Interrupt routine เมอออกจาก Interrupt

routine นนๆกจะท างานในค าสงทตอจากค าสง ทท าให PCON.0=1 ดงนนอาจใชบต GF0 หรอ GF1 ชวยแสดงวาระบบเปน Idle mode กอนเขาส Interrupt routine

Page 14: MCS-51 MICROCONTROLLER - ecpe.nu.ac.th 01.pdf · P89V51RD2 64 k internal program memory ... Internal Organization of MCS-51 4 Memory Structure. Memory Structure

Reset

การ reset จะเกดขนเมอม logic 1 เกดขนทขา RST และการ reset อยางสมบรณจะเกดขนหลงจากนน 1-2 machine cycles คาของหนวยความจ าใน RAM จะไมไดรบผลกระทบอะไร ดงนนจงควรมการเคลยรคาของ RAM ในตอนตนโปรแกรมเพอปองกนการน าขอมลกอนการ reset มาประมวลผล

14

Page 15: MCS-51 MICROCONTROLLER - ecpe.nu.ac.th 01.pdf · P89V51RD2 64 k internal program memory ... Internal Organization of MCS-51 4 Memory Structure. Memory Structure

Port Structures and Operation

พอรตของ MCS-51 สามารถเปนไดทง

อนพตและเอาตพต และทพอรตจะมตว

ตานทานพลอพไวภายใน ยกเวนท

พอรต0 แตละพอรตสามารถจายกระแส

หรอทเรยกวา กระแสซอรซ (Source Current) ไดสงสดประมาณ 20 mA และ

ทกขาของพอรตรวมกนจะสามารถจาย

กระแสรวมกนไดไมเกนประมาณ 80 mA ดงนนเพอไมใหเกดปญหาจงควร

ตอวงจรบฟเฟอรทางเอาตพตเพอชวย

ในการขบกระแส เชน IC 74HCT245

15

Page 16: MCS-51 MICROCONTROLLER - ecpe.nu.ac.th 01.pdf · P89V51RD2 64 k internal program memory ... Internal Organization of MCS-51 4 Memory Structure. Memory Structure

Port Structures and Operation 16

การใชงานเปนพอรตอนพต พอรตสามารถรบการเปลยนแปลงระดบลอจกจากระดบสงสระดบต าเทานน ดงนนจะตอง

เรมตนดวยการเขยนขอมลใหกบพอรตหรอบตนนๆใหเปนลอจก “1” กอน เพอหยดการท างานของเฟต (FET) และท าใหขาสญญาณของพอรตเชอมตอเขากบวงจรพลอพภายในโดยตรง สงผลใหขาพอรตนนมลอจกเปน “1” และสามารถทจะรบสญญาณจากลอจก “0”จากอปกรณภายนอกได

การใชงานเปนพอรตเอาตพต การใชงานเปนพอรตเอาตพตสามารถสงขอมลออกไปไดเลย เชน ถาตองการสงขอมลทเปน

ลอจก “0” ไปยงวงจรแลตชซงจะสงตอไปขบเฟต ท าใหเฟตท างานและทขาพอรตก าหนดใหท างานกจะเกดลอจก “0” ขน ในท านองเดยวกน ถาตองการสงขอมลทเปนลอจก “1” ออกไปกสามารถทเขยนขอมล “1” ไปยงวงจรแลตช วงจรขบกจะหยดการท างานเปนผลใหทขาของพอรตเชอมตอกบวงจรพลอพภายในเกดเปนลอจก “1”ทขาพอรตนน

Page 17: MCS-51 MICROCONTROLLER - ecpe.nu.ac.th 01.pdf · P89V51RD2 64 k internal program memory ... Internal Organization of MCS-51 4 Memory Structure. Memory Structure

Machine Cycle 17

ส าหรบ MCS-51 Machine cycle จะประกอบดวยการท างาน 6 States (S1 – S6) ซงในแตละ State ของการท างาน จะใชเวลา 2 Clocks ดงนน 1 Machine cycle จะใชเวลา 12 clocks

แตละ Machine cycle จะมการ Fetch รหสค าสง 2 ครง ถงแมวาค าสงนนๆ จะเปนค าสงแบบ 1 Byte กตาม

Page 18: MCS-51 MICROCONTROLLER - ecpe.nu.ac.th 01.pdf · P89V51RD2 64 k internal program memory ... Internal Organization of MCS-51 4 Memory Structure. Memory Structure

Timing Diagram

18

เมอ Fetch Op-code จาก External Program Memory

MCS-51

P0P1

P3 P2

PSEN

ALE

EA

INTERNALROM

LATCH

OE

EPROM

INSTR.

ADDR

Page 19: MCS-51 MICROCONTROLLER - ecpe.nu.ac.th 01.pdf · P89V51RD2 64 k internal program memory ... Internal Organization of MCS-51 4 Memory Structure. Memory Structure

Timing Diagram

19

เมอ MCS-51 อานขอมลจาก External Data Memory

เมอ MCS-51 เขยนขอมลเขา External Data Memory

MCS-51

P0P1

P3 P2

ALE

EA

INTERNAL

ROMLATCH

OE

RAM

DATA

ADDR

WR

VCC

WR

RD

Page 20: MCS-51 MICROCONTROLLER - ecpe.nu.ac.th 01.pdf · P89V51RD2 64 k internal program memory ... Internal Organization of MCS-51 4 Memory Structure. Memory Structure

Microprocessor Architectures

Fetch ค าสงจากหนวยความจ าโปรแกรมและเพมคาของ Program Counter หลงจาก Fetch ค าสงจากหนวยความจ าโปรแกรม

20

Microprocessor

PC000BH

015

Program Memory 07

000AH

000BH000CH000DH000EH

Instruction 1

Main

Program

0010H0011H

000FH

Address Bus

Instruction 2

Microprocessor

PC

000CH

015

Program Memory 07

000AH

000BH000CH000DH000EH

Instruction 2

Main

Program0010H0011H

000FH

Address Bus

Instruction 1

000BH

Page 21: MCS-51 MICROCONTROLLER - ecpe.nu.ac.th 01.pdf · P89V51RD2 64 k internal program memory ... Internal Organization of MCS-51 4 Memory Structure. Memory Structure

MCS-51 Addressing Modes

Register Addressing

MOV A, Rn; Op-code 1 1 1 0 1 r r r

MOV Rn, A; Op-code 1 1 1 1 1 r r r

Register –specific Instructions

DA A ; Op-code 1 1 0 1 0 1 0 0

Immediate Addressing

MOV A, #100

MOV Rn, #64H

Op-code 01 1 1 0100 8 bits data

Direct Addressing MOV A, FFH

21

Microprocessor

PC

0000000000001011

015

Program Memory 07

000AH

000BH000CH000DH000EH

11100101

0010H0011H

000FH

Address Bus

Microprocessor

PC

0000000000001101

015

Data Memory 07

00FAH

00FBH00FCH00FDH00FEH

1000000010100H0101H

00FFH

Data Bus

Instruction 11111111

A

11110101

07

A

10000001

07

MOV A0FFH

Page 22: MCS-51 MICROCONTROLLER - ecpe.nu.ac.th 01.pdf · P89V51RD2 64 k internal program memory ... Internal Organization of MCS-51 4 Memory Structure. Memory Structure

MCS-51 Addressing Modes Indirect Addressing MOV A, @R0

Indexed Addressing

MOV A, @A+DPTR

22

Microprocessor

PC

0000000000001011

015

Program Memory 07

000AH

000BH000CH000DH000EH

11100110

0010H0011H

000FH

Address Bus

Microprocessor

PC

0000000000001101

015

Data Memory 07

00FAH

00FBH00FCH00FDH00FEH

100000111

0100H0101H

00FFH

Data Bus

Instruction A

11110101

07

A

10000111

07

MOV A, @R0

R0

11111010

07

R0

11111010

07Address Bus

Page 23: MCS-51 MICROCONTROLLER - ecpe.nu.ac.th 01.pdf · P89V51RD2 64 k internal program memory ... Internal Organization of MCS-51 4 Memory Structure. Memory Structure

Microprocessor Architectures

Stack Hardware Stack

Software Stack

23

รปแสดง การใชงาน Stack ดวยค าสง PUSH และ POP รปแสดง การใชงาน Stack ขณะอานค าสงเรยกโปรแกรมยอย

Microprocessor

PC

000DH

015

SP

0007H

015

Program Memory 07

000AH000BH

000DH000EH

Sub

routine

000FH

ACALL 0500H

RET

Address Bus

0500H

0501H0502H0503H

1

STACK

BASE

Data Memory 07

0007H

0010H0011H

000CH ACALL 0500H

Main

Program

000CH

Data Bus

Microprocessor

Register

DATA

07

SP

ADDRESS+1

015

STACK

BASE

Data Memory 07

Data Bus

PUSH

POP

Address Bus 0007H

Page 24: MCS-51 MICROCONTROLLER - ecpe.nu.ac.th 01.pdf · P89V51RD2 64 k internal program memory ... Internal Organization of MCS-51 4 Memory Structure. Memory Structure

Microprocessor Architectures

24

รปแสดงการใชงาน stack ขณะปฏบตตามค าสงเรยกโปรแกรมยอย

STACK

BASE

Data Memory 07

Microprocessor

PC

0500H

015

SP

0008H

015

Program Memory 07

000AH

000BH000CH000DH

Sub

routine

000EH

ACALL 0500H

RET

Address Bus

0500H

0501H0502H0503H

2

0007H0008H

(000DH)

000DH

Address Bus

Main

Program

0010H0011H

000FH

Data Bus

STACK

BASE

Data Memory 07

Microprocessor

PC

0504H

015

SP

0007H

015

Program Memory 07

000AH

000BH000CH000DH

Sub

routine

000EH

ACALL 0500H

RET

Address Bus

0500H

0501H0502H0503H

3

0007H0008H

Data Bus

000DH

Main

Program

0010H0011H

000FH

RET

000DH

000DH

0503H

รปแสดงการใชงาน stack ขณะอานและปฏบตตามค าสงกลบจากโปรแกรมยอย