Microcontroller 8051
Microcontroller 8051
Microcontroller 8051
8051 MICROCONTROLLER
By
Prof. Anand N. Gharu
(Assistant Professor)
PVGCOE Computer Dept.
8 330_01
Microcontroller
Micro controller
A self-contained system in which a processor,
support, memory, and input/output (I/O) are all
contained in a single package.
A small computer system on a single IC
10
History of Microcontroller
First used in 1975(Intel 8048)
The introduction of EEPROM in 1993, allowed
microcontrollers to be electrically erased
The same year, Atmel introduced the first
microcontroller using Flash memory.
Microcontroller
Types of microcontroller
Basic microcontroller architecture
Difference between microprocessor
& microcontroller
Microprocessor Microcontroller
Contains ALU, general purpose register, stack Contains the circuitary of microprocessor & in
pointer, programme counter, clock timing & addition it has built in ROM, I/O devices, timer
interrupt circuit & counter
It has too many instructions to move the data It has one or two instruction to move the data
between CPU & memory between CPU & memory
It has one or two bit handling instruction It has many bit handling instruction
Access time for memory & I/O devices is more Less access time for built in memory & I/O
devices
Microprocessor based system requires more Microcontroller based system requires less
hardware hardware, reducing PCB size & increasing the
reliability
More flexible in design point of view Less flexible in design point of view
It has single memory map for data & code It has separate memory map for data & code
Less number of pins are malfunctioned More number of pins are malfunctioned
Disadvantages of microprocessor
The overall system cost is high
A large sized PCB is required for assembling
all the components
Overall product design requires more time
Physical size of the product is big
A discrete components are used, the system is
not reliable
SJCET
Advantages of Microcontroller
based System
As the peripherals are integrated into a single chip,
the overall system cost is very less
The product is of small size compared to micro
processor based system
The system design now requires very little efforts
As the peripherals are integrated with a
microprocessor the system is more reliable
Though microcontroller may have on chip
ROM,RAM and I/O ports, addition ROM, RAM I/O
ports may be interfaced externally if required
On chip ROM provide a software security
Salient Features
(1). 8 bit microcontroller originally developed by Intel in 1980.
(2). High-performance CMOS Technology.
(3). Contains Total 40 pins.
(4). Address bus is of 16 bit & data bus is of 8 bit.
(5). 4K bytes internal ROM (program).
(6). 128 bytes internal RAM (data).
(7). Four 8-bit I/O ports.
(8). Two 16-bit timers.
(9). Serial interface Communication.
(10). 64K external code & data memory space.
(11). 210 bit-addressable locations.
(12). Internal memory consists of on-chip ROM and on-chip data RAM.
(13). 8051 implements a separate memory space for programs (code) and data.
(14). Operating frequency is 24MHz-33MHz.
(15). +5V Regulated DC power supply is required to operate .
(16). It has four 8 bit ports, total 32 I/O lines.
(17). RAM, ROM, I/O ports, one serial port and timers are all on-chip.
(18). 6-interrupts (2 are external with 2 priority levels).
(19). Low-power Idle and Power-down Modes.
(20). Full duplex UART.
19
(21). 8051 has 21 special function registers (SFRs).
8051 Block Diagram
20
21
PROGRAM STATUS WORD
(PSW)
CY AC F0 RS1 RS0 OV P
RS R BANK SELECTION
0 S1
0 0 00H 07H BANK0
0 1 08H 0FH BANK 1
1 0 10H 17H BANK2
1 1 18H 1FH BANK 3
22
Memory Organization
The 8051 memory organization is rather complex.
The 8051 has separate address spaces for Program
Memory, Data Memory, and external RAM.
This is refereed to as a Harvard architecture.
Both program memory and external data memory are
8 bits wide and use 16 bits of address. The internal
data memory is accessed using an 8-bit address.
23
Memory Structure of 8051
24
Data memory map 8051
25
Internal Memory Organization
General
purpose
RAM area
Bit
addressable
RAM area
4 register
bank of each
8 bytes
26
Internal Memory
(1). 8051 implements a separate memory space for programs (code) and data.
(2). Both code and data may be internal, however, both expand using external
components to a maximum of 64K code memory and 64K data memory.
(3). Internal memory consists of on-chip ROM and on-chip data RAM.
(4). On-chip RAM contains a rich arrangement of general purpose storage, bit
addressable storage, register banks, and special function registers.
(5). In the 8051, the registers and input/output ports are memory mapped and
accessible like any other memory location.
(6). In the 8051, the stack resides within the internal RAM, rather than in external
RAM.
27
Register RAM memory Allocation in
8051
28
4-Register banks in the 8051
Microcontroller
29
Special Function Registers
(1). ACC
(2). B
(3). PSW
(4). SP
(5). DPTR
(5). IP
(6). PMODE
(7). PCON
(8). TMODE
(9). TCON etc. 30
Special Function Registers
(1). 8051 has 21 special function registers (SFRs) at the top of internal
RAM from address 80H to FFH.
(2). Most of the addresses from 80H to FFH are not defined, except for 21
of them.
(3). Some SFRs are both bit-addressable and byte addressable, depending
on the instruction accessing the register.
(5). All 8051 CPU registers, I/O ports, timers and other architecture
components are accessible in 8051 C through SFRs
31
B Register
32
PSW (Program Status word) / Flag Register
33
Stack Pointer
(1). Stack pointer (SP) is an 8-bit register at address 81H.
(2). It contains the address of the data item currently on top of the
stack.
(3). Stack operations include pushing data on the stack
andpopping data off the stack.
(4). Pushing increments SP before writing the data
(5). Popping from the stack reads the data and decrements the SP
(6). 8051 stack is kept in the internal RAM
(7). Depending on the initial value of the SP, stack can have
different sizes
(8). Example: MOV SP,#5FH
(9). On 8051 this would limit the stack to 32 bytes since the
uppermost address of on chip RAM is 7FH. 34
Data pointer (DPTR)
(1). Data pointer (DPTR): is used to access external data or code.
(2). DPTR is a 16 bit register at addresses 82H (low byte) and 83H (high
byte).
(3). The data pointer is used in operations regarding external RAM and
some instructions involving code memory.
(4). Example: the following instructions write 55H into external RAM
location 1000H:
MOV A,#55H
MOV DPTR,#1000H
MOVX @DPTR,A 35
I/O Ports
(1). One of the major features of a microcontroller is the versatility built
into the I/O circuits that connect the microcontroller to the outside
world .
(2). To be commercially viable, the 8051 had to incorporate as many I/O
functions as were technically and economically possible.
(3). One of the most useful features of the 8051 is four bidirectional I/O
ports.
(4). Each port has an 8-bit latch in the SFR space as mentioned earlier.
(5). To reduce the overall package pin count, the 8051 employs multiple
functions for each port.
(6). Each port also has an output drive and an input buffer.
(7). These ports can be used to general purpose I/O, as an address and
data lines.
(8). The four 8-bit I/O ports P0, P1, P2 and P3 each uses 8 pins 36
I/O Ports
37
PORT 0
(1). Port 0 is 8-bitbidirectional I/O port.
(3). Port 0 is also the multiplexed low-order address and data bus
during accesses to external program and data memory.
39
PORT 2
(1). Port 2 is an 8-bit bidirectional I/O port.
(2). Port 2 emits the high-order address byte during fetches from
external program memory and during accesses to external data
memory that use 16-bit addresses (MOVX @DPTR).
42
IMPORTANT PINS (IO Ports)
One of the most useful features of the 8051 is that it contains
four I/O ports (P0 - P3)
Each port can be used as input or output (bi-direction)
Port 0
pins 32-39 P0.0P0.7
o 8-bit R/W - General Purpose I/O
o Or acts as a multiplexed low byte
address and data bus for external
memory design
IMPORTANT PINS (IO Ports)
Port 1
pins 1-8 P1.0P1.7
o Only 8-bit R/W - General Purpose
I/O
IMPORTANT PINS (IO Ports)
Port 2
pins 21-28P2.0P2.7
o 8-bit R/W - General Purpose
I/O
o Or high byte of the address
bus for external memory
design
IMPORTANT PINS (IO Ports)
Port 3
pins 10-17 P3.0P3.7
o General Purpose I/O
o if not using any of the internal
peripherals (timers) or external
interrupts.
Port 3 Alternate Functions
ALE - Address latch enable
to select valid address4
Register MOV A, B
Example:
MOV R0, A
Examples:
Example:
Example:
ACALL PORT_INIT ;PORT_INIT should be
;located within 2k bytes.
Example:
LCALL TIMER_INIT ;TIMER_INIT address (16-bits
;long) is specified as the
;operand; In C, this will be a
;function call: Timer_Init().
TIMER_INIT: ORL TMOD,#01H ;TIMER_INIT subroutine
Indexed Addressing
The Indexed addressing is useful when there is a need to retrieve data from a
look-up table
A 16-bit register (data pointer) holds the base address and the accumulator
holds an 8-bit displacement or index value
The sum of these two registers forms the effective address for a JMP or
MOVC instruction
Example:
MOV A,#08H ;Offset from table start
MOV DPTR,#01F00H ;Table start address
MOVC A,@A+DPTR ;Gets target value from the table
;start address + offset and puts it
;in A.
After the execution of the above instructions, the program will branch to
address 1F08H (1F00H+08H) and transfer into the accumulator the data
byte retrieved from that location (from the look-up table)
8051 Instruction
Instruction Types
The C8051F020 instructions are divided into five
functional groups:
o Arithmetic operations
o Logical operations
o Data transfer operations
o Boolean variable operations
o Program branching operations
Arithmetic Operations
With arithmetic instructions, the C8051F020 CPU has no special knowledge of
the data format (e.g. signed binary, unsigned binary, binary coded decimal,
ASCII, etc.)
The appropriate status bits in the PSW are set when specific conditions are met,
which allows the user software to manage the different data formats
Examples:
internal RAM location and an MOV @Ri, #data [@Ri] = immediate data
SFR location without going MOV DPTR, #data 16 [DPTR] = immediate data
addressing
MOVX A,@DPTR A = Data byte from external ram [@DPTR]
The upper 128 bytes of data
MOVX @Ri, A External[@Ri] = A
RAM are accessed only by MOVX @DPTR,A External[@DPTR] = A
indirect addressing and the PUSH direct Push into stack
The operations include set, clear, CLR bit Clear direct bit
Also included are bitlevel moves or SETB bit Set direct bit
conditional jump instructions CPL C Complement c
All bit accesses use direct addressing CPL bit Complement direct bit
CJNE @Ri,#data,rel
DJNZ Rn,rel
Decrement and Jump if Not Zero
DJNZ direct,rel
NOP No Operation
Timers and Counters
(1). Many microcontroller applications require the counting of external events, such as
frequency of a pulse train, or the generation of precise internal time delays
between actions.
(3). The 8051 has two 16-bit registers that can be used as either timers or counters.
(4). These two up counters are name T0 and T1 and are provided for general use of
the programmer.
(5). Each counter may be programmed to count internal clock pulses, act as a timer, or
programmed to count external events as a counter.
(6). The counters are divided into two 8-bit registers called the timer low (TL0, TL1)
and timer high (TH0, TH1) bytes. 67
TCON (Timer/Counter Control
Register)
68
TMOD (Timer/Counter Control Register)
69
SCON (Serial Port Control Register)
70
PCON (Power Mode Control Register)
71
Interrupts
An interrupt is a special feature which Allows the 8051 to provide the illusion of
"multitasking, although in reality the 8051 is only doing one thing at a time. The
word "interrupt" can often be substituted with the word "event.
Whenever any device needs its service, the device notifies the microcontroller by
sending it an interrupt signal.
2 TF0 000BH
3 IE1 0013H
4 TF1 001BH
5 SERIAL 0023H 73
Interrupt Priority (IP) SFR
74
Interrupt Enable (IE) SFR
75
Thank You
8/27/2017 76