Go Down

Topic: LCD 4-Bit & attachInterrupt - Causes Hanging? (Read 2339 times) previous topic - next topic

Josh

First, a disclaimer - I'm new to this and could be way off on my diagnosis - but I need help.  I have some very simple code that tries to use an interrupt using the "attachInterrupt", and also interfaces to an LCD using the 4-bit library.  If I don't attach an interrupt - everything runs fine without any issues, looping on into infinity.  If in the setup I attachInterrupt to either 0 or 1, first or last thing I do in setup, etc. - The loop will run, but after a few itterations through the loop - it will stop and hang the board.

If I remove all other code for the LCD except for the "lcd.init()" - everything still works fine - it seems that it is only an issue if inside the loop I try to do anything to the LCD - even an "lcd.clear" will eventually cause everything to hang.  I'm confident all my wiring is correct as everything works fine until I try and attach an interrupt.

Any thoughts?  Do you need code examples or hardware diagrams to know?  I am just doing the basic 4-bit LCD stuff, and almost nothing else at all.

Thanks,
Josh

Josh

Ok, so while thinking about it some more I decided to try to detach the interrupt befor doing anything LCD related..... and sure enough that has "worked".  I would prefer not to have to do this though, does anyone know why or how to prevent this?

Thanks,
Josh

Josh

Hopefully this will not turn into a one person "conversation", but what I had originally called "worked" - was not entirely accurate.  Apparently, I can "clear" the LCD while the interrupt is off, but it still flakes out on me if I try to do anything else.

Example time - If I do this - it works fine:
Code: [Select]

 detachInterrupt(0);
 delay(500);
 lcd.clear();
 //lcd.printIn("Current State:");
 //print something on the display's second line.
 //lcd.cursorTo(2, 0);  //line=2, x=0.
 //if (state == 0) {
 //  lcd.printIn("OFF");
 //} else {
 //  lcd.printIn("ON");
 //}
 delay(500);
 attachInterrupt(0, buttonPressed, RISING);


If I uncomment anything here, it will lock up - ie:
Code: [Select]

 detachInterrupt(0);
 delay(500);
 lcd.clear();
 lcd.printIn("Current State:");
 //print something on the display's second line.
 //lcd.cursorTo(2, 0);  //line=2, x=0.
 //if (state == 0) {
 //  lcd.printIn("OFF");
 //} else {
 //  lcd.printIn("ON");
 //}
 delay(500);
 attachInterrupt(0, buttonPressed, RISING);


Any thoughts?  I'm desperate here as I need to use the 4 bit library because I need two interrupts.  Oh, and I should add one more thing - I modified the LCD4Bit.cpp to move the enable pin from 2 to 6 (pin 2 is an interrupt pin).  Honestly, that is all I modified there - just one line.

Thanks,
Josh

Josh

Ok, so it has ended up as a one person conversation.  I have found the problem - as described already in another thread on using the interrupts.  Basically it would appear the documentation on using it is lacking some of the details (perhaps many already know enough that this seems obviuse).  Anyway, the answers are here:

http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1172808516/1

Could we have a contributor to the wiki update the docs to at least point here?  It could save many others much more time.

Thanks,
Josh

neillzero

#4
Mar 11, 2007, 01:49 pm Last Edit: Mar 11, 2007, 02:19 pm by neillzero Reason: 1
Hi Josh,

Sorry, I check the forum only infrequently these days.
Thanks for writing up your progress here.

Quote

Could we have a contributor to the wiki update the docs


That's you!  Everyone can contribute to the wiki!  See http://this "Participate!" page for how to get an account:
http://www.arduino.cc/playground/Main/Participate

For now, I added a pointer here from the 4-bit LCD wiki page, under the title working with interrupts.  You'll know best yourself where else the information should be placed.

mellis

Josh: I'd be happy to update the documentation on attachInterrupt() or detachInterrupt().  What was the problem in your case?  Did you have the interrupt pin connected to anything?  

Josh

Thanks all again for the help.  I love the arduino and the support of this forum is priceless!  I did have the interrupt pin connected to both a button at one point and later another IC that has an interrupt line (PCF8457).  The items that made it all work for me, though in hind-site seem obvious, are:

1.) Never use a delay or any library which introduces a delay while in an interrupt (like the Wire library or LCD libraries).  Serial communication, or any communication requiring a clock will also fail.
2.) Use an external pull-up on the interrupt line.

I was violating both of these rules under the assumption that the interrupt would have an internal pull-pull, and so as the interrupt line was "floating" it was causing it to sometimes trigger during execution, and then get stuck as I was attempting to use variuse communications in the interrupt code like accessing the LCD and/or set a debug LED with a delay, talking I2C/TWI, etc. in my interrupt code.

The fix then was to add the external pull-up to prevent it from firing incorrectly, and then to modify my code so that I only set a flag variable and do all my other processing in the main loop.

Thanks,
Josh

Go Up