67
Microprocessor System Summarize Apr 2009 Hisham Mat Hussin Senior Lecturer (Electronics) University Kuala

Microprocessor system - summarize

Embed Size (px)

Citation preview

Page 1: Microprocessor system - summarize

Microprocessor SystemSummarizeApr 2009

Hisham Mat HussinSenior Lecturer (Electronics)University Kuala Lumpur-BMI

Page 2: Microprocessor system - summarize

Contents:IntroductionBlock Diagram and Pin Description of the 8051RegistersSome Simple InstructionsStructure of Assembly language and Running an 8051 programMemory mapping in 8051 8051 Flag bits and the PSW registerAddressing Modes16-bit, BCD and Signed Arithmetic in 8051Stack in the 8051LOOP and JUMP InstructionsCALL InstructionsI/O Port Programming

Page 3: Microprocessor system - summarize

Numerical Bases Used in Programming

• Hexadecimal

• Binary

• Decimal

Page 4: Microprocessor system - summarize

Hexadecimal Basis• Hexadecimal Digits:

0 1 2 3 4 5 6 7 8 9 A B C D E F

A=10 B=11 C=12 D=13 E=14 F=15

Page 5: Microprocessor system - summarize

Decimal, Binary & Hexadecimal Numbers

(43)10=

( 0010 1011 )2 =

( 2 B )16

Page 6: Microprocessor system - summarize

Introduction

• CPU for Computers• No RAM, ROM, I/O on CPU chip itself• Example : Intel’s x86, Motorola’s 680x0

CPU

General-Purpose Micro-processor

RAM ROM I/O Port

TimerSerial COM Port

Data Bus

Address Bus

General-Purpose Microprocessor System

Many chips on mother’s board

General-purpose microprocessor

Page 7: Microprocessor system - summarize

General-purpose microprocessor

Page 8: Microprocessor system - summarize

• A smaller computer• On-chip RAM, ROM, I/O ports...• Example : Motorola’s 6811, Intel’s 8051, Zilog’s Z8 and PIC 16X

RAM ROM

I/O Port

TimerSerial COM Port

Microcontroller

CPU

A single chip

Microcontroller :

Page 9: Microprocessor system - summarize

Micro-controller

Microcontroller Application :

Page 10: Microprocessor system - summarize
Page 11: Microprocessor system - summarize
Page 12: Microprocessor system - summarize

Microprocessor • CPU is stand-alone, RAM,

ROM, I/O, timer are separate

• designer can decide on the amount of ROM, RAM and I/O ports.

• expansive• versatility • general-purpose

Microcontroller• CPU, RAM, ROM, I/O and

timer are all on a single chip• fix amount of on-chip ROM,

RAM, I/O ports• for applications in which cost,

power and space are critical• single-purpose

Microprocessor vs. Microcontroller

Page 13: Microprocessor system - summarize

Inside the MicroprocessorINTEL 4004(1971)

Page 14: Microprocessor system - summarize

• Embedded system means the processor is embedded into that application.

• An embedded product uses a microprocessor or microcontroller to do one task only.

• In an embedded system, there is only one application software that is typically burned into ROM.

• Example : printer, keyboard, video game player

Embedded System

Page 15: Microprocessor system - summarize

The Intel 8051 microcontroller

Page 16: Microprocessor system - summarize

The Intel 8051 microcontroller system circuitry

Intel8051

Page 17: Microprocessor system - summarize

Overview of the 8051 FamilyOne of the oldest (Intel MCS-51 in 1981) and probably the most popular microcontroller. Many derivatives are marketed by a number of manufacturers

Common features,

– 8-bit processor

– 4 I/O ports each 8bits wide

– max of 64K on-chip ROM (usually 0k to 4k)

– max of 64K external data memory

– max of 64K external code memory

– 2 timers, one serial port

– 128 bytes of on-chip RAM

– various speeds from 12MHz

Clones may have different on-chip memory, timers etc

Page 18: Microprocessor system - summarize

Pin Description of the 8051

1234567891011121314151617181920

4039383736353433323130292827262524232221

P1.0P1.1P1.2P1.3P1.4P1.5P1.6P1.7RST

(RXD)P3.0(TXD)P3.1

(T0)P3.4(T1)P3.5

XTAL2XTAL1

GND

(INT0)P3.2(INT1)P3.3

(RD)P3.7(WR)P3.6

VccP0.0(AD0)P0.1(AD1)P0.2(AD2)P0.3(AD3)P0.4(AD4)P0.5(AD5)P0.6(AD6)P0.7(AD7)EA/VPPALE/PROGPSENP2.7(A15)P2.6(A14)P2.5(A13)P2.4(A12)P2.3(A11)P2.2(A10)P2.1(A9)P2.0(A8)

8051(8031)

Page 19: Microprocessor system - summarize

DIP40: plastic dual in-line package; 40 leads (600 mil)

The 8051 Package

Page 20: Microprocessor system - summarize

PLCC44: plastic leaded chip carrier; 44 leads

Page 21: Microprocessor system - summarize

LQFP44: plastic low profile quad flat package; 44 leads

Page 22: Microprocessor system - summarize

Block Diagram

CPU

On-chip RAM

On-chip ROM for program code

4 I/O Ports

Timer 0

Serial PortOSC

Interrupt Control

External interrupts

Timer 1

Timer/Counter

Bus Control

TxD RxDP0 P1 P2 P3

Address/Data

Counter Inputs

Page 23: Microprocessor system - summarize

RegistersA

B

R0

R1

R3

R4

R2

R5

R7

R6

DPH DPL

PC

DPTR

PC

Some 8051 16-bit Register

Some 8-bitt Registers of the 8051

=Temporary memory (storage)

Page 24: Microprocessor system - summarize

The 8051 Programming

Page 25: Microprocessor system - summarize

Mnemonic Operand(s) DescriptionACALL addr11 Absolute subroutine callADD A,Rn Add register to AccumulatorADD A,direct Add direct byte to AccumulatorADD A,@Ri Add indirect RAM to AccumulatorADD A,#data Add immediate data to AccumulatorADDC A,Rn Add register to Accumulator with carryADDC A,direct Add direct byte to Accumulator with carryADDC A,@Ri Add indirect RAM to Accumulator with carryADDC A,#data Add immediate data to Accumulator with carryAJMP addr11 Absolute jumpANL A,Rn AND Register to AccumulatorANL A,direct AND direct byte to AccumulatorANL A,@Ri AND indirect RAM to AccumulatorANL A,#data AND immediate data to AccumulatorANL direct,A AND Accumulator to direct byteANL direct,#data AND immediate data to direct byteANL C,bit AND direct bit to carryANL C,/bit AND complement of direct bit to carryCJNE A,direct,rel Compare direct byte to Acc and jump if not equalCJNE A,#data,rel Compare immediate to Acc and jump if not equalCJNE RN,#data,rel Compare immediate to register and jump if not equalCJNE @Ri,#data,rel Compare immediate to indirect and jump if not equalCLR A Clear AccumulatorCLR C Clear carryCLR bit Clear direct bitCPL A Complement AccumulatorCPL C Complement carryCPL bit Complement direct bitDA A Decimal Adjust AccumulatorDEC A Decrement AccumulatorDEC Rn Decrement RegisterDEC direct Decrement direct byteDEC @Ri Decrement indirect RAMDIV AB Divide A by BDJNZ Rn,rel Decrement register and jump if not zero

DJNZ direct,rel Decrement direct byte and jump if not zeroINC A Increment AccumulatorINC Rn Increment registerINC direct Increment direct byteINC @Ri Increment indirect RAMINC DPTR Increment Data PointerJB rel Jump if direct bit is setJBC bit,rel Jump if direct bit is set and clear bitJC rel Jump if carry is setJMP @A+DPTR Jump indirect relative to the DPTRJNB rel Jump if direct bit is not setJNC rel Jump if carry not setJNZ rel Jump if Accumulator is not zeroJZ rel Jump if Accumulator is zeroLCALL addr16 Long subroutine callLJMP addr16 Long jumpMOV A,Rn Move register to AccumulatorMOV A,direct Move direct byte to AccumulatorMOV A,@Ri Move indirect RAM to AccumulatorMOV A,#data Move immediate data to AccumulatorMOV Rn,A Move Accumulator to registerMOV Rn,direct Move direct byte to registerMOV RN,#data Move immediate data to registerMOV direct,A Move Accumulator to direct byteMOV direct,Rn Move register to direct byteMOV direct,direct Move direct byte to directMOV direct,@Ri Move indirect RAM to direct byteMOV direct,#data Move immediate data to direct byteMOV @Ri,A Move Accumulator to indirect RAMMOV @Ri,direct Move direct byte to indirect RAMMOV @Ri,#data Move immediate data to indirect RAMMOV DPTR,#data16 Load Data Pointer with a 16-bit constantMOV C,bit Move direct bit to carryMOV bit,C Move carry to direct bitMOVC A,@A+DPTR Move Code byte relative to DPTR to AccumulatorMOVC A,@A+PC Move Code byte relative to PC to AccumulatorMOVX A,@Ri Move external RAM (8-bit addr) to AccumulatorMOVX A,@DPTR Move external RAM (16-bit addr) to AccumulatorMOVX A,@Ri,A Move Accumulator to external RAM (8-bit addr)MOVX @DPTR,A Move Accumulator to external RAM (16-bit addr)MUL AB Multiply A and BNOP none No operationORL A,Rn OR register to AccumulatorORL A,direct OR direct byte to AccumulatorORL A,@Ri OR indirect RAM to AccumulatorORL A,#data OR immediate data to AccumulatorORL direct,A OR Accumulator to direct byteORL direct,#data OR immediate data to direct byteORL C,bit OR direct bit to carryORL C,/bit OR complement of direct bit to carryPOP direct Pop direct byte from stackPUSH direct Push direct byte onto stackRET Return from subroutineRETI Return from interruptRL A Rotate Accumulator leftRLC A Rotate Accumulator left through the carryRR A Rotate Accumulator rightRRC A Rotate Accumulator right through the carrySETB C Set carrySETB bit Set direct bitSJMP rel Short jump (relative addr)SUBB A,Rn Subtract Register from Accumulator with borrowSUBB A,direct Subtract direct byte from Accumulator with borrowSUBB A,@Ri Subtract indirect RAM from Accumulator with borrowSUBB A,#data Subtract immediate data from Acc with borrowSWAP A Swap nibbles within the AccumulatorXCH A,Rn Exchange register with AccumulatorXCH A,direct Exchange direct byte with AccumulatorXCH A,@Ri Exchange indirect RAM with AccumulatorXCHD A,@Ri Exchange low-order digit indirect RAM with AccXRL A,Rn Exclusive-OR register to AccumulatorXRL A,direct Exclusive-OR direct byte to Accumulator

The 8051 Instruction set

Page 26: Microprocessor system - summarize

Assembly Language programming:

• Hand assembly: - translate manually- key-in opcodes for instruction- time consuming & error prone.

• Assembly language.- use assembler program to generate machine codes.- easier and faster.

Page 27: Microprocessor system - summarize

Assembly Language programming:

• address / program pointer

• opcodes

• Assembly language

Page 28: Microprocessor system - summarize

Program Development

Stop

Create /editsource codes

Assemblesource codes

Syntax Errors?

Test / debugprogram

Logical Errors?

Start

Yes

Yes

No

No

Text editor

Debugger

Assembler

Page 29: Microprocessor system - summarize

Programming is both a science and art !!

Science – rules of grammar, punctuation, spelling & structure.

Art – how the words are arranged

Page 30: Microprocessor system - summarize

How to tell a lump of sand what to do:

1. study common programming techniques2. analyze example programs3. write many practice programs

Page 31: Microprocessor system - summarize

Structure of Assembly language and Running an 8051 program

ORG 0H MOV R5,#25H MOV R7,#34H MOV A,#0 ADD A,R5 ADD A,#12H END

EDITORPROGRAM

ASSEMBLERPROGRAM

LINKERPROGRAM

OHPROGRAM

Myfile.asm

Myfile.objOther obj file

Myfile.lst

Myfile.abs

Myfile.hex

Page 32: Microprocessor system - summarize

Move Data concepts:• data is stored at a source address moved to (actually, data is copied)

a destination address.

= Addressing modesAddressing modes.

Page 33: Microprocessor system - summarize

Move Data concepts:

• 24 mnemonicsfor move

•MOVMOV

•MOVXMOVX

•MOVCMOVC

Page 34: Microprocessor system - summarize

Move Data concepts:

• 2 + 4 mnemonics for :•PUSH & POPPUSH & POP

•XCHXCH

Page 35: Microprocessor system - summarize
Page 40: Microprocessor system - summarize
Page 41: Microprocessor system - summarize

MOVMOV A,#45HA,#45HMOVMOV A,R0A,R0MOVMOV A,40HA,40HMOVMOV A,@R0A,@R0MOVC A,@A+DPTRMOVC A,@A+DPTR

Immediate Addressing Register Addressing Direct Addressing Register Indirect Addressing Indexed Addressing

Modes Examples

To summarize:To summarize:

For external memory MOVX A,R3MOVX A,R3

Page 42: Microprocessor system - summarize

ExchangeXCH A,Rn Exchange register with AccumulatorXCH A,direct Exchange direct byte with AccumulatorXCH A,@Ri Exchange indirect RAM with AccumulatorXCHD A,@Ri Exchange low-order digit indirect RAM with Acc

Example:

XCH A,R3XCH A,22HXCH A,@R1XCHD A,@R1

Simulation

Page 43: Microprocessor system - summarize

Arithmetic & Logic

Page 44: Microprocessor system - summarize

ArithmeticADD A,Rn Add register to AccumulatorADD A,direct Add direct byte to AccumulatorADD A,@Ri Add indirect RAM to AccumulatorADD A,#data Add immediate data to AccumulatorADDC A,Rn Add register to Accumulator with carryADDC A,direct Add direct byte to Accumulator with carryADDC A,@Ri Add indirect RAM to Accumulator with carryADDC A,#data Add immediate data to Accumulator with carry

DIV AB Divide A by B

MUL AB Multiply A and B

SUBB A,Rn Subtract Register from Accumulator with borrowSUBB A,direct Subtract direct byte from Accumulator with borrowSUBB A,@Ri Subtract indirect RAM from Accumulator with borrowSUBB A,#data Subtract immediate data from Acc with borrow

Page 45: Microprocessor system - summarize

LogicANL A,Rn AND Register to AccumulatorANL A,direct AND direct byte to AccumulatorANL A,@Ri AND indirect RAM to AccumulatorANL A,#data AND immediate data to AccumulatorANL direct,A AND Accumulator to direct byteANL direct,#data AND immediate data to direct byteANL C,bit AND direct bit to carryANL C,/bit AND complement of direct bit to carry

ORL A,Rn OR register to AccumulatorORL A,direct OR direct byte to AccumulatorORL A,@Ri OR indirect RAM to AccumulatorORL A,#data OR immediate data to AccumulatorORL direct,A OR Accumulator to direct byteORL direct,#data OR immediate data to direct byteORL C,bit OR direct bit to carryORL C,/bit OR complement of direct bit to carry

Page 46: Microprocessor system - summarize

LogicXRL A,Rn Exclusive-OR register to AccumulatorXRL A,direct Exclusive-OR direct byte to AccumulatorXRL A,@Ri Exclusive-OR indirect RAM to AccumulatorXRL A,#data Exclusive-OR immediate data to AccumulatorXRL direct,A Exclusive-OR Accumulator to direct byteXRL direct,#data Exclusive-OR immediate data to direct byte

Example: ANL A,1AHANL 22H,#11HORL A,R1ORL A,@R1XRL A,4FHXRL 2AH,#AAH

Simulation

Page 47: Microprocessor system - summarize

ADD A, Source ;A=A+SOURCE

ADD A,#6 ;A=A+6

ADD A,R6 ;A=A+R6

ADD A,6 ;A=A+[6] or A=A+R6

ADD A,0F3H ;A=A+[0F3H]

Page 48: Microprocessor system - summarize

SETB bit ; bit=1CLR bit ; bit=0

SETB C ; CY=1SETB P0.0 ;bit 0 from port 0 =1SETB P3.7 ;bit 7 from port 3 =1SETB ACC.2 ;bit 2 from ACCUMULATOR =1SETB 05 ;set high D5 of RAM loc. 20h

Note:

CLR instruction is as same as SETBi.e:

CLR C ;CY=0

But following instruction is only for CLR:CLR A ;A=0

Bit Addressable Page 359,360

Page 49: Microprocessor system - summarize

SUBB A,source ;A=A-source-CY

SETB C ;CY=1SUBB A,R5 ;A=A-R5-1

ADC A,source ;A=A+source+CY

SETB C ;CY=1ADC A,R5 ;A=A+R5+1

Page 50: Microprocessor system - summarize

DEC byte ;byte=byte-1INC byte ;byte=byte+1

INC R7DEC ADEC 40H ; [40]=[40]-1

CPL A ;1’s complementExample:

MOV A,#55H ;A=01010101 BL01: CPL A ;A=10101010 B

MOV P1,AACALL DELAYSJMP L01

NOP & RET & RETI

All are like 8086 instructions.

CALL

Page 51: Microprocessor system - summarize

ANL - ORL - XRLEXAMPLE:

MOV R5,#89HANL R5,#08H

RR – RL – RRC – RLC AEXAMPLE:

RR A

Page 52: Microprocessor system - summarize

Program control instructions

Page 53: Microprocessor system - summarize

Program Control - Jump

AJMP addr11 Absolute jump

JB rel Jump if direct bit is setJBC bit,rel Jump if direct bit is set and clear bitJC rel Jump if carry is setJMP @A+DPTR Jump indirect relative to the DPTRJNB rel Jump if direct bit is not setJNC rel Jump if carry not setJNZ rel Jump if Accumulator is not zeroJZ rel Jump if Accumulator is zero

LJMP addr16 Long jump

SJMP rel Short jump (relative addr)

Page 54: Microprocessor system - summarize

Program branching instructions

Page 55: Microprocessor system - summarize

Example:

Unconditional Jump-

LOOP MOV A,#30HMOV R1,Abla…bla…AJMP LOOP

LJMPSJMP

Page 56: Microprocessor system - summarize
Page 57: Microprocessor system - summarize
Page 58: Microprocessor system - summarize

8051 Instruction SetACALL: Absolute Call

ADD, ADDC: Add Acc. (With Carry)

AJMP: Absolute Jump

ANL: Bitwise AND

CJNE: Compare & Jump if Not Equal

CLR: Clear Register

CPL: Complement Register

DA: Decimal Adjust

DEC: Decrement Register

DIV: Divide Accumulator by B

DJNZ: Dec. Reg. & Jump if Not Zero

INC: Increment Register

JB: Jump if Bit Set

JBC: Jump if Bit Set and Clear Bit

JC: Jump if Carry Set

JMP: Jump to Address

JNB: Jump if Bit Not Set

JNC: Jump if Carry Not Set

JNZ: Jump if Acc. Not Zero

JZ: Jump if Accumulator Zero

LCALL: Long Call

LJMP: Long Jump

MOV: Move Memory

MOVC: Move Code Memory

MOVX: Move Extended Memory

MUL: Multiply Accumulator by B

NOP: No Operation

ORL: Bitwise OR

POP: Pop Value From Stack

PUSH: Push Value Onto Stack

RET: Return From Subroutine

RETI: Return From Interrupt

RL: Rotate Accumulator Left

RLC: Rotate Acc. Left Through Carry

RR: Rotate Accumulator Right

RRC: Rotate Acc. Right Through Carry

SETB: Set Bit

SJMP: Short Jump

SUBB: Sub. From Acc. With Borrow

SWAP: Swap Accumulator Nibbles

XCH: Exchange Bytes

XCHD: Exchange Digits

XRL: Bitwise Exclusive OR

Undefined: Undefined Instruction

Page 59: Microprocessor system - summarize

The 8051 I/O programming

Page 60: Microprocessor system - summarize

ABITEC Application Board programming:1. Eight large LED arrays2. Eight switches array3. Two Dual seven segment displays4. DC Motor & Control5. Semiconductor temperature sensor6. Speaker7. Heater control circuit8. Telephone type keypad matrix9. Fibre optic transmitter and receiver10. Slider potentiometer - variable analogue voltage11. An 8 bit Digital to Analogue Converter (DAC) and

comparator to enable programming of ADC functions

• Optional:• LCD interface• Stepper Motor Control (D4 - D7)

Page 61: Microprocessor system - summarize
Page 62: Microprocessor system - summarize

Time Delay•To write accurate time delay routine.•Using the DJNZ or CJNE instruction.

Page 63: Microprocessor system - summarize
Page 64: Microprocessor system - summarize

Interrupts

1. Enabling and Disabling Interrupts2. Interrupt Priority3. Writing the ISR (Interrupt Service

Routine)

Page 65: Microprocessor system - summarize

Interrupt Enable (IE) Register :

• EA : Global enable/disable.• --- : Undefined.• ET2 :Enable Timer 2 interrupt.• ES :Enable Serial port interrupt.• ET1 :Enable Timer 1 interrupt.• EX1 :Enable External 1 interrupt.• ET0 : Enable Timer 0 interrupt. • EX0 : Enable External 0 interrupt.

Page 66: Microprocessor system - summarize

Interrupt VectorsInterrupt Vector Address

System Reset 0000H

External 0 0003H

Timer 0 000BH

External 1 0013H

Timer 1 001BH

Serial Port 0023H

Timer 2 002BH

Page 67: Microprocessor system - summarize

Writing the ISRExample:

Writing the ISR for Timer0 interrupt

ORG 0000H ;resetLJMP MAINORG 000BH ;Timer0 entry point

T0ISR: . ;Timer0 ISR begins.RETI ;return to main program

MAIN: . ;main program..END