EE222 Lecture 13-24 8051 Microcontroller

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 94

Microprocessor Systems

Basic Course Outline:


• Register transfer and micro-operations
• Basic computer organization, design, & programming
• Central Processing Unit (CPU)
• 8051 microcontroller assembly+C+peripheral interfacing
• PIC18 microcontroller using C only

Dr. Memoon Sajid


Assistant Professor
Faculty of Electrical Engineering
GIK Institute of Engineering Sciences and Technology
Phone # +923468710421
Email : [email protected]
Office Hours: As displayed on door
The 8051 Microcontroller
Lecture 13
MICROPROCESSORS
• Brain of a computer of digital control device.
• The processor tells your computer what to do and
when to do it, it decides which tasks are more
important and prioritizes them to your computers
needs.
• Today Intel is the leading manufacturer of
microprocessor for desktop computers
• Other major microprocessor manufacturers are Atmel,
PIC, ARM, Siemens, Texas Instruments and many
more.
APPLICATION OF MICROPROCESSORS

• Microprocessors/controllers are found in


almost all "smart" electronic devices. 

• Microwave, automobiles, stores, mobile


phones, PDA’s, displays, robots etc.
COURSE CONTENTS
• Learn how a processor works
• How to program a microcontroller
• How to interface devices with a
microprocessor/controller
• How to design microcontroller based application

• We shall be using 8051 and PIC18F458 microcontrollers


in class to accomplish these tasks
• Students are free to use any 8 bit microcontroller to
learn these concepts 
INTRODUCTION TO COMPUTING
• Main parts of computer are
– Processor
– Memory devices
– I/O devices
Address Bus

Memory
Processor I/O Devices
RAM,ROM

Data Bus
COMPUTING DEVICES
• Desktop computers
• Embedded systems
– An embedded system is an application that
contains at least one programmable computer and
which is used by individuals who are, in the main,
unaware that the system is computer-based.
INSIDE A PROCESSOR
• Registers to store information temporarily
• ALU to perform arithmetic and logic functions
• Program Counter
• Instruction Decoder
INSIDE A PROCESSOR

Address Bus
Program Counter

Flags ALU
Instruction Register

Control Bus
Instruction
decoder, timing,
And control

Data Bus
Registers
A MICROPROCESSOR BASED SYSTEM

• Any system implemented using


microprocessor can also be implemented
using logic gates
MICROPROCESSORS vs
MICROCONTROLLERS
Microcontroller
Microprocessor
• CPU,
CPU isRAM, ROM, I/O RAM,
stand-alone, and timer
ROM,areI/O,
all on a single
timer chip
are separate
• fix amountcan
designer of decide
on-chipon
ROM,
theRAM, I/O ports
amount of ROM, RAM and I/O ports.
•• for applications in which cost, power and space are critical
expansive
•• single-purpose
versatility
• general-purpose

EE-323 MICROPROCESSOR INTERFACING


FALL 2010
MICROPROCESSORS vs MICROCONTROLLERS

Memory
Memory ROM RAM
CPU
Processor I/O
I/O Subsystems:
Timers, Counters, Analog
On a Board Interfaces, I/O interfaces

A single chip
PIC MICROCONTROLLERS
ARM MICROCONTROLLERS
• ARM7 processor family
• ARM9 processor family
• ARM11 processor family
• Cortex
ATMEL MICROCNTROLLERS
• AVR 8 bit, 32 bit
• 8051 Architecture
• ARM Based
8051 MICROCONTROLLER
OVERVIEW OF 8051 FAMILY
• 8051 was introduced by
Intel in 1981
• 8 bit microcontroller
• Popular versions of 8051
are;
– AT89C51
– DS5000
– P89C51

EE-323 MICROPROCESSOR INTERFACING


FALL 2010
INSIDE 8051 MICROCONTROLLER
Hardware Connections of 8051
Chapter 8
PIN DIAGRMS
Pin Description
• Vcc ( pin 40)
– Vcc provides supply voltage to the chip.
– The voltage source is +5V.
• GND ( pin 20 )
– ground
• XTAL1 and XTAL2 ( pins 19,18 )
Pin Description
• Using a quartz crystal oscillator
• We can observe the frequency on the XTAL2 pin.

XTAL2
30pF

XTAL1
30pF

GND

EE-323 MICROPROCESSOR INTERFACING


FALL 2010
Pin Description
• RST ( pin 9 )
– It is an input pin and is active high ( normally 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.
– Registers in RAM are set to default values.
Power On Reset Circuit
Vcc

+
10 uF
31
EA/VPP
30 pF X1
19
11.0592 MHz
8.2
K X2
30 pF 18
9 RST
Pin Description
• EA ( pin 31 )
– external 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.
• PSEN ( pin 29 )
– Program store enable
– This is an output pin and is connected to the OE pin of the ROM.
Pin Description
• ALE ( pin 30 )
– address 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.
• I/O port pins
– The four ports P0, P1, P2, and P3.
– Each port uses 8 pins.
– All I/O pins are bi-directional.
Pin Description
• 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.
• However, for a programmer, it is the same to program P0, P1, P2 and
P3.
Port 3 Alternate Functions
P3 Bit Function Pin

P3.0 RxD 10
P3.1 TxD 11
P3.2 INT0 12
P3.3 INT1 13
P3.4 T0 14
P3.5 T1 15
P3.6 WR 16
P3.7 RD 17
Memory Structure &
Addressing Modes
Chapter 5
Internal Code Memory

EE323 Microprocessor Interfacing


Fall 2010
Code Memory and Interrupt Vector
INTERRUPT ADDRESS

Reset 000H

EX0 003H

Timer 0 00BH

EX1 013H

Timer 1 01BH

Serial (UART) 023H

EE323 Microprocessor Interfacing


Fall 2010
On-Chip DATA Memory: RAM
Internal RAM
Internal Data Memory
Special Function Registers
Special Function Registers
• PSW (Program Status Word)

EE323 Microprocessor Interfacing


Fall 2010
Special Function Registers
• B register
• SP (Stack Pointer)
• DPTR (Data Pointer)
• Port registers
• TMOD , TCON
• SCON, SBUF
• IE, IP
Special Function Registers
• PCON

EE323 Microprocessor Interfacing


Fall 2010
Code Memory and Interrupt Vector
INTERRUPT ADDRESS

Reset 000H

EX0 003H

Timer 0 00BH

EX1 013H

Timer 1 01BH

Serial (UART) 023H

EE323 Microprocessor Interfacing


Fall 2010
Assembly Language of 8051
On-Chip DATA Memory: RAM
Internal RAM
Internal Data Memory
Special Function Registers
ASSEMBLY LANGUAGE PROGRAMMING OF
8051

• An assembly language code is set of instructions


• It consists of mnemonics, operands
• Assembler converts assembly language code to machine
language which involves following steps
– Code file is stored with .asm extension
– Assembler converts file into machine language (.hex)
– .lst file lists addresses of all instruction as well as errors detected
by assembler (if any)

EE323 Microprocessor Interfacing


Fall 2009
CODE IN ROM
ADDRESSING MODES
• Immediate Addressing
• Direct Addressing
• Indirect Addressing
• Register Addressing
• Indexed Addressing
IMMEDIATE ADDRESSING
• MOV register,# data
• MOV direct, # data
– MOV A,#12H ; load 12H in A
– ADD A,#23H ; add 23H in A
– MOV DPTR,#2365H
DIRECT ADDRESSING
• MOV direct,direct
• RAM location 30H-7FH is generally used by
direct addressing mode
• Stack is used by this addressing mode
• SFR’s are accessed by this mode
• MOV A,12H ; load contents of location 12H in A
REGISTER ADDRESSING
• Same as direct addressing but involves the
names of registers instead of location
– MOV Rn,A
– MOV A,Rn
– MOV R1,R2 is incorrect
– MOV 01,R2 or MOV R1,02, or MOV 01,02 is used
Indirect Addressing
• MOV A,@R0
• MOV @R0,A
• Only works with register R0 and R1
• This mode helps in dynamic access of memory
INDEXED ADDRESSING
• MOVC A,@A+DPTR
• Used to access on chip ROM
EXAMPLE
ORG 00H
MOV R0,#12H ; move 12H data in register R0
MOV A,32H ; move data from RAM location 32H to A
ADD A,R0 ; add contents of R0 and A, result is placed in A
MOV 65H,A ; store the result in RAM location 65H
END
REGISTER BANK & STACK
• Stack is accessed using PUSH and POP
instruction
ORG 00H
MOV A,#13H
MOV R0,#12H
PUSH 0E0H ; 0E0H is the address of A register in RAM
ADD A,RO
POP 02H
END
BANK SWITCHING
ORG 00H
MOV R0,#20H
SETB 00
SETB 07
MOV A,@RO
SETB RS0
ADD A,R0
PUSH 0E0
MOV 18H,R0
SETB RS1
POP 01
END
LOOP, JUMP AND CALL
INSTRUCTION
CALL INSTRUCTION
• A control transfer instruction used to call a
subroutine
• When a specific piece of code is to be
executed a number of times, it is written just
once in memory, and processed whenever
used using call instruction
LOOPS
• To repeat a set of instructions
– Finite Loops
• Program execute set of instructions for a specific
number of times
– Infinite Loops
• Program keeps running in loop

• Program branching instructions are used to


achieve this objective
JUMPS
• Unconditional Jumps
– SJMP, LJMP
• Conditional Jumps
– JZ, JNZ, DJNZ, CJNE, JC, JNC, JB, JNB, JBC
– All conditional jumps are short jumps
UNCONDITIONAL JUMPS
• Unconditional jump : JMP
• LJMP : 3 byte instruction

• SJMP : 2 byte instruction


EXAMPLE
CALL INSTRUCTION
• It’s a control transfer instruction which is used
to call subroutines

• LCALL (3 byte instruction) Long call


• ACALL (2 byte instruction) Absolute call

• Call instruction uses stack to store and retrieve


address
GENERATING TIME DELAY
• Time delay can be achieved by executing
dummy instruction for required period

• First step : Calculate no. of machine cycles required by


instruction
• Second step : Calculate the duration of one machine cycle
• Third step : Calculate the no. of times instruction has to be
executed to create required delay
I/O PORT PROGRAMMING
PORT 1, 2 & 3 (INTERNAL STRUCTURE)
PORT 0 (INTERNAL STRUCTURE)
ARITHMETIC AND LOGIC INSTRUCTIONS AND
PROGRAMMING
ADDITION & SUBTRACTION
ADDITION & SUBTRACTION
BCD NUMBER SYSTEM
DECIMAL ADJUST FOR ADDITION
• DA A

• Used after decimal addition & works only with


Accumulator
MULTIPLICATION
DIVISION
SIGNED ADDITION
Timers Programming in 8051
TIMER PROGRAMMING OF 8051
CLOCK SOURCE
Mode 1 for Delay

MOV THx,#12H ; MOV TLx,#34H)


(SETB TRx)
(JNB TFx , $)
(CLR TRx ; CLR TFx)

MOV THx,#12H ; MOV TLx,#34H)


(For small delays, it is important to keep in mind extra machine cycles contributed
by other commands, Better use Mode2)
Mode 2 for Delay
COUNTER
USE OF GATE
GATE behaves in a similar way as C/T pin. It will start the time when external pin
receives the signal given that TRx is also SET at that time

You might also like