Unit 2 - Microcontrollers and Programming
Unit 2 - Microcontrollers and Programming
interrupt
CPU
Data Bus (Bi directional)
Control Lines
OSC
Microcontroller
Ext int
interrupt
Timer0
Timer1
ROM RAM
CPU
Bus 4 I/o
Serial
control ports port
OSC
Microprocessors and Microcontrollers
The microprocessor is a processor on one silicon chip.
A uP system is uP plus all the components it requires to do a certain
task
The microcontrollers are used in embedded computing.
The microcontroller is a microprocessor with added circuitry.
They are a complete computer on a chip containing direct input and
output capability and memory along with the uP on a single chip.
Many times they contain other specialized application-specific
components as well
MICROPROCESSOR VS MICROCONTROLLER
Microprocessor Microcontroller
Since memory and I/O are connected Since memory and I/O are present together,
externally, the circuit becomes large in size. the internal circuit is small in size.
Cost is high Cost is low
RAM, ROM, I/O units, and other peripherals RAM, ROM, CPU and other peripherals are
are not embedded on a single chip. embedded on a single chip.
Used in personal computers. Used in embedded systems.
Uses an external bus. Uses an internal controlling bus.
Based on the Von Neumann model Based on the Harvard architecture
It is a byproduct of the development of
It is a CPU on a single silicon-based integrated
microprocessors with a CPU along with other
chip.
peripherals.
Complex and expensive due to a large number Simple and inexpensive due to less number of
of instructions to process. instructions to process.
Can run at a very high speed.
Can run up to 200MHz or more.
10
Microprocessors and Microcontrollers
Microprocessors and Microcontrollers
Microcontroller
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.
Features of Microcontroller
Some features of microcontrollers include:
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
Microcontrollers
Architecture of Microcontrollers
Architecture of Microcontrollers
CPU (Central Processing Unit): CPU act as a mind of any processing machine. It synchronizes and manages all processes that are carried out
in microcontroller. User has no power to control the functioning of CPU. It interprets the program stored in ROM and carries out from
storage and then performs it projected duty. CPU manage the different types of registers available in 8051 microcontroller.
Interrupts: Interrupts is a sub-routine call that given by the microcontroller when some other program with high priority is request for
acquiring the system buses the n interrupts occur in current running program.
• Timer 0 overflow interrupt - TF0
• Timer 1 overflow interrupt - TF1
• External hardware interrupt - INT0
• External hardware interrupt - INT1
• Serial communication interrupt - RI/TI
Memory: Microcontroller also required memory for storage of data and operands for the short duration. In microcontroller 8051 there is
code or program memory of 4 KB that is it has 4 KB ROM and it also comprise of data memory (RAM) of 128 bytes.
Bus : Bus is a group of wires which uses as a communication canal or acts as means of data transfer. The different bus configuration includes
8, 16 or more cables. Therefore, a bus can bear 8 bits, 16 bits all together.
8051 microcontrollers is consisting of 16 bit address bus. It is generally be used for transferring the data from Central Processing
Unit to Memory. The data bus is of 8 bits. It is generally be used for transferring the data from one peripherals position to other
peripherals.
Oscillator: To perform timer operation inside microcontroller it required externally connected or on-chip oscillator. Microcontroller is used
inside an embedded system for managing the function of devices. Therefore, 8051 uses the two 16 bit counters and timers. GENERATES
PULSE TIMELY
Memory Organization
The 8051 has two types of memory and these are Program Memory
and Data Memory.
Program Memory (ROM) is used to permanently save the program
being executed.
Data Memory (RAM) is used for temporarily storing data and
intermediate results created and used during the operation of the
microcontroller
Depending on the model(of 8051) in use at most a few Kb of ROM
and 128 or 256 bytes of RAM is used.
All 8051 microcontrollers have a 16-bit addressing bus and are
capable of addressing 64 kb memory.
Memory Organization
Program Memory
The first models of the 8051 microcontroller family did not have
internal program memory. It was added as an external separate chip.
Even though such an amount of memory is sufficient for writing
most of the programs, there are situations when it is necessary to
use additional memory as well. A typical example is so called
lookup tables.
They are used in cases when equations describing some processes
are too complicated or when there is no time for solving them. In
such cases all necessary estimates and approximates are executed in
advance and the final results are put in the tables
Memory Organization
Program Memory
EA=0 In this case, the microcontroller completely ignores internal program memory and executes
only the program stored in external memory.
EA=1 In this case, the microcontroller executes first the program from built-in ROM, then the
program stored in external memory. In both cases, P0 and P2 are not available for use since
being used for data and address transmission. Besides, the ALE and PSEN pins are also used.
Memory Organization
Program Memory
Memory Organization
Data Memory
Data Memory is used for temporarily storing
data and intermediate results created and used
during the operation of the microcontroller.
Besides, RAM memory built in the 8051 family
includes many registers such as hardware DIRECTLY OR
counters and timers, input/output ports, serial INDIRECTLY
data buffers etc.
Up to 256 bytes of internal data memory are
available depending on the 8051 derivative.
INDIRECTLY
These are a set of eight registers and a scratch
pad memory. These eight registers are R0 toR7.
The address range 00H to 07H is used to access
the registers, and the rest are scratch pad
memory.
Memory Organization
Data Memory
8051 Provides four register bank, but only one
register bank can be used at any point in time. To
select the register bank, two bits of PSW
(Program Status Word) are used.
The interrupt program can use one bank, and the
interrupt Service Subroutine (ISS) can access
another bank for better performance. As there are
four banks, so for nested interrupts these can be
used.
The first 128 bytes of internal data memory are
both directly and indirectly addressable. The
upper 128 bytes of data memory (from 0x80 to
0xFF) can be addressed only indirectly.
The 8051 provides 128 bytes of memory for
Special Function Registers (SFRs). SFRs are bit,
byte, or word-sized registers that are used to
control timers, counters, serial I/O, port I/O, and
peripherals
Pin diagram of 8051
Microcontroller
The 8051 microcontroller is a popular 8-bit
microcontroller widely used in embedded
systems. It is a single-chip microcontroller
with a Harvard architecture that includes a
CPU, RAM, ROM, and several peripherals.
The 8051 microcontroller has a 40-pin dual
in-line package (DIP) that provides various
inputs and outputs for communication with
external devices.
These 40 pins serve different functions like
read, write, I/O operations, interrupts etc.
8051 has four I/O ports wherein each port
has 8 pins which can be configured as input
or output depending upon the logic state of
the pins
8051 Pin configuration
TIP TO LEARN:
0-> OUTPUT
1-> INPUT
Description of Pins
Pin 1 to Pin 8 (Port 1) – Pin 1 to
Pin 8 are assigned to Port 1 for
simple I/O operations. They can be
configured as input or output pins
depending on the logic control i.e. if
logic zero (0) is applied to the I/O
port it will act as an output pin and
if logic one (1) is applied the pin
will act as an input pin. These pins
are also referred to as P1.0 to P1.7
(where P1 indicates that it is a pin
in port 1 and the number after ‘.’
tells the pin number i.e. 0 indicates
first pin of the port. So, P1.0 means
first pin of port 1, P1.1 means
second pin of the port 1 and so on).
These pins are bidirectional pins.
Description of Pins
Pin 9 (RST) – Reset pin.
It is an active-high, input pin.
Therefore if the RST pin is high
for a minimum of 2 machine
cycles, the microcontroller will
reset i.e. it will close and
terminate all activities. It is often
referred as “power-on-reset” pin
because it is used to reset the
microcontroller to it’s initial
values when power is on (high).
Description of Pins
Pin 10 to Pin 17 (Port 3) –
Pin 10 to pin 17 are port 3 pins
which are also referred to as
P3.0 to P3.7. These pins are
similar to port 1 and can be
used as universal input or
output pins. These pins are
bidirectional pins.
Description of Pins
These pins also have some additional functions which are as follows:
P3.0 (RXD) : 10th pin is RXD (serial data receive pin) which is for
serial input. Through this input signal microcontroller receives data
for serial communication.
P3.1 (TXD) : 11th pin is TXD (serial data transmit pin) which is
serial output pin. Through this output signal microcontroller
transmits data for serial communication.
P3.2 and P3.3 (INT0′, INT1′ ) : 12th and 13th pins are for
External Hardware Interrupt 0 and Interrupt 1 respectively. When
this interrupt is activated(i.e. when it is low), 8051 gets interrupted
in whatever it is doing and jumps to the vector value of the interrupt
(0003H for INT0 and 0013H for INT1) and starts performing
Interrupt Service Routine (ISR) from that vector location.
P3.4 and P3.5 (T0 and T1) : 14th and 15th pin are for Timer 0 and
Timer 1 external input. They can be connected with 16 bit
timer/counter.
P3.6 (WR’) : 16th pin is for external memory write i.e. writing data
to the external memory.
P3.7 (RD’) : 17th pin is for external memory read i.e. reading data
from external memory.
Description of Pins
Pin 18 and Pin 19 (XTAL2 And XTAL1) – These
pins are connected to an external oscillator which is
generally a quartz crystal oscillator. They are used
to provide an external clock frequency of 4MHz to
30MHz. TO CONNECT PULSE OSCILATOR
FROM OUTSIDE
Pin 20 (GND) – This pin is connected to the
ground. It has to be provided with 0V power
supply. Hence it is connected to the negative
terminal of the power supply.
Pin 21 to Pin 28 (Port 2) – Pin 21 to pin 28 are
port 2 pins also referred to as P2.0 to P2.7. When
additional external memory is interfaced with the
8051 microcontroller, pins of port 2 act as higher-
order address bytes. These pins are bidirectional.
INPUT /OUTPUT OR COMMUNICATE WITH
EXTERNAL MEMORY
Description of Pins
Pin 29 (PSEN) – PSEN stands for Program Store Enable. It
is output, active-low(0) pin. This is used to read external
memory. In 8031 based system where external ROM holds
the program code, this pin is connected to the OE pin of the
ROM.
Pin 30 (ALE/ PROG) – ALE stands for Address Latch
Enable. It is input, active-high(1) pin.USED TO DESCIDE
TO TALK WITH WHICH CHIP WHEN MANY
MEMORY CHIPS ARE ATTACHED. This pin is used to
distinguish between memory chips when multiple memory
chips are used. It is also used to de-multiplex the
multiplexed address and data signals available at port 0.
During flash programming i.e. Programming of EPROM,
this pin acts as program pulse input (PROG).
Pin 31 (EA/ VPP) – EA stands for External Access input. It
is used to enable/disable external memory interfacing. In
8051, EA is connected to Vcc as it comes with on-chip
ROM to store programs. For other family members such as
8031 and 8032 in which there is no on-chip ROM, the EA
pin is connected to the GND.
Description of Pins
Pin 32 to Pin 39 (Port 0) – Pin 32 to pin
39 are port 0 pins also referred to as P0.0 to
P0.7. They are bidirectional input/output
pins. They don’t have any internal pull-ups.
Hence, 10 K? pull-up registers are used as
external pull-ups. Port 0 is also designated
as AD0-AD7 because 8051 multiplexes
address and data through port 0 to save
pins.
Pin 40 (VCC) – This pin provides power
supply voltage i.e. +5 Volts to the circuit.
Ports in 8051
Pin configuration, i.e. the pin can be configured as 1 for
input and 0 for output as per the logic state.
Input/Output (I/O) pin − All the circuits within the microcontroller must be connected to one of its
pins except P0 port because it does not have pull-up resistors built-in.
Input pin − Logic 1 is applied to a bit of the P register. The output FE transistor is turned off and the
other pin remains connected to the power supply voltage over a pull-up resistor of high resistance.
➢ 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.
➢ RLA;- rotate the accumulator left
➢ SWAPA;-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.
1.TCON (Timer Control Register): The timers’ start and stop are aided by this register.
additionally indicates whether the timer is done counting.
2.TMOD (Timer Mode Register): The timers’ mode is adjusted using this register. It
whether the timers will record events occurring outside of the microcontroller or record
TCON (Timer Control Register) :
The 8051 microcontroller has a unique function register called the TCON (Timer
Control Register). In order to provide precise output, timers and counters are
controlled by it. The data in the registers may overflow if these timers and counters are
not under control. Thus, the TCON is utilized to control the timers and counters.
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 ISR this flag becomes
zero.
Note: Set to 1 when Timer 1 overflows else 0.
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.
Note:Set to 1 when Timer 0 overflows else 0.
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 ).
Note: Set to 1 to start Timer 1, and set to 0 to stop Timer 1.
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 ).
Note: Set to 1 to start Timer 0, and set to 0 to stop Timer 0.
IE0 This stands for interrupt external for INT0. whenever this flag is HIGH, it means an interrupt occurs
at INT0.
Note: Set to 1 when an external interrupt 0 occurs else 0.
IE1 This stands for interrupt external for INT1. whenever this flag is HIGH, it means an interrupt occurs
at INT1.
Note: Set to 1 when an external interrupt 1 occurs else 0.
IT1 IT1 refers to the interrupt type for INT1 (external interrupt 1).
If IT1 = 0, the interrupt is level triggered.
If IT1 = 1, the interrupt is edge triggered.
IT0 IT0 refers to the interrupt type for INT0 (external interrupt 0).
If IT0 = 0, the interrupt is level triggered.
If IT0 = 1, the interrupt is edge triggered.
TMOD (Timer Mode Register):
The TMOD (Timer Mode Register) is a special function register in the 8051
microcontroller. Timer 0 and Timer 1 are the modes of operation that it is utilized to set.
Whether a timer or counter needs to be set, it is done so using this register. The eight
bits of the TMOD register are split into two sections: Timer 0 is controlled by the lower
four bits, and Timer 1 is controlled by the upper four bits.
TMOD flag bits Description
To select modes:
M1 M0 Mode
• Serial transfer: In serial transfer, data is transfer to device located many meters
away this method is used for long distance data transfer. Serial communication is
mostly used for transmitting and receiving the signal. The 8051 microcontroller is
consisting of Universal Asynchronous Receiver Transmitter (UART) used for
serial communication. The signals are transmitted and received by the Rx and Tx
pins of microcontroller.
• There are two ways to transmit serial data: Simplex and Duplex.
• In simplex transmissions, the computer can only send data. There is only one wire.
• If the data can be transmitted and received, then it is a duplex transmission.
Duplex transmissions can be half or full duplex depending on whether or not the
data transfer can be simultaneous
• If the communication is only one way at a time, it is half duplex.
• If both sides can communicate at the same time, it is full duplex
• Full duplex requires two wire conductors for the data lines (in addition to the
signal ground)
65
• 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.
66
SCON register:
Serial port control and status register is the special function register SCON. It is a bit addressable
register used to set the mode in which serial communication takes place in the controller..
This register contain not only the mode selection bits but also the 9th data bit for transmit and
receive (TB8 and RB8) and the serial part interrupt bits (TI and RI)
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 ISR.
• For every interrupt, there is a fixed location in memory that holds the address
of its 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.
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 ISR.
• 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 be responded
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.