Embd Unit-1 Notes
Embd Unit-1 Notes
• The 8051 is an 8-bit microcontroller with 8 bit data bus and 16-bit address bus. Microprocessor based system design is Microcontroller based system design is
• The 16 bit address bus can address a 64K( 216) byte code memory space and a separate complex and expensive rather simple and cost effective
64K byte of data memory space. The instruction set of a Microcontroller is
The Instruction set of microprocessor is
• The 8051 has 4K on-chip read only code memory and 128 bytes of internal Random very simple with less number of
AccessMemory(RAM) complex with large number of
instructions. For, ex: PIC microcontrollers
• Besides internal RAM, the 8051 has various Special Function Registers (SFR) such as instructions.
have only 35 instructions.
the Accumulator, the B register, and many other control registers.
A microprocessor has zero status flag. A microcontroller has no zero flag.
• 34 8-bit general purpose registers in total.
• The ALU performs one 8-bit operation at a time.
PIN DIAGRAM OF 8051
• Two 16 bit /Counter timers
Pin out Description:
• 3 internal interrupts (one serial), 2 external interrupts.
Pins 1-8: Port 1 Each of these pins can be
• Four 8-bit I/O ports
configured as an input or an output.
• Some 8051 chips come with UART for serial communication and ADC for analog to
Pin 9: RST A logic one on this pin
digital Conversion
disables the microcontroller and clears the
contents of most registers. In other words,
Comparisons of Microcontroller and Microprocessor: the positive voltage on this pin resets the
microcontroller. By applying logic zero to
Microcontroller Microprocessor
this pin, the program starts execution from
A microprocessor is a general purpose A microcontroller is a dedicated chip which the beginning.
device which is called a CPU is also called single chip computer. Pins10-17: Port 3 Similar to port 1, each
A microcontroller includes RAM, ROM, of these pins can serve as general input or
output. Besides, all of them have
A microprocessor do not contain on chip serial and parallel interface, timers, alternative functions:
I/O Ports, Timers, Memories etc. interrupt circuitry (in addition to CPU) in a Pin10: RXD Serial asynchronous
single chip. communication input or Serial
synchronous communication output.
Microprocessors are most commonly Microcontrollers are used in small,
Pin11: TXD Serial asynchronous
used as the CPU in microcomputer minimum component designs performing
communication output or Serial
systems. control-oriented applications. synchronous communication clock output.
Pin 12: INT0 Interrupt 0 inputs. ➢ It has some amount of data RAM built in the device for internal processing. This
Pin 13: INT1 Interrupt 1 input. area is used for stack operations and temporary storage of data.
Pin 14: T0 Counter 0 clock input. ➢ 8051 is supported with on-chip peripheral functions like I/O ports, Timers/
Counters, Serial communication port.
Pin 15: T1 Counter 1 clock input.
Pin 16: WR Write to external (additional) RAM.
Pin 17: RD Read from external RAM.
Pin 18, 19: XTAL2/XTALI is for oscillator input
Pin 20: GND-Ground.
Pin 21-28: Port 2- If there is no intention to use external memory then these port pins are
configured as general inputs/outputs. In case external memory is used, the higher address
byte, i.e. addresses A8-A15 will appear on this port. Even though memory with capacity
of 64Kb is not used, which means that not all eight port bits are used for its addressing,
the rest of them are not available as inputs/outputs.
Pin 29: PSEN’- Program Store Enable. If external ROM is used for storing program, then
a logic zero(0) appears on it every time the microcontroller reads a byte from memory.
Pin 30: ALE – Address latch enable
1 – Address on AD 0 to AD 7
0 – Data on AD 0 to AD 7
Pin 31: EA’ – it indicates the presence of external memory
Figure 2: Block Diagram of 8051
Pin 32-39: Port 0 Similar to P2.
CENTRAL PROCESSING UNIT
Pin 40: VCC → +5V power supply.
➢ The CPU is the brain of the microcontrollers expected task reading user's
FUNCTIONAL BLOCK DIAGRAM OF 8051 MICROCONTROLLER programs and executing the as per instructions stored there in.
➢ The Intel 8051 contains two separate buses for both program and data. So, it has ➢ Its primary elements are an Accumulator (AC), Stack Pointer (SP) Program
two distinctive memory spaces of 64K x 8 size for both program and data. Counter (PC), Program Status Word (PSW), Data Pointer (DTPR) and few more
➢ It is based on an 8 bit central processing unit with an 8 bit accumulator and 8-bit register.
another 8-bit B register as main processing blocks. ARITHMETIC LOGIC UNIT (ALU)
➢ Other portions of the architecture include few 8 bit and 16b it registers and 8-bit ➢ The arithmetic / logic unit performs the computing functions; it includes the
memory accumulator, temporary register, arithmetic and logic circuits.
locations. ➢ The temporary register is used to hold data during an arithmetic / logic operation.
➢ The result is stored in the accumulator register.
ACCUMULATOR PROGRAM STATUS WORD (PSW)
➢ The accumulator register (ACC or A) act as an operand register, in case of some This set of flags contains the status information and is considered as one of the special
instructions. function registers.
➢ This may either be implicit or specified in the instruction. D7 D6 D5 D4 D3 D2 D1 D0
➢ The ACC register has been allotted on address in the on-chip special function PSW.7 PSW.6 PSW.5 PSW.4 PSW.3 PSW.2 PSW.1 PSW.0
register bank
CY AC FO RS1 RS0 OV --- P
Figure 3: Program Status Word (PSW)
PSW.0 : CY: Carry Flag
PSW.1 : --- : User Definable Flag
PSW.2 : OV: Overflow Flag
PSW.3 : RS0: Register Bank Select Bit 0
PSW.4 : RS1: Register Bank Select Bit 1
PSW.5 : FO: Flag 0 available for general purpose
PSW.6 : AC: Auxiliary Carry Flag
PSW.7 : CY: Carry Flag
The bits PSW.3 and PSW.4 are denoted as RS0 and RS1. These bits are used to select the
bank register of the RAM location.
Register
RS1 RS0 Address
bank
0 0 0 00H-07H
08H-
0 1 1
OFH
1 0 2 10H-17H
1 1 3 18H-1FH
Figure 3: Architecture of 8051 Table 1: selection of the register banks and their addresses
STACK POINTER (SP) • If a byte is written to SBUF, it initiates serial transmission and if the SBUF is read, it
• This 8-bit register is incremented before the data is stored onto the stack using PUSH or reads received serial data.
CALL instructions. TIMER REGISTER
• This registers contains 8-bit stack top address. The stack may be defined anywhere in • These two 16-bit register can be accessed as the lower and upper bytes.
the on-chip 128 byte RAM. After reset, the SP register is initialized to 07.
• For examples, TL0 represents the lower byte of the timing register 0, while TH0
• After each write two stack operation, the 8-bit contents of the operand are stored onto represents the upper byte of the timing register 0.
the stack, after incrementing the SP register by 1.
• Similarly, TL1 and TH1 represent lower and higher byte of the timing register 1.
• Thus if SP contains 07H, the forthcoming PUSH operation will store the date at address
• All these registers an be accessed using the four addresses allotted to them which lie in
08H in the internal RAM.
the special function register (SFR) address range,i.e.80Hto FF.
• The SP content will be incremented to 08.the 8051 stack is not a top-down data
CONTROL REGISTER
structure, like other Intel processors.
• The special function register IP, IE, TMOD, TCON, SCON and PCON contain control
• This register has also been allotted on address in th especial function register bank.
and status information for interrupts, Timer / Counters and serial port.
DATA POINTER (DTPR)
• All of the registers have been allotted addresses in the special function register bank of
• This 16-bit register contains a higher byte (DPH) and the lower byte (DPL) of a 16-bit 8051.
external data RAM address.
TIMING AND CONTROL UNIT
• It is accessed as a 16-bit register or two 8-bit registers as specified above. It has been • This unit derives all the necessary timing and control signals recovered for the internal
allotted to two address in the special function register bank, for its two bytes DPH and operation of the circuit.
DPL.
• It also derives control signals recovered for controlling the system bus.
PORT 0 TO 3 LACHES AND DRIVES
OSCILLATOR
• This four latches and driver pairs are allotted to each of the four on-chip I/O ports.
This circuit generates the basic timing clock signal for the operation of the circuit using
• These latches have been allotted address in the especial function register bank. crystal oscillator
• Using the allotted addresses, the users can communicate with these ports. These are INSTRUCTION REGISTER
identified as P0, P1 and P3.
This register decodes the Opcode of an instruction to be executed and gives information
SERIAL DATA BUFFER to the timing and control unit to generate necessary signal for the execution of the
• The serial data buffer internally contains two independent registers. instruction.
• One of them is a transmit buffer which is necessarily a parallel–in serial–out (PISO) EPROM and Program Address Register
register. This block provides an on-chip EPROM and a mechanism to internally address it.
• The other is called receive buffer which is a serial–in parallel–out (SIPO) register. (Note that EPROM is not available in all versions of 8051)
• Loading a byte to the transmit buffer initiates serial transmission of that byte.
• The serial data buffer is identified as SBUF and is one of the special function register.
RAM and RAM Address Register ADDRESSING MODES OF 8051
Theseblocks provide internal 128 bytes of RAM and a mechanism to address it internally. The addressing Modes are the ways of accessing data in register or in memory or be
provided as an immediate value. The 8051 mnemonics are written with the destination
address named first followed by the source address.
SFR (Special Function Register) Register Bank
The following addressing modes are used to access data:
• This is a set of special function registers, which can be addressed using their respective
l. Immediate addressing mode
addresses which lie in the range 80H to FFH .
2. Register addressing mode
• Finally, the interrupt, serial port and timer units control and perform their specific
function under the control of the timing and control unit. 3. Direct addressing mode
4. Register indirect addressing mode
5. Indexed addressing mode.
1. Immediate Addressing Mode
• When a source operand is a constant rather than a variable, then the constant can be
embedded into the instruction itself.
• This kind of instructions takes two bytes and first one specifies the Opcode and second
byte gives the required constant.
• The operand comes immediately after the Opcode. The mnemonic for immediate data is
the poundsign (#).
• This addressing mode can be used to load information into any of the registers
including DPTR register.
Examples:
2. Register Addressing Mode 4. Register Indirect Addressing Mode
• Register addressing accesses the eight working registers (R0 - R7) of the selected • In this mode a register is used as a pointer to the data. If the data is inside the CPU, only
register bank. registers R0 and Rl are used for this purpose.
• The least significant three bits of the instruction opcode indicate which register is to be • When R0 and Rl hold the addresses of RAM locations they must be preceded by the
used for the operation. "@" sign.
• One of the four banks of registers is to be predefined in the PSW before using register Examples:
addressing instruction. MOV @ R1, A: Move content sof A into RAM location whose address is held by R1.
• ACC, B and DPTR can also be addressed in this mode. MOV B, @ R0: Move contents of RAM location whose address is held by R0into B.
Examples:
Figure 8: IE Register
Figure : Organization of Internal RAM (IRAM) memory • Although 128 byes of the SFR address space is defined only 21 SFR registers are
defined
Register Banks: 00h to 1Fh
in the standard 8051.
The 8051 uses 8 general-purpose registers R0 through R7 (R0, R1, R2, R3, R4, R5, R6,
and R7). • Undefined SFR addresses should not be accessed as this might lead to some
ADD A, R2 ; adds the value contained in R2 to the accumulator • Note some of the SFR registers are bit addressable. SFRs are accessed just like normal
• The 8051 supports a special feature which allows access to bit variables.
• This is where individual memory bits in Internal RAM can be set or cleared.
Timer/Counters
• The 8051 has two 16-bit timer/counters. These two timer/counters can be programmed
independently.
• There is a bit in the TMOD SFR that specifies whether it is a timer or a counter.
• If this bit is set, the timer/counter will work as a counter; and
• if this bit is reset, the timer/counter will work as a timer.
TIMER MODE
• When a timer/counter is functioning as a timer, the timer register (TH 1 and/or TL1 for
Timer 1 or TH0 and/or TL0 for Timer 0) is incremented after every machine cycle.
• That is, it will be working at 1/12th of the oscillator frequency because each machine
cycle has got 12 oscillator periods.
• As an example, after starting the spin motor in a washing machine, the next operation,
i.e. the motor shut down, is performed after a fixed time interval.
MODES OF TIMER:
These timers can function in four different modes, namely: i) mode 0, ii) mode 1,iii)
mode 2 and iv) mode 3.
• These modes are achieved by setting certain bits in the TMOD-register.
• Mode 0 to mode 2 are common for both the timers but not mode 3
i) Mode 0:
• In this mode, TL0and TH0 for Timer 0 (or TLl and THl for Timer 1) are used as a 13-
bit register,
• i.e. all the 8 bits of the TL0 or TLl are utilized and the five lower most bits of the TH0
or THl are used for counting purposes.
Figure. SFR Register Layouts
• As the count rolls over from all 1s in the register to all 0s, the interrupt flag is set.
• This timer interrupt flag is a bit, namely TF0 (for Timer 0) in the TCON, which is a
special function register.
• From Figure 1, it is clear that if C/T = 0, then the register is incremented after every
machine cycle.
iii) Mode 2:
• In this mode, the timer register is 8 bits wide. TL0 for Timer 0 (or TLl for Timer 1) is
used
for this purpose (Figure 3).
• This mode is also called the auto-reload mode as the timer generates an interrupt on
overflow and after generating the interrupt, will also reload the present value from TH0
into TL0. This present value can be put in the TH0 through software.
• The interrupt flag is set when TL0 goes from all 1s to all 0s.After generating an
interrupt,
it also reloads the TL0 with the value from TH0 (80H in this case) and then starts
counting
Figure 1: Timer0, mode0 _13-bit Counter
again.
• Case 1:
TR0 = 1 (high)
Gate = 0 (low) and INT0= 0 (low)
• Case 2 :
TRO = 1 (high)
INTO = 1 (high) Gate = 1 (high)
• Mode 0 for both the timer/counters is the same, the only thing to do is to change TR0 to
TR1, INT0’ to INTl’, and Gate bit for Timer 0 to Gate bit for Timer 1.
Figure 3: Timer 0, mode 2 _ auto reload.
ii) Mode 1:
iv) Mode 3:
This mode (Figure 2) is similar to mode
0, except that in this, 16 bits, that is, full • "If the Timer 0 is put into mode 3 (Figure 4), then it acts as two 8-bit counters(TL0 and
TL0 and TH0 become two separate counters).
TH0 for Timer 0 (or TLl andTH1 for • In this case, all the Timer 0 control bits (C / T’,Gate, TRO, TFO and INT0’) are used by
Timer 1) are used for counting. So, the
TL0 itself and TH0 register is locked into a timerfunction.
interrupt flag will be
• TH0 is counting machine cycles and has taken over the use of TRl and TFl from Timer
set only when the 16 bits go from all 1s
1.
to all 0s.
• Therefore TH0 will now control Timer 1 interrupt. If the Timer 1 is put into mode 3,it
Figure 2: Timer 0, mode 1 _ 16-bit counter.
just holds the count.
• The effect is same as setting TRI = 0, hence opening the switch. • If C/T = 1, the timers function as counters to count the negative transitions' at T0 or T1
pins,
• If C/T= 0, the timers function as timers, that is, they basically count the number of
machine cycles.
• Gate = 0 means that the timer is controlled by TR1 or TR0 only, irrespective of INT0’
or INT1’.
• Gate = 1 means that the timer control will depend on INT0’or INT1’ and also on TR0
or TR1 bits.
2. TCON: Timer Control Register
Figure 6: mode1
Mode2:Multiprocessor Mode
• 11-bits are transmitted through TXD or received through RXD.
• it consists of one start bit(0),8 data bits, programmable 9thbit and a stop bit(1).
• On transmission 9th data bit is 0/1, 8-bit is loaded into SBUF and copied from TB8 in
SCON register. On reception it goes into RB8 of SCON register of SFR.
Mode 3:
• it is same as mode 2 except baud rate, since mode 3 supports variable baud rate and
calculated similar to mode1 using timer1.
Port 2 :
• Port-2 has 8-pins (P2.0-P2.7)
• Port-2 is used for higher external address byte or a normal input/output port.
• The I/O operation is similar to Port-1. Port-2 latch remains stable when Port-2 pin are
used for
external memory access.
• Here again due to internal pull-up there is limited current driving capability.
Alternate Functions of Port 3:
• P3.0 and P3.1 are used for the RxD (Receive Data) and TxD (Transmit Data) serial
communications signals.
• Bits P3.2 and P3.3 are meant for external interrupts.
• Bits P3.4 and P3.5 are used for Timers 0 and 1 and P3.6 and P3.7 are used to provide
the write
and read signals of external memories connected in 8031 based systems