Module 1 - Microcontroller Introuction - Bec405a
Module 1 - Microcontroller Introuction - Bec405a
TEXT BOOK:
1. The “8051 Microcontroller and Embedded Systems – Using Assembly and C”, Muhammad Ali
Mazidi and Janice Gillespie Mazidi and Rollind. Mckinlay; Phi, 2006 / Pearson, 2006.
2. “The 8051 Microcontroller”, Kenneth j. Ayala, 3rd edition, Thomson/Cengage Learning.
3. “Programming And Customizing The 8051 Microcontroller”.Myke Predko Tata Mc Graw-Hill
Edition 1999 (reprint 2003).
REFERENCEBOOKS:
1. “The 8051 Microcontroller Based Embedded Systems”, Manish K Patel, McGraw Hill, 2014,
ISBN: 978-93-329-0125-4
2. “Microcontrollers: Architecture, Programming, Interfacing and System Design”, Raj Kamal,
Pearson Education, 2005.
MICROCONTROLLER: Introduction
PREPARED BY: Prof. Manjunatha M/ Prof. SANTOSH KUMAR. N – AP/ECE, SRI SAIRAM COLLEGE OF ENGG, ANEKAL 1
Regulation – 2022 (CBCS Scheme) MICROCONTROLLERS – BEC405A
• A microcontroller is a small integrated circuit, usually composed of a central processing unit, memory,
parallel input and output interfaces, a clock generator, one or more analog-to-digital converters, and
serial communication interfaces.
OR
• A microcontroller is a compact integrated circuit designed to perform a specific task in an
embedded system. It is a self-contained system with a processor, memory, I/O ports, Serial Port
Timers and Counters and other peripherals, all on a single chip.
• Microcontrollers are widely used in automatically controlled products and devices, such as
automobile engine control systems, implantable medical devices, remote controls, office machines,
appliances, power tools, toys, and other embedded systems.
Key Components of a Microcontroller:
• Central Processing Unit (CPU):
o The brain of the microcontroller that executes instructions from the memory.
• Memory:
o Flash/ROM: Stores the program code.
o RAM: Stores temporary data during program execution.
o EEPROM: Stores data that must be retained even when the power is off.
• Input/output Ports (I/O):
o Allow the microcontroller to interact with external devices, such as sensors, actuators, and
other peripherals.
• Timers/Counters:
o Used for timing operations, counting events, or generating precise time delays.
• Analog-to-Digital Converter (ADC):
o Converts analog signals (from sensors) to digital data that the CPU can process.
• Digital-to-Analog Converter (DAC):
o Converts digital data to analog signals (used in some microcontrollers).
• Communication Interfaces:
o UART, SPI, I2C: Enable communication with other devices or microcontrollers.
o USB, CAN, and Ethernet: Advanced communication protocols in some microcontrollers.
• Clock Generator:
o Provides the timing signal for the CPU and other components.
• Interrupts:
o Allow the microcontroller to respond to external or internal events in real-time.
Applications of Microcontrollers:
• Consumer Electronics: TVs, remote controls, washing machines, etc.
• Automotive: Engine control units, airbag systems, infotainment systems.
• Industrial Automation: Robotics, PLCs, motor control.
• Medical Devices: Pacemakers, glucose monitors, infusion pumps.
Criteria for Choosing a Microcontroller:
• Meeting the computing needs of the task at hand efficiently and cost effectively
• Speed
PREPARED BY: Prof. Manjunatha M/ Prof. SANTOSH KUMAR. N – AP/ECE, SRI SAIRAM COLLEGE OF ENGG, ANEKAL 2
Regulation – 2022 (CBCS Scheme) MICROCONTROLLERS – BEC405A
• Packaging
• Power consumption
• The amount of RAM and ROM on chip
• The number of I/O pins and the timer on chip
• How easy to upgrade to higher performance or lower power-consumption version
• Cost per unit
• Availability of software development tools, such as compilers, assemblers, and debuggers
• Wide availability and reliable sources of the microcontroller
• The 8051 family has the largest number of diversified (multiple source) suppliers
o Intel (original)
o Atmel
o Philips/Signetics
o AMD
o Infineon (formerly Siemens)
o Matra
o Dallas Semiconductor/Maxim
MICROCONTROLLER Vs MICROPROCESSOR
[Link] Microcontroller Microprocessor
01
PREPARED BY: Prof. Manjunatha M/ Prof. SANTOSH KUMAR. N – AP/ECE, SRI SAIRAM COLLEGE OF ENGG, ANEKAL 3
Regulation – 2022 (CBCS Scheme) MICROCONTROLLERS – BEC405A
RISC Vs CISC
[Link] RISC CISC
01 Reduced Instruction Set Computer Complex Instruction Set Computer.
02 Faster due to simple and less instructions Slower due to complex instructions and More number
of instructions.
03 Instructions are pipelined and increased execution No instruction pipelining and slow execution speed.
speed
04 Large number of registers are available Less number of general purpose register are available.
05 The operations are performed on registers only. The Operations are performed either in registers or
memory operations are load and store memory depending on the instructions
06 Single instructions , Fixed-length instructions More, variable instructions
07 Harvard architecture Harvard or Von Neumann architecture will be
followed
08 Orthogonal instructions Non – orthogonal instructions
(All instruction types use addressing mode)
09 Small, simple, and highly optimized set of Large, complex, and versatile set of instructions.
instructions.
10 Single clock cycle for most instructions Multiple clock cycles for complex instructions
11 More power-efficient due to simpler design. Less power-efficient due to complex hardware.
12 ARM, MIPS, RISC-V, AVR, PIC x86 (Intel, AMD), 8051, Z80.
13 Embedded systems, mobile devices, IoT, real-time Desktop PCs, servers, legacy systems.
applications.
14 Simpler, requires more RAM for storage More complex, requires less RAM
Single memory for both data and instructions. Separate memories for data and instructions.
02 Single bus for data and instruction transfer Separate buses for data and instruction transfer.
(shared).
03 Slower due to data and instructions compete for Faster as data and instructions can be fetched
bus access. simultaneously.
04 Simpler design with a single memory and bus More complex due to separate memories and
system buses.
05 Lower cost due to simpler hardware. Higher cost due to additional hardware (separate
memories and buses).
06 General-purpose computing Embedded systems, DSPs (Digital Signal
Processors), microcontrollers.
PREPARED BY: Prof. Manjunatha M/ Prof. SANTOSH KUMAR. N – AP/ECE, SRI SAIRAM COLLEGE OF ENGG, ANEKAL 4
Regulation – 2022 (CBCS Scheme) MICROCONTROLLERS – BEC405A
07 Von Neumann is simpler and cheaper to Harvard is more complex and expensive due to
implement the need for separate memories and buses.
08
FEATURES OF 8051:
• 8 bit CPU , 16 bit Address lines
• On chip clock oscillator
• 5 addressing modes: Immediate, Direct, Indirect, Register and Indexed addressing mode
• CISC (Complex Instruction Set Computing): Supports a rich set of instructions.
• Harvard Architecture: Separate memory for program and data
o 4Kbytes of internal program memory (code memory) [ROM]
o 128 bytes of internal data memory [RAM]
▪ Working registers [00 – 1F]
▪ Bit addressable memory area [20 – 2F]
▪ General purpose memory area (Scratch pad memory) [30-7F]
o 60 Kbytes of external program memory address space.
o 64 Kbytes of external data memory address space.
• 4 I/O Ports (P0, P1, P2, P3): Each is an 8-bit port (Total: 32 I/O lines).32 bi directional I/O lines
• Two 16-bit timers/counters:
o T0, T1 Used for timing operations, frequency generation, and event counting.
• UART (Universal Asynchronous Receiver-Transmitter):
o Supports Full Duplex serial data receiver/transmitter register (SBUF).
o SBUF holds the byte to be transmitted or the received byte, when serial communication is used.
• 4 Register banks with 8 registers in each bank.
• 16 bit Program counter (PC) and a data pointer (DPTR)
• 8 Bit Program Status Word (PSW)
• 8 Bit Stack Pointer
• Five vector interrupt structure (RESET not considered as an interrupt.)
o Two External and Three Internal Interrupt sources.
o Interrupts are events which interrupt the normal sequence of execution of instructions.
• 21 Special Function Registers (SFRs)
PREPARED BY: Prof. Manjunatha M/ Prof. SANTOSH KUMAR. N – AP/ECE, SRI SAIRAM COLLEGE OF ENGG, ANEKAL 5
Regulation – 2022 (CBCS Scheme) MICROCONTROLLERS – BEC405A
o Includes Accumulator (A), B register, Stack Pointer (SP), Program Status Word (PSW) etc
• ALU can perform arithmetic and logic functions on 8 bit variables.
• 12 MHz Default Clock Frequency: Executes one machine cycle in 12 clock cycles.
• Control Registers: TCON, TMOD, SCON, PCON, IP and IE which controls the operations of the timers,
serial port and interrupts.
Block diagram 8051
PREPARED BY: Prof. Manjunatha M/ Prof. SANTOSH KUMAR. N – AP/ECE, SRI SAIRAM COLLEGE OF ENGG, ANEKAL 6
Regulation – 2022 (CBCS Scheme) MICROCONTROLLERS – BEC405A
Architecture 8051
► 8051 microcontroller generic part includes a whole family of microcontrollers that have numbers
ranging from 8031 to 8751 and are available in N-Channel Metal Oxide Silicon (NMOS) and
complementary Metal Oxide Silicon (CMOS) construction in a variety of package type.
► Arithmetic and Logic Unit: Performs arithmetic and logical operations on 8-bit data.
► Registers of 8051:
► A and B registers:
• These two 8-bit registers hold the result of many arithmetic and logical operations of the CPU
and they are bit-addressable registers i.e., each bit can be accessed for reading or for altering.
• Accumulator (A) is used in many operations, including addition, subtraction, integer
multiplication, division, Boolean bit manipulation. Also used for data transfers between 8051
and any external memory.
• Register B may be used as a location where data may be stored. It is also used with register A
for multiplication and division operations and has no other function.
MEMORY:
PREPARED BY: Prof. Manjunatha M/ Prof. SANTOSH KUMAR. N – AP/ECE, SRI SAIRAM COLLEGE OF ENGG, ANEKAL 7
Regulation – 2022 (CBCS Scheme) MICROCONTROLLERS – BEC405A
PREPARED BY: Prof. Manjunatha M/ Prof. SANTOSH KUMAR. N – AP/ECE, SRI SAIRAM COLLEGE OF ENGG, ANEKAL 8
Regulation – 2022 (CBCS Scheme) MICROCONTROLLERS – BEC405A
PREPARED BY: Prof. Manjunatha M/ Prof. SANTOSH KUMAR. N – AP/ECE, SRI SAIRAM COLLEGE OF ENGG, ANEKAL 9
Regulation – 2022 (CBCS Scheme) MICROCONTROLLERS – BEC405A
o CY (Carry Flag):
• This flag is set whenever there is a carry after an addition or a barrow after subtraction.
Affected after 8 bit addition and subtraction. It is used to detect error in unsigned arithmetic
operation. It can be used as single bit storage. SETB C → for cy = 1, CLR C → for cy = 0
o AC (Auxiliary Carry Flag):
• This flag is set whenever there is a carry from lower nibble to upper nibble or barrow from upper
nibble to lower nibble. This flag is used by the instructions that perform BCD arithmetic.
o OV (Overflow Flag):
• This flag is set whenever the result of a signed operation is too large, causing the high-order bit to
overflow into the sign bit. In general, carry flag is used to detect errors in unsigned arithmetic
operations. The overflow flag is used to detect errors in signed arithmetic operations.
o P (Parity Flag):
• This reflects the number of 1’s in the accumulator register. If it has odd number of 1’s, then P=1.
If it has even number of 1’s, then P=0.
o RS1, RS0:
• Select register bank as shown in fig. by default register bank 0 is selected.
PREPARED BY: Prof. Manjunatha M/ Prof. SANTOSH KUMAR. N – AP/ECE, SRI SAIRAM COLLEGE OF ENGG, ANEKAL 10
Regulation – 2022 (CBCS Scheme) MICROCONTROLLERS – BEC405A
addressable. This feature allows the programmer to change only what needs to be altered, leaving the
remaining bits in that SFR unchanged. Not all the addresses from 80h to FFh are used for SFRs
Name: Function: Internal RAM address (HEX)
A Accumulator E0
B Arithmetic F0
DPH Addressing External Memory 83
DPL Addressing External Memory 82
IE Interrupt Enable Control A8
IP Interrupt Priority B8
P0 Input / Output Port Latch 80
P1 Input / Output Port Latch 90
P2 Input / Output Port Latch A0
P3 Input / Output Port Latch B0
PCON Power Control 87
PSW Program Status Word D0
SCON Serial Port Control 98
SBUF Serial Port Data Buffer 99
SP Stack Pointer 81
TMOD Timer Mode Control register 89
TCON Timer/Counter Control 88
TL0 Timer 0 low byte 8A
TH0 Timer 0 high byte 8C
TL1 Timer 1 low byte 8B
TH1 Timer 1 high byte 8D
Ports:
• There are 4 ports and each ports are having 8 pins to transmit and receive the information. Total 32
Input and Output data lines. Each port is 8 bit wide and has special function register P0, P1, P2, P3
which are bit addressable means each bit can be set or reset by the Bit instructions (SETB for high,
CLR for low) independently. The data at any port which is transmitting or receiving is in these
registers.
Stack and Stack Pointer:
• The stack refers to an area of internal RAM that is used in conjunction with certain opcodes to store
and retrieve data quickly.
• The 8-bit stack pointer (SP) register is used by the 8051 to hold an internal RAM address that is called
the "top of the stack." The top of the stack is the location in internal RAM where the last byte of data
was stored by a stack operation.
• The SP increments before storing data on the stack so that the stack grows up as data is stored.
• As data is retrieved from the stack, the byte is read from the stack, and then the SP decrements to point
to the next available byte of stored data.
• The SP is set to 07h when the 8051 is reset and can be changed to any internal RAM address by the
programmer
PREPARED BY: Prof. Manjunatha M/ Prof. SANTOSH KUMAR. N – AP/ECE, SRI SAIRAM COLLEGE OF ENGG, ANEKAL 11
Regulation – 2022 (CBCS Scheme) MICROCONTROLLERS – BEC405A
•
.Interrupt:
• Interrupts stop the microcontroller’s current task.
• Interrupts are caused when some other program has a higher priority request for execution.
• When an interrupt occurs, the ongoing task stops, the sub-routine for the interrupt is executed, and
then the previous job resumes.
INT0 It is an external interrupt having code 0. External hardware cause this interrupt.
INT1 It is an external interrupt having code 1. External hardware cause this interrupt.
TF0 This is for timer 0 overflow interrupt.
TF1 This is for timer 1, overflow interrupt
RI/TI This is a serial communication interrupt.
Timers and Counters :
• Timer Registers: Register pairs (TH0, TL0) & (TH1, TL1) are the two 16 bit counting registers for
Timer/Counter 0 and 1 respectively.
• Serial Port
• There are two pins available for serial communication TXD and RXD
• Normally TXD is used for transmitting serial data which is in SBUF register, RXD is used for receiving
the serial data.
• SCON register is used for controlling the operation.
Oscillator Circuit and Timing of 8051:
o The heart of the 8051 is the circuitry that generates the clock pulses by which all the internal
operations are synchronized.
o In most microcontrollers there is built-in circuitry which allows a simple connection of a crystal or
ceramic resonators or an external clock source.
o Pins XTAL1 and XTAL2 are provided for connecting a resonant network to form an oscillator.
o Typically, a quartz crystal and capacitors are employed as shown below.
o Ceramic resonators may be used as a low-cost alternative to crystal resonators.
o However, decreases in frequency stability and accuracy make the ceramic resonator a poor choice if
high-speed data serial communication with other systems, or critical timing, is to be done.
PREPARED BY: Prof. Manjunatha M/ Prof. SANTOSH KUMAR. N – AP/ECE, SRI SAIRAM COLLEGE OF ENGG, ANEKAL 12
Regulation – 2022 (CBCS Scheme) MICROCONTROLLERS – BEC405A
PREPARED BY: Prof. Manjunatha M/ Prof. SANTOSH KUMAR. N – AP/ECE, SRI SAIRAM COLLEGE OF ENGG, ANEKAL 13
Regulation – 2022 (CBCS Scheme) MICROCONTROLLERS – BEC405A
30 ALE •
Address latch enable is an output pin and is active high.
•
ALE Address Latch Enable is used to de multiplex the address and data
signal of port 0 (for external memory interfacing.)
• When address moves on port 0, ALE will be high.
• It is the indication to hold the Address in latch
31 EA • External Access, it is input pin and must be connected to either VCC or GND
• EA= 1 (Vcc), indicate that the program code is stored in the
microcontroller’s onchip ROM.
• EA= 0 (GND), indicate that the program code has to be accessed from
the external memory,
32–39 AD0–AD7 Address/Data pins. Used as low-order address pins A0 – A7 at the beginning
of machine cycle, and as data pins D0 – D7 at the later part of the machine
40 VCC Usually +5V DC is given to this pin
1. Port 0:
• The Port 0 or P0 is a General Purpose I/O Port.
• It consists of 8 pins starting from pin 32 to pin 39.
• An 8-bit bidirectional I/O port is port 0.
• It can be applied to operations involving input and output.
• A different name for it is the "low-order" address bus.
• However, this port can also be utilized as a multiplexed Address and Data bus (from AD0 to AD7).
2. Port 1
• The Port 1 or P1, is also an 8-bit port starting from pin 1 to pin 8.
• Although similar to the P0, the P1 is also a General Purpose I/O Port, however, unlike the other
three ports, P1 does not serve any dual purpose.
• Another 8-bit bidirectional I/O port is Port 1, which is.
• It can be applied to operations involving input and output.
• A different use for it is to offer a collection of all-purpose I/O pins or external interrupt pins
• Hence the sole purpose of P1 is for interfacing.
3. Port 2
• The pins from 21 to 28 belong to the Port 2, or P2.
• An 8-bit bidirectional I/O port is known as Port 2.
• It can be applied to operations involving input and output.
• Additionally, it functions as the high-order address bus for accessing external memory
• Now when there is no presence of an external memory, the P2 act as a General Purpose I/O Port.
• However, in the presence of external memory, P2 acts as an Address Bus, starting from A8 to A15.
4. Port 3:
• Though the Port 3 or P3 usually acts as a normal I/O Port, it can provide some other functions as
well.
• The pin numbers are from 10 to 17.
• A bidirectional 8-bit I/O port with additional specific functionality is port 3.
PREPARED BY: Prof. Manjunatha M/ Prof. SANTOSH KUMAR. N – AP/ECE, SRI SAIRAM COLLEGE OF ENGG, ANEKAL 14
Regulation – 2022 (CBCS Scheme) MICROCONTROLLERS – BEC405A
• It contains two external interrupt pins (INT0, INT1) that can cause hardware interruptions from
outside the device.
• TXD and RXD, two additional pins, are provided for serial communication
• The other functions are below.
PREPARED BY: Prof. Manjunatha M/ Prof. SANTOSH KUMAR. N – AP/ECE, SRI SAIRAM COLLEGE OF ENGG, ANEKAL 15
Regulation – 2022 (CBCS Scheme) MICROCONTROLLERS – BEC405A
PREPARED BY: Prof. Manjunatha M/ Prof. SANTOSH KUMAR. N – AP/ECE, SRI SAIRAM COLLEGE OF ENGG, ANEKAL 16
Regulation – 2022 (CBCS Scheme) MICROCONTROLLERS – BEC405A
PREPARED BY: Prof. Manjunatha M/ Prof. SANTOSH KUMAR. N – AP/ECE, SRI SAIRAM COLLEGE OF ENGG, ANEKAL 17
Regulation – 2022 (CBCS Scheme) MICROCONTROLLERS – BEC405A
▪ In 8051, when the pin is connected to VCC, program fetches to addresses 0000H through 0FFFH
are directed to the internal ROM and program fetches to addresses 1000H through FFFFH are directed
to external ROM/EPROM.
▪ On the other hand when pin is grounded, all addresses (0000H to FFFFH) fetched by program
are directed to the external ROM/EPROM.
▪ The signal is used to activate output enable signal of the external ROM/EPROM.
▪ The port 0 is used as a multiplexed address/bus. It gives lower order 8-bit address in the initial T-cycle
and later it is used as a data bus.
▪ The 8-bit address is latched using external latch and ALE signal generated by 8051.
INTERFACING OF DATA MEMORY:
▪ The CPU generates RD and WR signals as needed during external RAM access
▪ is connected to VCC
▪ ALE = 1 microcontroller sends out address on AD0-AD7
▪ When ALE = 0 , then the lines are used for data transfer
PREPARED BY: Prof. Manjunatha M/ Prof. SANTOSH KUMAR. N – AP/ECE, SRI SAIRAM COLLEGE OF ENGG, ANEKAL 18
Regulation – 2022 (CBCS Scheme) MICROCONTROLLERS – BEC405A
PREPARED BY: Prof. Manjunatha M/ Prof. SANTOSH KUMAR. N – AP/ECE, SRI SAIRAM COLLEGE OF ENGG, ANEKAL 19
Regulation – 2022 (CBCS Scheme) MICROCONTROLLERS – BEC405A
PREPARED BY: Prof. Manjunatha M/ Prof. SANTOSH KUMAR. N – AP/ECE, SRI SAIRAM COLLEGE OF ENGG, ANEKAL 20
Regulation – 2022 (CBCS Scheme) MICROCONTROLLERS – BEC405A
PREPARED BY: Prof. Manjunatha M/ Prof. SANTOSH KUMAR. N – AP/ECE, SRI SAIRAM COLLEGE OF ENGG, ANEKAL 21