1
\$\begingroup\$

I want to learn how to work with interrupts and I have done my first try.

I have done a very simple circuit based on ATtiny13.

There's a led in PB1 and a button in PB3. When the button is pressed, PB3 pin change its state from low level to high level, this produces an external interrupt. The code of this interrupt, makes the led turn on for 500ms.

schematic

This is my code, what's wrong here? I'm getting this warning:

'PCINT3_vect' appears to be a misspelled signal handler, missing __vector prefix [-Wmisspelled-isr]

What's wrong here?

#define F_CPU 9600000UL

#include <avr/io.h>
#include <util/delay.h>
#include <avr/interrupt.h>

ISR(PCINT3_vect)
{
        PORTB |= (1<<PB1);
        _delay_ms(500);
        PORTB &= ~(1<<PB1);
        _delay_ms(500);
}

void SystemInit(void)
{
        PCMSK |= (1<<PCINT3);   // pin change mask: listen to portb, pin PB3
        GIMSK |= (1<<PCIE); // enable PCINT interrupt
        sei();          // enable all interrupts

}

int main(void)
{       
    DDRB |= (1<<PB1);
    DDRB |= (1<<PB3);
    SystemInit();

    while (1) 
    {       
    }
}

Thanks guys!

\$\endgroup\$
8
  • \$\begingroup\$ I think ATTiny13 supports interrupts only on PC0. \$\endgroup\$ May 17, 2017 at 14:23
  • \$\begingroup\$ @VladimirCravero AVR pin change interrupts have one vector per port and there is a mask register to enable individual pins. If multiple pins are used, the port must be checked to see which pin changed. \$\endgroup\$
    – Matt Young
    May 17, 2017 at 14:31
  • 1
    \$\begingroup\$ Never ever call blocking delays in ISR, an ISR should be executed as fast as possible. \$\endgroup\$ May 17, 2017 at 14:39
  • \$\begingroup\$ so, where should I place the delays? \$\endgroup\$
    – Salva
    May 17, 2017 at 14:41
  • 1
    \$\begingroup\$ Set a flag in the ISR, check the flag in main, if set blink the LED with delays and clear the flag as well. Currently it is not an issue for you but it is a bad habit so you should not get used to it. \$\endgroup\$ May 17, 2017 at 14:42

2 Answers 2

3
\$\begingroup\$

This is the ATTiny13's interrupt vector table.

enter image description here

PCINT3 is not an interrupt vector. You need to change that to PCINT0. It looks like you have the PCI and mask set properly. You should be able to make that change and have your interrupts work. You also need to add initialization to make your push button work.

DDRB |= (0<<PB3); //Pushbutton input
PORTB |= (1<<PB3); //Enable Pushbutton pull-up
DDRB |= (1<<PB1); //LED output

That will enable the internal pull-up and allow an edge to happen on a button press. This will likely work for simulation, but a real circuit will need some debounce either in hardware or software.

\$\endgroup\$
7
  • \$\begingroup\$ so, what I have to change is ISR(PCINT3_vect) into ISR(PCINT0_vect), right? \$\endgroup\$
    – Salva
    May 17, 2017 at 14:30
  • \$\begingroup\$ @Salva That should do it. \$\endgroup\$
    – Matt Young
    May 17, 2017 at 14:31
  • \$\begingroup\$ it seems not to work: i65.tinypic.com/33y2kyb.png \$\endgroup\$
    – Salva
    May 17, 2017 at 14:35
  • \$\begingroup\$ @Salva See my edit. \$\endgroup\$
    – Matt Young
    May 17, 2017 at 14:42
  • \$\begingroup\$ right now, it enters in the interrupt when it PB3 goes from low to high level, and from high to low as well. How do you do just to set the interrupt to be called when PB3 goes from low to high ? \$\endgroup\$
    – Salva
    May 17, 2017 at 14:54
2
\$\begingroup\$

According to the table here, there is no PCINT3 interrupt on ATtiny13: enter image description here

\$\endgroup\$
2
  • \$\begingroup\$ so, I can't place an interrupt in PB3, right? I though I could \$\endgroup\$
    – Salva
    May 17, 2017 at 14:39
  • \$\begingroup\$ You can, but you can only enable one interrupt per port. So you could have one interrupt in PORTB, one in PORTD, one in PORTD etc., but you cannot have multiple interrupts in a single port. \$\endgroup\$ May 17, 2017 at 15:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.