Documente Academic
Documente Profesional
Documente Cultură
Contents:
Introduction
Block Diagram of 8051 and its architecture Registers Memory organization in 8051 Addressing modes
Instruction set
Timers Interrupts
Microprocessor
Micro controller
CPU with integrated memory and peripherals
8031
8032 8051 8052 8751
None
None 4k*8 ROM 8k*8 ROM
Timer 1 Timer 0
Counter Inputs
CPU
Serial Port
OSC
Bus Control
4 I/O Ports
8 8 8 8
P0 P2 P1 P3
TxD RxD
Address/Data
Registers
8-bit registers General purpose: R0, R1, ... R7 Special function: A, B, PSW, SP, I/O ports(p0,p1,p2,p3),scon,ie,ip,tcon,tmod..
32 I/O pins arranged as four 8 bit ports (P0 P3) 2 16-bit timer/counters: T0 and T1 Full duplex serial data receiver/transmitter: SBUF Control registers: TCON, TMOD, SCON, PCON, IP and IE 2 external and 3 internal interrupt sources Oscillator and clock circuits
P3.0: RXD Serial asynchronous communication input or Serial synchronous communication output.
P3.1: TXD Serial asynchronous communication output or Serial synchronous communication clock output. P3.2: INT0 Interrupt 0 input. P3.3: INT1 Interrupt 1 input.
8051 memory
8051 implements a separate memory space for programs (code) and data. Both code and data may be internal, however, both expand using external components to a maximum of 64K code memory and 64K data memory. Internal memory consists of on-chip ROM and on-chip data RAM Memory capacities : Internal RAM-128 Bytes Internal ROM-4 K Bytes External RAM-64 K Bytes External ROM-64 K Bytes
FFFFH
FFFFH
PSEN
EXTERNAL RAM SFR 80H memory 7FH INTERNAL RAM 00H FFH 0FFFH INTERNNAL ROM
EXTERNAL ROM
0000H
0000H
0000H
EA
External ROM is accessed whenever the EA(external access) pin is connected to ground.
MOV P1,A
;move A to port 1
Note 1:
MOV A,#72H MOV A,72H After instruction MOV A,72H the content of 72th byte of RAM will replace in Accumulator.
Note 2:
MOV A,R3 MOV A,3
ADD A, Source
ADD A,#6 ADD A,R6
;A<=A+SOURCE
;A<=A+6 ;A<=A+R6
ADD A,6
ADD A,0F3H
;A<=A+[6] or A<=A+R6
;A<=A+[0F3H]
SUBB
SUBB
A, Source
A,#6
;A<=A-SOURCE-C
;A=A-6-c
SUBB
A,R6
;A=A+R6-c
; 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
CLR CLR
C A
DEC INC
INC DEC DEC
byte byte
R7 A 40H
;byte=byte-1 ;byte=byte+1
; [40]=[40]-1
RR RL RRC RLC A
EXAMPLE: RR A RR: RRC: RL: RLC:
C
C
CPL
Example: MOV L01: CPL MOV ACALL SJMP
;1s complement
A,#55H ;A=01010101 B A P1,A DELAY L01
Bit-Addressable RAM
20H 1FH 18H 17H 10H 0FH 08H 07H 00H
Example: MOV MOV MOV PUSH PUSH PUSH R6,#25H R1,#12H R4,#0F3H 6 1 4
SP=08H
SP=09H
SP=08H
Jump if A=0
Jump if A/=0 Decrement and jump if A/=0
CJNE A,byte
CJNE reg,#data JC JNC JB JNB JBC
Jump if A/=byte
Jump if byte/=#data Jump if CY=1 Jump if CY=0 Jump if bit=1 Jump if bit=0 Jump if bit=1 and clear bit
DJNZ:
Write a program to clear ACC, then add 3 to the accumulator ten times Solution: MOV MOV ADD DJNZ MOV A,#0 R2,#10 A,#03 R2,AGAIN ;repeat until R2=0 (10 times) R5,A
AGAIN:
LJMP(long jump) LJMP is an unconditional jump. It is a 3-byte instruction. It allows a jump to any memory location from 0000 to FFFFH. AJMP(absolute jump) In this 2-byte instruction, It allows a jump to any memory location within the 2k block of program memory. SJMP(short jump) In this 2-byte instruction. The relative address range of 00FFH is divided into forward and backward jumps, that is , within -128 to +127 bytes of memory relative to the address of the current PC.
CALL Instructions
Another control transfer instruction is the CALL instruction, which is used to call a subroutine.
LCALL(long call) This 3-byte instruction can be used to call subroutines located anywhere within the 64K byte address space of the 8051. ACALL (absolute call) ACALL is 2-byte instruction. the target address of the subroutine must be within 2K byte range.
Example: Write a program to copy a block of 10 bytes from RAM location starting 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
8051 timer/counter
They can be also used as counters 1. The timer is used as a time delay generator.
2. An event counter.
Timer usage
Set the initial value of registers Start the timer and then the 8051 counts up. Input from internal system clock (machine cycle) When the registers equal to 0 and the 8051 sets a bit to denote time out
8051
P2 Set Timer 0
P1 TH0 TL0
to LCD
Counter usage
Count the number of events
Show the number of events on registers External input from T0 input pin (P3.4) for Counter 0 External input from T1 input pin (P3.5) for Counter 1 External input from Tx input pin. We use Tx to denote T0 or T1.
8051
TH0 P1
TL0
P3.4 a switch T0
to LCD
TMOD Register
Timer mode register: TMOD
MOV TMOD,#21H An 8-bit register Set the usage mode for two timers Set lower 4 bits for Timer 0 (Set to 0000 if not used) Set upper 4 bits for Timer 1 (Set to 0000 if not used) Not bit-addressable
(LSB) M0
Gate
(LSB) M0
M1 M0 Mode Operating Mode 0 0 0 13-bit timer mode 8-bit THx + 5-bit TLx (x= 0 or 1) 0 1 1 16-bit timer mode 8-bit THx + 8-bit TLx 1 0 2 8-bit auto reload 8-bit auto reload timer/counter; THx holds a value which is to be reloaded into TLx each time it overflows. 1 1 3 Split timer mode
(LSB) M0
Timer modes
IE1
(LSB) IT0
IE1
(LSB) IT0
TF1
TR1
TF0
TR0
IE1
IT1
IE0
IT0
Stop timer
FFFC
FFFD
FFFF
0000
TF = 0
TF = 0 TF
TF = 0
TF = 1
8051 Interrupts
Interrupts Programming
An interrupt is an external or internal event that interrupts the microcontroller to inform it that a device needs its service. The program which is associated with the interrupt is called the interrupt service routine (ISR) or interrupt handler.
Upon executing the RETI the microcontroller returns to the place where it was interrupted. Get pop PC from stack
Interrupt Sources
Original 8051 has 6 sources of interrupts
Reset Timer 0 overflow Timer 1 overflow External Interrupt 0 External Interrupt 1 Serial Port events (buffer full, buffer empty, etc)
Interrupt Vectors
Each interrupt has a specific place in code memory where execution of interrupt service routine begins.
External Interrupt 0: Timer 0 overflow: External Interrupt 1: Timer 1 overflow: Serial : Timer 2 overflow(8052+) 0003h 000Bh 0013h 001Bh 0023h 002bh
Example
A 10khz square wave with 50% duty cycle
ORG LJMP ORG T0ISR:CPL RETI 0 MAIN 000BH P1.0
;Reset entry point ;Jump above interrupt ;Timer 0 interrupt vector ;Toggle port bit ;Return from ISR to Main program
ORG 0030H MAIN: MOV TMOD,#02H MOV TH0,#50 SETB TR0 MOV IE,#82H SJMP $ END
;Main Program entry point ;Timer 0, mode 2 ;50 us delay ;Start timer ;Enable timer 0 interrupt ;Do nothing just wait
IE1
(LSB) IT0
External Interrupts
Level-triggered (default) INT0 (Pin 3.2) 0 1 2 IT0 IE0 (TCON.3) 0003
Edge-triggered
Edge-triggered
Interrupt Priorities
What if two interrupt sources interrupt at the same time? The interrupt with the highest PRIORITY gets serviced first. All interrupts have a power on default priority order.
1. External interrupt 0 (INT0) 2. Timer interrupt0 (TF0) 3. External interrupt 1 (INT1) 4. Timer interrupt1 (TF1) 5. Serial communication (RI+TI)
IP.7: reserved IP.6: reserved IP.5: timer 2 interrupt priority bit(8052 only) IP.4: serial port interrupt priority bit IP.3: timer 1 interrupt priority bit IP.2: external interrupt 1 priority bit IP.1: timer 0 interrupt priority bit IP.0: external interrupt 0 priority bit
---
PT2
PS
PT1
PX1
PT0
PX0
A high-priority interrupt can interrupt a low-priority interrupy All interrupt are latched internally Low-priority interrupt wait until 8051 has finished servicing the high-priority interrupt
Thank you
Mail me at : kishor.mahi@gmail.com
Ph : 98495 19071