Go Down

Topic: Pin change interrupt - need some help *SOLVED* (Read 1 time) previous topic - next topic

skyjumper

Sep 01, 2011, 04:40 am Last Edit: Sep 01, 2011, 09:10 am by skyjumper Reason: 1
Hi All...

Okay I have tried a bunch of variations on this. This should be simple. I'm trying to get pin 19 to trigger an interrupt that toggles a LED on or off. I read this document:

http://www.me.ucsb.edu/~me170c/Code/How_to_Enable_Interrupts_on_ANY_pin.pdf

That seemed to cover it reasonably well, but I still can't get it to work. The MPU is a 1284P. Physical pin 22 is Arduino pin 19, which is PCINT19, PCIE2 and PCMSK2. Did i make a simple mistake or did I totally screw this up?

Code: [Select]

bool volatile state = 1;

ISR(PCINT2_void)
{
 if(state)
   state = false;
 else
   state = true;  
}

void setup()
{
 // establish interrupt processing / ISR
 PCICR |= (1<<PCIE2);
 PCMSK2 |= (1<<PCINT19);
 MCUCR = (1<<ISC01);

 pinMode(19, OUTPUT);
 
 // Set LED on this pin for output
 pinMode(7, OUTPUT);
}

void loop()
{
 while(1)
 {
   if(state)
     digitalWrite(7, HIGH);
   else
     digitalWrite(7, LOW);    
 }
}

Delta_G

Do I understand correctly, you want the variable state to change whenever there is a pin change on pin 19?

If so, wouldn't pin 19 need to be an input? 

madworm

• Upload doesn't work? Do a loop-back test.
• There's absolutely NO excuse for not having an ISP!
• Your AVR needs a brain surgery? Use the online FUSE calculator.
My projects: RGB LED matrix, RGB LED ring, various ATtiny gadgets...
• Microsoft is not the answer. It is the question, and the answer is NO!

Grumpy_Mike

This project uses pin change interrupts, look at the code for an example of how to use them:-
http://www.thebox.myzen.co.uk/Hardware/Crazy_People.html

skyjumper


Do I understand correctly, you want the variable state to change whenever there is a pin change on pin 19?

If so, wouldn't pin 19 need to be an input? 


I thought so too, but the examples seem to show it set as an output.

skyjumper

Got it working, code below...

You guys caught the issues. PinMode does need to be set to INPUT, and the _void was supposed to be _vect. I can take some solace that it was wrong in the tutorial ;-)

Thanks everyone!


Code: [Select]
bool volatile state = 1;

ISR(PCINT2_vect)
{
  if(state)
    state = false;
  else
    state = true; 
}

void setup()
{
  // establish interrupt processing / ISR
  PCICR |= (1<<PCIE2);
  PCMSK2 |= (1<<PCINT19);
  MCUCR = (1<<ISC01);

  pinMode(19, INPUT);
 
  // Ser LED on this pin for output
  pinMode(7, OUTPUT);
}

void loop()
{
  while(1)
  {
    if(state)
      digitalWrite(7, HIGH);
    else
      digitalWrite(7, LOW);   
  }
}

CrossRoads

That's all it takes? Damn!

I'm keeping a copy of that handy!  I couldn't get the examples I found to work when I was tryng that in January.
Designing & building electrical circuits for over 25 years.  Screw Shield for Mega/Due/Uno,  Bobuino with ATMega1284P, & other '328P & '1284P creations & offerings at  my website.

skyjumper

#7
Sep 01, 2011, 11:12 am Last Edit: Sep 01, 2011, 11:16 am by skyjumper Reason: 1
Hi Bob! Hey I have been planning to email you to see how well you weathered the hurricane! We lost that big pair tree in the front yard, which disappointed Kelly. I planted that for her when we moved in 14 years ago. Other that that, no damage.

As for the ISR, just so everyone who reads this later knows, the trick is knowing what to set those registers to. It varies from one MPU to another. And anyone who uses that example should be warned that the button is not debounced, so you'll see funny stuff if you have a dirty switch (like say, a bent paperclip). Of course this is just a little code to get pin change interrupts working. I didn't actually need to switch the LED on and off. ;-) 

The next test was that I added code on the loop(){} method to pull pin 19 low then high and yup, it triggered the ISR. So that's good.

Now, if I could just get that UART to trigger an interrupt I would be home free! If I pull pin 2 down with a digitalWrite() I can trigger it. But hey, progress is being made...



CrossRoads

That's too bad about tree. We just had a mostly dead small tree fall over, and lost power Sunday morning until 2AM Monday.

Are you setting up softare interrupts with Xon/Xoff characters beng received, or creating and RHR interrupt when so many characters are received?
See 7.3.1, 7.5, and 8.3, 8.4, 8.12 of the SC16IS752 data sheet.
Designing & building electrical circuits for over 25 years.  Screw Shield for Mega/Due/Uno,  Bobuino with ATMega1284P, & other '328P & '1284P creations & offerings at  my website.

skyjumper


That's too bad about tree. We just had a mostly dead small tree fall over, and lost power Sunday morning until 2AM Monday.

Are you setting up softare interrupts with Xon/Xoff characters beng received, or creating and RHR interrupt when so many characters are received?
See 7.3.1, 7.5, and 8.3, 8.4, 8.12 of the SC16IS752 data sheet.



Well that's one way to get rid of a dead tree!

I set the trigger on the RHR to trigger I think after 48 characters. I don't recall exactly what the number was but I do recall setting it. I didn't look at the Xon/Xoff stuff because I have none of that, just the two data lines. I'll look the data sheet over again tonight to see if I need to do something with that even though those lines are not wired.

Thanks...

CrossRoads

Xon/Xoff are not lines, they are control characters you would put in the message, the UART would look for them coming in.
Seems like you have set some bits in a couple of registers too for either type of interrupt.
If you can make PCINTs work, I am sure you can get there with this one :)
Designing & building electrical circuits for over 25 years.  Screw Shield for Mega/Due/Uno,  Bobuino with ATMega1284P, & other '328P & '1284P creations & offerings at  my website.

skyjumper

If you can make PCINTs work, I am sure you can get there with this one :)


Fingers crossed! I'll be back late tonight to fight with it.

Go Up