Pages: [1]   Go Down
Author Topic: Pin change interrupt - need some help *SOLVED*  (Read 1390 times)
0 Members and 1 Guest are viewing this topic.
Smithfield, Rhode Island
Offline Offline
God Member
*****
Karma: 2
Posts: 843
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
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);    
  }
}
« Last Edit: September 01, 2011, 02:10:54 am by skyjumper » Logged

Offline Offline
God Member
*****
Karma: 10
Posts: 646
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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? 
Logged

"The old Europe"
Offline Offline
Edison Member
*
Karma: 1
Posts: 2005
Bootloaders suck!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
PCINT2_void

That looks suspicious.
Logged

• 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!

Manchester (England England)
Offline Offline
Brattain Member
*****
Karma: 506
Posts: 31345
Solder is electric glue
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

Smithfield, Rhode Island
Offline Offline
God Member
*****
Karma: 2
Posts: 843
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Smithfield, Rhode Island
Offline Offline
God Member
*****
Karma: 2
Posts: 843
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
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);   
  }
}
Logged

Global Moderator
Boston area, metrowest
Offline Offline
Brattain Member
*****
Karma: 436
Posts: 23623
Author of "Arduino for Teens". Available for Design & Build services. Now with Unlimited Eagle board sizes!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Designing & building electrical circuits for over 25 years. Check out the ATMega1284P based Bobuino and other '328P & '1284P creations & offerings at  www.crossroadsfencing.com/BobuinoRev17.
Arduino for Teens available at Amazon.com.

Smithfield, Rhode Island
Offline Offline
God Member
*****
Karma: 2
Posts: 843
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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...


« Last Edit: September 01, 2011, 04:16:02 am by skyjumper » Logged

Global Moderator
Boston area, metrowest
Offline Offline
Brattain Member
*****
Karma: 436
Posts: 23623
Author of "Arduino for Teens". Available for Design & Build services. Now with Unlimited Eagle board sizes!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Designing & building electrical circuits for over 25 years. Check out the ATMega1284P based Bobuino and other '328P & '1284P creations & offerings at  www.crossroadsfencing.com/BobuinoRev17.
Arduino for Teens available at Amazon.com.

Smithfield, Rhode Island
Offline Offline
God Member
*****
Karma: 2
Posts: 843
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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...
Logged

Global Moderator
Boston area, metrowest
Offline Offline
Brattain Member
*****
Karma: 436
Posts: 23623
Author of "Arduino for Teens". Available for Design & Build services. Now with Unlimited Eagle board sizes!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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 smiley
Logged

Designing & building electrical circuits for over 25 years. Check out the ATMega1284P based Bobuino and other '328P & '1284P creations & offerings at  www.crossroadsfencing.com/BobuinoRev17.
Arduino for Teens available at Amazon.com.

Smithfield, Rhode Island
Offline Offline
God Member
*****
Karma: 2
Posts: 843
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

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

Pages: [1]   Go Up
Jump to: