0% found this document useful (0 votes)
2 views

Embedded system

A microcontroller (MCU) is a compact computer on a single chip designed for controlling specific tasks in embedded systems, combining a CPU, memory, and I/O interfaces. They are widely used in various applications, including home appliances, automotive systems, and medical devices, and come in different types such as 8-bit, 16-bit, and ARM microcontrollers. Key features include programmable memory, low power consumption, and built-in peripherals for communication and control functions.

Uploaded by

Lakshay shonak
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Embedded system

A microcontroller (MCU) is a compact computer on a single chip designed for controlling specific tasks in embedded systems, combining a CPU, memory, and I/O interfaces. They are widely used in various applications, including home appliances, automotive systems, and medical devices, and come in different types such as 8-bit, 16-bit, and ARM microcontrollers. Key features include programmable memory, low power consumption, and built-in peripherals for communication and control functions.

Uploaded by

Lakshay shonak
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 31

Unit-1

Microcontroller and its Types   


Introduction:
A microcontroller (MCU) is a small computer on a single integrated circuit that is designed to control
specific tasks within electronic systems. It combines the functions of a central processing unit (CPU),
memory, and input/output interfaces, all on a single chip.
Microcontrollers are widely used in embedded systems, such as home appliances, automotive
systems, medical devices, and industrial control systems. They are also used in consumer electronics
products, such as gaming systems, digital cameras, and audio players.
A typical microcontroller consists of a processor core, volatile and non-volatile memory, input/output
peripherals, and various communication interfaces. The processor core is responsible for executing
instructions and controlling the other components of the microcontroller. The memory is used to store
data and program code, while the input/output peripherals are used to interact with the external
environment.
Microcontrollers are programmable, which means that they can be customized to perform specific
tasks. The programming languages used to write code for microcontrollers vary depending on the
manufacturer and the type of microcontroller. Some of the commonly used programming languages
include C, C++, and assembly language.
A microcontroller is a self-contained desktop that can be utilized in an embedded system. A few
microcontrollers may run at clock rate rates and use four-bit expressions. Because many of the
devices they control are battery operated, microcontrollers must often be low-power. Microcontrollers
are found in a wide range of products, including consumer electronics, automobile engines, computer
peripherals, and test and measurement equipment. These are also well-suited to long-term battery
usage. The vast majority of microcontrollers in use today are embedded in other devices.
The microcontroller used in Embedded System. for example:
• Security Systems
• Laser Printers
• Automation System
• Robotics
Working of Microcontroller:
The microcontroller chip is a high-speed device, yet it is slow when compared to a computer. As a
result, each command will be executed quickly within the microcontroller. The quartz oscillator is
enabled and through control logic register once the supply is powered on. Parasite capacitors will be
recharged for a few seconds while the early preparation is taking place. Once the voltage level reaches
its maximum value and the oscillator’s frequency stabilizes, the operation of writing bits through
special function registers becomes stable. Everything is controlled by the oscillator’s CLK, and the
whole electronics will begin to function. All of this happens in a matter of nanoseconds.
A microcontroller’s major role is that it can be thought of as a self-contained system with a processor
memory. Its peripherals can be used in the same way that an 8051 microcontroller can. The bulk of
microcontrollers in use today are embedded in other types of machinery such as telephones,
appliances, vehicles, and computer system peripherals.
Types of Microcontrollers:
Here are some of the most common types of microcontrollers:
 8-bit Microcontrollers: These are the most basic type of microcontrollers, typically used in simple
applications such as toys, small appliances, and remote controls. They have a limited processing
power and memory capacity, but they are easy to use and cost-effective.
 16-bit Microcontrollers: These are more advanced than 8-bit microcontrollers and are capable of
performing more complex tasks. They are commonly used in applications such as medical devices,
automotive systems, and industrial control systems.
 32-bit Microcontrollers: These are the most powerful and feature-rich microcontrollers, capable of
handling large amounts of data and performing high-speed processing. They are used in applications
such as gaming systems, multimedia devices, and high-end industrial automation.
 ARM Microcontrollers: These microcontrollers are based on the ARM architecture and are widely
used in a variety of applications, including mobile devices, automotive systems, and industrial control
systems.
 PIC Microcontrollers: These microcontrollers are manufactured by Microchip Technology and are
commonly used in a wide range of applications, including home appliances, automotive systems, and
medical devices.
 AVR Microcontrollers: These microcontrollers are manufactured by Atmel Corporation and are
commonly used in applications such as robotics, industrial control systems, and consumer electronics.
 FPGA-based Microcontrollers: These microcontrollers use field-programmable gate arrays (FPGAs)
to provide highly customizable and flexible processing capabilities. They are commonly used in
applications such as digital signal processing, video processing, and high speed networking.
 CPU: The microcontroller is referred to as a CPU device since it is utilized to carry and decode data
before effectively completing the assigned duty. All microcontroller components are connected to a
specific system utilizing a central processing unit. The CPU can decode instructions retrieved from
the programmable memory.
 Memory: The memory chip of a microcontroller functions similarly to a microprocessor in that it
stores all of the data as well as programming. Microcontrollers have a limited quantity of
RAM/ROM/flash memory for storing program source code.
 Input and Output ports: In general, these ports are used to interface or otherwise drive various
appliances like LEDs, LCDs, printers, and so on.
 Serial Ports: Serial ports are used to offer serial interfaces between the microcontroller and a range
of additional peripherals, such as the parallel port.
 Timers: Timers and counters are included in a microcontroller. In a microcontroller, they are used to
manage all timing and counting activities. The fundamental function of a counter is to count external
pulses, whereas timers conduct clock tasks, pulse production, modulations, frequency measurement,
and oscillations, among other things.
 ADC (Analog to Digital Converter): ADC is an acronym for Automated Data Collection (Analog to
Digital Converter). Analog to digital converter is abbreviated as ADC. The primary function of an
ADC is to convert analog signals to digital signals. The required input signals for ADC are analog,
and the resulting digital signal is employed in a variety of digital applications such as measurement
equipment.
 Control Interpretation: This controller is used to provide delayed control to a running application,
with interpretation. is internal or external.
 Block with Special Functions: A specific function block included in some special microcontrollers
built for particular devices such as robots and space systems. This block has additional ports for doing
specific tasks.
Microcontroller Applications:
In contrast to microprocessors, which are used in personal computers and other devices,
microcontrollers are mostly employed in embedded devices. These are mostly utilized in a variety of
products such as implantable medical devices, machine tools, automotive engine control systems,
office equipment, remote-controlled appliances, and so on. The following are some of the most
common uses for microcontrollers.
Microcontroller Properties:
 Microcontroller devices can have words longer than 64 bits.
 Microcontrollers consist of RAM, ROM, Timer, I/O Ports.
 Microcontroller ROM is used for program storage and RAM is used for data storage.
 It is designed by using CISC architecture.
 The power consumption of modern microcontrollers is significantly lower and have operating
voltage range from 1.8V to 5.5V
 The latest feature of microcontroller is flash memory like EPROM and EEPROM.
 The most recent feature of a microcontroller is flash memory, such as EPROM and EEPROM.
Uses of Microcontroller:
Microcontrollers are used in a wide range of electronic devices and systems, including:
1. Home Appliances: Many home appliances, such as washing machines, refrigerators, and air
conditioners, use microcontrollers to perform various functions, such as temperature control, timing,
and monitoring.
2. Automotive Systems: Microcontrollers are used in automotive systems, such as engine control
units, anti-lock braking systems, and airbag systems, to control various functions and ensure safe and
efficient operation.
3. Medical Devices: Medical devices, such as insulin pumps, heart monitors, and blood glucose
meters, use microcontrollers to perform various functions and provide accurate and reliable results.
4. Industrial Control Systems: Microcontrollers are used in industrial control systems, such as
robotics, process control systems, and manufacturing equipment, to control and monitor various
processes and operations.
5. Consumer Electronics: Many consumer electronics devices, such as digital cameras, gaming
systems, and audio players, use microcontrollers to perform various functions and provide advanced
features and capabilities.
6. IoT Devices: Internet of Things (IoT) devices, such as smart home systems, wearables, and
environmental sensors, use microcontrollers to connect to the internet and perform various functions.
7. Aerospace and Defence Systems: Microcontrollers are used in aerospace and defense systems, such
as satellites, avionics, and missiles, to control and monitor various functions and ensure safe and
efficient operation.
Issues in Microcontroller:
some of the most common issues that can arise with microcontrollers:
1. Timing Issues: Microcontrollers rely on precise timing to execute instructions and perform tasks.
Any issues with timing can cause errors and malfunctions, which can be difficult to diagnose and fix.
2. Power Issues: Microcontrollers require a stable and consistent power supply to operate correctly.
Any fluctuations or disruptions in the power supply can cause the microcontroller to malfunction or
fail.
3. Heat Issues: Microcontrollers generate heat during operation, and excessive heat can damage the
device or cause it to malfunction. Heat issues can be caused by poor design, inadequate cooling, or
high ambient temperatures.
4. Noise Issues: Microcontrollers can be affected by electromagnetic interference (EMI) and radio
frequency interference (RFI) from other electronic devices, which can cause errors and malfunctions.
5. Code Issues: The programming code used to control the microcontroller can contain errors and
bugs, which can cause the device to malfunction or fail.
6. Security Issues: Microcontrollers can be vulnerable to security breaches, including unauthorized
access, data theft, and malware attacks.
7. Compatibility Issues: Microcontrollers may not be compatible with other electronic components or
devices, which can cause errors and malfunctions.

Microcontrollers memory types:


1. Flash Memory (Program Memory)

 Non-volatile memory (retains data even when power is off).


 Stores the firmware or program code that the microcontroller executes.
 Can be erased and reprogrammed multiple times.
 Example: EEPROM, NOR Flash.

2. RAM (Random Access Memory)

 Volatile memory (loses data when power is off).


 Used for temporary data storage during program execution.
 Faster than Flash but smaller in size.
 Types:
o SRAM (Static RAM): Faster but consumes more power and is expensive.
o DRAM (Dynamic RAM): Slower but more power-efficient and cost-
effective.
3. EEPROM (Electrically Erasable Programmable Read-Only Memory)

 Non-volatile memory, used to store small amounts of data that need to be retained
between power cycles.
 Slower than Flash but allows byte-level erasing and writing.
 Used for storing calibration parameters, device configurations, and user preferences.

4. ROM (Read-Only Memory)

 Non-volatile memory that stores permanent firmware (e.g., bootloader).


 Types:
o Mask ROM: Programmed during chip manufacturing, cannot be altered.
o PROM (Programmable ROM): Can be written once.
o EPROM (Erasable PROM): Can be erased using UV light and
reprogrammed.

5. Cache Memory

 A small, high-speed memory located inside the microcontroller.


 Stores frequently accessed instructions and data to speed up execution.

6. External Memory

 Some microcontrollers support external RAM or Flash memory for extended storage.
 Used when internal memory is insufficient for large applications.

Microcontrollers features: clocking, i/o pins, interrupts, timers, peripherals.

1. Clocking System

 Clock Source: Determines the speed at which the microcontroller executes


instructions.
 Types of Clock Sources:
o Internal Oscillator: Low-power, lower precision, and typically used for basic
applications.
o External Crystal/Oscillator: More accurate and stable, used for timing-
sensitive applications.
 Clock Speed: Measured in MHz or GHz, affecting processing speed (e.g., 8 MHz, 16
MHz, 100 MHz).
 PLL (Phase-Locked Loop): Allows dynamic frequency scaling for power
optimization.

2. I/O Pins (General-Purpose Input/Output - GPIO)

 Used to interface with external devices such as sensors, LEDs, motors, and
communication modules.
 Pin Modes:
o Input: Reads external signals (e.g., from sensors, buttons).
o Output: Drives external components (e.g., turning LEDs on/off).
o Alternate Function: Some pins can serve multiple purposes (e.g., UART,
PWM, SPI).
o Analog Input: Some pins can read analog signals using ADC (Analog-to-
Digital Converter).
 Pull-up/Pull-down Resistors: Configurable to prevent floating inputs.

3. Interrupts

 Allow the microcontroller to respond to events asynchronously without polling.


 Types of Interrupts:
o External Interrupts: Triggered by external signals (e.g., button press, sensor
trigger).
o Internal Interrupts: Generated by peripherals (e.g., timer overflow, ADC
completion).
 Interrupt Vector Table (IVT): Stores addresses of interrupt service routines (ISR).
 Nested Interrupt Controller (NVIC): Manages interrupt priority (common in ARM
Cortex-M).

4. Timers

 Used for precise timing, delays, event counting, and waveform generation.
 Types of Timers:
o Basic Timers: Simple counting for delays.
o Watchdog Timer (WDT): Resets microcontroller if the software hangs.
o PWM Timers: Generate Pulse Width Modulation signals for motor control,
LED dimming.
o Capture/Compare Timers: Measure input signal duration or generate output
pulses.

5. Peripherals

Microcontrollers include built-in peripherals to communicate with external devices. Common


ones include:

 Communication Interfaces:
o UART (Universal Asynchronous Receiver-Transmitter): Serial
communication (e.g., with PCs, sensors).
o SPI (Serial Peripheral Interface): High-speed communication with sensors,
displays, and memory chips.
o I2C (Inter-Integrated Circuit): Multi-device communication with minimal
wiring (e.g., EEPROM, sensors).
o CAN (Controller Area Network): Used in automotive and industrial
applications.
o USB: Enables direct connection to computers and other USB devices.
 Analog Peripherals:
o ADC (Analog-to-Digital Converter): Converts analog signals (e.g.,
temperature, voltage) to digital.
o DAC (Digital-to-Analog Converter): Converts digital values into analog
output signals.
 Power Management:
o Low-Power Modes: Sleep and deep sleep modes to conserve energy.
o Brown-Out Detection (BOD): Resets the microcontroller when voltage drops
below a safe level.

The Essential Features of Microcontrollers


We have different types of microcontrollers from different manufacturers characterized by different
architectures and capabilities. Some may suit a specific application while others may be totally
inappropriate for the same application.

Some of the common features of most microcontrollers are discussed as follows:

Contents:

 1 The Supply Voltage

 2 The Clock

 3 Interrupts

 4 Timers

 5 Watchdog Timer

 6 Reset Input

 7 Brown-out Detector

 8 Analog-to-Digital Converter

 9 EEPROM Data Memory

 10 Serial Input-Output

 11 USB interface

 12 Motor Control Interface

 13 CAN Interface

 14 ZigBee Interface

 15 Ethernet Interface

 16 Power-on-Reset

 17 Current Sink/Source Capability

 18 LCD Drivers
The Supply Voltage

Most of the microcontrollers you will encounter operate with the standard logic voltage of +5 V. Some
microcontrollers can operate at as low as +2.7 V, and some will tolerate +6V without any issue. For
example, PIC18F452 microcontrollers can operate with a power supply of +2V to +5V. Always check
the manufacturer’s data sheet which contains the information about the allowed limits of the power
supply voltage.

In a practical setting, a voltage regulator is typically used to obtain the required power supply voltage
when the device is operated from a mains adapter or batteries. For instance, a 5V regulator is needed
if the microcontroller is operated from a 5V supply using a 9V battery.

Figure: Internal parts of a microcontroller

The Clock

All microcontrollers require a clock (or an oscillator) to function; this is usually provided by external
timing devices connected to the microcontroller. Generally, these external timing devices are a crystal
plus two small capacitors. In some cases, they are resonators or an external resistor-capacitor pair. We
have some microcontrollers that have built-in timing circuits and don’t need external timing
components. If your application is not time-sensitive, then external or internal resistor-capacitor
timing components are the best option since they are simple and inexpensive.

In a microcontroller, an instruction is executed by fetching it from the memory and then decoding it.
This normally takes several clock cycles and is known as the instruction cycle. In PIC
microcontrollers, an instruction cycle takes four clock periods. Hence, the microcontroller operates at
a clock rate that is one-quarter of the actual oscillator frequency. The PIC18F series of
microcontrollers can operate with clock frequencies up to 40 MHz.

Interrupts

An interrupt causes the microcontroller to respond to external and internal such as timer events very
fast. When an interrupt occurs, the microcontroller leaves its normal flow of program execution and
jumps to a special part of the program referred to as the interrupt service routine (ISR). The program
code inside the ISR is executed, and upon return from the ISR the program resumes its normal flow of
execution.

Timers

Timers play an important role in any microcontroller. A timer is primarily a counter which is driven
from either an external clock pulse or the microcontroller’s internal oscillator. A timer can be 8 bits or
16 bits wide. Data can be loaded into a timer under a program control and the timer can be stopped or
started by program control.

Most timers can be configured to generate an interrupt when they reach a certain count (typically
when there is an overflow). The user program can use an interrupt to carry out accurate timing related
operations inside the microcontroller. The PIC18F series of microcontrollers have at least 3 timers.

An illustration of the application of timers in some microcontrollers is where there is capture and
compare feature, in this case, a timer value can be read when an external event occurs, or the timer
value can be compared to a preset value and an interrupt is generated when this value is reached. A
number of PIC18F microcontrollers have at least two, capture and compare modules.

Watchdog Timer

Most microcontrollers have at least one watchdog feature. The watchdog is basically a time that is
refreshed by the user program. When the program fails to refresh the watchdog, a reset occurs. The
watchdog timer is used to detect a system problem, such as the program being in an endless loop. This
safety facility prevents runaway software and stops the microcontroller from executing meaningless
and unwanted code.

Reset Input

A reset input is used to reset a microcontroller externally. Resetting puts the microcontroller into a
known state such that the program execution starts from address 0 of the program memory. An
external reset action is usually achieved by connecting a push-button switch to the reset input. When
the switch is pressed, the microcontroller is reset.

Brown-out Detector

Brown-out detector, which is common in most microcontrollers, reset the microcontroller if the
supply voltage falls below a nominal value. This safety facility can be utilized to prevent
unpredictable operation of low voltages, especially to protect the contents of EEPROM-type
memories.

Analog-to-Digital Converter

An analog-to-digital converter (A/D) is used to convert an analog signal like voltage, to digital form
so that a microcontroller can read and process it. Some microcontrollers have built-in A/D converters.
External A/D converter can also be connected to any type of microcontroller. A/D converters are
typically 8 to 10 bits, having 256 to 1024 quantization levels. Most PIC microcontrollers with A/D
features have multiplexed A/D converters which provide more than one analog input channel. For
instance, the PIC18F452 microcontroller has 10-bit 8-channel A/D converters.

The A/D conversion process must be started by the user program and may take several hundred
microseconds to complete.

A/D converters are particular of use in instrumentation, control and monitoring applications, since
most sensors such as temperature sensors, pressure sensors, force sensors, produce analog output
voltages.
EEPROM Data Memory

This feature is very common in most microcontrollers. EEPROM memory enables the programmer to
store non-volatile data and change this data whenever it is needed.

Serial Input-Output

Serial communication enables a microcontroller to be connected to another microcontroller or to a PC


using a serial cable. Some microcontrollers have in-built hardware referred to as universal
synchronous-asynchronous receiver-transmitter (USART) to implement a serial communication
interface. The PIC18F series of microcontrollers have in-built USART modules.

Some microcontrollers e.g. PIC18F incorporate serial peripheral interface (SPI) or integrated
interconnect (I2C) hardware bus interfaces. This feature enables a microcontroller to interface with
other compatible devices easily.

USB interface

This is a common computer interface specification that is used to connect various peripheral devices
to computers and microcontrollers. Some PIC microcontrollers provide built-in USB modules for
instance PIC18F2x50.

Motor Control Interface

Some PIC microcontrollers for instance the PIC18F2x31 provide motor control interface capability.
CAN Interface

CAN bus is a bus system used in automation applications. Some PIC18F-series microcontrollers such
as PIC18F4680 provide CAN interface capability.

ZigBee Interface

ZigBee is usually used in wireless home automation applications. Some PIC18F series
microcontrollers provide ZigBee interface capabilities, making the design of such systems very easy.

Ethernet Interface

We have some PIC microcontrollers such as PIC18F97J60 that provide Ethernet interface capabilities
and therefore can be employed in network-based applications.

Power-on-Reset

Some microcontrollers have in-built power-on-reset circuits which keep the microcontrollers in the
reset state until all the circuitry has been initialized. This feature is helpful as it starts the
microcontroller from a known state on power-up. An external reset can also be provided, where the
microcontroller is reset when an external button is pressed.

Current Sink/Source Capability

This feature is important especially if the microcontroller is to be connected to an external device that
might draw a large amount of current to operate. PIC microcontrollers can source and sink 25 mA
current from each output port pin. This current is normally enough to drive buzzers, LEDs, small
lamps, small relays, etc. The current capability can be increased by connecting external transistor
switching circuits or relays to the output port pins.

LCD Drivers

LCD driver enable a microcontroller to be connected to an external LCD display directly.


Nonetheless, these drivers aren’t commonly used as most of the functions they provide can be
implemented in software. For instance, the PIC18F6490 microcontroller has an in-built LCD driver
module.
Unit-2
Introduction to PIC Microcontrollers

PIC (Peripheral Interface Controller) microcontrollers, developed by Microchip Technology, are


widely used in embedded systems due to their:

 Low power consumption


 High performance
 Simple architecture
 Wide range of peripherals
 Reprogrammability (Flash memory-based PICs)

PIC microcontrollers are used in robotics, automotive systems, industrial control, IoT
applications, and home automation.

Architecture of PIC Microcontrollers

PIC microcontrollers follow Harvard Architecture, which means:

 Separate memory buses for program memory and data memory.


 Faster execution since fetching and execution happen simultaneously.
 Reduced Instruction Set Computing (RISC) for optimized speed.

Memory Organization

PIC microcontrollers have three main types of memory:

1. Program Memory (Flash/ROM): Stores program instructions.


2. Data Memory (RAM): Stores temporary data, stack, and working registers.
3. EEPROM: Non-volatile memory for storing configuration settings.

Key Architectural Components

 CPU Core: Executes instructions using ALU, registers, and control logic.
 I/O Ports: GPIO pins for communication with external devices.
 Peripherals: Includes ADC, Timers, PWM, USART, SPI, I²C, etc.
 Interrupt System: Supports external and internal interrupts.
 Clock System: Controls execution speed (Internal RC oscillator or external crystal).

Pipelining in PIC Microcontrollers

PIC microcontrollers use a two-stage instruction pipeline:

1. Fetch Stage → The instruction is fetched from program memory.


2. Execute Stage → The instruction is executed while the next instruction is being fetched.

Benefits of Pipelining

 Increases execution speed (most instructions complete in one cycle).


 Reduces instruction execution time (ideal for real-time applications).
 Optimized power consumption.

Example of Pipelined Execution

Clock Cycle Fetch Stage Execute Stage

1 Fetch Instr 1 -

2 Fetch Instr 2 Execute Instr 1

3 Fetch Instr 3 Execute Instr 2

4 Fetch Instr 4 Execute Instr 3

Program Memory Considerations

 PIC microcontrollers use Flash memory to store program code.


 Program memory is non-volatile, meaning it retains data after power-off.
 Memory paging is used in larger PICs to handle large programs.
 Configuration Bits (stored in program memory) define:
o Clock source
o Watchdog timer
o Power-up timer
o Oscillator settings
 Some PICs support self-programming, useful for bootloaders.

Addressing Modes in PIC Microcontrollers

Addressing modes determine how operands (data) are accessed in instructions.

Common Addressing Modes

1. Immediate Addressing → Operand is specified directly in the instruction.


o Example: MOVLW 0x55 (Move literal value 0x55 to WREG)
2. Direct Addressing → Accesses data stored in registers or memory locations.
o Example: MOVF 0x20, W (Move contents of register 0x20 to WREG)
3. Indirect Addressing → Uses a pointer register to access memory locations dynamically.
o Example: MOVF INDF, W (Access memory location pointed by FSR)

CPU Registers in PIC Microcontrollers

PIC microcontrollers have special function registers (SFRs) and general-purpose registers
(GPRs).

Important CPU Registers

1. WREG (Working Register) → Main register for arithmetic and logic operations.
2. STATUS Register → Stores flags (Zero, Carry, Digit Carry, Negative).
3. FSR (File Select Register) & INDF (Indirect Register) → Used for indirect addressing.
4. PCL (Program Counter Low) → Stores the lower 8 bits of the program counter.
5. PCLATH (Program Counter Latch High) → Stores higher bits of the program counter for
program memory access.
6. OPTION Register → Controls timer settings and pull-ups.

Instruction Set of PIC Microcontrollers

PIC microcontrollers have RISC-based instructions, which are:

 Simple (single-word, mostly single-cycle)


 Fast (executed in one or two cycles)
 Efficient (optimized for embedded systems)

Categories of Instructions

1. Data Transfer Instructions


o MOVLW k → Move literal value k to WREG.
o MOVWF f → Move WREG contents to file register f.
o MOVF f, W → Move file register f to WREG.
2. Arithmetic Instructions
o ADDLW k → Add literal k to WREG.
o ADDWF f, d → Add WREG to file register f.
o SUBWF f, d → Subtract WREG from file register f.
3. Bit Manipulation Instructions
o BSF f, b → Set bit b in register f.
o BCF f, b → Clear bit b in register f.
o BTG f, b → Toggle bit b in register f.
4. Logical Instructions
o ANDWF f, d → AND WREG with file register f.
o IORWF f, d → OR WREG with file register f.
o XORWF f, d → XOR WREG with file register f.
5. Control Instructions
o GOTO addr → Jump to address addr.
o CALL addr → Call subroutine at addr.
o RETURN → Return from subroutine.
6. Branching Instructions
o BTFSC f, b → Skip next instruction if bit b in register f is clear.
o BTFSS f, b → Skip next instruction if bit b in register f is set.

Simple Operations in PIC

Here are some basic operations performed using PIC instructions:

1. Load a value into WREG


MOVLW 0x55 ; Load 0x55 into WREG

2. Move WREG to a file register


MOVWF 0x20 ; Move WREG contents to register 0x20

3. Add two values


MOVLW 0x10 ; Load 0x10 into WREG
ADDWF 0x20, W ; Add WREG to register 0x20, store result in WREG

4. Toggle an LED connected to Port B, Pin 0


BSF PORTB, 0 ; Set RB0 (turn LED ON)
BCF PORTB, 0 ; Clear RB0 (turn LED OFF)

5. Check a Button Press


BTFSS PORTB, 1 ; Skip next instruction if RB1 is pressed
GOTO Main ; Jump back to Main loop

Conclusion

PIC microcontrollers are versatile, efficient, and widely used in embedded applications. Their
Harvard architecture, pipelining, addressing modes, and instruction set make them fast and
reliable. Understanding the CPU registers, program memory considerations, and instruction set
is key to writing optimized code for PIC-based projects.

Program Memory Considerations in PIC Microcontrollers

Program memory in PIC microcontrollers is responsible for storing firmware (program code),
which remains intact even after power loss. It plays a crucial role in performance, reliability, and
flexibility. Understanding how program memory works is essential for optimizing memory usage,
execution speed, and program efficiency.

1. Program Memory Type

PIC microcontrollers primarily use Flash memory to store the program code. This is a non-volatile
memory, meaning the code remains stored even after the device is powered off.

 Older PICs used ROM (Read-Only Memory) or EPROM (Erasable Programmable


ROM).
 Modern PICs use Flash memory, which supports multiple write/erase cycles.

2. Program Memory Organization

PIC microcontrollers have different program memory sizes, ranging from 512 words to several
megabytes.

Memory Structure

 Program memory is divided into words, where each word typically contains an instruction.
 Each word in program memory is typically 12-bit, 14-bit, 16-bit, or 32-bit long, depending
on the PIC family:
o PIC10/PIC12/PIC16 → 12-bit or 14-bit instruction words
o PIC18 → 16-bit instruction words
o PIC24/dsPIC → 24-bit or 32-bit instruction words
Example: PIC16F877A (14-bit Instruction Word)

 Program Memory Size: 8K x 14 bits (8K instructions)


 Address Range: 0000h - 1FFFh (Each address stores a 14-bit instruction)

3. Program Counter (PC)

 The Program Counter (PC) keeps track of the next instruction to execute.
 It increments sequentially unless a branch/jump instruction is encountered.
 Some PICs have Memory Paging or Bank Switching to address large program memory.

4. Paging in Program Memory

 PIC microcontrollers with large program memory use paging.


 The program memory is divided into pages (like sections).
 The PCLATH (Program Counter Latch High) register is used to switch between pages.

Example: Paging in a Large Program


MOVLW 0x02 ; Load page number (Page 2)
MOVWF PCLATH ; Switch to Page 2
CALL 0x1000 ; Call function from address 0x1000

5. Configuration Bits

Configuration bits are special memory locations in program memory that define hardware settings
before execution begins.

Common Configuration Bit Settings:

 Oscillator Selection (Internal/External Crystal)


 Watchdog Timer Enable/Disable
 Power-up Timer Enable
 Code Protection (Prevents unauthorized code reading)
 Brown-Out Reset (BOR) (Detects low voltage and resets)

Example: Setting Configuration Bits in MPLAB (PIC16F877A)


__CONFIG _XT_OSC & _WDT_OFF & _LVP_OFF

 _XT_OSC → Uses an external crystal oscillator.


 _WDT_OFF → Disables the watchdog timer.
 _LVP_OFF → Disables low-voltage programming.

6. Interrupt Vector Table (IVT)

 Interrupts require fixed memory locations to store the Interrupt Service Routine (ISR)
address.
 In PIC16F877A, the Interrupt Vector Address is 0x0004.
 If an interrupt occurs, the program automatically jumps to this address.

Example: Simple ISR Handling


ORG 0x0004 ; Interrupt vector location
GOTO ISR ; Jump to the Interrupt Service Routine
7. Self-Programming and Bootloaders

 Some PIC microcontrollers support self-programming, allowing them to modify program


memory while running.
 This is useful for:
o Bootloaders (Loading programs via UART or USB).
o Firmware Updates without external programmers.

8. Code Protection & Security

 Some PICs offer Code Protection (CP) Bits to prevent unauthorized reading of program
memory.
 Once enabled, external devices cannot read or copy the firmware.

Conclusion

Program memory in PIC microcontrollers is a critical resource that must be efficiently managed for
optimized performance. Understanding paging, configuration bits, program counter handling, and
security features ensures reliable and secure execution of firmware.

Addressing Modes in PIC Microcontrollers

Addressing modes define how the operand (data) is accessed during program execution.
Understanding these modes is essential for efficient data handling, memory management, and
optimizing program performance.

PIC microcontrollers typically use three primary addressing modes due to their RISC (Reduced
Instruction Set Computing) architecture.

1. Immediate Addressing Mode

 The operand (data) is directly embedded within the instruction.


 Used for loading constants into registers.

Example
MOVLW 0x55 ; Move literal (0x55) to WREG

 MOVLW (Move Literal to WREG) places the constant 0x55 into WREG (Working Register).
 This is useful for loading immediate values without accessing memory.

2. Direct Addressing Mode

 The instruction specifies the exact address of the register (file register) containing the
 operand.
 Used for accessing General-Purpose Registers (GPRs) or Special Function Registers
(SFRs).

Example
MOVF 0x20, W ; Move contents of register 0x20 to WREG
MOVWF 0x30 ; Move WREG contents to register 0x30

 0x20 and 0x30 are register addresses in the RAM.


 Used for reading/writing data in registers.

3. Indirect Addressing Mode

 Instead of using a direct address, a pointer register (FSR - File Select Register) holds the
address of the target register.
 The INDF (Indirect Register) is used to access data at the location pointed by FSR.

Example
MOVLW 0x30 ; Load address 0x30 into WREG
MOVWF FSR ; Store it in File Select Register (FSR)
MOVF INDF, W ; Fetch data from the location pointed by FSR into WREG

 The value stored in FSR acts as a pointer to another memory location.


 Used in loops, array processing, and dynamic memory access.

Comparison of Addressing Modes


Addressing Mode Operand Location Example Instruction Use Case

Immediate In the instruction itself MOVLW 0x55 Loading constants

Direct Explicit memory/register address MOVWF 0x20 Accessing registers

Indirect Address stored in FSR MOVF INDF, W Working with arrays


Unit-3
I/O Ports in 8051 Microcontroller
The Intel 8051 microcontroller has four 8-bit parallel I/O ports:

 Port 0 (P0)
 Port 1 (P1)
 Port 2 (P2)
 Port 3 (P3)

Each port can be used for input or output depending on the application.

Overview of I/O Ports


Port Pins Functionality

P0.0 - Open-drain, can be used as I/O or acts as lower byte of address/data bus (AD0-AD7)
P0
P0.7 in external memory access

P1.0 -
P1 Simple I/O port with internal pull-ups
P1.7

P2.0 - Can be used as I/O or acts as the upper byte of the address bus (A8-A15) in external
P2
P2.7 memory access

P3.0 -
P3 I/O port with alternate functions like interrupts, serial communication, and timers
P3.7

Detailed Description of Each Port

1. Port 0 (P0.0 - P0.7)

 Bidirectional (Input/Output) but open drain (requires external pull-up resistors).


 Multiplexed Address/Data Bus (AD0 - AD7) in external memory interfacing.
 Needs pull-up resistors when used as an input or output port.

2. Port 1 (P1.0 - P1.7)

 Simple I/O port (no alternate function).


 Has internal pull-up resistors (unlike Port 0).
 Can be used for LEDs, switches, LCDs, and other digital I/O operations.

 This sends a binary pattern (alternating 1s and 0s) to Port 1.

3. Port 2 (P2.0 - P2.7)

 Used as I/O or as high-order address lines (A8-A15) for external memory interfacing.
 Has internal pull-up resistors.
 If external memory is not used, it can work as a general-purpose I/O port.

4. Port 3 (P3.0 - P3.7)


 Dual-function port (I/O + special functions).
 Supports serial communication, timers, and interrupts.

Pin Alternate Function

P3.0 RXD (Serial Data Input)

P3.1 TXD (Serial Data Output)

P3.2 INT0 (External Interrupt 0)

P3.3 INT1 (External Interrupt 1)

P3.4 T0 (Timer 0 External Input)

P3.5 T1 (Timer 1 External Input)

P3.6 WR (External Memory Write)

P3.7 RD (External Memory Read)

I/O Port Operations

1. To configure a port as output → Write 1s to it.


2. To configure a port as input → Write 0s to it.

Conclusion

 The 8051 microcontroller has 4 I/O ports (P0, P1, P2, P3).
 Port 0 and Port 2 are used for external memory interfacing.
 Port 3 has alternate functions like serial communication, timers, and interrupts.
 Port 1 is the simplest port with only general I/O functionality.

Internal RAM and Registers in 8051 Microcontroller

The 8051 microcontroller has an internal RAM (128 bytes in standard 8051) and various registers
that are used for data storage, stack operations, and program execution.

Internal Memory Organization of 8051

The internal RAM is divided into:

1. General-Purpose RAM (00h – 7Fh) → 80 Bytes


2. Bit-Addressable RAM (20h – 2Fh) → 16 Bytes
3. Register Banks (00h – 1Fh) → 32 Bytes
4. Special Function Registers (SFRs) (80h – FFh) (Control Registers)

1. General-Purpose RAM (00h – 7Fh)

 Size: 80 bytes (128 locations)


 Used for temporary storage of data and variables.
 Can be accessed using direct or indirect addressing.
2. Register Banks (00h – 1Fh)

 Divided into four banks (Bank 0 to Bank 3).


 Each bank has 8 registers (R0 - R7).
 The active bank is selected using PSW register.

Bank Address Range Registers


Bank 0 00h - 07h R0 - R7

Bank 1 08h - 0Fh R0 - R7

Bank 2 10h - 17h R0 - R7

Bank 3 18h - 1Fh R0 - R7

3. Bit-Addressable RAM (20h – 2Fh)

 16 bytes (128 bits) can be accessed individually.


 Used for bitwise operations, flags, and control bits.

4. Special Function Registers (SFRs) (80h – FFh)

 Controls the operation of the 8051 microcontrollers.


 Located in upper RAM (addresses 80h – FFh).
 Not all addresses are used.

Common SFRs

Register Address Function

A (Accumulator) E0h Stores arithmetic and logic results

B Register F0h Used in multiplication and division

PSW (Program Status Word) D0h Contains status flags

SP (Stack Pointer) 81h Points to the top of the stack

DPTR (Data Pointer) 82h-83h Used for external memory addressing

PC (Program Counter) 0000h-FFFFh Stores the address of the next instruction

P0, P1, P2, P3 80h, 90h, A0h, B0h I/O port registers

TCON (Timer Control) 88h Timer control flags

SCON (Serial Control) 98h Serial communication settings

5. Stack Pointer (SP)

 The SP (Stack Pointer) register stores the address of the last used stack location.
 Default value after reset = 07h (Stack starts from 08h).

Conclusion

 8051 microcontroller has 128 bytes of RAM, divided into register banks, general-purpose
RAM, and bit-addressable RAM.
 Special Function Registers (SFRs) control operations like I/O, timers, serial
communication, and interrupts.
 The Stack Pointer (SP) is used for function calls and stack storage.

Interrupts in 8051 Microcontroller


1. Introduction to Interrupts

An interrupt is a mechanism that temporarily stops the normal execution of a program to handle an
urgent event. After executing the Interrupt Service Routine (ISR), the microcontroller resumes the
main program.

Types of Interrupts in 8051

8051 microcontroller supports five interrupts:

Vector Interrupt
Interrupt Description
Address Number

Triggered by a low signal on pin


External Interrupt 0 (INT0) 0003H 0
P3.2

Timer Interrupt 0 (TF0) 000BH 1 Triggered when Timer 0 overflows

Triggered by a low signal on pin


External Interrupt 1 (INT1) 0013H 2
P3.3

Timer Interrupt 1 (TF1) 001BH 3 Triggered when Timer 1 overflows

Serial Communication Triggered when data is


0023H 4
Interrupt (RI/TI) received/transmitted

2. Enabling and Controlling Interrupts

Interrupt Enable (IE) Register

The IE (Interrupt Enable) register is used to enable or disable specific interrupts.

Bit Name Function

7 EA Enable All Interrupts (1 = Enable, 0 = Disable)

6 — Reserved

5 — Reserved
Bit Name Function

4 ES Enable Serial Interrupt

3 ET1 Enable Timer 1 Interrupt

2 EX1 Enable External Interrupt 1

1 ET0 Enable Timer 0 Interrupt

0 EX0 Enable External Interrupt 0

3. External Interrupts (INT0 and INT1)

External interrupts are triggered by signals on P3.2 (INT0) and P3.3 (INT1).

Trigger Modes for External Interrupts

The TCON (Timer Control) register determines how external interrupts are triggered.

Bit Function Value 0 Value 1

IT0 Interrupt 0 Mode Level Triggered Edge Triggered

IE0 Interrupt 0 Flag 0 1 when triggered

IT1 Interrupt 1 Mode Level Triggered Edge Triggered

IE1 Interrupt 1 Flag 0 1 when triggered

4. Timer Interrupts (TF0 and TF1)

Timer interrupts are generated when Timer 0 or Timer 1 overflows.

Steps to Enable Timer Interrupt:

1. Set Timer Mode (TMOD register).


2. Enable Timer Interrupt (IE register).
3. Start Timer (TR0 or TR1 in TCON).
4. Write an ISR at vector address.

5. Serial Communication Interrupt

Triggered when:

 A byte is received (RI = 1).


 A byte is transmitted (TI = 1).

6. Writing an Interrupt Service Routine (ISR)

 ISRs should not use the RET instruction, instead use RETI (Return from Interrupt).
 The 8051 automatically jumps to the ISR when an interrupt occurs.

7. Interrupt Priority in 8051

 If multiple interrupts occur simultaneously, priority determines which one executes first.
 The IP (Interrupt Priority) register sets the priority.

Bit Interrupt Priority (0 = Low, 1 = High)


7 — Reserved

6 — Reserved

5 — Reserved

4 PS Serial Interrupt

3 PT1 Timer 1 Interrupt

2 PX1 External Interrupt 1

1 PT0 Timer 0 Interrupt

0 PX0 External Interrupt 0

Conclusion

 8051 has five interrupts: External (INT0, INT1), Timer (TF0, TF1), and Serial.
 Interrupts can be enabled/disabled using the IE register.
 Priority can be assigned using the IP register.
 ISRs must end with RETI instead of RET.

Assembly Language Programming of Microcontroller 8051

1. Introduction

Assembly language is a low-level programming language that is closely tied to the


hardware architecture. In the case of the 8051 microcontroller, assembly language allows
direct control over hardware resources such as memory, I/O ports, timers, and interrupts.

2. 8051 Microcontroller Architecture Overview

 8-bit CPU
 16-bit Program Counter (PC)
 128 Bytes of Internal RAM
 4 I/O Ports (P0–P3)
 2 Timers
 Serial communication
 5 Interrupt sources

3. Basic Syntax of 8051 Assembly Language


LABEL: MNEMONIC OPERAND ; COMMENT

 LABEL – Optional, marks the address of a line


 MNEMONIC – Operation (e.g., MOV, ADD, SJMP)
 OPERAND – Registers, data, or addresses
 ; COMMENT – Optional explanation

4. Common 8051 Assembly Instructions

Category Instruction Description


Data Transfer MOV A, #25 Move immediate data to Accumulator
Arithmetic ADD A, R1 Add register to Accumulator
Logical ANL A, #0Fh AND Accumulator with value
Jump SJMP LOOP Short jump to label LOOP
Loop/Branch DJNZ R1, LOOP Decrement and jump if not zero
I/O MOV P1, A Output Accumulator to Port 1

6. Assembly Directives

Directive Purpose
ORG Set origin address
END End of program
DB Define byte
EQU Constant definition

7. Assembler and Tools

 Keil uVision (IDE + Assembler)


 Proteus (for simulation)
 ASM51 (assembler for 8051)
 AVR Studio / MPLAB (in advanced microcontroller contexts)

8. Tips for 8051 Assembly Programming

 Use comments liberally.


 Keep track of register use
 Minimize use of nested loops for delay (use timers instead)
 Test on simulators before burning code.

9. Common Applications
 LED patterns
 Motor control
 LCD interfacing
 Keypad scanning
 Serial communication (USART)

Summary

Feature 8051 Assembly


Language Type Low-level
Speed Very fast execution
Hardware Control Direct
Learning Curve Steep but rewarding
Ideal For Embedded beginners, timing-critical tasks

Unit-4
Case Study: Embedded System for a Smart Card

1. Introduction to Smart Cards

A smart card is a pocket-sized card embedded with an integrated circuit (IC) that can either be a
microcontroller or memory chip. Smart cards are widely used in banking, ID systems, SIM cards,
public transportation, and secure authentication.

2. What is an Embedded System?

An embedded system is a combination of hardware and software designed to perform a specific


function within a larger system. In the case of a smart card, the embedded system handles data
processing, security, and communication.

3. Components of an Embedded System in a Smart Card


Component Function

Microcontroller/CPU Executes program code and manages security/authentication

ROM/Flash Memory Stores firmware and secure applications (e.g., payment logic)

EEPROM Stores modifiable user data like PINs, balance, transaction logs

RAM Temporary storage for operations

Crypto Processor Performs encryption, decryption, hashing

Interface (I/O) Supports communication via ISO/IEC 7816 (contact) or NFC (contactless)

4. Smart Card Use Case Example: Payment Card

✅ Objective

Design an embedded system in a smart payment card that securely stores user credentials and
performs encrypted transactions.

Functionality
1. Authentication:
o Card validates itself using a cryptographic challenge-response protocol.
o Optional: User enters a PIN on a connected terminal.
2. Transaction:
o The card processes a request (e.g., deduct $20).
o Internal logic verifies if the balance is sufficient.
o If valid, it updates the balance in EEPROM and logs the transaction.
3. Security:
o All communication is encrypted (e.g., using AES or RSA).
o Implements tamper detection and data isolation.
o Supports multi-level access control (e.g., admin/user mode).
4. Communication:
o Uses serial communication protocols (T=0, T=1) for contact cards.
o Uses RF interface (13.56 MHz) for contactless cards (NFC).

5. Embedded Software Architecture

+------------------------+
| Application | --> Balance checking, logging, access control
+------------------------+
| Operating System | --> Lightweight RTOS / Smart Card OS (e.g., Java Card OS)
+------------------------+
| Cryptographic Library | --> RSA, AES, SHA algorithms
+------------------------+
| Hardware Abstraction | --> Interfaces for EEPROM, timers, communication
+------------------------+
| Microcontroller |
+------------------------+
6. Technical Specifications (Typical)

Parameter Value

Microcontroller 8-bit or 16-bit smart card MCU

Memory 64KB Flash, 8KB RAM, 16KB EEPROM

Communication ISO 7816 (serial), ISO 14443 (RF)

Power 1.8V to 5V via terminal (or NFC field)

OS Java Card, MULTOS, or proprietary OS

7. Advantages of Embedded Smart Cards

 ✅ Secure data storage


 ✅ Portability and durability
 ✅ Can perform cryptographic operations
 ✅ Low power consumption
 ✅ Reprogrammable and customizable

8. Real-Life Examples

 EMV Smart Payment Cards (Visa/MasterCard)


 SIM Cards in mobile phones
 e-Passports
 Metro/RFID Cards (e.g., Oyster, Octopus)
 Health ID Cards

9. Summary

Aspect Smart Card Embedded System

Type Real-time, secure embedded system

Role Authentication, data processing, secure communication

Key Feature Data security and encryption

Common MCU Infineon, NXP, STMicroelectronics

Real-Time? Yes (in critical financial or ID systems)

1. Introduction

An Embedded System is a microprocessor/microcontroller-based system designed to perform a


dedicated function or set of functions. Unlike general-purpose computers, it is optimized for specific
tasks, often with real-time constraints.

2. Classification of Embedded Systems


Type Description

Real-Time Performs tasks within a strict time frame (e.g., airbags, pacemakers)

Standalone Works independently (e.g., washing machines, calculators)

Networked Connected to a network (e.g., routers, smart meters)

Mobile Portable systems (e.g., smartphones, cameras)

Embedded with OS Runs an OS (e.g., Android in smartphones)

3. Processors in Embedded Systems

Type Use Case

Microcontroller (MCU) Controls small devices (e.g., 8051, ATmega328)

Microprocessor (MPU) Higher processing (e.g., ARM Cortex-A)

Digital Signal Processor (DSP) Signal processing tasks (e.g., TI C6000 series)

Application Specific IC (ASIC) Custom chips for specific tasks

FPGA Reprogrammable hardware (flexible but complex)

4. Hardware Units in Embedded Systems

 Processor (CPU/MCU)
 Memory (RAM, ROM, EEPROM)
 Timers and Counters
 I/O Interfaces (GPIO, UART, SPI, I2C)
 Peripherals (sensors, actuators)
 Power Supply Unit
 Clock/Crystal Oscillators

5. Software Embedded into System

Software Type Purpose

Firmware Low-level control software stored in ROM/Flash

Device Drivers Interface between hardware and software

Real-Time Operating System (RTOS) Manages tasks and timing

Middleware Protocols and interfaces for communication

Application Software Specific end-user functionality

6. Applications and Products of Embedded Systems


Domain Examples

Consumer Smart TVs, Washing Machines, Cameras

Automotive ABS, Airbags, ECU, GPS

Medical Pacemakers, Monitors, Infusion Pumps

Industrial PLCs, SCADA, CNC Machines

Telecommunication Routers, Modems, Base Stations

Defense/Aerospace Radars, Drones, Satellites

7. Structural Units in a Processor

Unit Function

ALU (Arithmetic Logic Unit) Performs arithmetic and logic operations

Control Unit Directs data flow and operations

Registers Temporary data storage

Bus Interface Unit Connects processor to buses

Instruction Decoder Interprets machine instructions

Program Counter Tracks instruction address

8. Memory Devices

Type Examples Use

RAM SRAM, DRAM Volatile memory for temporary storage

ROM Mask ROM, PROM, EPROM, EEPROM Non-volatile, stores firmware

Flash NOR, NAND Rewritable non-volatile memory

Cache L1, L2 Fast memory between CPU and RAM

9. I/O Devices

Input Devices Output Devices

Sensors, Switches, Keypads Displays, Buzzers, Motors, LEDs

10. Buses

Type Function

Data Bus Transfers actual data


Type Function

Address Bus Carries address of data

Control Bus Carries control signals

Serial Buses SPI, I2C, UART – for peripheral communication

11. Interfacing of Processor, Memory, and I/O Devices

 Memory Interfacing: Address decoding and control signals are used to map RAM/ROM to
specific address ranges.
 I/O Interfacing: I/O devices connected using GPIO, Serial buses, or Memory-mapped I/O.
 Techniques:
o Polling – CPU checks device status repeatedly.
o Interrupts – Devices notify the CPU when ready.
o DMA (Direct Memory Access) – Bypasses CPU for high-speed data transfer.

Conclusion

An embedded system is a complete hardware-software solution tailored for specific applications. Its
design emphasizes efficiency, reliability, and performance, often operating in real-time
environments. Understanding its architecture is crucial for building efficient and effective systems in
diverse industries.

Would you like a PowerPoint presentation, handwritten-style notes, or diagrammatic


representation of any specific topic?

You might also like