KEMBAR78
Unit 1 Notes | PDF | Arduino | Computing
0% found this document useful (0 votes)
6 views123 pages

Unit 1 Notes

Arduino is an open-source platform for building electronics projects, utilizing microcontrollers like the ATmega328P. The document details various Arduino boards, their specifications, and key features such as digital and analog I/O pins, programming options, and communication interfaces. It also covers the Arduino IDE, programming concepts, and the architecture of the ATmega microcontroller.

Uploaded by

Akila V
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views123 pages

Unit 1 Notes

Arduino is an open-source platform for building electronics projects, utilizing microcontrollers like the ATmega328P. The document details various Arduino boards, their specifications, and key features such as digital and analog I/O pins, programming options, and communication interfaces. It also covers the Arduino IDE, programming concepts, and the architecture of the ATmega microcontroller.

Uploaded by

Akila V
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 123

WELCOME TO :

ARDUINO FOR BEGINNERS


What is Arduino ?
● Arduino is an open-source platform used for building electronics
projects.

● All Arduino boards have a microcontroller.

● A microcontroller is basically a really


small computer.
Some Popular Arduino Boards
BOARD: Arduino UNO Arduino Nano Arduino Due Arduino Mega

Processor ATmega328P ATmega168 ATSAM3X8E ATmega2560


ATmega328P

Processor 16 MHz 16 MHz 84 MHz 16 MHz

Best Application Desktop prototyping Low cost, Small High Performance High I/O
& use with Arduino profile, Simple prototyping with requirements with
Shields project superior analog I/O more memory
space
DC Power Jack
Voltage
Regulator USB Port

Reset Button
USB Interface
chip
Crystal
Oscillator Analog Reference
Voltage
Ground
Power Source -

ATmega328 Digital
Pins
Analog pins

Serial
5V ON Indication
in -Circuit Serial
Communication
Programming Header
USB Port
This is a printer USB port used to load a
program from the Arduino IDE onto the Arduino
board. The board can also be powered through
this port.
Power port:
The Arduino board can be powered
through an AC-to-DC adapter or a battery.
The power source can be connected by
plugging in a 2.1mm center-positive plug
into the power jack of the board.

The Arduino UNO board operates at a voltage of


5 volts, but it can withstand a maximum voltage
of 20 volts. If the board is supplied with a higher
voltage, there is a voltage regulator (it sits
between the power port and USB connector) that
protects the board from burning out.
2.1mm center-positive plug
Microcontroller

It is brains of your Arduino. The microcontroller used on the UNO board is


Atmega328P by Atmel Atmega328P has the following components in it:

● Flash memory of 32KB. The program loaded from Arduino IDE is stored
here.
● RAM of 2KB
● CPU: It controls everything that goes on within the device. It fetches the
program instructions from flash memory and runs them with the help of
RAM.
● Electrically Erasable Programmable Read Only Memory (EEPROM) of
1KB. This is a type of nonvolatile memory, and it keeps the data even
after device restart and reset.
The Arduino UNO board has 6 analog input pins, labeled “Analog 0

Analog input pins: to 5.” These pins can read the signal from an analog sensor like a
temperature sensor and convert it into a digital value so that the
system understands. These pins just measure voltage and not the
current because they have very high internal resistance. Hence,
only a small amount of current flows through these pins.

Although these pins are labeled analog and are analog input by
default, these pins can also be used for digital input or output.
Digital Pins You can find these pins labeled “Digital 0 to 13.” These pins can be
used as either input or output pins.

When used as output: pins act as a power supply source for the
components connected to them. (supply 4 mA of current at 5 V)

When used as input : they read the signals from the component
connected to them.
Some of the digital pins are labeled with
tilde (~) symbol next to the pin numbers (pin
numbers 3, 5, 6, 9, 10, and 11). These pins
act as normal digital pins but can also be
used for Pulse-Width Modulation (PWM),
which simulates analog output like fading an
LED in and out.
Arduino Board
Programming
What is Arduino
• Open source electronics platform
– Easy to use hardware & software
• Equipped with sets of analog & digital I/Os
• Can be interfaced to extension boards ( shields)
• Features USB based communication interface
• Provided with Integrated Development
Environment
• Aimed to provide low-cost and easy way for
students & professionals
• GNU General Public License
Arduino Board
Hardware Block Diagram
Digital
SPI
I/O

Analog ATMEL AVR I2C


Inputs MCU

USB
Interface UART

POWER
+5 Volts Clock
+3.3 Reset
Volts
Arduino Uno Pinout
Arduino IDE
• Allows to write program and upload them into the board
• Online Web Editor
• Arduino Desktop IDE
Sketches
• Program written using Arduino Software
• Written in text editor & save with extension of .ino
• Arduino uses this name for a program
• Code which will be uploaded and run on the board
IDE Basic Options
• New
– Creates a new sketch
• Open
– Allows to load the sketch from the system
• Save
– Saves the sketch
• Verify
– Checks the code for errors compiling it
• Upload
– Compiles the code and uploads to configured board
• Serial Monitor
– Opens a serial monitor
• Quit
– Closes all IDE windows
Variables
• a place to store the data
• Has name, value and type
– Example declaration
– int pin = 2;
• Specified only once and can be used many times
• Variable assignment
– pin = 3;
Functions
• Setup
– Called when a sketch starts
– Used to initialize variables, pin modes, start using
libraries
– Will run only once, after each power up / reset of
the board
• Loop
– Called after creating setup function
– Function loops consecutively
– Allows the program to change & respond
– Actively controls the arduino
Digital I/O
• Generic Pin on an IC / Board
• Configured as Input or Output
• Can be enabled / disabled
• Logic Level
– High
– Low
• Input values are readable
• Output values are writable / readable
• Input configured pins also can be used as Interrupt
• No predefined purpose
Digital Pin Functions
• pinMode
– Configures pin as Output or Input
– pinMode(pin, mode)
• pin : pin number
• mode: INPUT, OUTPUT, INPUT_PULLUP
• digitalWrite
– Writes a High or Low value to a digital pin
– digitalWrite(pin, value)
• pin : pin number
• value : HIGH or LOW
Digital Pin Functions…
• digitalRead
– Reads the current value of specified pin
– int digitalRead(pin)
• pin : pin number
• Returns the current value
UART / Serial Port
• Univeral Asynchronuos Receiver / Transmitter
• Supports NRZ encoding format
• 7 or 8 bit data
• 1 or 2 Stop bits
• Hardware Flow Control
– RTS – Request to Send
– CTS – Clear to Send
• Programmable Parity ( even, odd and none )
• RS485, RS232 compatible
UART Interface

TXD
RXD
UART
NXP Serial
IMX6 RTS
Device
CTS
Serial function
• begin
• end
• print
• println
• readBytes
• readString
• write
• setTimeout
• available
• availableForWrite
Inter Integrated Circuit Inteface
• Multi master, multi slave serial interface bus
• Uses only two bi-directional open drain lines
– SDA : Serial Data Line
– SCL : Serial Clock Line
• Since lines are open drain, these pins needs to be pulled high.
– Normally pull up resistor will be 4.7 K / 10K
I2C Model
VCC

R R

SDA

SCL

I2C
I2C I2C I2C
Master
Slave Slave Slave
uC
I2C / Wire Functions
• begin
• receive
• send
• onRequest
• RequestFrom
Serial Peripheral Interface
• Synchronous Serial Communication Interface
• Used for Short distance. Basically with in the
board
• Full duplex mode with master slave architecture
• Also called as four-wire bus
• Pins
– MISO : Master Input Slave Output
– MOSI : Master Output Slave Input
– SCK : Serial Clock
– SS : Slave Select
SPI Model
MOSI
MISO
SPI SPI
Master SCK Slave 1
SS1

SPI
Slave 2
SS2
Architecture ATmega328 and
Ardunio
18ECO108J
Architecture
AVR Architecture

🞂 ​Clocks and
Power
AVR Architecture

🞂 ​CPU
🞂​ Details
coming
AVR Architecture

🞂 ​Harvard architecture
🞂 ​Flash – program
memory
🞂​ 32K
🞂 ​SRAM – data
memory
🞂​ 2K
🞂 ​EEPROM
🞂​ For long-term data
🞂​ On I/O data bus
Memory

🞂 ​Flash (32K) (15-bit addresses)


🞂​ Program memory – read only
🞂 ​ Non-volatile
🞂 ​ Allocate data to Flash using PROGMEM
keyword
🞂​ see documentation
🞂 ​SRAM (2K)
🞂​ Temporary values, stack, etc.
🞂​ Volatile
🞂​ Limited space!
🞂 ​EEPROM (1K)
🞂​ Long-term data
🞂​ see documentation on EEPROM library
AVR C P U

🞂 ​Instruction
Fetch and
Decode
AVR C P U

🞂 ​ALU
Instructions
AVR C P U

🞂 ​I/O
and
special
functions
AVR Register File

🞂 ​32 8-bit GP registers


🞂 ​Part of SRAM memory
space
Special Addressing Registers

🞂 ​X,Y and Z registers


🞂​ 16-bit registers made using registers
26 – 31
🞂 ​Support indirect addressing
AVR Memory

🞂 ​Program memory –
Flash

🞂 ​Data memory -
SRAM
Stack Pointer Register

🞂 ​Special register in I/O space [3E, 3D]


🞂​ Enough bits to address data space
🞂 ​ Initialized to RAMEND (address of highest
memory address)

🞂 ​Instructions that use the stack pointer


Program Status Register (PSR)

🞂 ​\
🞂 ​ Status bits set by instructions/Checked by
Branch/Skip instructions
🞂​ I – Global interrupt enable
🞂​ T – Flag bit
🞂​ H – Half carry (BCD arithmetic)
🞂​ S – Sign
🞂​ V – Overflow
🞂​ N – Negative
🞂​ Z – Zero
🞂​ C – Carry
AVR Architecture

🞂 ​Three timers
🞂 ​Very flexible
🞂​ Choose clock rate
🞂 ​ Choose “roll-over”
value
🞂 ​ Generate
interrupts
🞂 ​ Generate PWM
signals
🞂​ (represent 8-bit value
with using a clock
signal)
Arduino Timing Functions

🞂 ​delay(ms)
🞂​ wait for ms milliseconds before continuing
🞂 ​delayMicroseconds(us)
🞂​ wait for us microseconds before continuing
🞂 ​unsigned long millis( )
🞂​ return number of milliseconds since program
started
🞂 ​unsigned long micros( )
🞂​ return number of microseconds since
program started
🞂 ​ resolution of 4 microseconds
AVR Architecture

🞂 ​Interface to pins
🞂 ​Each pin
directly
programmabl
e
🞂​ Program
direction
🞂​ Program value
🞂​ Program pull-
ups
🞂 ​ Some pins are
special
🞂​ Analog vs.
Digital
🞂​ Clocks
I/O Ports

🞂 ​3 8-bit Ports (B, C, D)


🞂 ​Each port controlled by 3 8-bit
registers
🞂​ Each bit controls one I/O pin
🞂​ DDRx – Direction register
🞂​Defines whether a pin is an input (0) or and
output (1)
🞂​ PINx – Pin input value
🞂​ Reading this “register” returns value of pin
🞂​ PORTx – Pin output value
🞂​ Writing this register sets value of pin
I/O Ports

🞂 ​Pullups
🞂​ If a pin is an input (DDRxi = 0):
🞂​ PORTxi = 0 – pin is floating
🞂​ PORTxi = 1 – connects a pullup to the pin
 Keeps pin from floating if noone driving
 Allows wired-OR bus

🞂 ​Individual bits can be set cleared using


bit-ops
🞂 ​A bit can be toggled by writing 1 to
PINxi
🞂​ SBI instruction e.g.
Arduino Digital and Analog I/O Pins

🞂 ​ Digital pins:
🞂​ Pins 0 – 7: PORT D [0:7]
🞂​ Pins 8 – 13: PORT B [0:5]
🞂​ Pins 14 – 19: PORT C [0:5] (Arduino analog pins
0 – 5)
🞂​ digital pins 0 and 1 are RX and TX for serial
communication
🞂​ digital pin 13 connected to the base board LED
🞂 ​ Digital Pin I/O Functions
🞂​ pinMode(pin, mode)
🞂​ Sets pin to INPUT or OUTPUT mode
🞂​ Writes 1 bit in the DDRx register
🞂​ digitalWrite(pin, value)
🞂​ Sets pin value to LO W or HIGH (0 or 1)
🞂​ Writes 1 bit in the PORTx register
🞂​ int value = digitalRead(pin)
🞂​ Reads back pin value (0 or 1)
🞂​ Read 1 bit in the PINx register
Arduino Analog I/O

🞂 ​ Analog input pins: 0 – 5


🞂 ​ Analog output pins: 3, 5, 6, 9, 10, 11 (digital
pins)
🞂 ​ Analog input functions
🞂​ int val = analogRead(pin)
🞂​ Converts 0 – 5v. voltage to a 10-bit number
(0 – 1023)
🞂​ Don’t use pinMode
🞂​ analogReference(type)
🞂​ Used to change how voltage is converted
(advanced)
🞂 ​ Analog output
🞂​ analogWrite(pin, value)
🞂​ value is 0 – 255
🞂​ Generates a PWM output on digital pin (3, 5,
6, 9, 10, 11)
🞂​ @490Hz frequency
AVR Architecture

🞂 ​Analog inputs
🞂 ​Convert voltage
to a 10-bit digital
value
🞂 ​Can provide
reference voltages
PWM – Pulse Width Modulation

🞂 ​Use one wire to represent a multi-bit


value
🞂​ A clock with a variable duty cycle
🞂​ Duty cycle used to represent value
🞂​ We can turn it into a analog voltage using an
integrating filter
Port Special Functions

🞂 ​Lots of special uses for


pins
🞂​ Clock connections
🞂​ Timer connections
🞂​e.g. comparator output for
PWM
🞂​ Interrupts
🞂​ Analog references
🞂​ Serial bus I/Os
🞂​ USART
🞂​ PCI
Reading and Writing Pins Directly

🞂 ​Only one pin can be changed using the


Arduino I/O functions
🞂​ Setting multiple pins takes time and instructions
🞂 ​To change multiple pins simultaneously, directly
read/write the pin registers
🞂​ DDR{A/B/C}
🞂​ PORT{A/B/C}
🞂​ PIN{A/B/C}
🞂 ​ e.g. to set all digital pins 0 – 7 to a value:
🞂​ PORTD = B01100101;
AVR Architecture

🞂 ​Special I/O
support
🞂​ Serial protocols
🞂 ​Uses special pins
🞂 ​Uses timers
🞂 ​Beyond scope of
this course
Arduino C Programs

🞂 ​Arduino calls these “sketches”


🞂​ Basically C with libraries
🞂 ​Program structure
🞂​ Header: declarations, includes, etc.
🞂​ setup()
🞂​ loop()
🞂 ​Setup is like Verilog initial
🞂​ executes once when program starts
🞂 ​loop() is like Verilog always
🞂​ continuously re-executed when the end is
reached
Blink Program

int ledPin = 13; // LED connected to digital pin


13

// The setup() method runs once, when the sketch

starts void setup() {


// initialize the digital pin as an output:
pinMode(ledPin, OUTPUT);
}

// the loop() method runs over and over again,


// as long as the Arduino has power
void loop()
{
digitalWrite(ledPin, // set the LED on
HIGH); delay(1000); // wait for a second
digitalWrite(ledPin, LOW); // set the LED off
delay(1000); // wait for a second
}
The Arduino C++ Main Program

int main(void)
{
init();

setup();

for (;;)
loop();

return 0;
}
Arduino Serial I/O
🞂 ​Communication with PC via USB
serial line
🞂​ Use the Serial Monitor in the IDE
🞂 ​ Or set up a C or Java (or you-
name-it) interface
🞂 ​Example Serial library calls
🞂​ Serial.begin(baud-rate)
🞂​ 9600 default
🞂​ Serial.println(string)
🞂​ int foo = Serial.read()
🞂​ Read one byte (input data is buffered)
🞂 ​See documentation for more
Example Program
Pulse Width Modulation Using an
Arduino

Abstract
Pulse Width Modulation is a process that is used in many applications. One of the
easiest ways to implement this is by using an Arduino. The Arduino can do this in a number of
ways.
This application note will look at what Pulse Width Modulation is and will also explain
how to perform Pulse Width Modulation using two different methods.

Keywords
Pulse Width Modulation, Arduino, AnalogWrite, DigitalWrite

66
Table of Contents

Abstract 1

Introduction 3

Objective 4

Methods 4

AnalogWrite 4

DigitalWrite 6

Conclusion 7

Reference 8

67
Introduction

To understand how to implement Pulse Width Modulation, it is first necessary to


understand what it does. Pulse Width Modulation (PWM) is a way to provide a variable signal
from a given set signal. PWM does this by changing the pulse width, which in turn, changes
the duty cycle of a square wave to alter how much power is supplied to the attached
component. It varies because the signal takes the duty cycle and averages the power signal
that is output. This can be seen in Figure 1 below.

Figure 1. Varying Duty cycle [2]

So the larger the duty cycle percentage, the larger the output signal will be. In the picture
above and for the Arduino, it is the voltage that varies.

The Arduino should also be briefly explained before moving on to how to create the
PWM. There are many variations of Arduinos on the market. The Arduino Uno will be the focus
of this application note. The Arduino is a board that allows the user to access, program and
build with a microcontroller. The Arduino has 14 digital input/output pins [1]. Six of these pins
can be used for with the PWM function that is already built into the Arduino. The pins that do
not have this ability can still be used for PWM but must be used in another way. They output
either a High (5V) or Low (0V) depending on what is coded. The coding will be explained in
the sections that describe the different methods for PWM.

68
Objective

The objective of this application note is to provide an understanding of how to create


a PWM signal using multiple methods on the Arduino Uno.

Methods

This application note will talk about the two most basic ways of implementing PWM.
These methods are analogWrite and digitalWrite.

AnalogWrite

The Arduino has six pins that can already do PWM without varying much. Some
sample code is provided in Figure 2 below.

Figure 2. analogWrite

The code is broken down into three sections. The first section is where the variables that are
going to be used are initialized. For instance, the line that says “int led = 9;” sets pin 9 on the
Arduino to the variable “led”. The variable “led” can now be used in the code and it will tell
the
69
Arduino that it is using pin 9. This makes the code cleaner and it helps keep track what each
line in the code is actually affecting. The next section of the code is the void setup. This section
is where pins are set as either inputs or outputs. This is done with the pinMode function. The
function takes your pin or variable and then sets it to what you specify, either input or output. In
this example, the variable “led” is being set as an output. The last section of code is the void
loop. This is where the bulk of the code goes. It will run an infinite loop of the code that is put
in this section.

AnalogWrite function generates a square wave that can be varied in the function. The
two things the function needs to run are a pin/variable and a value to set the duty cycle. The
value that gets input for this function has to be between 0 and 255 [3]. A duty cycle of 100%
occurs if the value is set at 255 and a value of 0 gives a duty cycle of 0%. To get a specific
duty cycle a value for analogWrite needs to be calculated. This is shown below.

𝑑𝑢𝑡𝑦 𝑐𝑦𝑐𝑙e ∗ 255 = 𝑎𝑛𝑎𝑙o𝑔W𝑟i𝑡e 𝑣𝑎𝑙𝑢e

For example, a duty cycle of 20% would need to be set at a value of 51. The example
code in Figure 2 has code that changes the duty cycle while it is running. The output of this
code is shown in Figures 3, 4, 5 and 6 below. Each figure shows a different time. So the
pictures from left to right shows the code changing the pulse width.

Figure 3. Figure 4. Figure 5. Figure 6.

70
DigitalWrite

The digitalWrite function can be used on all 14 digital input/output pins. Unlike
analogWrite, a square wave has to be made manually. This is done by using both the digitalWrite
and delay functions. The digitalWrite function can only provide a High (5V) or a Low (0V) [4].
So manually doing this in the code provides a square wave. Sample code is shown in Figure 7
below.

Figure 7. digitalWrite

Each time the digitalWrite function is used, it needs to be followed with the delay function.
The above code currently has a duty cycle of 50%. To get a desired duty cycle, the math
becomes a little harder than for the analogWrite function.

𝑑e𝑙𝑎𝑦 𝑡i𝑚e 𝐻𝐼𝐺𝐻


= 𝐷e𝑠i𝑟e𝑑
𝑑e𝑙𝑎𝑦 𝑡i𝑚e 𝐻𝐼𝐺𝐻 + 𝑑e𝑙𝑎𝑦
𝑑𝑢𝑡𝑦 𝑐𝑦𝑐𝑙e
𝑡i𝑚e 𝐿𝑂W
Using the same example as before, a 20% duty cycle could be implemented using a delay time
HIGH as 400 and a delay time LOW as 1600. The good thing about the digitalWrite function is
that frequency can also be changed. Frequency is the inverse of the period or total time it takes
for one cycle. So to change frequency, the total time just needs to be increased or decreased.
The current example takes 2 seconds so the frequency is 0.5 Hz.

71
Conclusion

The two methods have their pros and cons. AnalogWrite already generates a square
wave and it is simple to calculate the value needed to get a desired duty cycle. The AnalogWrite
function cannot have a different frequency than what is already set for that pin. The
DigitalWrite function requires more math to get the desired duty cycle but it allows for the
frequency to be changed. More information about both of these functions can be found on the
Arduino website [5]. Arduino is an open source microcontroller board so people are constantly
writing new code to be implemented on these boards. Other methods of generating a PWM
signal are available online but these are the most reliable because they were made available
with the Arduino board.

72
References

1 http://arduino.cc/en/Main/ArduinoBoardUno

2 http://motorcontroller.blogspot.com/2009/07/today-i-looked-over-my-ece-618-junior.html

3 http://arduino.cc/en/Reference/analogWrite

4 http://arduino.cc/en/Reference/digitalWrite

5 http://arduino.cc/

73
Lecture 6 – ATmega328 Timers
and Interrupts
Arduino Digital and Analog I/O Pins
🞂 ​ Digital pins:
🞂​ Pins 0 – 7: PORT D [0:7]
🞂​ Pins 8 – 13: PORT B [0:5]
🞂​ Pins 14 – 19: PORT C [0:5] (Arduino analog pins
0 – 5)
🞂​ digital pins 0 and 1 are RX and TX for serial
communication
🞂​ digital pin 13 connected to the base board LED
🞂 ​ Digital Pin I/O Functions
🞂​ pinMode(pin, mode)
🞂​ Sets pin to INPUT or OUTPUT mode
🞂​ Writes 1 bit in the DDRx register
🞂​ digitalWrite(pin, value)
🞂​ Sets pin value to LO W or HIGH (0 or 1)
🞂​ Writes 1 bit in the PORTx register
🞂​ int value = digitalRead(pin)
🞂​ Reads back pin value (0 or 1)
🞂​ Read 1 bit in the PINx register
Port Pin Definitions
#define PINB _SFR_IO8(0x03) #define PORTC _SFR_IO8(0x08)
#define PINB0 0 #define PORTC0 0
... ...
#define PINB7 7 #define PORTC6 6

#define DDRB _SFR_IO8(0x04) #define PIND _SFR_IO8(0x09)


#define DDB0 0 #define PIND0 0
... ...
#define DDB7 7 #define PIND7 7

#define PORTB _SFR_IO8(0x05) #define DDRD _SFR_IO8(0x0A)


#define PORTB0 0 #define DDD0 0
... ...
#define PORTB7 7 #define DDD7 7

#define PINC _SFR_IO8(0x06) #define PORTD _SFR_IO8(0x0B)


#define PINC0 0 #define PORTD0 0
... ...
#define PINC6 6 #define PORTD7 7

#define DDRC _SFR_IO8(0x07)


#define DDC0 0
...
#define DDC6 6
Interrupts
🞂 ​Allow program to respond to events when
they occur
🞂 ​Allow program to ignore events until the
occur
🞂 ​External events e.g.:
🞂​ UART ready with/for next character
🞂​ Signal change on pin
🞂​ Action depends on context
🞂​ # of edges arrived on pin
🞂 ​Internal events e.g.:
🞂​ Power failure
🞂​ Arithmetic exception
🞂​ Timer “tick”
ATmega328 Interrupts
ATmega328 Interrupts (cont)
Interrupt Model
🞂 ​When an interrupt event occurs:
🞂​ Processor does an automatic procedure call
🞂​ CALL automatically done to address for that interrupt
🞂​ Push current PC, Jump to interrupt address
🞂​ Each event has its own interrupt address
🞂 ​ The global interrupt enable bit (in SREG) is
automatically cleared
🞂​ i.e. nested interrupts are disabled
🞂​ SREG bit can be set to enable nested interrupts if desired
🞂 ​Interrupt procedure, called as “interrupt handler”
🞂​ Does whatever it needs to, then returns via RETI
🞂 ​ The global interrupt enable bit is automatically set on
RETI
🞂 ​ One program instruction is always executed after RETI
Interrupts
🞂 ​Type1 – Event is remembered when interrupt is
disabled
🞂​ If interrupt is not enabled, flag is set
🞂​ When interrupt is enabled again, interrupt
takes place, and flag is reset

🞂 ​Type 2 – Event is not remembered when


interrupt is disabled
🞂​ Signal level causes interrupt
🞂​ If level occurs when interrupt is enabled, interrupt
takes place
🞂​ If interrupt is not enabled, and level goes
away before the interrupt is enabled, nothing
happens
Interrupt Model
🞂 ​Interrupt hander is invisible to program
🞂​ Except through side-effects, e. g. via flags or
variables
🞂 ​ Changes program timing
🞂​ Can’t rely on “dead-reckoning” using instruction
timing
🞂 ​Must be written so they are invisible
🞂​ Cannot stomp on program state, e. g.
registers
🞂 ​ Save and restore any registers used
🞂​ Including SREG
Interrupt Vectors
🞂 ​Table in memory containing the first instruction
of each interrupt handler
🞂 ​ Typically at program address 0
Interrupt Vectors
🞂 ​If interrupts are not used, this memory can be
used as part of the program
🞂​ i.e. nothing special about this part of memory
🞂 ​ Example interrupt routine
🞂​ RESET: Sets up the stack pointer
Defined ISR’s
#define INT0_vect _VECTOR(1) /* External Interrupt Request 0 */
#define INT1_vect _VECTOR(2) /* External Interrupt Request 1 */
#define PCINT0_vect _VECTOR(3) /* Pin Change Interrupt Request 0 */
#define PCINT1_vect _VECTOR(4) /* Pin Change Interrupt Request 0 */
#define PCINT2_vect _VECTOR(5) /* Pin Change Interrupt Request 1 */
#define WDT_vect _VECTOR(6) /* Watchdog Time-out Interrupt */
#define TIMER2_COMPA_vect _VECTOR(7) /* Timer/Counter2 Compare Match A */
#define TIMER2_COMPB_vect _VECTOR(8) /* Timer/Counter2 Compare Match A */
#define TIMER2_OVF_vect _VECTOR(9) /* Timer/Counter2 Overflow */
#define TIMER1_CAPT_vect _VECTOR(10) /* Timer/Counter1 Capture Event */
#define TIMER1_COMPA_vect _VECTOR(11) /* Timer/Counter1 Compare Match A */
#define TIMER1_COMPB_vect _VECTOR(12) /* Timer/Counter1 Compare Match B */
#define TIMER1_OVF_vect _VECTOR(13) /* Timer/Counter1 Overflow */
#define TIMER0_COMPA_vect _VECTOR(14) /* TimerCounter0 Compare Match A */
#define TIMER0_COMPB_vect _VECTOR(15) /* TimerCounter0 Compare Match B */
#define TIMER0_OVF_vect _VECTOR(16) /* Timer/Couner0 Overflow */
#define SPI_STC_vect _VECTOR(17) /* SPI Serial Transfer Complete */
#define USART_RX_vect _VECTOR(18) /* USART Rx Complete */
#define USART_UDRE_vect _VECTOR(19) /* USART, Data Register Empty */
#define USART_TX_vect _VECTOR(20) /* USART Tx Complete */
#define ADC_vect _VECTOR(21) /* ADC Conversion Complete */
#define EE_READY_vect _VECTOR(22) /* EEPROM Ready */
Interrupts
🞂 ​Global interrupt enable
🞂​ Bit in SREG
🞂​ Allows all interrupts to be disabled with one
bit
🞂​ sei() – set the bit
🞂​ cli() – clear the bit
🞂 ​Interrupt priority is determined by order in
table
🞂​ Lower addresses have higher priority
🞂 ​ISR(vector) – Interrupt routine definition
🞂 ​reti() – return from interrupt
🞂​ automatically generated for ISR
External Interrupts
🞂 ​Monitorschanges in signals on pins
🞂 ​What causes an interrupt can be configured
🞂​ by setting control registers appropriately
🞂 ​Pins:
🞂​ INT0 and INT1 – range of event options
🞂​ INT0 – PORT D [2]
🞂​ INT1 – PORT D [3]
🞂​ PCINT[23:0] – any signal change (toggle)
🞂​ PCINT[7:0] – PORT B [7:0]
🞂​ PCINT[14:8] – PORT C [6:0]
🞂​ PCINT[23:16] – PORT D [7:0]
🞂 ​Pulseson inputs must be slower than I/O
clock rate
INT0 and INT1
🞂 ​External Interrupt Control
Register:

🞂 ​Sense Control (INT0 is the


same)
INT0 and INT1
🞂 ​External Interrupt Mask
Register

🞂​ If INT# bit is set (and the SREG I-bit is set),


then interrupts are enabled on pin INT#
🞂 ​ External Interrupt Flag Register

🞂​ Interrupt flag bit is set when a change


triggers an interrupt request
🞂​ Flag is cleared automatically when interrupt routine is
executed
🞂 Flag can be cleared by writing a 1 to it
Arduino Language Support for
External Interrupts
🞂 ​attachInterrupt(interrupt, function,
mode)
🞂​ interrupt: 0 or 1
🞂 ​ function: interrupt function to call
🞂 ​ mode: LOW, CHANGE, RISING,
FALLING
🞂 ​detachInterrupt(interrupt)
🞂 ​interrupts( ) – Enable interrupts :
sei( )
🞂 ​noInterrupts( ) – Disable
interrupts : cli( )
PCINT[23:0]
🞂 ​Pin
Change Interrupt Control
Register

🞂​ PCIE2 enables interrupts for


PCINT[23:16]
🞂 ​ PCIE1 enables interrupts for
PCINT[14:8]
🞂 ​ PCIE0 enables interrupts for
PCINT[7:0]
🞂 ​Pin Change Interrupt Flag Register
🞂 ​ PCIF# set if corresponding pins generate an interrupt
request
🞂 ​ Cleared automatically when interrupt routine is
executed
PCINT[23:0]
🞂 ​Pin
Change Mask
Register 2

🞂 ​Each bit controls whether interrupts are enabled


for the corresponding pin
🞂 ​ Change on any enabled pin causes an interrupt
🞂 ​ (Mask registers 1 and 0 are similar)
8-bit Timer/Counter 0
(1 and 2 are very similar)
Prescaler for Timer/Counter 0 & 1
Clock Source Select (CS0[2:0])

🞂​ T0 pin – PORT D[4]


🞂 ​ T1 pin – PORT D[5]
🞂 ​ Pin can be configured as output pin
🞂 Program can generate clock
External Clock Source
Timer/Counter Registers
🞂 ​TCNT0 – Timer/Counter Register (8-bit)
🞂 ​OCR0A – Output Compare Register A
🞂 ​OCR0B – Output Compare Register B
🞂 ​TCCR0A/B – Timer/Counter Control
Registers
🞂 ​TIMSK0 – Timer/Counter Interrupt Mask
Register
🞂​ TOV interrupt
🞂​ Compare A&B interrupts
🞂 ​TIFR0– Timer/Counter Interrupt Flag
Register
🞂​ TOV interrupt
🞂​ Compare A&B interrupts
Normal Mode (0)
🞂 ​Timer increments
🞂 ​Wraps around at TOP = 0xFF
🞂 ​Starts again at 0
🞂 ​TOV0 interrupt flag set when TCNT0 reset
to 0

🞂 ​Useful for generating interrupts every N


time units
🞂 ​Useful for generating an interrupt in N time
units
🞂​ Set TCNT0 to an initial value (255 – N)
C T C (Clear Timer on Compare) Mode (2)
🞂 ​ Timerincrements
🞂 ​ Wraps around at OCR0A
🞂​ OCR0A defines top value of counter
🞂 ​ Startsagain at 0
🞂 ​ OCF0A interrupt flag set when TCNT0 reset to
0
🞂 ​ Pin OC0A can be made to toggle when
counter resets
🞂​ Generate output waveform
Fast PWM Mode (3/7)
🞂 ​ Timer increments
🞂 ​ Wraps around at 0xFF (3) or
OCR0A (7)
🞂 ​ Start again at 0
🞂 ​ Pin OC0x generates waveform
🞂​ Set (reset) when timer=0
🞂​ Reset (set) when timer=OCR0x
Phase-Correct PWM Mode (1/5)
🞂 ​ Timer increments then decrements
🞂 ​ Increments from 0
🞂 ​ Up to 0xFF (1) or OCR0A (5)
🞂 ​ Than back down to 0
🞂 ​ Pin OC0x generates waveform
🞂​ Set when timer=OCR0x while
incrementing
🞂​ Reset when timer=OCR0x while
decrementing
Timer/Counter Control Registers
🞂 ​Timer/Counter Control
Register A

🞂 ​Timer/Counter Control
Register B
Mode Summary
COM0A[1:0] (COM0B[1:0] similar)
Timer/Counter 0 Interrupts
🞂 ​Timer/Counter 0 Interrupt
Mask

🞂​ TOIE0 – Timer Overflow


interrupt
🞂 ​ OCIE0A/B – Compare A/B
interrupt
🞂 ​Timer/Counter 1 Interrupt
Flags

🞂​ TOV0 – Timer Overflow flag


🞂 ​ OCF0A/B – Compare A/B
interrupt flag
Timer/Counter 2 (16-bit)
🞂 ​Similar to Timer/Counter 1
🞂​ 16-bit counter vs. 8-bit counter
🞂​ 16-bit registers
🞂​ Uses shared temporary 8-bit register to enable 16-bit
read/write
🞂​ Input capture register
Input Capture Unit
Input Capture Unit
🞂 ​Event on input causes:
🞂​ Counter value (TCNT1) to be written to
ICR1
🞂​ Time-stamp
🞂​ Interrupt flag ICF1 to be set
🞂​ Causing an interrupt, if enabled
🞂 ​PinICP1 – Port B [0]
🞂 ​Noise Canceller
🞂​ Pulses less than 4 clock cycles long are
filtered
🞂 ​Usefulfor measuring frequency and
duty cycle
🞂​ PWM inputs
Timer/Counter 1
🞂 ​ICR can also be used as the TOP
value
🞂​ Allows OCR1A to be used for PWM
generation
Timer/Counter 1 Modes
Timer/Counter 2 (8-bit)
🞂 ​Identical
to
Timer/Counter 1
🞂 ​Except for clock sources
External and Pin Change Interrupts
#defin PCIF _SFR_IO8(0x1B #defin PCMSK0 _SFR_MEM8(0x6
e R ) 0 e PCINT0 B) 0
#defin PCIF #defin
e 0 e
#define PCIF1 1 ...
#define PCIF2 2 #defin PCINT7 7
e

#define EIFR _SFR_IO8(0x1C) #defin PCMSK1 _SFR_MEM8(0x6C)


e
#define INTF0 0 #defin PCINT8 0
e
#define INTF1 1 ...
#defin PCINT14 6
e
#define EIMSK _SFR_IO8(0x1D)
#define INT0 0 #defin PCMSK2 _SFR_MEM8(0x6D)
e
#define INT1 1 #defin PCINT16 0
e
...
#define PCICR _SFR_MEM8(0x68) PCINT23 7
#defin
e
#define PCIE0 0
Timer/Counter 0 Registers
#define TCCR0A _SFR_IO8(0x24) #define OCR0A _SFR_IO8(0x27)
#define WGM00 0 #define OCROA_0 0
#define WGM01 1 ...
OCROA_7 7
#define COM0B0 4 #define
#define COM0B1 5
#define COM0A0 6 #define OCR0B _SFR_IO8(0x28)
#define COM0A1 7 #define OCR0B_0 0
...
#define TCCR0B _SFR_IO8(0x25) OCR0B_7 7
#define
#define CS00 0
#define CS01 1
#define CS02 2
#define WGM02 3
#define FOC0B 6
#define FOC0A 7

#define TCNT0 _SFR_IO8(0x26)


#define TCNT0_0 0
...
#define TCNT0_7 7
Timer/Counter Interrupts
#define TIFR0 _SFR_IO8(0x15) #define TIMSK0 _SFR_MEM8(0x6E)
#define TOV0 0 #define TOIE0 0
#define OCF0A 1 #define OCIE0A 1
#define OCF0B 2 #define OCIE0B 2

#define TIFR1 _SFR_IO8(0x16) #define TIMSK1 _SFR_MEM8(0x6F)


#define TOV1 0 #define TOIE1 0
#define OCF1A 1 #define OCIE1A 1
#define OCF1B 2 #define OCIE1B 2
#define ICF1 5 #define ICIE1 5

#define TIFR2 _SFR_IO8(0x17) #define TIMSK2 _SFR_MEM8(0x70)


#define TOV2 0 #define TOIE2 0
#define OCF2A 1 #define OCIE2A 1
#define OCF2B 2 #define OCIE2B 2
Timer/Counter 1
#define TCCR1A _SFR_MEM8(0x80) #defin TCNT1 _SFR_MEM16(0x84)
e
#define WGM10 0
#define WGM11 1 #defin TCNT1L _SFR_MEM8(0x84)
e
#define COM1B0 4 #defin TCNT1L0 0
e
#define COM1B1 5 ...
#define COM1A0 6 #defin TCNT1L7 7
e
#define COM1A1 7
#defin TCNT1H _SFR_MEM8(0x85)
#define TCCR1B _SFR_MEM8(0x81)
e TCNT1H0 0
#defin
e
#define CS10 0 ...
#define CS11 1 #defin TCNT1H7 7
e
#define CS12 2
#defin WGM12 3 #defin ICR1 _SFR_MEM16(0x86)
e WGM13 4 e
#defin
e
Timer/Counter 1 (cont)
#define OCR1A _SFR_MEM16(0x88)

#define OCR1AL _SFR_MEM8(0x88)


#define OCR1AL0 0
...
#define OCR1AL7 7

#define OCR1AH _SFR_MEM8(0x89)


#define OCR1AH0 0
...
#define OCR1AH7 7

#define OCR1B _SFR_MEM16(0x8A)

#define OCR1BL _SFR_MEM8(0x8A)


#define OCR1BL0 0
...
#define OCR1BL7 7

#define OCR1BH _SFR_MEM8(0x8B)


#define OCR1BH0 0
...
#define OCR1BH7 7
Timer/Counter 2
#define TCCR2A _SFR_MEM8(0xB0) #define OCR2A _SFR_MEM8(0xB3)
#define WGM20 0 #define OCR2_0 0
#define WGM21 1 ...
OCR2_7 7
#define COM2B0 4 #defin
e
#define COM2B1 5
#define COM2A0 6 #define OCR2B _SFR_MEM8(0xB4)
#define COM2A1 7 #define OCR2_0 0
...
#define TCCR2B _SFR_MEM8(0xB1) OCR2_7 7
#defin
e
#define CS20 0
#define CS21 1 #define ASSR _SFR_MEM8(0xB6)
#define CS22 2 #define TCR2BUB 0
#define WGM22 3 #define TCR2AUB 1
#define FOC2B 6 #define OCR2BUB 2
#define FOC2A 7 #define OCR2AUB 3
#define TCN2UB 4
#define TCNT2 _SFR_MEM8(0xB2) #define AS2 5
#define TCNT2_0 0 #define EXCLK 6
...
#define TCNT2_7 7
Timer Interrupt Program Example
void setup() {
DDRB = ; // Pin 13 as output
// Using timer 2
// Set to Normal mode, Pin OC0A disconnected
TCCR2A = ;
// Prescale clock by 1024
// Interrupt every 256K/16M sec = 1/64
sec TCCR2B = ;
// Turn on timer overflow interrupt flag
TIMSK2 = ;
// Turn on global interrupts
sei();
}
char timer = 0;

ISR( _vect) {
timer++;
PORTB = ;
}

void loop()
{
// Nothing to do
}
Timer Example #2
void setup() {
DDRB = ; // Pin 13 OUTPUT
// Using timer 2
// Set to CTC mode, Pin OC0A disconnected
TCCR2A = ;
// Prescale clock by 1 (no prescale)
TCCR2B = ;

// Set compare register


OCR2A = ;
// Turn on timer compare A interrupt
flag TIMSK2 = ;
// Turn on global interrupts
sei();
}
char timer = 0;

ISR( _vect) {
timer++;
PORTB = ;
}
void loop()
{
// Nothing to do
}
External Interrupt Example
#define pinint0 // Print out the information
#define pinint1 void loop()
void setup() {
{ );
pinMode(pinint0, );
pinMode(pinint1,
Serial.begin(9600); Serial.print("X: ");
// External interrupts 0 and Serial.print(percent0);
1 Serial.print(" Y: ");
// Interrupt on rising Serial.println(percent1);
edge EICRA = ; }
// Enable both interrupts
EIMSK = ;
// Turn on global interrupts
sei();
}
ISR( _vect) {

}
ISR( _vect) {

You might also like