dj0tt / arduino

Automatically exported from code.google.com/p/arduino
Other
0 stars 0 forks source link

Collision between LiquidCrystal, Serial, and Wire libraries causes uC to crash #125

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
I am having a problem in Arduino-0017 where my Arduino crashes and restarts
repeatedly due to some sort of interaction between the LiquidCrystal,
Serial, and Wire (TWI/I2C) libraries.  This issue has been mentioned on the
forum here:

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

...and one of the posters kindly included a simple program that
demonstrates the issue (though I've added one extra parameter to the lcd
object initialization):

    #include <Wire.h>
    #include <LiquidCrystal.h>
    LiquidCrystal lcd(12, 11, 10, 5, 4, 3, 2);

    int ledPin =  13;    // LED connected to digital pin 13

    void setup()
    {
        pinMode(ledPin, OUTPUT);
        Serial.begin(9600);
        //Serial.println("a");    // uncommenting this line will cause
sketch to run properly
        Wire.begin();
    }

    void loop()
    {  
        digitalWrite(ledPin, HIGH);   // check for quick blinky
        delay(100);                   // to see if sketch runs
        digitalWrite(ledPin, LOW);    
        delay(100);                  
    }

When I upload this to my Arduino Duemilanove (ATmega328p), I end up
watching the pin 13 LED blink about once every two seconds, which is not
the correct way for it to run (I suppose some Arduinos may not have the LED
blink at all).

However, if I uncomment the Serial.println("a") line in the setup()
function, the program runs correctly and the LED blinks five times per second.

I have tried the above code in Arduino-0015 (hence the added parameter I
mentioned), as well as the main project I've been working on, and both
programs run correctly in that version.  It appears that there has been a
change in the way the Wire library works which has interfered with
serial/LCD communication.

Thanks.

Original issue reported on code.google.com by mul...@gmail.com on 3 Oct 2009 at 5:23

GoogleCodeExporter commented 9 years ago
I don't have lcd hardware to recreate your test condition here.
However nothing that I envision could cause that problem was in the one-line 
change
to Wire library between 0015 and 0017.  That said there were significant 
changes to
HardwareSerial and because of those changes, there may be a subtle difference 
in how
a bug in Wire.init() existing in both versions is manifested. Can you apply the
attached patch to twi.c under hardware/libraries/Wire/utility/, delete twi.o, 
then
recompile your test sketch and let me know if it makes any difference.  The 
patch
allocates buffers used by the twi interrupt handler before the interrupt is 
enabled
instead of afterward, potentially preventing memory being stomped on.

Original comment by ckjohn...@gwi.net on 5 Nov 2009 at 9:12

Attachments:

GoogleCodeExporter commented 9 years ago
Huh.  I'm unable to reproduce my original problem now, either with the project 
code
I've been working on, or with the sample code I gave in my problem report.  I 
tried
deleting the twi.c.o file in ~/sketchbook/<name>/applet/Wire/utility before 
running
with either version of the twi.c file, and it hasn't had any effect.  I also 
tried
removing the entire ~/sketchbook/<name>/applet folder and that didn't change 
anything
either.

I'm using Debian Linux for my development system, so I suppose it's possible 
that
some part of the AVR libc or GNU utilities changed out underneath me, but that 
seems
a bit unlikely to be the culprit.

Original comment by mul...@gmail.com on 6 Nov 2009 at 3:59

GoogleCodeExporter commented 9 years ago
Hold that thought.  As I mentioned in my report, I'd added one extra parameter 
to the
LiquidCrystal object initialization because the old arduino-0015 software didn't
support the shorter initialization method.

When I use this:

    LiquidCrystal lcd(12, 3, 11, 7, 6, 5, 4);

...the Arduino runs fine with 0017.  But when I change it to this:

    LiquidCrystal lcd(12, 11, 7, 6, 5, 4);

...the Arduino crashes and restarts, resulting in a slow blink.  There's some 
sort of
problem with removing the read/write pin.

Original comment by mul...@gmail.com on 6 Nov 2009 at 4:11

GoogleCodeExporter commented 9 years ago
mulad6, indeed adding the extra param in the lcd constructor solved the problem.
thanks.

Original comment by eli.ko...@gmail.com on 16 Jan 2010 at 7:35

GoogleCodeExporter commented 9 years ago
I am not sure if it due to the same issue. I have tried adding the extra 
parameter and still no luck.
I am using arduino 0021 and every time I try and do anything with the wire 
library my arduino crashes and reboots.

The link to the LiquidCrystal library is the only thing i can find but the 
above fix fails.

I am also using Ethernet, 1wire, SPI, DallasTemperature but i have tried in 
different combinations and It only seems to occur with LiquidCrystal 

Original comment by shivalw...@gmail.com on 13 Oct 2010 at 6:35

GoogleCodeExporter commented 9 years ago
Hi

I have encountered roughly the same problem, but none of the above solution 
worked for me.
My problem is that I get odd characters on my lcd. In Setup I write "Welcome" 
and it comes out fine. After that I get 4 odd characters for a little while 
then other odd 4 characters a.s.o.

My code looks like this:

// include the library code:
#include <Wire.h>
#include <LiquidCrystal.h>
#define RTC_I2C_ADDRESS 0x68
#define RTC_READ Wire.read
#define RTC_WRITE Wire.write

//Global var
byte Second, Minute, Hour, DayOfWeek, DayOfMonth, Month, Year;
byte zero;

// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(12, 6, 11, 5, 4, 3, 2);

void setup(){
  // set up the LCD's number of columns and rows: 
  lcd.begin(16, 2);
  lcd.write("Welcome");
  // initialize the serial communications:
  Serial.begin(9600);
  //Serial.println("a");
  Wire.begin(0x68);
  zero=0x00;
  //SetRTC();
}

byte DecToBcd(byte val)
{
  return ((val/10*16)+(val%10)); 
}

byte BcdToDec(byte val)
{
  return ((val/16*10)+(val%16)); 
}

void SetRTC()
{
  Wire.beginTransmission(RTC_I2C_ADDRESS);
  RTC_WRITE(zero);
  RTC_WRITE(DecToBcd(0) & 0x7F); //second
  RTC_WRITE(DecToBcd(11)); //Minute
  RTC_WRITE(DecToBcd(12)); //Hour
  RTC_WRITE(DecToBcd(2));
  RTC_WRITE(DecToBcd(18));
  RTC_WRITE(DecToBcd(9));
  RTC_WRITE(DecToBcd(12));
  Wire.endTransmission(); 
}

void GetRTC()
{
  Wire.beginTransmission(RTC_I2C_ADDRESS);
  RTC_WRITE(zero);
  Wire.endTransmission();
  Wire.requestFrom(RTC_I2C_ADDRESS, 7);

  Second = BcdToDec(RTC_READ() & 0x7F);
  //Second = RTC_READ() & 0x7F;
  //Second = RTC_READ();
  Minute = BcdToDec(RTC_READ());
  Hour = BcdToDec(RTC_READ() & 0x3F);
  DayOfWeek = BcdToDec(RTC_READ());
  DayOfMonth = BcdToDec(RTC_READ());
  Month = BcdToDec(RTC_READ());
  Year = BcdToDec(RTC_READ());

  Serial.println(Second,DEC);
  lcd.home();
  lcd.write(Second);
}

void loop()
{
  // when characters arrive over the serial port...
  if (Serial.available()) {
    // wait a bit for the entire message to arrive
    delay(100);
    // clear the screen
    lcd.clear();
    // read all the available characters
    while (Serial.available() > 0) {
      // display each character to the LCD
      lcd.write(Serial.read());
    }
  }

  //Read RTC
  digitalWrite(13,HIGH);
  GetRTC();
  delay(1000);
  digitalWrite(13,LOW);
  delay(1000); 
}

I am using a Leonardo with a parallel LCD attached and an I2C-RTC.

Original comment by tonny.ve...@gmail.com on 18 Sep 2012 at 10:19

GoogleCodeExporter commented 9 years ago
I see this problem reproducibly using an Arduino Uno and a hacktronics 20x4 
backlit LCD:

http://www.amazon.com/dp/B003B22UR0/ref=pe_175190_21431760_M3T1_ST1_dp_3
ASIN: B003B22UR0

I'm using LiquidCrystal and Serial.  

If I remove the call to "Serial.begin(19200)" then I can use my LCD, but 
otherwise the LCD only produces gibberish.  I have tried a number of different 
baud rates (from 300-57600), and they change the gibberish but don't fix things.

I've attached a minimal .ino file which reproduces the issue.  Same code pasted 
here below:

#include <LiquidCrystal.h>

LiquidCrystal lcd(2, 1, 4, 3,6,5,7);

int backlight = A1; // HIGH for backlight on

void setup() {
  // http://code.google.com/p/arduino/issues/detail?id=125#makechanges
  // Serial.begin(300); // uncomment this line to break the LCD output

  pinMode(backlight, OUTPUT);
  digitalWrite(backlight, HIGH); // turn backlight on. Replace 'HIGH' with 'LOW' to turn it off.
  lcd.begin(20,4);              // columns, rows.  use 16,2 for a 16x2 LCD, etc.
  lcd.clear();                  // start with a blank screen
}

void alert(int x, int y, char *fmt, ...) {
  char tmp[200];
  va_list args;
  va_start (args, fmt);
  vsnprintf(tmp, 20, fmt, args);
  va_end (args);

  lcd.setCursor(x,y); // column, row
  lcd.print(tmp);

  // flash the LCD to indicate that it has updated
  digitalWrite(backlight, LOW);    
  delay(100);
  digitalWrite(backlight, HIGH);   
}

void loop() {
  for (int i=0; i < 100; i++) {
    if (i % 4 == 0) lcd.clear();
    alert(i % 20, i % 4, "i: %d", i);
    delay(2000);
  }
}

Original comment by mark.jef...@gmail.com on 23 Mar 2013 at 3:17

Attachments:

GoogleCodeExporter commented 9 years ago
Using pin 0 or 1 in the LCD constructor AND using serial, will cause some 
issues.
As the serial communication uses the rx tx pins.  ( 0 & 1 )

Change the lcd to not use pin 0 or 1

Original comment by lennaert...@gmail.com on 3 Aug 2014 at 7:26

GoogleCodeExporter commented 9 years ago
I observe strange behavior of LCD when Wire communication is started. At the 
beggining binary characters are displayed and after few hours program hungs up. 
Program is too complex to send it here. I use arduino-1.0.6 with included 
LiquidCrystal and Wire.

Original comment by piotrekn...@gmail.com on 27 Sep 2014 at 10:42