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

S04 Slides

Uploaded by

Kalindu Liyanage
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views

S04 Slides

Uploaded by

Kalindu Liyanage
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 118

Internal Peripherals & Hardware

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)

• An interrupt service routine (ISR) is a software routine that hardware invokes in


response to an interrupt. ISR examines an interrupt and determines how to handle
it executes the handling, and then returns a logical interrupt value (Basically, the
code block executes when an interrupt occurs).
• For each interrupt there must be ISR.
• For every interrupt there is a fixed location in memory that holds the address of its
ISR.
• Group of ISR address is called interrupt vector table (IVT).
INTERRUPT VECTOR TABLE (IVT) OF ATMEGA328P
STEPS IN EXECUTING AN INTERRUPT
• Microcontroller finishes the instruction it is executing and saves the address
of the next instruction (PC – Program Counter) on the stack.
• It saves the current status of all the interrupts internally.
• It jumps to a fixed location in memory called the interrupt vector table.
• The microcontroller gets the address of ISR from the Interrupt vector table
and jumps to it and starts to execute the ISR until it reaches the last
instruction RETI (Return from ISR).
• The microcontroller returns to the place where it was interrupted, it gets the
PC address from stack by popping the top two bytes of the stack into the PC
and then starts executes from that address.
ENABLING AND DISABLING AN INTERRUPT

• On reset interrupts are disabled(masked).


• Interrupts must be enabled by the software.
• I bit of SREG (Status register) is responsible for enabling and disabling the
Bit 7 – I: Global Interrupt Enable
interrupts globally. Bit 6 – T: Bit Copy Storage
Bit 5 – H: Half Carry Flag
Bit 4 – S: Sign Bit
Bit 3 – V: Two’s Complement Overflow
Bit 2 – N: Negative
Bit 1 – Z: Zero
Bit 0 – C: Carry Flag

• CLI for disable interrupt globally.


• SEI for enable interrupt globally.
NOTE
• Upon activation of the interrupt, the I bit is clear by the AVR itself to make
sure another interrupt can not interrupt the Microcontroller while it is
serving the current one.
• At the end of ISR, RETI instruction will make I = 1 to allow another interrupt
to come in.
AVR TIMER0 OVERFLOW INTERRUPT

Make I bit 1 to Enable global


interrupts

Make TOIE0 – Timer overflow


Interrupt Enable bit 1 (If you are
using timer in normal mode)

This bit (Timer overflow flag) will


set to 1 automatically by AVR
When overflow happen.

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

Set WGM12 bit to 1 for CTC mode


Calculate OCR1A assuming 1024
Prescaler to have a relevant delay
CS12 and CS10 bits for 1024
Prescaler
Set OCIE1A bit to enable interrupt
on Compare Match A
Enable interrupts globally

Toggle LED once ISR is called


SIMULATION
EX: Write a C program to generate square wave of 16µs on PB5 using Timer0 overflow interrupt while
at the same time data is being transferred from PORTC to PORTD. Assume 16MHz AVR clock.
Import Interrupt library
Make PB5 Output

Make PORTD Output


Make PORTC Input
Load relevant count for TCNT0 to get 16µs
Make relevant configuration on TCCR0X
Enable Timer0 overflow interrupt
Enable interrupts globally

Copy PINC to PORTD

Toggle PB5 once ISR called


SIMULATION
AVR EXTERNAL HARDWARE INTERRUPT
Upon activation of these pins AVR is interrupted from its task and jumps
to the vector table to ISR

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

When an edge or logic change on the INT1 pin triggers an interrupt


request, INTF1 becomes set (one). The flag is cleared when the interrupt
routine is executed.
EXAMPLE
Toggle the red LED at PD7 with a button press on PD2 and blink a green LED at
PD6 with a 1-second delay.
WITH HARDWARE INTERRUPTS

Activate External Interrupt on INT0


Making it falling edge triggered
Enable interrupts globally

On interrupt, change the LED


• Toggle led at PB4 continuously and when INT0 is activated stop toggling of led
at PB4 and start toggling led at PB5. Then reverse the action on next press.

Defining a global variable to keep


the toggle PIN number.

Making PB4 and PB5 Outputs.


Making PD2 Input Pull-up
Activate External Interrupt on INT0
Making it falling edge triggered
Enable interrupts globally

Toggling the LED pin

On interrupt, change the toggle pin


SIMULATION
ADC and Serial Communication
AVR SERIAL COMMUNICATION
• Data transfer between two points.
• We transfer data digitally because microcontroller is digital circuit.
• Serial communication is slower but cheaper.
• Two methods of Serial Communication
• Synchronous : Transfer block of data at a time.
• Asynchronous : Transfer a single byte at a time.
• In ATMEGA328P we have USART to do this (Universal Synchronous
Asynchronous Receiver-Transmitter).
REGISTERS ASSOCIATED WITH SERIAL COMMUNICATION
The transmit data buffer register (TXB) will be
the destination for data written to the UDRn
register location. Reading the UDRn register
location will return the contents of the
receive data buffer register (RXB).

Use to setup various configurations and to


check status.
REGISTERS ASSOCIATED WITH SERIAL COMMUNICATION

This is a 12-bit register which contains the


USART baud rate
C - ARRAYS

Initializing Arrays
AVR SERIAL TRANSMISSION PROGRAMMING

Waiting for USART Data


Register Empty to write
Setting up required
new value.
configurations
Writing new value to UDR0
register

Sending data character by


character.
SIMULATION
ADC - ANALOG TO DIGITAL CONVERTER IN AVR
MICROCONTROLLER
• The AVR ADC allows the AVR microcontroller to convert analog voltages to
digital values with few to no external parts.
Analog
ADC CPU
Sensor
• 10-bit ADC Block diagram
Vref

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.

ADC Enable - Writing this bit to


one enables the ADC. By writing ADC Interrupt Flag - This bit is ADC Interrupt Enable - When
it to zero, the ADC is turned off. set when an ADC conversion this bit is written to one and the
ADC Start Conversion - write this completes, and the data registers I-bit in SREG is set, the ADC
bit to one to start each are updated. conversion complete interrupt is
conversion. activated.

ADC Auto Trigger Enable - When


this bit is written to one, auto
triggering of the ADC is enabled.
By default, the successive approximation circuitry requires an
input clock frequency between 50kHz and 200kHz to get
maximum resolution. If a lower resolution than 10 bits is needed,
the input clock frequency to the ADC can be higher than 200kHz
to get a higher sample rate. A normal conversion takes 13 ADC
clock cycles.
REGISTERS ASSOCIATED WITH ADC IN AVR
ADC Auto Trigger Source - If
ADATE in ADCSRA is written to
one, the value of these bits selects
which source will trigger an ADC
conversion. If ADATE
is cleared, the ADTS2:0 settings
will have no effect.

ADC5D..ADC0D: ADC5..0 Digital


Input Disable - When this bit is
written logic one, the digital input
buffer on the corresponding ADC
pin is disabled. The corresponding
PIN register bit will always read as
zero when this bit is set. When an
analog signal is applied to the
ADC5..0 pin and the digital input
from this pin is not needed, this
bit should be written logic one to
reduce power consumption in the
digital input buffer.
EXAMPLE
• Read analog voltage on PC2 and serial transmit the value.
EXAMPLE Selects Vcc as reference and enables the ADC2
Disables digital input
buffer on PC2 to save
power

enables the ADC and


select 128 division factor.
16Mhz/128 = 125kHz,
Which is in between
50Khz - 200kHz

Convert ADC value to


char array
Starting the conversion
Waiting for Conversion
flag to High
Return converted value
SIMULATION
• Read the analog voltage on the PC2 pin and proportionally adjust the
brightness of the LED on the PD6 pin.

Making PC2 Input


Making PD6 Output enables the ADC and select 128 division factor.
16Mhz/128 = 125kHz, Which is in between 50Khz - 200kHz
Selects Vcc as reference and enables the ADC2
Disables digital input buffer on PC2 to save power
Starting the conversion
Waiting for Conversion flag to High
Assign the result to OCR0A register (Divided
by 4 Since ADC is 10-bit and OCRA0 is 8-bit)
Setting up corresponding PWM
settings.(Explained)
Boards, Platforms and Frameworks
EMBEDDED BOARDS
• Embedded boards are specialized hardware platforms that integrate
microcontrollers or microprocessors with essential components to perform
specific tasks within an embedded system.
• They serve as the central unit in embedded systems, controlling various
processes and interfacing with other hardware components.

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.

• I/O Ports (Digital, Analog)


• Typically having headers.

• 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.

• STMicroelectronics STM32 Series:


• High-performance, low-power, wide range of peripherals.

• Nordic Semiconductor nRF52:


• Bluetooth Low Energy (BLE), low-power wireless applications.

• Texas Instruments MSP430:


• Ultra-low-power applications, precision sensing.

• NXP LPC Series:


• ARM Cortex-M series, industrial applications.
DEVELOPMENT PLATFORMS
• Development platforms are software tools or environments that facilitate the development,
testing, and deployment of embedded applications. They provide a comprehensive ecosystem
that includes a compiler, debugger, code editor, libraries, and often, hardware abstraction layers.
• Importance:
• Streamline the development process by providing ready-to-use tools and libraries.
• Enable rapid prototyping, debugging, and deployment.
• Ensure compatibility with specific hardware, making it easier to write and test code.

Vendor-Specific Platforms: Third-Party Platforms:


• Platforms provided by microcontroller or • Independent platforms that support multiple
microprocessor manufacturers, tailored to their hardware vendors.
specific hardware. • Offer flexibility by allowing developers to use
• Typically offer optimized libraries, peripheral drivers, different microcontrollers or processors within
and dedicated tools for their hardware. the same environment.
• Examples include STM32Cube (STMicroelectronics), • Examples include PlatformIO, Arduino IDE, and
MPLAB X IDE (Microchip), and Code Composer Keil uVision.
Studio (Texas Instruments).
POPULAR DEVELOPMENT PLATFORMS
• PlatformIO: A versatile IDE and build system supporting multiple hardware platforms, ideal for
cross-platform embedded development.
• STM32Cube: A comprehensive tool suite for STM32 microcontrollers, offering deep control over
peripherals and real-time performance.
• Arduino IDE: A beginner-friendly platform for rapid prototyping, supporting a wide range of
Arduino and third-party hardware.
• Zephyr RTOS: A scalable RTOS for resource-constrained IoT devices, focusing on real-time
capabilities, low power, and secure communication.
• ESP-IDF: The official framework for ESP32 microcontrollers, optimized for developing IoT
applications with Wi-Fi and Bluetooth support.
• mbed OS: An open-source OS for IoT devices, emphasizing security, connectivity, and cloud
integration for ARM Cortex-M microcontrollers.
FRAMEWORKS
A framework is a pre-built structure or set of libraries that provide a
standardized way to develop software applications, including embedded
systems. It typically includes APIs, tools, and best practices to simplify
development and ensure consistency.
Importance:
• Frameworks reduce development time by providing reusable code and
libraries.
• They help manage complexity by offering modular components and
hardware abstraction layers.
• Facilitate cross-platform development, allowing code to be more portable
across different hardware.
FRAMEWORKS
• Vendor-Specific Frameworks: Tailored frameworks provided by microcontroller
vendors, offering optimized libraries and drivers for specific hardware, like
CMSIS for ARM Cortex-M and STM32Cube for STM32.
• Open-Source Frameworks: Community-developed frameworks that are flexible
and transparent, with broad support, such as Arduino, Zephyr RTOS, and
libOpenCM3.
• Real-Time Operating System (RTOS) Frameworks: Frameworks designed for
managing real-time tasks with features like scheduling and inter-task
communication, including FreeRTOS, Zephyr RTOS, and ESP-IDF.
• IoT-Specific Frameworks: Frameworks focused on developing IoT applications
with emphasis on connectivity, cloud integration, and device management, such
as ESP-IDF, mbed OS, and Zephyr RTOS.
• .
HARDWARE ABSTRACTION
• Hardware abstraction refers to the process of
creating a layer of software that provides a
simplified, standardized interface to the hardware
components of a system.
• This abstraction layer hides the complexities of
the underlying hardware, allowing developers to
interact with hardware resources through high-
level APIs instead of dealing with hardware-
specific details.
PROS
• Portability: Code developed with hardware abstraction can be more easily ported to
different hardware platforms, as it is less dependent on the specifics of the hardware.
• Reduced Complexity: Simplifies development by providing a uniform interface for
interacting with hardware components, making it easier to write and maintain code.
• Easier Upgrades: Allows for easier upgrades or changes in hardware without requiring
extensive modifications to the application code.
• Improved Productivity: Developers can focus on application logic rather than low-level
hardware interactions, speeding up development and reducing the likelihood of errors.
• Code Reusability: Facilitates code reuse across different projects and hardware
platforms, as the same abstraction layer can be employed in multiple systems.
CONS
• Performance Overhead: The abstraction layer can introduce performance overhead due to
additional processing required to translate high-level commands into hardware-specific operations.
• Limited Hardware Access: Some low-level features or optimizations may be inaccessible or
difficult to achieve due to the abstraction layer’s limitations.
• Increased Complexity in Abstraction Layer: Designing and maintaining the abstraction layer itself
can be complex and may require significant effort to ensure it covers all necessary hardware
features.
• Potential for Reduced Efficiency: The generic nature of the abstraction layer might not be
optimized for all hardware configurations, potentially leading to less efficient use of hardware
resources.
• Learning Curve: Developers need to understand the abstraction layer’s API and its limitations,
which can add to the learning curve and may affect development speed initially.
EMBEDDED BOARDS VS. PLATFORMS VS. FRAMEWORKS
• Embedded Boards
• Physical hardware boards containing microcontrollers or microprocessors.
• Provide the base hardware for embedded systems development.
• Arduino Uno, STM32 Nucleo, ESP32 Dev Kit.

• 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

What are these

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

How does it become


one line of code?
DIGITAL I/O AND DELAY
TOGGLE LED WITH BUTTON PRESS
COMPARISON
EEPROM LIBRARY
UPDATE TOGGLE CODE
Useful Extensions
C/C++ EXTENSION PACK
The C/C++ Extension Pack in Visual Studio Code is quite useful when you're working with
Arduino and Atmel (AVR) programming for several reasons:
1. Syntax Highlighting and IntelliSense: The extension provides enhanced syntax highlighting
and IntelliSense (code completion) for C and C++ code, making writing and understanding
your Arduino and AVR code easier.
2. Error Checking: The extension offers real-time error checking and warnings as you write
your code, helping you catch mistakes early before compiling.
3. Code Navigation: It allows for easy navigation through your code, with features like Go to
Definition, Find All References, and symbol search, which is particularly useful when dealing
with complex projects.
4. Debugging Support: Although debugging can be more
complex in embedded environments, the extension
provides integration with debugging tools like GDB, which
can be useful if you have a setup that supports it.
5. Configuration Management: The extension allows you to
set up and manage different build configurations, which is
helpful if you're switching between different
microcontroller targets or build options.
GITHUB COPILOT
GitHub Copilot is an AI-powered code assistant integrated into Visual Studio Code and
other IDEs. It's designed to help developers by providing code suggestions,
autocompletion, and even generating entire functions or code snippets based on your
writing context.
1. Code Suggestions and Autocompletion
2. Learning and Exploring Code
3. Error Reduction
4. Documentation and Comments
5. Speeding Up Development
6. Debugging Assistance
7. Support for Multiple Languages GitHub Copilot is available for free to
students as part of the GitHub Student
Developer Pack for UoM email.
PWM

How do we change Fast mode and


the frequency? phase correct mode?
FADING A LED
EXTERNAL INTERRUPTS

Time interrupts? All other interrupts


were there in the
Interrupt Vector
Table?
TOGGLE BUTTON WITH INTERRUPT
SERIAL COMMUNICATION
SERIAL COMMUNICATION
Serial.print() vs Serial.write()
• Data Format: Serial.print() is for
sending data in a readable text format,
while Serial.write() is for sending raw
binary data.
• Use Cases: Use Serial.print() when you
want to output human-readable
information (like debugging messages)
to a serial monitor. Use Serial.write()
when you're dealing with
communication protocols or devices
that expect binary data (like sending
data to a microcontroller or a sensor).
SENDING “HELLO WORLD!”
ADC

Change in clock Set reference


frequency? voltage?

All other modes like


interrupt after
completion?
READ ANALOG VOLTAGE AND SERIAL TRANSMIT THE VALUE
COMPARISON

What is better?
Useful Arduino Functions
MILLIS() AND MICROS()
SWITCH DEBOUNCE Will this logic work
practically?

• Switch debouncing in an electronic design ensures


that the device that is sampling the switch waveform
does not misinterpret a single button press as many.
Time to wait while "de-
bouncing” button at press

Time to wait while "de-


bouncing” button at release

Not the best way


ANALYZE SWITCH DEBOUNCE
25ms
1
LED LED
BUTTON BUTTON
4

2
LED
BUTTON

LED
BUTTON
3
DEBOUNCING WITH
INTERRUPTS

Analyzing the time


gap between two
consecutive counts

If a press toggle
the LED
PULSEIN()
ULTRASONIC SENSOR
Working Principle

• Ultrasonic sensors measure distance by emitting ultrasonic waves and


measuring the time it takes for the echo to return.
• The sensor emits a sound wave at a frequency above the human hearing
range (typically 40 kHz).
• The time delay between the transmission and reception of the echo is used
to calculate the distance to an object.

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

Returns the number of


microseconds after emit

Assuming speed of sound as


340𝑚𝑠 −1
TONE()
BUZZER
• Active Buzzer
• Generates sound on its own when powered.
• Simple on/off control.
• Passive Buzzer
• Requires an external signal (like PWM) to generate
sound.
• Can produce various tones and melodies.
• Applications
• Alarm systems.
• Notification sounds.
• Musical tones.
SIMULATION
MORE FUNCTIONS BUILT-IN ARDUINO
https://docs.arduino.cc/l
anguage-reference/
Understanding and Using
Libraries in Arduino
LIBRARIES IN ARDUINO
• Libraries in Arduino are collections of pre-written code that make it easier to perform
complex tasks.
• They encapsulate functions and procedures for specific hardware modules, sensors, or
software features.
Core Libraries
Purpose • Included with the Arduino IDE by default (e.g., Wire,
EEPROM, Servo).
• Simplify coding by providing pre-built
Contributed Libraries
functions.
• Created by the Arduino community and available
• Reduce code redundancy. for download (e.g., Adafruit_Sensor, LiquidCrystal,
FastLED).
• Enable reusability and modularity in Custom Libraries
projects. • User-defined libraries for specific projects or
hardware.
SERVO MOTOR
• A servo motor is a rotary actuator that allows
precise control of angular position.
• It consists of a motor coupled with a sensor for
position feedback
• Key Characteristics
The width of the PWM signal determines the
• Control Signal: Typically controlled by Pulse Width
position of the servo motor.
Modulation (PWM).
• Movement Range: Usually from 0° to 180°. Pulse Width:
• 1ms (1000 µs): Moves the servo to 0°
• Internal Components:
• 1.5ms (1500 µs): Centers the servo at 90°
• DC Motor: Provides the movement.
• 2ms (2000 µs): Moves the servo to 180°
• Gearbox: Reduces speed and increases torque.
• Potentiometer: Provides position feedback.
• Applications
• Robotics, RC vehicles, automated systems.
ADDING LIBRARY TO THE PLATFORMIO PROJECT
6

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!!!

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy