Embedded system
Embedded system
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.
5. Cache Memory
6. External Memory
Some microcontrollers support external RAM or Flash memory for extended storage.
Used when internal memory is insufficient for large applications.
1. Clocking System
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
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
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.
Contents:
2 The Clock
3 Interrupts
4 Timers
5 Watchdog Timer
6 Reset Input
7 Brown-out Detector
8 Analog-to-Digital Converter
10 Serial Input-Output
11 USB interface
13 CAN Interface
14 ZigBee Interface
15 Ethernet Interface
16 Power-on-Reset
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.
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
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.
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.
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
PIC microcontrollers are used in robotics, automotive systems, industrial control, IoT
applications, and home automation.
Memory Organization
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).
Benefits of Pipelining
1 Fetch Instr 1 -
PIC microcontrollers have special function registers (SFRs) and general-purpose registers
(GPRs).
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.
Categories of Instructions
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 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.
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.
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)
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.
5. Configuration Bits
Configuration bits are special memory locations in program memory that define hardware settings
before execution begins.
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.
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 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.
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.
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
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
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.
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
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.
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.
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.
Common SFRs
P0, P1, P2, P3 80h, 90h, A0h, B0h I/O port registers
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.
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.
Vector Interrupt
Interrupt Description
Address Number
6 — Reserved
5 — Reserved
Bit Name Function
External interrupts are triggered by signals on P3.2 (INT0) and P3.3 (INT1).
The TCON (Timer Control) register determines how external interrupts are triggered.
Triggered when:
ISRs should not use the RET instruction, instead use RETI (Return from Interrupt).
The 8051 automatically jumps to the ISR when an interrupt occurs.
If multiple interrupts occur simultaneously, priority determines which one executes first.
The IP (Interrupt Priority) register sets the priority.
6 — Reserved
5 — Reserved
4 PS Serial Interrupt
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.
1. Introduction
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
6. Assembly Directives
Directive Purpose
ORG Set origin address
END End of program
DB Define byte
EQU Constant definition
9. Common Applications
LED patterns
Motor control
LCD interfacing
Keypad scanning
Serial communication (USART)
Summary
Unit-4
Case Study: Embedded System for a Smart Card
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.
ROM/Flash Memory Stores firmware and secure applications (e.g., payment logic)
EEPROM Stores modifiable user data like PINs, balance, transaction logs
Interface (I/O) Supports communication via ISO/IEC 7816 (contact) or NFC (contactless)
✅ 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).
+------------------------+
| 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
8. Real-Life Examples
9. Summary
1. Introduction
Real-Time Performs tasks within a strict time frame (e.g., airbags, pacemakers)
Digital Signal Processor (DSP) Signal processing tasks (e.g., TI C6000 series)
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
Unit Function
8. Memory Devices
9. I/O Devices
10. Buses
Type Function
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.