NerdKits - electronics education for a digital generation

You are not logged in. [log in]

NEW: Learning electronics? Ask your questions on the new Electronics Questions & Answers site hosted by CircuitLab.

Microcontroller Programming » Some Interrupt Code

July 15, 2011
by SpaceGhost
SpaceGhost's Avatar

Hello all, I have been playing around with, and trying to learn interrupts. I put together some code based on stuff I seen in some forum examples.

What I wanted to do was see if I could was write a program that would "interrupt" another program while it was performing a sequence of events -

For example, pushing a button to stop the program in the middle of what it is doing, do something else, and then resume the original program sequence from the point where it had left off...

The code I wrote does a "Nightrider" type of LED sequence - a lit LED sequences back and forth through 8 LEDs. When I push the button (PC5) once, the sequence stops immediately, then another LED (not one in the sequence) blinks twice, and then the Nightrider sequence resumes where it had left off.

   #define F_CPU 14745600

    #include <avr/io.h>
    #include <inttypes.h>
    #include <avr/interrupt.h>

    #include "../libnerdkits/delay.h"
    #include "../libnerdkits/lcd.h"

    // PIN DEFINITIONS:
    //
    // PC4 -- LED anode

    void init_interrupt() {

    PCICR |= (1<<PCIE1);

    PCMSK1 |= (1<<PCINT13); // PC5 fires the interrupt.

    }

    int main() {

  DDRC |= (1<<PC4); // non sequenced LED

  DDRB |= (1<<PB1);
  DDRB |= (1<<PB2);
  DDRB |= (1<<PB3);
  DDRB |= (1<<PB4);
  DDRB |= (1<<PB5);
  DDRC |= (1<<PC0);
  DDRC |= (1<<PC1);
  DDRC |= (1<<PC2);

    DDRC &= ~(1<<PC5); // input

    PORTC |= (1<<PC5); // int. resisistor

    init_interrupt();

    sei();

    while(1) {

    PORTB |= (1<<PB1); // turn on LED

    delay_ms(500);

    PORTB &= ~(1<<PB1); // turn off LED

    PORTB |= (1<<PB2); // turn on LED

    delay_ms(500); //

    PORTB &= ~(1<<PB2); // turn off LED

    PORTB |= (1<<PB3); // turn on LED

    delay_ms(500);

    PORTB &= ~(1<<PB3); // turn off LED

    PORTB |= (1<<PB4); // turn on LED

    delay_ms(500);

    PORTB &= ~(1<<PB4); // turn off LED

    PORTB |= (1<<PB5); // turn on LED

    delay_ms(500);

    PORTB &= ~(1<<PB5); // turn off LED

    PORTC |= (1<<PC0); // turn on LED

    delay_ms(500);

    PORTC &= ~(1<<PC0); // turn off LED

    PORTC |= (1<<PC1); // turn on LED

    delay_ms(600);

    PORTC &= ~(1<<PC1); // turn off LED

    PORTC |= (1<<PC2); // turn on LED

    delay_ms(600);

    PORTC &= ~(1<<PC2); // turn off LED

    PORTC |= (1<<PC1); // turn on LED

    delay_ms(600);

    PORTC &= ~(1<<PC1); // turn off LED

    PORTC |= (1<<PC0); // turn on LED

    delay_ms(500);

    PORTC &= ~(1<<PC0); // turn off LED

    PORTB |= (1<<PB5); // turn on LED

    delay_ms(500);

    PORTB &= ~(1<<PB5); // turn off LED

    PORTB |= (1<<PB4); // turn on LED

    delay_ms(500);

    PORTB &= ~(1<<PB4); // turn off LED

    PORTB |= (1<<PB3); // turn on LED

    delay_ms(500);

    PORTB &= ~(1<<PB3); // turn off LED

    PORTB |= (1<<PB2); // turn on LED

    delay_ms(500);

    PORTB &= ~(1<<PB2); // turn off LED

    PORTB |= (1<<PB1); // turn on LED

    delay_ms(500);

    }

 }
    ISR(PCINT1_vect) {

    if (PINC&(1<<PC5)) {

    PORTB = 0;

    PORTC &= ~((1<<PC0) | (1<<PC1) | (1<<PC2));

        PORTC |= (1<<PC4); // on

        delay_ms(1000);

        PORTC &= ~(1<<PC4); // off

        delay_ms(1000);

        PORTC |= (1<<PC4); // on

        delay_ms(1000);

        PORTC &= ~(1<<PC4); //off

        delay_ms(1000);

    }

    return 0;

    }

This is the first interrupt program I have attempted to write. The code works on my MCU.

However, one thing concerns me - when I write the code to the MCU, I get

In function '__vector_4' :
warning: 'return with a value, in function returning void

Is this something I need to be concerned about? I'm not sure exactly what this warning is trying to tell me. And, the program does what I want it to do. Could someone offer me some suggestions on how I might clean the code up a bit?

Thanks for any advice,

Dave

July 15, 2011
by esoderberg
esoderberg's Avatar

Dave,

Line 162: return 0; You don't need that.

Eric

July 15, 2011
by SpaceGhost
SpaceGhost's Avatar

Hey thanks Eric, I took the return 0; out and it compiled just fine :) .

This was my first try at doing an interrupt from scratch - pretty neat how it works.

January 31, 2012
by tchilzer2
tchilzer2's Avatar

Hey SpaceGhost, I just want to say thanks for this code cause I have been trying to figure out these interrupts for about two weeks. this is just the example I needed to make my sensor work!

Post a Reply

Please log in to post a reply.

Did you know that multiple microcontrollers can communicate with each other? Learn more...