Vous êtes sur la page 1sur 71

Introduction To MCS-51

By

Charoen Vongchumyen Department of Computer Engineering Faculty of Engineering KMIT Ladkrabang

8051 Hardware Basic


Content
02/18/2000

Overview Architechture Memory map Register Interrupt Timer/Counter Serial communication


8051 Hardware Basic Tong+

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

02/18/2000

8051 Hardware Basic

Tong+

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

8051 Series

02/18/2000

8051 Hardware Basic

Tong+

8051 Series

02/18/2000

8051 Hardware Basic

Tong+

8051 Pin
Port 0 Data and Address bus / Port Port 1 Bidirectional port Port 2 Address bus / Port Port 3 Alternate Function / Port ALE Address latch enable EA\ External memory access RST Reset PSEN\ Program strobe enable XTAL1,2 Crystal in/out

02/18/2000

8051 Hardware Basic

Tong+

8051 Architecture

02/18/2000

8051 Hardware Basic

Tong+

8051 Ports

02/18/2000

8051 Hardware Basic

Tong+

8051 Ports

02/18/2000

8051 Hardware Basic

Tong+

8051 Ports

02/18/2000

8051 Hardware Basic

Tong+

8051 Ports

02/18/2000

8051 Hardware Basic

Tong+

8051 Machine Cycles


Clock
P1 P2 P1 P2 P1 P2 State 3 S3 P1 State 4 S4 P2 P1 State 5 S5 P2 P1 State 6 S6 P2 State 1 S1 State 2 S2

ALE
Fetch Opcode Fetch Opcode

S1

S2

S3

S4

S5

S6

02/18/2000

8051 Hardware Basic

Tong+

8051 Machince Cycles (Con t)


C = Machine Cycle of instrution CF = Crystal Frequency
Such as ADD A,R1 XTAL 12 MHz XTAL 16 MHz -> use 1 Machine Cycle -> use 1 uSec -> use 0.75 uSec
of
Tong+

T = ( C * 12 ) / CF

Note* Sometime we use XTAL 11.0592 serial communication


02/18/2000 8051 Hardware Basic

8051 Memory
Code Memory
External Code memory Internal Code memory 0000 - FFFFh 0000 - 0FFFh 0000 - FFFFh 00 - 1Fh 20h - 2Fh 30h - 7Fh 80h - 7Fh
Tong+

Data Memory

External Data memory Internal Data memory ? Register Bank ? Bit addressable ? Byte addressable ? Special function register
8051 Hardware Basic

02/18/2000

8051 Code Memory


FFFFh External code memory 1000h 0FFFh 0000h

Internal code EA\ = 1

External code EA\ = 0

02/18/2000

8051 Hardware Basic

Tong+

8051 External Code Fetches

02/18/2000

8051 Hardware Basic

Tong+

8051 Data Memory


FFFFh

Overlab Memory
FFh 80h 7Fh 0

Special Function Register Internal Data RAM


0000

External Data Memory

02/18/2000

8051 Hardware Basic

Tong+

8051 Internal Data Memory


7Fh FFh FFh

Byte Addressable RAM


20h

Extend RAM Special Function Register Access by Indirect addressing only

20h 18h 10h 8h 0

Bit Addressable RAM Bank 3 Bank 2 Bank 1 Bank 0

80h

80h

02/18/2000

8051 Hardware Basic

Tong+

8051 Register Bank


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

R1 R1 R1 R1

R2 R2 R2 R2

R3 R3 R3 R3

R4 R4 R4 R4

R5 R5 R5 R5

R6 R6 R6 R6

R7 18h - 1Fh R7 10h - 17h R7 08h - 0Fh R7 00h - 07h

Bank 0 -> RS0, 1 = 00 Bank 1 -> RS0, 1 = 01 Bank 2 -> RS0, 1 = 10 Bank 3 -> RS0, 1 = 11 All bank has same name of register
02/18/2000 8051 Hardware Basic Tong+

8051 Bit Addressable RAM


2Eh 38 39 3A 2Ch 30 31 32 2Ah 28 29 2A 28h 20 21 22 26h 18 19 1A 24h 10 11 22h 08 09 20h 00 01 3B 3C 3D 3E 33 34 35 36 2B 2C 2D 2E 23 24 25 26 3F 78 79 7A 37 70 71 72 2F 68 69 6A 27 60 61 62 59 5A 51 52 49 4A 41 42 7B 7C 7D 7E 73 74 75 76 6B 66 6D 6E 63 64 65 66 7F 77 6F 2Fh 2Dh 2Bh

1B 1C 1D 12 13 14 15 0A 0B 0C 0D 02 03 04 05

1E 1F 58 16 17 50 0E 0F 48 06 07 40

67 29h 5B 5C 5D 5E 5F 27h 53 54 55 56 57 25h 4B 4C 4D 4E 4F 23h 43 44 45 46 47 21h

Bit addressable Byte addressable


02/18/2000 8051 Hardware Basic Tong+

8051 SFR

( ) In 8052 Only

02/18/2000

8051 Hardware Basic

Tong+

8051 SFR (con t)


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

Tong+

8051 SFR (con t)


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

02/18/2000

8051 Hardware Basic

Tong+

Program Status Word (PSW)

02/18/2000

8051 Hardware Basic

Tong+

Power Control (PCON)

02/18/2000

8051 Hardware Basic

Tong+

Timer Control (TCON)

02/18/2000

8051 Hardware Basic

Tong+

Timer Mode (TMOD)

02/18/2000

8051 Hardware Basic

Tong+

Serial Control (SCON)

02/18/2000

8051 Hardware Basic

Tong+

Interrupt Enable (IE)

02/18/2000

8051 Hardware Basic

Tong+

Interrupt Priority (IP)

02/18/2000

8051 Hardware Basic

Tong+

8051 Software Basic


Register and memory Addressing mode Instructionset Q Arithmetic
Q Q Q Q

Logical Boolean Data transfer Program control flow


8051 Software Basic Tong+

02/18/2000

8051 Register & Memory


Register
R0 R7 A (Accumulator) B DPTR (DPH, DPL)

Memory

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

02/18/2000

8051 Software Basic

Tong+

8051 Addressing Mode


Immediate addressing mode Direct addressing mode Indirect addressing mode Register addressing mode
MOV A,R0 MOV A,@R0 MOV A,20h MOV A,#20h

02/18/2000

8051 Software Basic

Tong+

8051 Instruction Set


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

02/18/2000

8051 Software Basic

Tong+

Arithmetic Instruction Set

Byte = register or one byte memory 8051 Software Basic Tong+

02/18/2000

Logical Instruction Set

02/18/2000

8051 Software Basic

Tong+

Boolean Instruction Set

02/18/2000

8051 Software Basic

Tong+

Data Transfer Instruction Set


Accessing internal data memory x,xx Accessing external data memory x,xx Lookup table read instruction x,xx

02/18/2000

8051 Software Basic

Tong+

Data Transfer Instruction Set (con t)


Accessing internal data memory

02/18/2000

8051 Software Basic

Tong+

Data Transfer Instruction Set (con t)


Accessing external data memory

02/18/2000

8051 Software Basic

Tong+

Data Transfer Instruction Set (con t)


Table Lookup read instruction

02/18/2000

8051 Software Basic

Tong+

Program Flow Control


Uncondition jump Condition jump
jmp, call, ret jz, jnz, djnz, cjne jc, jnc, jb, jnb, jbc

Bit condition jump

02/18/2000

8051 Software Basic

Tong+

Program Flow Control (con t)


Uncondition jump

02/18/2000

8051 Software Basic

Tong+

Program Flow Control (con t)


Condition jump

02/18/2000

8051 Software Basic

Tong+

Program Flow Control (con t)


Bit condition jump

02/18/2000

8051 Software Basic

Tong+

8051 Assembler (SXA51)


Defination & Preprocessor Lable & Comment End statement & Predefine Assembly

02/18/2000

8051 Assembler (SXA51)

Tong+

Defination & Preprocessor


EQU use to define variable or contant value
Counter EQU 20h -> define value of Counter Setting EQU 00011010b -> define setting as 1Ah mov -> use Counter as variable mov -> use Counter as constant value DW, DB use to define constant value DB Hello -> define string Hello in Code DW -> define value in word mov mov ll get 48h ( H )
8051 Assembler (SXA51) Tong+

02/18/2000

Lable and Comment


Use : to make lable to jmp or call Use ; to make comment in code Main: mov djnz cpl jmp r0,#05h r0,$ P1.4 Main ;load r0 with 5 ;decrement r0 and jmp if = 0 ;complement P1.4 ;always jump to main

02/18/2000

8051 Assembler (SXA51)

Tong+

End Statement And Predefine


Use END at end of program to indicate end for assembler Predefine Register Bank -> r0, r1 . R7 Register -> A, B, DPTR, TH0 SFR -> PCON, TMOD, PSW Flag -> C, Z, P

02/18/2000

8051 Assembler (SXA51)

Tong+

8051 Basic Code


Delay: Loop:

Delay Loop

push push mov mov djnz djnz pop pop ret

0 1 r0,#00h r1,#20h r1,$ r0,Loop 1 0

;save register r0 ;save register r1 ;r0 = 00 ;r1 = 20h ;loop r1 32 times ;loop r0 256 times ;restore register r1 ;restore register r0 ;return
Tong+

02/18/2000

8051 Basic Code

8051 Basic Code


Delay: push 0 ;2 push 1 ;2 mov r0,#00h ;1 Loop: mov r1,#20h ;1 X 256 = 256 djnz r1,$ ;2 X 32 X 256 = 16,384 djnz r0,Loop ;2 X 256 = 512 pop 1 ;2 pop 0 ;2 ret ;2 Total time = 2 + 2 + 1 + 256 + 16,384 + 512 + 2 + 2 + 2 = 17,163 uS
02/18/2000 8051 Basic Code Tong+

Delay Loop Cycle Time

8051 Basic Code


Pulse generator
Main: cpl call jmp p1.0 delay Main ;toggle p1.0 ;delay ;infinite loop Delay: Loop: push push mov mov djnz djnz pop pop ret 0 1 r0,#00h r1,#20h r1,$ r0,Loop 1 0

Duty cycle = 50% Pulse width = 17,163 uS Frequency = 58.26 Hz Cystals = 12 MHz
02/18/2000 8051 Basic Code

Tong+

ORG jmp ORG jmp Main: setb setb mov mov mov setb jmp
02/18/2000

Pulse generator use timer

8051 Basic Code


T0_ISR:

0000H Main 000BH T0_ISR EA ET0 TMOD,#00000010 TH,#20h TL,#20h TR0 $

push cpl pop ret

psw p1.0 psw

Duty cycle = 50% Pulse width = 32 uS Frequency = 31250 Hz Cystals = 12 MHz


Tong+

8051 Basic Code

External Interrupt

8051 Basic Code


0000H Main 0003H INT0_ISR EA EX0 IT0 P0,#0 $ INT0_ISR: push inc pop reti psw P0 psw

ORG jmp ORG jmp setb setb setb mov jmp

Main:

Count pulse on INT0 pin (external Interrupt 0) and display counter on

02/18/2000

8051 Basic Code

Tong+

8051 Interface

C1, C2 20 - 40 pF

02/18/2000

8051 Interface

Tong+

8051 Interface
Power On Reset

02/18/2000

8051 Interface

Tong+

8051 Interface

02/18/2000

8051 Interface

Tong+

8051 Interface
External Program Memory Read Cycle

02/18/2000

8051 Interface

Tong+

8051 Interface

02/18/2000

8051 Interface

Tong+

8051 Interface
Exteranl Data Memory Read Cycle

02/18/2000

8051 Interface

Tong+

8051 Interface
Exteranl Data Memory Write Cycle

02/18/2000

8051 Interface

Tong+

8051 Interface
VCC 8051 P0 P1-P3 10K

Input/Output
Mov Mov a,P0 P1,a

02/18/2000

8051 Interface

Tong+

8051 Interface
Input with switch, Output with LED
VCC 8051 P0 P1-P3 300 LED 10K VCC

Mov a,P0 Mov P1,a Mov P1,#00 Mov P1,#FFh

02/18/2000

8051 Interface

Tong+

8051 Interface
Matrix keyboard by scanning techinque
8051 P1.0, Row0 P1.1, Row1 P1.2, Row2 P1.3, Col0 P1.4, Col1 P1.5, Col2 1 4 7 2 5 8 3 6 9

Mov P1,#FEh Jnb P1.3,Key1 Jnb P1.4,Key2 Jnb P1.5,Key3 Mov P1,#FDh Jnb P1.3,Key4 Jnb P1.4,Key5 Jnb P1.5,Key6 Mov P1,#FBh Jnb P1.3,Key7 Jnb P1.4,Key8 Jnb P1.5,Key9

02/18/2000

8051 Interface

Tong+

8051 Interface
7-Segment Display by Scan Tecnique
8051 P1 P2.0 P2.1 P2.2 P2.3 8 Loop: 7-Segment (common Anode) Mov P1,#Num1 Mov P2,#00h Call Delay Mov P1,#Num2 Mov P2,#01h Call Delay Mov P1,#Num2 Mov P2,#01h Call Delay Mov P1,#Num2 Mov P2,#01h Call Delay Jmp Loop
Tong+

02/18/2000

8051 Interface

8051 Interface
Serial Communication Via RS-232C Port
8051

TX RX GND

PC

Use null modem connector And XTAL = 11.0592MHz

02/18/2000

8051 Interface

Tong+

8051 Interface
Serial Communication Via RS-232C Port

02/18/2000

8051 Interface

Tong+

Start:

ORG Jmp ORG Jmp ORG Clr Mov Mov Mov Setb Setb Setb Mov

0000H Serial_ISR: Start 0023H Serial_ISR 0040H PCON.7 ;clear SMOD SCON,#52h ;set mode TMOD,#20h;timer mode 2 TH1,#FDh ;set reload value TX_Evnt : EA ;enable all Int. REN ;enable Receive End_ISR: TR1 ;start Timer DPTR,#0000h
8051 Interface

Serial Communication Via RS-232C Port


Push Push Jnb Mov jnb Mov Clr Jmp Inc Pop Pop Reti PSW Acc RI,TX_Evnt A,SBUF TI,$ SBUF,A TI End_ISR DPTR Acc PSW
Tong+

8051 Interface

02/18/2000

8051 Interface
Baud Rate

02/18/2000

8051 Interface

Tong+

Assigment
Bubble Sort
0000h = Counter, 0001h - 00xxh = External Data (< 256 bytes) Start N = Data[0] Data = Data[1-N] For I = 1 to N-1 For J = 1 to I-1 If I > J then Swap End

Vous aimerez peut-être aussi