Unit 2 Combined
Unit 2 Combined
1
Introduction
A microcontroller is a small computer integrated onto a single chip.
It combines one or more CPUs (processor cores) with memory and
programmable input/output peripherals.
Microcontrollers have contributed exponentially in the development of many
computer applications, industrial instrumentation and controls.
The whole CPU of a computer gets fabricated on a single chip using LSI and VLSI
technology with the advancement of semiconductor technology.
2
Features of microcontrollers
On-chip peripherals
• Many microcontrollers have a variety of on-chip peripherals, such as timers, serial ports, and analog-to-
digital converters, which allow them to interface with external devices.
Memory
• Microcontrollers have both program memory, which stores the instructions that are executed by the
processor, and data memory, which is used to store variables and other data.
Input/output (I/O) pins
• Microcontrollers have a set of I/O pins that can be used to interface with external devices, such as sensors
or actuators.
Low power consumption
• Microcontrollers are designed to be low-power, which makes them suitable for use in battery-powered
devices.
Cost
• Microcontrollers are typically less expensive than general-purpose processors, as they are designed for
specific tasks and do not have as many capabilities
Size
• Microcontrollers are small, which makes them suitable for use in compact devices.
Flexibility
• Microcontrollers are highly flexible and can be programmed to perform a wide range of tasks.
3
Basic components of
microcontrollers
Basic components of
Microcontroller includes:
• CPU: Executes instructions and
performs calculations.
• Memory: Stores program and
data.
• I/O Ports: Interface with other
devices.
• Timers/Counters: Measure time
intervals or count events.
• Communication Interfaces:
Connect to other systems (e.g.,
UART, SPI, I2C).
4
Microprocessor
• A microprocessor is a processor that is contained on a
microchip, or integrated circuit (IC). It is a central
processing unit (CPU) that executes the instructions of
a computer program. Some features of
microprocessors include:
• Instruction set: Microprocessors have a specific
instruction set that defines the operations that they
can perform.
• Clock speed: The clock speed of a microprocessor
determines how fast it can execute instructions.
Microprocessors typically have high clock speeds,
which allows them to perform tasks quickly.
• Data bus: The data bus is a communication pathway
that is used to transfer data between the
microprocessor and other components in a system.
• Address bus: The address bus is a communication
pathway that is used to transfer the address of a
memory location between the microprocessor and
other components in a system.
• Cache: Many microprocessors have one or more levels
of cache, which is a small amount of high-speed
memory that is used to store frequently accessed data.
This helps to improve the performance of the
microprocessor.
5
Microprocessor vs
Microcontroller
6
Architecture of
Microcontrollers
7
Memory Organization
The 8051 microcontroller's memory is divided into Program
Memory and Data Memory.
The first 128 bytes of internal data memory are both directly
and indirectly addressable and the upper 128 bytes of data
memory (80-FFH) can be addressed only indirectly.
8
Pin diagram of 8051 Microcontroller
connected by its end to 5V power supply, the pins of this port have this resistor left out .
Ports in 8051
• Port 1
P1 is a true I/O port as it doesn’t have any alternative functions as
in P0, but this port can be configured as general I/O only. It has a
built-in pull-up resistor and is completely compatible with TTL
circuits.
• Port 2
P2 is similar to P0 when the external memory is used. Pins of this
port occupy addresses intended for the external memory chip.
This port can be used for higher address byte with addresses A8-
A15. When no memory is added then this port can be used as a
general input/output port similar to Port 1.
• Port 3
In this port, functions are similar to other ports except that the
logic 1 must be applied to appropriate bit of the P3 register.
Addressing Modes in 8051
Method of specifying the data to be operated by the
instruction
CPU can access the data in various ways.
-- data could be in reg./Memory/immediate value
The C in MOVC instruction refers to code byte. For the first instruction, let us
consider A holds 30H. And the PC value is1125H. The contents of program
memory location 1155H (30H + 1125H) are moved to register A.
Implied Addressing mode
In the implied addressing mode, there will be a single operand. These types
of instruction can work on specific registers only. These types of instructions
are also known as register specific instruction.
RL A;- rotate the accumulator left
SWAP A;-interchanges the low-and high-order nibbles (four-bit fields) of the
Accumulator (bits 3-0 and bits 7-4)
Instruction set of 8051
• The process of writing program for the microcontroller mainly consists of giving
instructions (commands) in the specific order in which they should be executed
in order to carry out a specific task.
• The first part of each instruction, called MNEMONIC refers to the operation an
instruction performs (copy, addition, logic operation etc.)
• Depending on operation they perform, all instructions are divided in several
groups:
– Arithmetic Instructions
– Branch Instructions
– Data Transfer Instructions
– Logic Instructions
– Bit-oriented Instructions
• Arithmetic Instructions: Arithmetic instructions perform several basic operations
such as addition, subtraction, division, multiplication etc. After execution, the
result is stored in the first operand. For example:
ADD A,R1 - The result of addition (A+R1) will be stored in the accumulator.
• Branch Instructions: There are two kinds of branch instructions:
– Unconditional jump instructions: upon their execution a jump to a new location from where the
program continues execution is executed.
– Conditional jump instructions: a jump to a new program location is executed only if a specified
condition is met. Otherwise, the program normally proceeds with the next instruction.
• Data Transfer Instructions
• Data transfer instructions move the content of one register to another. The
register the content of which is moved remains unchanged. If they have the
suffix “X” (MOVX), the data is exchanged with external memory.
• Logic Instructions
• Logic instructions perform logic operations upon corresponding bits of two
registers. After execution, the result is stored in the first operand.
• Bit-oriented Instructions
• Similar to logic instructions, bit-oriented instructions perform logic operations.
The difference is that these are performed upon single bits.
Arithmetic Instructions:
Arithmetic instructions perform
several basic operations such as
addition, subtraction, division,
multiplication etc. After
execution, the result is stored in
the first operand.
For eg: ADD A,R1
The result of addition (A+R1)
will be stored in the
accumulator.
• Branch Instructions: There
are two kinds of branch
instructions:
Unconditional jump
instructions: upon their
execution a jump to a new
location from where the
program continues
execution is executed.
Conditional jump
instructions: a jump to a
new program location is
executed only if a specified
condition is met. Otherwise,
the program normally
proceeds with the next
instruction.
Data Transfer Instructions: They
move the content of one
register to another. The register
the content of which is moved
remains unchanged. If they
have the suffix “X” (MOVX), the
data is exchanged with external
memory.
Logic Instructions : They
perform logic operations
upon corresponding bits of
two registers. After
execution, the result is stored
in the first operand.
• Bit-oriented Instructions:
Similar to logic instructions,
bit-oriented instructions
perform logic operations.
The difference is that these
are performed upon single
bits.
Program Status Word (PSW) register is an 8-bit register, also referred to as Flag
register.
Timers of 8051
• There are two timers in 8051, both are 16 bit timers . These are
up counters. They can count from 0000 to FFFF ( 0 to
65,535 ). However, in each timer, one register is not enough to
store 16bits, so both the timers have a total 4 SFRs, each SFR
to store 8 bits. To trigger these timers they require a clock
( either external or internal depends on many parameters ).
• As 8051 has 8-bit architecture, each of these 16 bit timers can
be accessed by two separate 8-bit registers.
• The TIMERS are divided into two 8-bit SFR called Timer LOW (TL0,
TL1) & Timer HIGH (TH0, TH1) & these registers contain the latest
count of the TIMER. The TIMER action is controlled by two
more SFR's called Timer Mode Control Register(TMOD) &
Timer/Counter Control Register (TCON).
• TMOD and TCON registers.
TCON register:
It is also bit addressable( each of 8 bits can be controlled individually ) . Out of 8 bits of TCON,
4 bits are for timer and the other 4 bits are for INT0 and INT1 related purposes.
TCON flag bits Description
TF1 Timer overflow flag for timer 1. Whenever a timer overflow event occurs in
timer 1, this flag becomes 1 and then interrupt is sent to the processor and
as the processor goes to Interrupt Service Routine (ISR), this flag
becomes zero.
TF0 Timer overflow flag for timer 0. Whenever a timer overflow event occurs in
timer 0, this flag becomes 1 and sends an interrupt to the processor and as
the processor goes to ISR, this flag becomes zero.
TR1 Timer run for timer 1. Whenever the TR1 flag is 1, it means enabling the
timer 1 ( enabling timer / counter depends on another flag of another
SFR ). Cleared to 0 by program to halt timer.
TR0 Timer run for timer 0. Whenever the TR0 flag is 1, it means enabling the
timer 0 ( enabling timer / counter depends on another flag of another
SFR ). Cleared to 0 by program to halt timer.
IE0 This stands for interrupt external for INT0. whenever this flag is HIGH, it
means an interrupt occurs at INT0.
IE1 This stands for interrupt external for INT1. whenever this flag is HIGH, it
means an interrupt occurs at INT1.
IT1 IT1 stands for interrupt type for INT1.
If IT1 = 0, then it is level triggering otherwise edge triggering.
TMOD is an 8-bit register used to set timer mode of timer0 and timer1.
TMOD flag bits Description
GATE 1 = Enable Timer/Counter only when the INT0/INT1 pin is high and
TR0/TR1 is set.
0 = Enable Timer/Counter when TR0/TR1 is set.
To select modes:
M1 M0 Mode
45
• Serial Communication can be : Asynchronous and Synchronous.
• Synchronous Communication: Transfer a block of data (characters) at a
time. The events are referenced to a clock. Example: SPI bus, I2C bus.
• Asynchronous Communication: Transfer a single byte at a time. There is
no clock. The bytes are separated by start and stop bits. Example: UART.
• The rate of data transfer in serial data communication is state as bps (bits
per second)/ baud rate.
• IC’s for serial communication : UART (Universal asynchronous receiver-
transmitter), USART (Universal synchronous-asynchronous receiver-
transmitter).
• 8051 uses SBUF (serial buffer) register to hold data, SCON (serial port
control) register to control data communication and PCON (power mode
control) register to control data rates.
46
SCON register:
It is a bit addressable register used to set the mode in which serial communication takes place in
the controller
Interrupt
• An interrupt is a signal to the processor emitted by hardware or software
indicating an event that needs immediate attention.
• Whenever an interrupt occurs, the controller completes the execution of the
current instruction and starts the execution of an Interrupt Service Routine (ISR)
or Interrupt Handler.
• ISR tells the processor or controller what to do when the interrupt occurs. The
interrupts can be either hardware interrupts or software interrupts.
Hardware Interrupt
• A hardware interrupt is an electronic alerting signal sent to the processor from an
external device, like a disk controller or an external peripheral.
• For example, when we press a key on the keyboard or move the mouse, they
trigger hardware interrupts which cause the processor to read the keystroke or
mouse position.
Software Interrupt
• A software interrupt is caused either by an exceptional condition or a special
instruction in the instruction set which causes an interrupt when it is executed
by the processor.
• For example, if the processor's arithmetic logic unit runs a command to divide a
number by zero, to cause a divide-by-zero exception, thus causing the computer
to abandon the calculation or display an error message.
What is Polling?
• The state of continuous monitoring is known as polling. The microcontroller
keeps checking the status of other devices; and while doing so, it does no other
operation and consumes all its processing time for monitoring. This problem can
be addressed by using interrupts.
• In the interrupt method, the controller responds only when an interruption
occurs. Thus, the controller is not required to regularly monitor the status (flags,
signals etc.) of interfaced and inbuilt devices.
Interrupt Service Routine (ISR)
• For every interrupt, there must be an interrupt service routine (ISR), or interrupt
handler.
• When an interrupt occurs, the microcontroller runs the interrupt service
routine.
• For every interrupt, there is a fixed location in memory that holds the address
of its interrupt service routine, ISR.
• The table of memory locations set aside to hold the addresses of ISRs is called
as the Interrupt Vector Table.
Interrupt Vector Table
There are six interrupts including RESET in 8051.
Stack, Stack Pointer and Program Counter
The Program Counter (PC) register keeps track of the memory location of
the current instruction, while the stack pointer (SP) register keeps track of
the top of the stack.
Steps to handle an Interrupts
There are six interrupts including RESET in 8051.
• The microcontroller closes the currently executing instruction and saves the
address of the next instruction (PC) on the stack.
• It also saves the current status of all the interrupts internally (i.e., not on the
stack).
• It jumps to the memory location of the interrupt vector table that holds the
address of the interrupts service routine.
• The microcontroller gets the address of the ISR from the interrupt vector table
and jumps to it. It starts to execute the interrupt service subroutine, which is
RETI (return from interrupt).
• Upon executing the RETI instruction, the microcontroller returns to the location
where it was interrupted. First, it gets the program counter (PC) address from
the stack by popping the top bytes of the stack into the PC. Then, it start to
execute from that address.
Enabling and Disabling the Interrupts
• Upon Reset, all the interrupts are disabled even if they are activated. The
interrupts must be enabled using software in order for the microcontroller to
respond to those interrupts.
• IE (interrupt enable) register is responsible for enabling and disabling the
interrupt. IE is a bit addressable register.
Enabling and Disabling the Interrupts
• To enable an interrupt, we take the following steps −
• Bit D7 of the IE register (EA) must be high to allow the rest of register to take
effect.
• If EA = 1, interrupts will be enabled and will be responded to, if their
corresponding bits in IE are high.
• If EA = 0, no interrupts will respond, even if their associated pins in the IE
register are high.
List of Arithmetic instructions of 8051:
• In 8051 Microcontroller there are 24 different instructions under the
Arithmetic Group.
• In total there are 64 opcodes.
• The Carry Flag (CY), Auxiliary Carry (AC) and Overflow flag (OV) are affected
based on the result of ADD, ADDC, SUBB etc. instructions.