Unit52. ATmega328 PCInterrupt
Unit52. ATmega328 PCInterrupt
FY - Department of Engineering,
Technology Sciences and Humanities
Microcontroller
2. ATmega 328P
Interrupts -PCINT
1
Interrupts:-----
1. Polling
2. Interrupt
2
Sciences and Humanities
Interrupts:-----
Polling method:
Repeatedly reading a data port and testing the input data value.
The processor takes a measurement from a port/sensor at regular time intervals.
Vishwakarma Institute of
This happens, even if there has been no change in the connected device.
Guaranteed or predictable time slices are allocated to each device that is polled.
Polling is ...
•Simple to code.
Technology
• Not very efficient because the polling happens even if the sensor reading has not changed
FY - Department of Engineering,
For ex..
Pulses received from
Encoder on Pin D3
Missed pulses or
inputs
Time spent by controller in reading
other pins, polling and then return
3
Sciences and Humanities
Interrupts:-----
Interrupt is a signal generated by hardware or software that triggers the interrupt service
routine-ISR to run.
Vishwakarma Institute of
1. The original process stops/paused and the return-address is saved onto the stack.
2. The interrupt code ISR is executed.
3. The original process is re-started using the return-address retrieved from the stack.
Timer Interrupts
Most processors have built-in timers which can be used to trigger processes when the timer
interval ends. (Timer overflow)
4
Sciences and Humanities
Interrupts:-----
Interrupts allow program to respond to events when they occur and
allow program to ignore events until the occur
Vishwakarma Institute of
5
Sciences and Humanities
Interrupts:-----
The types of interrupts in AVR
We might use interrupts in following cases
Vishwakarma Institute of
Simple tasks for using an interrupt include --(To detect pin changes )
• Reading a rotary encoder
• Read a sound sensor that is trying to catch a sound of click
Technology
In all of these situations, using an interrupt can free the microcontroller to get
some other work done and still not missing the input.
7
PCINT - Pin Change
Interrupt
Sciences and Humanities
The names and locations of each of the pin change interrupt pins.
Vishwakarma Institute of
programs .
FY - Department of Engineering,
8
PCINT - Pin Change
Interrupt
Sciences and Humanities
2 Each interrupt has a dedicated Group of pins share the same PCINT vector
Technology
9
Pin Change Interrupt
Registers
In Atmega 328 there are three special registers associated with PCINT
10
Pin Change Interrupt
Control Register
To enable pin change interrupt on a pin, we need to manipulate the PCICR register
Sciences and Humanities
Group control Bit PCINT group pins Pin numbers Pin numbers
On ATmega 328 On Arduino board
Technology
11
Pin Change Interrupt Flag
Register
Sciences and Humanities
When interrupt is triggered by a pin, then corresponding group flag bits will be
set. These flag bits are found at register PCIFR:
Vishwakarma Institute of
Once the PCINT group is activated in PCICR, next step is to select which pins of that
group can trigger the interrupt.
12
To enable specific pin(s) within a group, PCMSK –(Pin Change Mask register) is used
Three PCMSK registers for each group. Each bit in the PCMSK register corresponds to a PCINT pin
Sciences and Humanities
13
Pin Change Interrupt –ISR Vectors
What is the interrupt vector?
Sciences and Humanities
When the interrupt is triggered, the main code is paused and control jumps to the interrupt
vector.
The code for that ISR is executed. The ISR is located at vector address in ROM.
Vishwakarma Institute of
Each of the 3 groups of pins has a ISR or Interrupt Service Routines at three possible vectors.
The code is executed when interrupt from that group occurs and the interrupt flag is set.
FY - Department of Engineering,
There are three vectors, PCINTx_vect for pins from group x. (x is 0, 1, 2 respectively)
• ISR (PCINT0_vect) // for pin group Port D //Direct access to
• ISR (PCINT1_vect) // for pin group Port C
AVR registers is not
• ISR (PCINT2_vect) // for pin group Port B
supported in
Example syntax----- unoArdusim…
ISR (PCINT2_vect)
{ // For PCINT of pins --, code here How do we write
} the code?
14
Example interrupt in Arduino
// toggles LED when interrupt pin changes state
Sciences and Humanities
used
const byte ledPin = 13;
pinMode(ledPin, OUTPUT);
pinMode(interruptPin, INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(interruptPin), blink,
CHANGE); }
Basically, a button is attached to an interrupt pin. Whenever the button is pressed, the interrupt
15
pin changes its state and triggers the interrupt. If the button is not pressed, the program stays
Example interrupt in Arduino
volatile byte state = LOW;
Sciences and Humanities
volatile - keyword known as a variable qualifier, it is usually used before the data-
type of a variable. Declaring a variable volatile is a directive to the compiler.
Vishwakarma Institute of
The compiler is software which translates C/C++ code into the machine code,
which are the real instructions for the Atmega chip in the Arduino.
Specifically, it directs the compiler to load the variable from RAM and not from a
Technology
variables are stored and manipulated using instructions i.e. they can be
controlled.
We used the INPUT_PULLUP option so that we can skip adding a pull-up resistor
FY - Department of Engineering,
to the button.
17
Sciences and Humanities
Example interrupt in Arduino
function
2. The ISR function named blink and
FY - Department of Engineering,
3. The mode which describes when the interrupt is triggered. Here, it’s set to
CHANGE which means the interrupt is triggered when the pin changes its
state. Other possible options are:
LOW- to trigger when the pin is low
RISING- to trigger when the pin goes from low to high
FALLING - to trigger when the pin goes from high to low
18
Example interrupt in Arduino
Sciences and Humanities
Vishwakarma Institute of
When using digitalRead() to read the state of the button, you must press the
button at the right time so that the Arduino can capture it. This might not
Technology
matter when the only thing inside your loop() is reading the button's state.
FY - Department of Engineering,
But when you have a lot of things going on, the controller might miss the
button press. When using interrupts, it doesn't matter when you press the
button because the controller will detect it.
19
Vishwakarma Institute of
FY - Department of Engineering,
Technology Sciences and Humanities
To use the sketch above, use this diagram:
20
Sciences and Humanities Interrupt Vector table
Vector Program
Number Address Interrupt definition Vector name
Vishwakarma Institute of
21
Interrupt
Vector table
Sciences and Humanities
Vector Program
Interrupt definition Vector name
Number Address
Vishwakarma Institute of
22
Sciences and Humanities Interrupt Vector table
Vector Program
Interrupt definition Vector name
Number Address
Vishwakarma Institute of
23
Code for displaying the count of people passing by---
volatile byte people=0; // To count how many times the pin has
changed
byte cmd=0; // command a place to put our serial
Vishwakarma Institute of
data
Serial.begin(9600);
Serial.print("Program to display number of people
passing through the
gate");
Serial.println();
pinMode(PIN, INPUT); //set the pin24to
Sciences and Humanities Code for displaying the count of people passing by---
void loop()
{
Vishwakarma Institute of
cmd=Serial.read();
if (cmd==‘g')
Technology
{
FY - Department of Engineering,
cmd=0;
digitalWrite(5,HIGH);
} 25
Thank you!
Next:Ports
Memory organization Atmega 328
26
27