Module 1 8051 Microcontroller 1
Module 1 8051 Microcontroller 1
Module 1 8051 Microcontroller 1
Index
• Introduction
• Microprocessor Vs Microcontroller
• 8051 Architecture
• Registers
• Pin Diagram
• Internal Memory Organization
• I/o Ports function
• External Memory (ROM & RAM ) interfacing
Important
Terminologies
Introduction
• Flip-flops:
Flip Flops is the memory element which is capable to store a single bit binary number
either 0 or 1.
Flip Flops are frequently used to store the data.
• The unit of data size:
Bit : a binary digit that can have the value 0 or 1 ex: 0 or 1
Byte : 8 bits ex: 10H or 00010000b
Nibble : half of a bye, or 4 bits ex: 8H or 1000b
Word : two bytes, or 16 bits ex: 2104H or 0010000100000100b
Introduction (contd…)
INSIDE THE COMPUTER :Internal Organization of Computers
• Memory:
RAM (Random Access Memory) –
The data is lost when computer is off.
Address bus:
The more address buses available, the larger the number of devices that can be
addressed
The number of locations with which a CPU can communicate is always equal to
2x, where x is the address lines, regardless of the size of the data bus
ex. a CPU with 24 address lines and 16 data lines can provide a total of 2^24 or
16Mbytes of addressable memory. Each location can have a maximum of 1byte
of data, since all general-purpose CPUs are byte addressable.
The address bus is unidirectional
• The smallest piece of data is the bit. The data will be transformed to its binary form.
• byte = 8 bits
• 1 kilobyte (KB) = 1024 bytes (210 =1 KB)
• 1 megabyte (MB) = 1024 kilobytes
• 1 gigabyte (GB) = 1024 megabytes
• 1 terabyte (TB) = 1024 gigabytes
• 1 petabyte (PB) = 1024 terabytes
• INSIDE THE CPU
• Registers
The CPU uses registers to store information temporarily
Values to be processed.
Address of value to be fetched from memory.
In general, the more and bigger the registers, the better the CPU.
Registers can be 8-, 16-, 32-, or 64-bit.
• ALU (arithmetic/logic unit)
Performs arithmetic functions such as add, subtract, multiply, and divide, and logic
functions such as AND, OR, and NOT
• Program counter
Points to the address of the next instruction to be executed.
As each instruction is executed, the program counter is incremented to point to the
address of the next instruction to be executed
• Instruction decoder
Interprets the instruction fetched into the CPU.
Block Diagram of a Microprocessor
Arithmetic
And
Logic Unit
Accumulator
Working
Register(s)
Program
Counter Stack Pointer
RAM ROM
Interrupt
Stack Pointer Circuits
8051
8751
AT89C51RD2 AT89C2051
Differences Between Microprocessors and Microcontrollers cntd…
• Microcontrollers based on the Harvard Architecture have separate data bus and an
instruction bus. This allows execution to occur in parallel.
• As an instruction is being “pre-fetched”, the current instruction is executing on the data
bus.
• Once the current instruction is complete, the next instruction is ready to go.
• This pre-fetch theoretically allows for much faster execution than Von-Neumann
architecture, on the expense of complexity.
• The Harvard Architecture executes instructions in fewer instruction cycles than the Von-
Neumann architecture.
• For example, the intel MCS-51 family of microcontrollers and PIC microcontrollers
uses Harvard Architecture.
CISC vs RISC
• CISC stands for Complex Instruction Set Computer
• Each instruction does multiple low level operations like
load from memory, perform arithmetic operations and
store the result in memory.
• Smaller program size
• A large number of instructions.
• Some instructions are dedicated for special tasks.
• Large variety for addressing modes.
• Variable length instruction formats
Ex: Intel, IBM processors
• RISC-Reduced Instruction Set Computer
• Relatively few instructions (128 or less)
• Few addressing Modes
• Memory access limited to load and store
• All operations done within CPU registers.
• Fixed length instructions(typically 4 bytes),hence easy to
decode.
• Single cycle instruction execution done by overlapping fetch,
decode and execute phases of two or three instructions.
• Large number of registers in the processor.
8051-Architecture
The salient features of 8051 are the following:
1. 8-bit CPU with two registers: A (Accumulator) and register B. The accumulator register is used
in most arithmetic and logical operations. Register B is used for integer multiplication and
division.
2. A 16-bit register called Program Counter(PC): it holds the address of the next instruction to be
executed.
3. A 16-bit data pointer register(DTPR): it is used to access external memory.
4. An 8-bit Program Status Word (PSW) register: It indicates certain conditions like status of carry,
Parity, sign, AC etc, after execution of some instructions.
5. It has Internal ROM of 4K bytes used to store Program Code.
6. Internal RAM of 128 bytes divided as:
Four register Bank of 8 registers each. (8*4=32 bytes)
16 bytes-which are bit addressable. The individual bits of these bytes can be altered.
80 Bytes of general RAM memory. So in total (32+16+80=128 bytes)
7. Four 8-bit Ports(P0-P3)- A port pin is a pin , where data can be transmitted to (output) or read
(input) from an external devices.
8. Two 16-bit timers (Timer 0 and Timer 1)- the timers can be used in Timer mode when counting
internal
clock pulses and in counter mode when counting external pulses.
9. Full Duplex Serial data transmitter/Receiver Register(SBUF)-This holds the byte to be transmitted
or the
received byte, when serial communication is used.
10. Two external interrupts and three internal interrupts. Interrupts are events which interrupt the
normal
sequence of execution of instructions.
11.Control Registers: 8051 has the following control registers like viz. TCON(Timer/Counter Control),
TMOD(Timer/Counter Mode Control),SCON(Serial Port Control), PCON( Power Control), IP
(Interrupt
Priority) ,IE( Interrupt Enable Control) which control the operation of the timers, serial port and
interrupts.
12. Oscillator and Clock circuits.
8051 programming Model
Registers of 8051
1. Program Counter (PC) :
• It is a 16bit register. The PC holds the address of the next instruction to be executed.
• The instructions are fetched from the memory location addressed by the PC. After the
CPU fetches the instruction, the PC is automatically incremented to point to the address
of the next instruction.
• The content of the PC may be altered by certain instruction's like CALL,RET.
• The PC is the only register which does not have an internal address.
• By default the PC is set to 0000H on reset.
2. Data pointer Register (DPTR):
• The DPTR is a 16-bit register, made of two 8-bit register called DPH and DPL.
• It is an index register that provides access to external memory.
• DPH and DPL holds the higher order byte and lower order byte of the address.
• DPH and DPL are assigned internal addresses separately by
DPH address as 83H and DPL address as 82H.
3) A and B register:
• These two are 8 bit registers.
• The A reg is used in many operations including the addition, subtraction, integer
multiplication and division and Boolean bit manipulation.
• A reg is bit-addressable register, meaning that each bit of the accumulator can be
accessed for reading or for altering.
• A register is also used for all data transfers between 8051 and any external memory.
• The B reg is used as normal register to store 8bit data. It is also used with the register
A for multiplication and division operation.
4) Special function registers: IP,IE,SCON,PCON,TMOD,TCON etc to be explained.
PSW (Program Status Word) Register:
• The PSW register is an 8bit register. It is also referred to as the flag register.
Although the PSW is an 8bit register, only 6 bits of it are used by the 8051.
• The two unused bits are user definable flags.
• Four of the flags are called conditional flags, meaning that they indicate some
conditions that result after an instruction is executed. These are CY,AC,P and OV.
• The following is a brief explanation of the individual flag bits of PSW reg:
• CY, the carry flag(PSW.7): This flag is set whenever there is a carry out from the D7
bit. The flag bit is affected after an addition and subtraction instruction is executed.
It can be set to 1 by an instruction SETB C . Similarly it can be reset by an
instruction CLR C.
• AC, the auxillary carry flag(PSW.6): if there is a carry from D3 to D4 bit during an
ADD or SUB operation, this bit is set(1), otherwise it is reset(0). This flag is used by
an instruction that perform BCD arithmetic.
• P, the Parity flag(PSW.0): The parity flag register reflects the number of 1s in the A
register only. If the A reg contains an odd numbers of 1s then P=1 (set). If A has an
even number of 1s then P=0(reset).
• OV, the overflow flag (PSW.2): This flag is set whenever the result of a signed
number operation is too large, causing the higher order bit to overflow into the sign
bit. In general, the CY flag is used to detect errors in unsigned arithmetic
operations. The OV flag is only used to detect errors in signed arithmetic
operations.
• RS1 and RS0 (PSW.4 and PSW.3) register bank selection bits.(see the previous
slide)
• F0(PSW.5) and PSW.1 flag bits are general purpose status flag bits and can be used
by the programmer for any purpose.
Example: If the signed arithmetic is used, find the bits of
PSW are altered when 28H is added to 60H.
28H 00101000
60H 01100000
Res 10001000
• The clock frequency determines the smallest interval of time within the microcontroller,
called the pulse time P.
• It is essentially the time period of the clock pulse and is equal to 1/f.
• In 8051 two pulses constitutes a state.
• The machine cycle is defined as the smallest interval of time needed to
accomplish any simple instruction.
• In 8051, the machine cycle is made up of 6 states .
• We can calculate the time needed in sec, for an instruction to be executes as
T= (C * 12)/f
Where C= the no of machine cycles, f= crystal frequency.
12 = because in 8051 microcontroller 12 clock pulses constitute one
machine cycle.
Example: In an 8051 system, driven by 11.0592MHz clock, find the time taken for
an instruction which takes 4 Machine cycles.
T= (C * 12)/ f= (4 * 12) / 11.0592M = 4.34Microseconds
4. RST : pin 9 is the RESET pin. Upon applying a high pulse to this pin, the microcontroller
will reset and terminate all activities.
• This is often referred to as a power on reset. Activating a power on reset will cause all
values in the register to be lost.
• It will set PC to all 0s.
• In order for the RESET input to be effective, it must have a minimum duration of two
machine cycles.
• In other words, the high pulse must be high for a minimum of two machine cycles before
it is allowed to go low.
5. EA :It stands for External Access is pin 31.
• It is an input pin and must be connected either to Vcc or GND .
• This pin is connected to Vcc to indicate that the program code is stored in the
microcontroller’s onchip ROM.
• To indicate that the program code has to be accessed from external memory, this pin
must be connected to GND.
• In 8031, where internal ROM is not there, it must be grounded.
6 PSEN : It is the Program Store Enable pin 29. It is an active low output pin signal . When
it has to access program code from an external ROM, it is connected to the Enable pin (OE)
of the ROM chip.
7 ALE: Address latch Enable(pin 30): It is an output pin and is active high.
• The ALE pin is used for demultiplexing the address and data by connecting to the G
pin of the 74LS373 chip.
• The ALE signal determines if the lines AD0-AD7 are used for address or data.
• If the ALE is high then the lines are used to transfer the lower order byte of
address. If the ALE is low, then the lines are used for data transfer.
8. Ports 0,1, 2 and 3: There are four ports P0, P1, P2 and P3 each use 8 pins, making them
8bit ports.
• All the ports upon reset are configured as input port, since P0-P3 have value FFH on them.
• P0: Port 0 is also designated as AD0-AD7, allowing to be used for both address and data.
• P1: dedicated for I/O
• P2: I/O and higher order address lines
• P3 as I/O along with the following functions
Ports
• Port 0
P3.0—–RXD
P3.1—– TXD
P3.2—– INT0 BAR
P3.3—– INT1 BAR
P3.4—– T0
P3.5—– T1
P3.6—– WR BAR
P3.7—– RD BAR
External Memory Interface