ATtiny13 Pin Change Interrupts

For Adafruit customers who seek help with microcontrollers

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
User avatar
noise
 
Posts: 9
Joined: Sun Feb 08, 2009 9:47 am

ATtiny13 Pin Change Interrupts

Post by noise »

I'm struggling with the following code that should be triggering an interrupt on PCINT0. It works fine using INT0 (and yes I realize those are on different pins).

Also, why is PCINT1_vect undefined for the ATtiny13, is it not a feature on this chip.

I'm using WinAVR btw.

Code: Select all

#define F_CPU 1200000                    // clock 1.2MHz, internal oscillator
 
#include <avr/io.h>
#include <util/delay.h>
#include <avr/interrupt.h>
 
#define LED PB4
 
int main(void) {
 

PCMSK = _BV (PCINT0);				// int - Enable external interrupts PCINT1
MCUCR = _BV (ISC00);				// int - PCINT1 is triggered on any change
sei();								// int - Global enable interrupts
DDRB |= (1 << LED);				// Set direction register output
 
    for (;;) {						// loop (endless)
		asm("NOP");
    }
 
return 0;
}
 
ISR (PCINT0_vect)					// Interrupt on PCINT1 vector
{ 
PORTB ^= (1 << 4);					// Toggle LED
_delay_ms (100);
PORTB ^= (1 << 4);					// Toggle LED
}

adafruit
 
Posts: 12151
Joined: Thu Apr 06, 2006 4:21 pm

Re: ATtiny13 Pin Change Interrupts

Post by adafruit »

you also need to set the pin itself in the pin change mask. the datasheet is a good place to look for a list of all registers necessary

User avatar
noise
 
Posts: 9
Joined: Sun Feb 08, 2009 9:47 am

Re: ATtiny13 Pin Change Interrupts

Post by noise »

I believe I am doing that with the line:

PCMSK = _BV (PCINT0);

But I see now that I wasn't enabling pin change interrupts with:

GIMSK = _BV (PCIE);

I thought that mask was specific to INT0.

Thanks!

Here is the updated code for posterity.

Code: Select all

#define F_CPU 1200000                    // clock 1.2MHz, internal oscillator
 
#include <avr/io.h>
#include <util/delay.h>
#include <avr/interrupt.h>
 
#define LED PB4
 
int main(void) {
 

GIMSK = _BV (PCIE);                                  // Pin Change Interrupt Enable
PCMSK = _BV (PCINT0);				// Enable external interrupts PCINT0
MCUCR = _BV (ISC00);				// PCINT0 is triggered on any change
sei();								// int - Global enable interrupts
DDRB |= (1 << LED);				// Set direction register output
 
    for (;;) {						// loop (endless)
		asm("NOP");
    }
 
return 0;
}
 
ISR (PCINT0_vect)					// Interrupt on PCINT0 vector
{ 
PORTB ^= (1 << 4);					// Toggle LED
_delay_ms (100);
PORTB ^= (1 << 4);					// Toggle LED
}

uhe
 
Posts: 178
Joined: Mon Sep 03, 2007 4:50 pm

Re: ATtiny13 Pin Change Interrupts

Post by uhe »

I'd also like to know why most of the IRQs in the AVR-LibC IRQ table are undefined (for the ATtiny13). Even the PCINT_vect doesn't exists for the ATtiny13! Which directly results in the question:
How to use a chip feature that is not covered by AVR-LibC (in this case PCINT1_vect or PCINT_vect on a ATtiny13)?
Of course without using ISR(BADISR_vect)

adafruit
 
Posts: 12151
Joined: Thu Apr 06, 2006 4:21 pm

Re: ATtiny13 Pin Change Interrupts

Post by adafruit »

look in the winavr header file

uhe
 
Posts: 178
Joined: Mon Sep 03, 2007 4:50 pm

Re: ATtiny13 Pin Change Interrupts

Post by uhe »

Hmmm I couldn't find it in the CVS. Maybe I'll do a checkout an grep for .h when I need it.

mtbf0
 
Posts: 1645
Joined: Sat Nov 10, 2007 12:59 am

Re: ATtiny13 Pin Change Interrupts

Post by mtbf0 »

from .../avr/include/iotn13.h

Code: Select all

/* From the datasheet:
   1 0x0000 RESET External Pin, Power-on Reset, Brown-out Reset, Watchdog Reset
   2 0x0001 INT0 External Interrupt Request 0
   3 0x0002 PCINT0 Pin Change Interrupt Request 0
   4 0x0003 TIM0_OVF Timer/Counter Overflow
   5 0x0004 EE_RDY EEPROM Ready
   6 0x0005 ANA_COMP Analog Comparator
   7 0x0006 TIM0_COMPA Timer/Counter Compare Match A
   8 0x0007 TIM0_COMPB Timer/Counter Compare Match B
   9 0x0008 WDT Watchdog Time-out
   10 0x0009 ADC ADC Conversion Complete */

/* External Interrupt 0 */
#define INT0_vect			_VECTOR(1)
#define SIG_INTERRUPT0			_VECTOR(1)

/* External Interrupt Request 0 */
#define PCINT0_vect			_VECTOR(2)
#define SIG_PIN_CHANGE0			_VECTOR(2)

/* Timer/Counter0 Overflow */
#define TIM0_OVF_vect			_VECTOR(3)
#define SIG_OVERFLOW0			_VECTOR(3)

/* EEPROM Ready */
#define EE_RDY_vect			_VECTOR(4)
#define SIG_EEPROM_READY		_VECTOR(4)

/* Analog Comparator */
#define ANA_COMP_vect			_VECTOR(5)
#define SIG_COMPARATOR			_VECTOR(5)

/* Timer/Counter Compare Match A */
#define TIM0_COMPA_vect			_VECTOR(6)
#define SIG_OUTPUT_COMPARE0A		_VECTOR(6)

/* Timer/Counter Compare Match B */
#define TIM0_COMPB_vect			_VECTOR(7)
#define SIG_OUTPUT_COMPARE0B		_VECTOR(7)

/* Watchdog Time-out */
#define WDT_vect			_VECTOR(8)
#define SIG_WATCHDOG_TIMEOUT		_VECTOR(8)

/* ADC Conversion Complete */
#define ADC_vect			_VECTOR(9)
#define SIG_ADC				_VECTOR(9)

#define _VECTORS_SIZE 20

User avatar
noise
 
Posts: 9
Joined: Sun Feb 08, 2009 9:47 am

Re: ATtiny13 Pin Change Interrupts

Post by noise »

Yeah sorry, I edited that question out of my post because I figured it out.

It is a limit in the number of pin change interrupts in the interrupt table. It seems that the number of allowable pin change interrupts is always smaller than the total number of potential inputs.

Sorry to offer an actual answer in English instead of a cryptic cut and paste out of the header file. :wink:

mtbf0
 
Posts: 1645
Joined: Sat Nov 10, 2007 12:59 am

Re: ATtiny13 Pin Change Interrupts

Post by mtbf0 »

noise wrote: Sorry to offer an actual answer in English instead of a cryptic cut and paste out of the header file.
sorry. now and then i'm too weary for english. i was responding to
uhe wrote:I'd also like to know why most of the IRQs in the AVR-LibC IRQ table are undefined (for the ATtiny13). Even the PCINT_vect doesn't exists for the ATtiny13! Which directly results in the question:
How to use a chip feature that is not covered by AVR-LibC (in this case PCINT1_vect or PCINT_vect on a ATtiny13)?
Of course without using ISR(BADISR_vect)
my point being that the correct interrupt on the tiny13 appears to be PCINT0_vect. always have to check the datasheet for the device in question.

uhe
 
Posts: 178
Joined: Mon Sep 03, 2007 4:50 pm

Re: ATtiny13 Pin Change Interrupts

Post by uhe »

The thing is... I went to the ATtiny13 datasheet and saw the pinout on the second page and what I found is:
* INT0 and
* PCINT0 to PCINT5

Okay! So i went to the IRQ tabel on page 42 and found:
0x0001 for INT0
0x0002 for PCINT0
Hmmm, nothing about PCINT0 to 5 but.. I read on.
On page 54 there is something about external IRQs:
This feature provides a way of generating a software interrupt. Pin change interrupts PCI will trigger if any enabled PCINT5..0 pin toggles.
Okay, in the IRQ table there is no PCI vect defined (or sth. else that catches every PCINTx IRQ), there is only PCINT0.
In contrast to this the ATtiny2313 has 0x000B - PCINT which catches all PCINTx IRQs.

So how to write a IRQ handler for PCINT1 to PCINT5 on the ATtiny13? Did they just not mention that PCINT0 will catch every PCINTx if enabled?

User avatar
noise
 
Posts: 9
Joined: Sun Feb 08, 2009 9:47 am

Re: ATtiny13 Pin Change Interrupts

Post by noise »

That's what threw me too. I thought I figured out that the 2313 has some limitation too, but I can't see why I thought that now.

mtbf0
 
Posts: 1645
Joined: Sat Nov 10, 2007 12:59 am

Re: ATtiny13 Pin Change Interrupts

Post by mtbf0 »

pcint0-5 will all trigger the PCINT0 interrupt. you then have to check PCMSK to see which pin triggered it.

i guess.

seems to be a little confusion about the naming in the datasheet.
Last edited by mtbf0 on Sun Jan 17, 2010 5:14 pm, edited 1 time in total.

uhe
 
Posts: 178
Joined: Mon Sep 03, 2007 4:50 pm

Re: ATtiny13 Pin Change Interrupts

Post by uhe »

Thanks for clarification mtbf0!

User avatar
noise
 
Posts: 9
Joined: Sun Feb 08, 2009 9:47 am

Re: ATtiny13 Pin Change Interrupts

Post by noise »

Hmmm, I thought I tried that, but maybe not. *goes to fetch breadboard*

EDIT: That did the trick. Thanks!!

Locked
Please be positive and constructive with your questions and comments.

Return to “Microcontrollers”