View
34
Download
0
Category
Preview:
Citation preview
Microprocessor SystemSummarizeApr 2009
Hisham Mat HussinSenior Lecturer (Electronics)University Kuala Lumpur-BMI
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
Numerical Bases Used in Programming
• Hexadecimal
• Binary
• Decimal
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
Decimal, Binary & Hexadecimal Numbers
(43)10=
( 0010 1011 )2 =
( 2 B )16
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
General-purpose microprocessor
• 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 :
Micro-controller
Microcontroller Application :
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
Inside the MicroprocessorINTEL 4004(1971)
• 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
The Intel 8051 microcontroller system circuitry
Intel8051
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
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)
DIP40: plastic dual in-line package; 40 leads (600 mil)
The 8051 Package
PLCC44: plastic leaded chip carrier; 44 leads
LQFP44: plastic low profile quad flat package; 44 leads
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
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)
The 8051 Programming
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
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.
Assembly Language programming:
• address / program pointer
• opcodes
• Assembly language
Program Development
Stop
Create /editsource codes
Assemblesource codes
Syntax Errors?
Test / debugprogram
Logical Errors?
Start
Yes
Yes
No
No
Text editor
Debugger
Assembler
Programming is both a science and art !!
Science – rules of grammar, punctuation, spelling & structure.
Art – how the words are arranged
How to tell a lump of sand what to do:
1. study common programming techniques2. analyze example programs3. write many practice programs
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
Move Data concepts:• data is stored at a source address moved to (actually, data is copied)
a destination address.
= Addressing modesAddressing modes.
Move Data concepts:
• 24 mnemonicsfor move
•MOVMOV
•MOVXMOVX
•MOVCMOVC
Move Data concepts:
• 2 + 4 mnemonics for :•PUSH & POPPUSH & POP
•XCHXCH
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
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
Arithmetic & Logic
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
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
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
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]
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
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
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
ANL - ORL - XRLEXAMPLE:
MOV R5,#89HANL R5,#08H
RR – RL – RRC – RLC AEXAMPLE:
RR A
Program control instructions
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)
Program branching instructions
Example:
Unconditional Jump-
LOOP MOV A,#30HMOV R1,Abla…bla…AJMP LOOP
LJMPSJMP
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
The 8051 I/O programming
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)
Time Delay•To write accurate time delay routine.•Using the DJNZ or CJNE instruction.
Interrupts
1. Enabling and Disabling Interrupts2. Interrupt Priority3. Writing the ISR (Interrupt Service
Routine)
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.
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
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
Recommended