0% found this document useful (0 votes)
0 views27 pages

Unit52. ATmega328 PCInterrupt

The document discusses the use of interrupts in microcontroller programming, specifically focusing on the ATmega 328P. It contrasts polling and interrupt methods for monitoring inputs, explaining that interrupts are more efficient and allow for automatic responses to events. The document details the Pin Change Interrupt (PCINT) mechanism, including the relevant registers and example code for implementing interrupts in Arduino.

Uploaded by

sddode96
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)
0 views27 pages

Unit52. ATmega328 PCInterrupt

The document discusses the use of interrupts in microcontroller programming, specifically focusing on the ATmega 328P. It contrasts polling and interrupt methods for monitoring inputs, explaining that interrupts are more efficient and allow for automatic responses to events. The document details the Pin Change Interrupt (PCINT) mechanism, including the relevant registers and example code for implementing interrupts in Arduino.

Uploaded by

sddode96
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/ 27

Vishwakarma Institute of

FY - Department of Engineering,
Technology Sciences and Humanities

Microcontroller
2. ATmega 328P

Interrupts -PCINT

1
Interrupts:-----

Methods to read/ monitor the inputs

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

• It only occurs when needed.


• Interrupts are more efficient than polling but the timing is less predictable.
• More difficult to code than polling.
Technology

When interrupt occurs 


FY - Department of Engineering,

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

External events e.g.--


• UART ready with/for next character
• Signal change on pin
Technology

• Action depends on context


FY - Department of Engineering,

• level/edges arrived on pin

Internal events e.g.--


• Power failure
• Arithmetic exception
• Timer “tick”

5
Sciences and Humanities
Interrupts:-----
The types of interrupts in AVR
We might use interrupts in following cases
Vishwakarma Institute of

1. To detect pin changes (eg. rotary encoders, button presses)


2. Watchdog timer (e.g. if nothing happens after 8 seconds, interrupt )
Technology

3. Timer interrupts – used for comparing/overflowing timers


FY - Department of Engineering,

4. SPI data transfers The next section


5. I2C data transfers discusses pin change
type of interrupt i.e.
6. USART data transfers To detect pin changes
7. ADC conversions (analog to digital)
8. EEPROM ready for use
9. Flash memory ready 6
Sciences and Humanities
Interrupts:-----
Interrupts are useful for making things happen automatically in microcontroller
programs, and can help solve timing problems as in Polling method.
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

• Read an infrared sensor (photo-interrupter)


FY - Department of Engineering,

• Read key press in 4 key keypad


• Read key press in 16 key keypad etc

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

1. Reading the state of an input pin is


necessary in microcontroller
Technology

programs .
FY - Department of Engineering,

2. A specific input pin changing state


can be notified. This is done by
using Pin Change INTerrupts.
3. Each of the digital I/O pins can be
configured as PCINT by writing into
interrupt registers in software.

8
PCINT - Pin Change
Interrupt
Sciences and Humanities

SN INT - Interrupt PCINT - Pin Change INTerrupt


1 INT refers to the dedicated PCINT refers to the interrupts that can be
Vishwakarma Institute of

hardware interrupt pins generated by almost any of the I/O pins.


PCINT0-PCINT23

2 Each interrupt has a dedicated Group of pins share the same PCINT vector
Technology

Vector address. This ROM address.


address is not shared within There are 3 PCINT groups.
interrupts.
FY - Department of Engineering,

3 The INT pin is linked to a Programmer needs to determine which pin


dedicated interrupt vector so change causes the interrupt within the ISR
you always know which pin before acting on it.
caused the interrupt when in
the ISR.

9
Pin Change Interrupt
Registers
In Atmega 328 there are three special registers associated with PCINT

1. PCICR Pin Change Interrupt Control Register

2. PCIFR Pin Change Interrupt Flag Register

3. PCMSKx Pin Change Mask Register.

X- indicates 0, 1 and 2. These are PCINT groups 0, 1 and 2

10
Pin Change Interrupt
Control Register
To enable pin change interrupt on a pin, we need to manipulate the PCICR register
Sciences and Humanities

PCIE2 PCIE1 PCIE0


Vishwakarma Institute of

Bit 7 Bit 6 Bit 5 Bit 4 Bit 3 Bit 2 Bit 1 Bit 0

Group control Bit PCINT group pins Pin numbers Pin numbers
On ATmega 328 On Arduino board
Technology

Set – Enable group


Clear– Disable group
FY - Department of Engineering,

PCIE0 PCINT0 to PCINT7 Port B0 to B7 6 pins -- pin Digital pins 8 to13


(8 pins) 8 pins mapping excludes crystal pins

PCIE1 PCINT8 to PCINT14 Port C0 to C6 6 pins --A0 to A5


7 pins. mapping excludes Reset pin
(7 pins)

PCIE2 PCINT16 to Port D0 to D7 8 pins – Digital pins 0 to 7


PCINT23 (8 pins) 8 pins Mapping all 8

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

Bits in PCIFR are PCIF0, PCIF1 and PCIF2


Technology

PCIF2 PCIF1 PCIF0


Bit 7 Bit 6 Bit 5 Bit 4 Bit 3 Bit 2 Bit 1 Bit 0
FY - Department of Engineering,

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

PCMSK0 is used to enable each PCINT pins 0 to 7


Vishwakarma Institute of
Technology

PCMSK1 is used to enable each PCINT pins 8 to 14


FY - Department of Engineering,

PCMSK2 is used to enable each PCINT pins 16 to 23

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.

ISR is a loop. The code to be executed is written in that loop.


Technology

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

const byte interruptPin = 3; //We can select A0 to A5 also in Arduino, Not in


Simulator volatile byte state = LOW; //Here only pins 0,1,2,3 are
Vishwakarma Institute of

used
const byte ledPin = 13;

void blink() { state = !state; }


Technology

void setup() { pinMode(5,OUTPUT);


FY - Department of Engineering,

pinMode(ledPin, OUTPUT);
pinMode(interruptPin, INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(interruptPin), blink,
CHANGE); }

void loop() { digitalWrite(5, HIGH);


digitalWrite(ledPin, state); }

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

storage register. Register, which is a temporary memory location where program


FY - Department of Engineering,

variables are stored and manipulated using instructions i.e. they can be
controlled.

A variable should be declared volatile whenever its value can be changed by


something beyond the control of the code section in which it appears, such as a
concurrently executing instruction.
In the Arduino, the only place that this is likely to occur is in sections of code
associated with interrupts
16
Sciences and Humanities
Example interrupt in Arduino
The interrupt service routine is named blink() in this case and contains a one-line
code that toggles the state of the LED pin.
Vishwakarma Institute of

Here the interrupt pin is D3 and is given the name interruptPin.


Just like any input pin, the interruptPin must be declared using pinMode()
pinMode(interruptPin, INPUT_PULLUP);
Technology

We used the INPUT_PULLUP option so that we can skip adding a pull-up resistor
FY - Department of Engineering,

to the button.

Next, we attach the interrupt using the attachInterrupt() function:


The syntaxis --
attachInterrupt(digitalPinToInterrupt(interruptPin), blink, CHANGE);

17
Sciences and Humanities
Example interrupt in Arduino

attachInterrupt(digitalPinToInterrupt(interruptPin), blink, CHANGE);


Vishwakarma Institute of

The attachInterrupt() function has three parameters:


1. The digital pin converted to Interrupt using the digitalPinToInterrupt()
Technology

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

Isn't the above sketch same as using digitalRead() inside loop()?

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

RESET External Pin, Power-on


1 0x0000 Reset, Brown-out Reset and RESET
Watchdog System Reset
Technology

2 0x0002 External Interrupt Request 0 INT0_vect

3 0x0004 External Interrupt Request 1 INT1_vect


FY - Department of Engineering,

4 0x0006 Pin Change Interrupt Request 0 PCINT0_vect

5 0x0008 Pin Change Interrupt Request 1 PCINT1_vect

6 0x000A Pin Change Interrupt Request 2 PCINT2_vect

7 0x000C Watchdog Time-out Interrupt WDT_vect

8 0x000E Timer/Counter2 Compare Match A TIMER2_COMPA_vect

21
Interrupt
Vector table
Sciences and Humanities

Vector Program
Interrupt definition Vector name
Number Address
Vishwakarma Institute of

9 0x0010 Timer/Counter2 Compare Match B TIMER2_COMPB_vect

10 0x0012 Timer/Counter2 Overflow TIMER2_OVF_vect

11 0x0014 Timer/Counter1 Capture Event TIMER1_CAPT_vect


Technology

12 0x0016 Timer/Counter1 Compare Match A TIMER1_COMPA_vect


FY - Department of Engineering,

13 0x0018 Timer/Counter1 Compare Match B TIMER1_COMPB_vect

14 0x001A Timer/Counter1 Overflow TIMER1_OVF_vect

15 0x001C Timer/Counter0 Compare Match A TIMER0_COMPA_vect

16 0x001E Timer/Counter0 Compare Match B TIMER0_COMPB_vect

17 0x0020 Timer/Counter0 Overflow TIMER0_OVF_vect

22
Sciences and Humanities Interrupt Vector table

Vector Program
Interrupt definition Vector name
Number Address
Vishwakarma Institute of

18 0x0022 SPI Serial Transfer Complete SPI_STC_vect

19 0x0024 USART Rx Complete USART_RX_vect


Technology

20 0x0026 USART Data Register Empty USART_UDRE_vect

21 0x0028 USART Tx Complete USART_TX_vect


FY - Department of Engineering,

22 0x002A ADC Conversion Complete ADC_vect

23 0x002C EEPROM Ready EE_READY_vect

24 0x002E Analog Comparator ANALOG_COMP_vect

25 0x0030 Two-wire Serial Interface TWI_vect

26 0x003A Store Program Memory Read SPM_READY_vect

23
Code for displaying the count of people passing by---

#define PIN 2 // the pin we are interested in


Sciences and Humanities

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

void peopleCount() { (people++); }


Technology

void setup() { pinMode(5,OUTPUT):


FY - Department of Engineering,

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,

Serial.print("People count as of now is:=


");
Serial.println(people, DEC);
}

cmd=0;
digitalWrite(5,HIGH);
} 25
Thank you!

Next:Ports
Memory organization Atmega 328

26
27

You might also like