S04 Slides
S04 Slides
Abstraction
Thamidu Naveen
AVR INTERRUPT
• What is Interrupt? - It’s an internal or external events that inform the CPU
something needs to be serviced (Action Taken).
• Interrupt vs Polling – Polling checks the status to see if something needs serving
(Checking for timer overflow bit in while loop). So, polling waste resources. But
Interrupt Service Routine(ISR) or Interrupt Handler code is automatically called
when an interrupt occurs.
• On receiving interrupt, the microcontroller interrupts whatever it is doing and
executes Interrupt Service Routine(ISR).
• Microcontroller can serve many devices. Each device gets attention based on the
priority.
ATMEGA328P PIN DIAGRAM
ISR (INTERRUPT SERVICE ROUTINE)
TOV0
Vector location
0x0020
TOIE0
I
DIFFERENCE BETWEEN RET AND RETI
INSTRUCTIONS
• Both perform the same actions of popping off the top bytes of the stack into
the program counter and making the AVR to where it left off.
• However, RETI also performs the additional task of setting the I flag, indicting
that the servicing of the interrupt is over and the AVR now can accept a new
interrupt.
• If we use RET instead of RETI then I flag will not be set automatically and no
new interrupt will be recognized even if it happens.
INTERRUPT VECTOR NAME FOR ATMEGA328P IN C
Vector Number Interrupt definition Vector name
2 External Interrupt Request 0 INT0_vect
3 External Interrupt Request 1 INT1_vect
4 Pin Change Interrupt Request 0 PCINT0_vect
5 Pin Change Interrupt Request 1 PCINT1_vect
6 Pin Change Interrupt Request 2 PCINT2_vect
7 Watchdog Time-out Interrupt WDT_vect
8 Timer/Counter2 Compare Match A TIMER2_COMPA_vect
9 Timer/Counter2 Compare Match B TIMER2_COMPB_vect
10 Timer/Counter2 Overflow TIMER2_OVF_vect
11 Timer/Counter1 Capture Event TIMER1_CAPT_vect
12 Timer/Counter1 Compare Match A TIMER1_COMPA_vect
13 Timer/Counter1 Compare Match B TIMER1_COMPB_vect
Vector Number Interrupt definition Vector name
14 Timer/Counter1 Overflow TIMER1_OVF_vect
15 Timer/Counter0 Compare Match A TIMER0_COMPA_vect
16 Timer/Counter0 Compare Match B TIMER0_COMPB_vect
17 Timer/Counter0 Overflow TIMER0_OVF_vect
18 SPI Serial Transfer Complete SPI_STC_vect
19 USART Rx Complete USART_RX_vect
20 USART Data Register Empty USART_UDRE_vect
21 USART Tx Complete USART_TX_vect
22 ADC Conversion Complete ADC_vect
23 EEPROM Ready EE_READY_vect
24 Analog Comparator ANALOG_COMP_vect
25 Two-wire Serial Interface TWI_vect
26 Store Program Memory Read SPM_READY_vect
EXAMPLE
Toggle the red LED at PD7 with a button press on PC4 and blink a green LED at
PD6 with a 1-second delay.
IS THIS POSSIBLE?
WITH TIME INTERRUPT
Import Interrupt library
Priority Decreases
REGISTER DESCRIPTION Note: We can use several
Make I bit 1 to pins for pin change
Enable global interrupts, and registers
interrupts associated with those
interrupts are different.
Make INT0 or
INT1 bit 1 to
Enable external
interrupts
Similarly, for INT1,
ISC11 and ISC10
Initializing Arrays
AVR SERIAL TRANSMISSION PROGRAMMING
D0
Vin
Analog Input Binary
Data
Output
Start
Conversion D9
STEP SIZE AND CONVERSION TIME OF AN ADC
• Step size (Resolution) is the smallest change at the input that can be detected by ADC.
𝑉𝑟𝑒𝑓
• Formula of Resolution =
2𝑛
.
• High Resolution ADC has more precise output.
• It can be improved by increasing number of bit conversion and by reducing reference voltage.
• Conversion time is the time taken by the ADC to convert input analog signal to equivalent
digital signal.
5V
• Ex: For a 10-bit ADC, if Vref = 2.56V.
Calculate D0-D9 output, if input is 0.2V
n-bit No of Step size (mV)
2.56
steps 10mV 0 𝑆𝑡𝑒𝑝 𝑠𝑖𝑧𝑒 = 𝑉𝑟𝑒𝑓/2𝑛 = = 2.5mV
D0 1024
8 256 5V/256 = 19.53 1
0.2𝑉
0 ∴ 𝐷𝑜𝑢𝑡 = = 80 In Decimal
10 1024 5V/1024 = 4.88 . 2.5𝑚𝑉
12 4096 5V/4096 = 1.2 . Which gives 0001010000
16 65536 5V/65536 = 0.076 D9 0
ANALOG TO DIGITAL CONVERTER IN ATMEGA328P
REGISTERS ASSOCIATED WITH ADC IN AVR
The value of these bits
selects which analog inputs
are connected to the ADC.
These bits select the voltage ADC Left Adjust Result bit affects
reference for the ADC, as shown the presentation of the ADC
in Table. If these bits are changed conversion result in the ADC data
during a conversion, the change register. Write one to ADLAR to
will not go in effect until this left adjust the result. Otherwise,
conversion is complete the result is right adjusted.
1.1v
REGISTERS ASSOCIATED WITH ADC IN AVR
When an ADC
conversion is
complete, the result
is found in these two
registers.
ADC Prescaler Select Bits -
REGISTERS ASSOCIATED WITH ADC IN AVR These bits determine the
division factor between the
system clock frequency and
the input clock to the ADC.
Microcontroller Microprocessor
• A microcontroller is a compact integrated • A microprocessor is a general-purpose
circuit designed to govern a specific processing unit that requires external memory
operation in an embedded system. and peripherals to function.
• Typically includes a CPU, memory (RAM, • Used in more complex systems like personal
ROM, Flash), and peripherals on a single chip. computers, smartphones, and servers.
• Commonly used in low-power, resource- • Microprocessors are typically more powerful
constrained applications like home but less integrated than microcontrollers.
automation, wearables, and IoT devices.
KEY COMPONENTS OF EMBEDDED BOARDS
• Microcontroller Unit (MCU)
• The MCU is the heart of the embedded board, containing the CPU,
memory, and essential peripherals.
• Power Supply
• Provides the necessary voltage and current to power the embedded board
and its components.
• Communication Interfaces
• Can also be used to upload code.
https://docs.arduino.cc/hardware/uno-rev3/
TYPES OF EMBEDDED BOARDS
• General-Purpose Boards
• General-purpose boards are versatile and widely used for a broad range
of applications, from prototyping to small-scale production. These
boards are designed for ease of use, with extensive documentation,
community support, and a variety of development tools.
• Examples
• Arduino Uno, Raspberry Pi
• Use Cases and Applications
• Rapid development and testing of ideas before moving to custom hardware.
• Examples include home automation prototypes, robotics, and wearable
devices.
• Widely used in educational settings to teach basic electronics, programming,
and system design.
TYPES OF EMBEDDED BOARDS
• Performance-Oriented Boards
• These boards are designed for applications requiring higher processing power,
more memory, and advanced peripherals. Suitable for tasks that involve real-
time processing, complex algorithms, or high-speed communication.
• Examples
• STM32 Nucleo, ESP32, Teensy
• Use Cases and Applications
• Applications requiring precise timing and synchronization, such as motor
control, robotics, and industrial automation.
• Applications that require internet connectivity, remote monitoring, and
control, like smart homes, industrial IoT, and wearable tech.
• Applications involving audio/video processing, such as sound synthesis,
digital signal processing, and embedded multimedia systems.
TYPES OF EMBEDDED BOARDS
• Low-Power Boards
• Designed for battery-powered applications where energy efficiency is critical.
Focus on minimizing power consumption, often with sleep modes and low-
power peripherals.
• Examples
• TI MSP430, Nordic nRF52
• Use Cases and Applications
• Devices like fitness trackers, smartwatches, and health monitors where
battery life is crucial.
• Sensors placed in locations without easy access to power sources, such
as environmental monitoring systems, wildlife tracking, and agricultural
sensors.
• Battery-operated IoT nodes that need to function for extended periods
without human intervention.
TYPES OF EMBEDDED BOARDS
• Specialized Boards
• These boards are designed for specific applications, offering unique features
that the market requires or advanced technologies. They often include
specialized processing capabilities, unique peripherals, or support for
advanced algorithms.
• Examples
• Intel Edison, BeagleBone, Kendryte K210
• Use Cases and Applications
• Embedded systems requiring on-device AI processing, such as smart
cameras, voice assistants, and edge AI devices.
• Distributed computing systems where data processing occurs at the edge
of the network, reducing latency and bandwidth use, critical in IoT and AI
applications.
COMMONLY USED EMBEDDED BOARDS IN INDUSTRY
• Microchip AVR Series (e.g., Arduino):
• prototyping to small-scale production.
• Espressif ESP8266/ESP32:
• Wi-Fi and Bluetooth connectivity, IoT applications.
• Development Platforms
• Environments or ecosystems supporting development on specific boards or hardware.
• Provide tools, software, and libraries for developing and deploying applications.
• PlatformIO, STM32CubeIDE, Arduino IDE.
• Frameworks
• Software libraries or systems providing standard APIs and tools for application development.
• Simplify development by abstracting hardware interactions and offering reusable code.
• Arduino, FreeRTOS, Zephyr RTOS, ESP-IDF.
INTRODUCTION TO ARDUINO
Arduino is an open-source electronics platform based on easy-to-use
hardware and software. It is designed for beginners and
professionals to create interactive projects.
Microcontroller Boards Arduino Software
• Arduino Uno: Based on the ATmega328P • A user-friendly environment for
microcontroller, suitable for general-purpose projects. writing, compiling, and uploading
code to Arduino boards.
• Arduino Nano: A smaller version of the Uno with
similar capabilities, ideal for compact projects.
• Arduino Mega: Features more I/O pins and memory,
suited for complex projects.
ARDUINO ON PLATFORM IO
Include Arduino
header file
Where is the
main method?
SETUP AND LOOP IN ARDUINO
• void setup() - Initializes settings and runs once the board is powered on or reset.
• void loop() - Contains the main logic of the program and runs repeatedly after setup.
DIGITAL I/O
What is better?
Useful Arduino Functions
MILLIS() AND MICROS()
SWITCH DEBOUNCE Will this logic work
practically?
2
LED
BUTTON
LED
BUTTON
3
DEBOUNCING WITH
INTERRUPTS
If a press toggle
the LED
PULSEIN()
ULTRASONIC SENSOR
Working Principle
Key Components
• Trigger Pin: Sends out the ultrasonic pulse.
• Echo Pin: Receives the reflected signal (echo).
Applications
• Distance measurement.
• Obstacle detection.
• Object tracking.
ULTRASONIC SENSOR
• Step 1: The microcontroller sends a high signal to the Trigger pin for 10 microseconds.
• Step 2: The sensor emits an ultrasonic wave that travels through the air.
• Step 3: If the wave encounters an object, it reflects back to the sensor.
• Step 4: The Echo pin goes high, and the microcontroller measures the duration of the high signal.
SIMULATION
2
4
3
1
5
ADDING LIBRARY TO THE PLATFORMIO PROJECT
8
SIMULATION OF SERVO
Not recommended
to Supply power
through Arduino
board
SPI COMMUNICATION
Serial Peripheral Interface (SPI) is a synchronous serial communication
protocol. Designed for short-distance communication, primarily in embedded
systems.
Key Components
Key Features • MISO (Master In Slave Out): Data from slave to
• High-speed data transfer. master.
• Master-Slave architecture. • MOSI (Master Out Slave In): Data from master to
• Full-duplex communication. slave.
• SCK (Serial Clock): Clock signal generated by the
master.
• SS (Slave Select): Selects the slave to communicate
with.
Clock Polarity (CPOL) & Clock Phase (CPHA)
• CPOL: Defines the base level of the clock.
• CPHA: Defines when data is sampled on the data
lines.
SPI COMMUNICATION
Master-Slave Architecture:
• Master initiates communication
and provides a clock.
• Slave responds based on the
master's clock signal.
Data Transmission:
• Master sends data through MOSI,
and slave sends data back on
MISO.
• Data transfer is synchronous, i.e.,
clock-dependent.
MULTIPLE SLAVES
If the master has multiple slave If only one slave select pin is
select pins, the slaves can be wired available, the slaves can be chained
in parallel like this like this
ARDUINO PINS
MOSI and MISO are derived from a
"Master-Slave" naming scheme, which
has been criticized for its connotations.
The newer names COPI and CIPO
replace "Master" and "Slave" with
"Controller" and "Peripheral," making
the terms neutral and clearer for
understanding the roles of devices.
2.8INCH SPI MODULE ILI9341
Simulation
I2C COMMUNICATION
Inter-Integrated Circuit (I2C) is a synchronous, half-duplex, multi-master, multi-
slave serial communication protocol. Designed for communication between
components on a circuit board.
Key Features Key Components:
• Two-wire protocol (SDA, SCL). • SDA (Serial Data Line): Carries data
• Master-Slave architecture with addressing. between devices.
• Slower than SPI but requires fewer connections. • SCL (Serial Clock Line): Carries the clock
signal from the master.
Master-Slave Relationship:
• Master initiates communication and
controls the clock.
• Slaves respond when addressed by the
master.
Data Transfer:
• 7-bit or 10-bit addressing scheme.
• Data is transferred in bytes, starting with
the least significant bit.
I2C COMMUNICATION
Start Condition: Master pulls SDA low while SCL is high, signaling the start of communication.
Addressing: The master sends the address of the slave it wants to communicate with.
Data Transfer: Master sends or requests data from the slave.
Stop Condition: Master pulls SDA high while SCL is high, signaling the end of communication.
I2C DATA TRANSMISSION
1. The master sends the start
condition to every connected slave by
switching the SDA line from a high
voltage level to a low voltage level
before switching the SCL line from
high to low
I2C DATA TRANSMISSION
2. The master sends each
slave the 7- or 10-bit address
of the slave it wants to
communicate with, along
with the read/write bit.
I2C DATA TRANSMISSION
3. Each slave compares the
address sent from the
master to its own address. If
the address matches, the
slave returns an ACK bit by
pulling the SDA line low for
one bit. If the address from
the master does not match
the slave’s own address, the
slave leaves the SDA line
high.
I2C DATA TRANSMISSION
4. The master sends or
receives the data frame
I2C DATA TRANSMISSION
5. After each data frame has been
transferred, the receiving device
returns another ACK bit to the
sender to acknowledge successful
receipt of the frame
I2C DATA TRANSMISSION
6. To stop the data transmission,
the master sends a stop condition
to the slave by switching SCL high
before switching SDA high
MULTIPLE MASTERS WITH
MULTIPLE SLAVES
• Multiple masters can be connected to a single slave or multiple
slaves.
• The problem with multiple masters in the same system comes
when two masters try to send or receive data at the same time
over the SDA line.
• To solve this problem, each master needs to detect if the SDA
line is low or high before transmitting a message.
DS1307 REAL-TIME CLOCK (RTC)
• DS1307 is a low-power, real-time clock that provides:
• Seconds, minutes, hours, day, date, month, and year
information.
• Automatic leap year correction.
• Communicates using the I2C protocol.
• Has a backup battery feature to keep time even when
power is off.
Simulation
THANK YOU!!!