Section 1 Microprocessors Course Dr. S.O.Fatemi By: Mahdi Hassanpour
Section 1 Microprocessors Course Dr. S.O.Fatemi By: Mahdi Hassanpour
Section 1 Microprocessors Course Dr. S.O.Fatemi By: Mahdi Hassanpour
Contents:
Introduction Block Diagram and Pin Description of the 8051 Registers Some Simple Instructions Structure of Assembly language and Running an 8051 program Memory mapping in 8051 8051 Flag bits and the PSW register Addressing Modes 16-bit, BCD and Signed Arithmetic in 8051 Stack in the 8051 LOOP and JUMP Instructions CALL Instructions I/O Port Programming
Mahdi Hassanpour
Introduction
General-purpose microprocessor
CPU for Computers No RAM, ROM, I/O on CPU chip itself ExampleIntels x86, Motorolas 680x0
Data Bus
RAM
ROM
I/O Port
Timer
Microcontroller :
A smaller computer On-chip RAM, ROM, I/O ports... ExampleMotorolas 6811, Intels 8051, Zilogs Z8 and PIC 16X
CPU
RAM ROM
A single chip
I/O Port
Serial Timer COM Port Microcontroller
Mahdi Hassanpour
Mahdi Hassanpour
Embedded System
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. Exampleprinter, keyboard, video game player
Mahdi Hassanpour
Mahdi Hassanpour
Block Diagram
External interrupts Interrupt Control On-chip ROM for program code
Timer/Counter
On-chip RAM
Timer 1 Timer 0
Counter Inputs
CPU
Serial Port
OSC
Bus Control
4 I/O Ports
P0 P1 P2 P3
TxD RxD
Address/Data
Sunday, February 03, 2013 Mahdi Hassanpour
Feature 8051 ROM (program space in bytes) 4K RAM (bytes) 128 Timers 2 I/O pins 32 Serial port 1 Interrupt sources 6
8052 8K 256 3 32 1 8
8031 0K 128 2 32 1 6
Mahdi Hassanpour
Mahdi Hassanpour
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
8051 (8031)
40 39 38 37 36 35 34 33 32 31 30 29 28 27 26 25 24 23 22 21
Vcc P0.0(AD0) P0.1(AD1) P0.2(AD2) P0.3(AD3) P0.4(AD4) P0.5(AD5) P0.6(AD6) P0.7(AD7) EA/VPP ALE/PROG PSEN P2.7(A15) P2.6(A14) P2.5(A13) P2.4(A12) P2.3(A11) P2.2(A10) P2.1(A9) P2.0(A8)
Mahdi Hassanpour
Pins of 80511/4
Vccpin 40 Vcc provides supply voltage to the chip. The voltage source is +5V. GNDpin 20ground XTAL1 and XTAL2pins 19,18 These 2 pins provide external clock. Way 1using a quartz crystal oscillator Way 2using a TTL oscillator Example 4-1 shows the relationship between XTAL and the machine cycle.
Mahdi Hassanpour
Pins of 80512/4
RSTpin 9reset It is an input pin and is active highnormally low. The high pulse must be high at least 2 machine cycles. It is a power-on reset. Upon applying a high pulse to RST, the microcontroller will reset and all values in registers will be lost. Reset values of some 8051 registers Way 1Power-on reset circuit Way 2Power-on reset with debounce
Mahdi Hassanpour
Pins of 80513/4
/EApin 31external access There is no on-chip ROM in 8031 and 8032 . The /EA pin is connected to GND to indicate the code is stored externally. /PSEN ALE are used for external ROM. For 8051, /EA pin is connected to Vcc. / means active low. /PSENpin 29program store enable This is an output pin and is connected to the OE pin of the ROM. See Chapter 14.
Mahdi Hassanpour
Pins of 80514/4
ALEpin 30address latch enable It is an output pin and is active high. 8051 port 0 provides both address and data. The ALE pin is used for de-multiplexing the address and data by connecting to the G pin of the 74LS373 latch. I/O port pins The four ports P0, P1, P2, and P3. Each port uses 8 pins. All I/O pins are bi-directional.
Mahdi Hassanpour
Mahdi Hassanpour
XTAL2
XTAL1
GND
Mahdi Hassanpour
Example :
Find the machine cycle for (a) XTAL = 11.0592 MHz (b) XTAL = 16 MHz. Solution:
(a) 11.0592 MHz / 12 = 921.6 kHz; machine cycle = 1 / 921.6 kHz = 1.085 s (b) 16 MHz / 12 = 1.333 MHz; machine cycle = 1 / 1.333 MHz = 0.75 s
Mahdi Hassanpour
19
Mahdi Hassanpour
31 10 uF 30 pF
EA/VPP X1
X2 RST 9 8.2 K
Mahdi Hassanpour
Registers
A B R0 R1 R2 R3 R4 R5 R6 Some 8051 16-bit Register PC PC DPTR DPH DPL
Mahdi Hassanpour
; dest = source
;A=72H ;A=r OR 72H ;R4=62H ;B=the content of F9th byte of RAM
Note 1:
MOV A,#72H After instruction MOV MOV A,72H A,72H the content of 72th byte of RAM will replace in Accumulator.
8086
MOV MOV MOV MOV AL,72H AL,r BX,72H AL,[BX]
8051
MOV MOV MOV A,#72H A,#r A,72H
Note 2:
MOV A,R3 MOV A,3
Mahdi Hassanpour
ADD A, Source
;A=A+SOURCE
Mahdi Hassanpour
SETB CLR
SETB SETB SETB SETB SETB
Note:
bit bit
C P0.0 P3.7 ACC.2 05
; bit=1 ; bit=0
; CY=1 ;bit 0 from port 0 =1 ;bit 7 from port 3 =1 ;bit 2 from ACCUMULATOR =1 ;set high D5 of RAM loc. 20h
Bit Addressable Page 359,360
CLR instruction is as same as SETB i.e: CLR C ;CY=0 But following instruction is only for CLR: CLR A ;A=0
Sunday, February 03, 2013 Mahdi Hassanpour
SUBB
SETB C SUBB A,R5
A,source ;A=A-source-CY
;CY=1 ;A=A-R5-1
ADC
SETB C ADC
A,source ;A=A+source+CY
;CY=1 A,R5 ;A=A+R5+1
Mahdi Hassanpour
DEC INC
INC DEC DEC
byte byte
R7 A 40H
;byte=byte-1 ;byte=byte+1
; [40]=[40]-1
CPL
Example:
A
MOV CPL MOV ACALL SJMP
;1s complement
A,#55H ;A=01010101 B A P1,A DELAY L01
L01:
CALL
Mahdi Hassanpour
ANL - ORL - XRL EXAMPLE: MOV R5,#89H ANL R5,#08H RR RL RRC RLC EXAMPLE:
RR
Sunday, February 03, 2013
A
Mahdi Hassanpour
Myfile.abs
OH PROGRAM Myfile.hex
Mahdi Hassanpour
8k
0000H
32k
1FFFH
8752 AT89C52
7FFFH
Mahdi Hassanpour
30H
2FH
Bit-Addressable RAM 20H 1FH 18H 17H 10H 0FH 08H 07H 00H Register Bank 2 (Stack) Register Bank 1 Register Bank 3
Register Bank 0
Mahdi Hassanpour
Carry flag Auxiliary carry flag Available to the user for general purpose Register Bank selector bit 1 Register Bank selector bit 0 Overflow flag User define bit Parity flag Set/Reset odd/even parity
CY AC -RS1 RS0 OV -P
RS1 0 0 1 1
RS0 0 1 0 1
Register Bank 0 1 2 3
Mahdi Hassanpour
Note: X can be 0 or 1
Mahdi Hassanpour
Addressing Modes
Immediate Register Direct Register Indirect Indexed
Mahdi Hassanpour
Mahdi Hassanpour
MOV MOV
DPTR, A Rm, Rn
Mahdi Hassanpour
In other word, the content of register R0 or R1 is sources or target in MOV, ADD and SUBB insructions. Example: Write a program to copy a block of 10 bytes from RAM location sterting at 37h to RAM location starting at 59h. Solution: MOV R0,37h MOV R1,59h MOV R2,10 L1: MOV A,@R0 MOV @R1,A INC R0 INC R1 DJNZ R2,L1
jump
Mahdi Hassanpour
Example: Assuming that ROM space starting at 250h contains Hello., write a program to transfer the bytes into RAM locations starting at 40h. Solution: ORG 0 MOV DPTR,#MYDATA MOV R0,#40H L1: CLR A MOVC A,@A+DPTR JZ L2 MOV @R0,A INC DPTR INC R0 SJMP L1 L2: SJMP L2 ;------------------------------------ORG 250H MYDATA: DB Hello,0
END Notice the NULL character ,0, as end of string and how we use the JZ instruction to detect that.
Sunday, February 03, 2013 Mahdi Hassanpour
Example: Write a program to get the x value from P1 and send x2 to P2, continuously . Solution: ORG 0 MOV DPTR, #TAB1 MOV A,#0FFH MOV P1,A L01: MOV A,P1 MOVC A,@A+DPTR MOV P2,A SJMP L01 ;---------------------------------------------------ORG 300H TAB1: DB 0,1,4,9,16,25,36,49,64,81 END
Mahdi Hassanpour
;B|A = A*B
;A=2, B=5
Mahdi Hassanpour
Bit-Addressable RAM
20H 1FH 18H 17H 10H 0FH 08H 07H 00H
Mahdi Hassanpour
Example: MOV MOV MOV PUSH PUSH PUSH R6,#25H R1,#12H R4,#0F3H 6 1 4
SP=08H
SP=09H
SP=08H
Mahdi Hassanpour
Mahdi Hassanpour
Jump if A=0
Jump if A/=0 Decrement and jump if A/=0 Jump if A/=byte Jump if byte/=#data Jump if CY=1 Jump if CY=0
JB
JNB JBC
Jump if bit=1
Jump if bit=0 Jump if bit=1 and clear bit
Mahdi Hassanpour
CJNE , JNC
Exercise: Write a program that compare R0,R1. If R0>R1 then send 1 to port 2, else if R0<R1 then send 0FFh to port 2, else send 0 to port 2.
Mahdi Hassanpour
CALL Instructions
Another control transfer instruction is the CALL instruction, which is used to call a subroutine. LCALL(long call) In this 3-byte instruction, the first byte is the opcode an the second and third bytes are used for the address of target subroutine. Therefore, LCALL can be used to call subroutines located anywhere within the 64K byte address space of the 8051.
Sunday, February 03, 2013 Mahdi Hassanpour
Port 1 is denoted by P1. P1.0 ~ P1.7 We use P1 as examples to show the operations on ports. P1 as an output port (i.e., write CPU data to the external pin) P1 as an input port (i.e., read pin data into CPU bus)
Mahdi Hassanpour
A Pin of Port 1
Read latch
TB2
Vcc
Load(L1)
P1.X
Clk Q
P1.X pin M1
P0.x
8051 IC
Mahdi Hassanpour
Tri-state Buffer
Output Input
Low
Highimpedance (open-circuit)
Mahdi Hassanpour
Vcc
Load(L1) 2. output pin is
Vcc 1 0
M1
P1.X
Clk Q
P1.X pin
output 1
Mahdi Hassanpour
8051 IC
Vcc
Load(L1) 2. output pin is
ground 0 1
M1
P1.X
Clk Q
P1.X pin
output 0
Mahdi Hassanpour
8051 IC
BACK:
Vcc
Q
P1.X
P1.X pin
Write to latch
Clk
M1
TB1 Read pin 3. Read pin=1 Read latch=0 Write to latch=1 8051 IC Sunday, February 03, 2013 Mahdi Hassanpour
Vcc
Q
P1.X
P1.X pin
Write to latch
Clk
M1
TB1 Read pin 3. Read pin=1 Read latch=0 Write to latch=1 8051 IC Sunday, February 03, 2013 Mahdi Hassanpour
BACK:
Mahdi Hassanpour
Description Bring into A the data at P2 pins Jump if pin P2.1 is low Jump if pin P1.3 is high Copy status of pin P2.4 to CY
Mahdi Hassanpour
Reading Latch
Exclusive-or the Port 1 MOV P1,#55H ;P1=01010101 ORL P1,#0F0H ;P1=11110101 1. The read latch activates TB2 and bring the data from the Q latch into CPU. Read P1.0=0 2. CPU performs an operation. This data is ORed with bit 1 of register A. Get 1. 3. The latch is modified. D latch of P1.0 has value 1. 4. The result is written to the external pin. External pin (pin 1: P1.0) has value 1.
Sunday, February 03, 2013 Mahdi Hassanpour
Vcc
Q
P1.X
0 M1
Clk
Read-modify-write Feature
Read-modify-write Instructions Table C-6 This features combines 3 actions in a single instruction 1. CPU reads the latch of the port 2. CPU perform the operation 3. Modifying the latch 4. Writing to the pin Note that 8 pins of P1 work independently.
Mahdi Hassanpour
Mahdi Hassanpour
Read-Modify-Write Instructions
Mnemonics
ANL ORL XRL JBC PX.Y, TARGET CPL INC DEC DJNZ PX, TARGET
Example
ANL P1,A ORL P1,A XRL P1,A JBC P1.1, TARGET CPL P1.2 INC P1 DEC P1 DJNZ P1,TARGET
MOV PX.Y,C
CLR PX.Y SETB PX.Y
Sunday, February 03, 2013
MOV P1.2,C
CLR P1.3 SETB P1.4
Mahdi Hassanpour
Mahdi Hassanpour
Other Pins
P1, P2, and P3 have internal pull-up resisters. P1, P2, and P3 are not open drain. P0 has no internal pull-up resistors and does not connects to Vcc inside the 8051. P0 is open drain. Compare the figures of P1.X and P0.X. However, for a programmer, it is the same to program P0, P1, P2 and P3. All the ports upon RESET are configured as output.
Mahdi Hassanpour
A Pin of Port 0
Read latch
TB2
P1.X
Clk Q
P0.X pin M1
P1.x
8051 IC
Mahdi Hassanpour
Mahdi Hassanpour
P0.0 DS5000 P0.1 P0.2 8751 P0.3 P0.4 8951 P0.5 P0.6 P0.7
Port 0
Mahdi Hassanpour
Mahdi Hassanpour
74LS373
PSEN ALE P0.0 P0.7
G D
74LS373
OE OC A0 A7
D0 EA P2.0 P2.7
Sunday, February 03, 2013
D7
A8 A15
Mahdi Hassanpour
8051
ROM
A7
A8 A12
8051
Sunday, February 03, 2013 Mahdi Hassanpour
ROM
74LS373
OE OC A0 A7
Address
D0 EA P2.0 P2.7
Sunday, February 03, 2013
8051
ROM
ALE Pin
The ALE pin is used for de-multiplexing the address and data by connecting to the G pin of the 74LS373 latch.
When ALE=0, P0 provides data D0-D7. When ALE=1, P0 provides address A0-A7. The reason is to allow P0 to multiplex address and data.
Mahdi Hassanpour
Mahdi Hassanpour
Function
RxD TxD INT0 INT1 T0 T1 WR RD
Mahdi Hassanpour
Pin
10 11 12 13 14 15 16 17