Netduino home hardware projects downloads community

Jump to content


The Netduino forums have been replaced by new forums at community.wildernesslabs.co. This site has been preserved for archival purposes only and the ability to make new accounts or posts has been turned off.
Photo

I2C LCD BUG


  • Please log in to reply
11 replies to this topic

#1 smarcus3

smarcus3

    Advanced Member

  • Members
  • PipPipPip
  • 134 posts

Posted 12 April 2012 - 05:03 AM

After a little bit the display starts displaying garbled text then goes blank. What is the cause of it.
Steve


My Other Hobby: Engineer Turned Baker

#2 smarcus3

smarcus3

    Advanced Member

  • Members
  • PipPipPip
  • 134 posts

Posted 12 April 2012 - 05:19 AM

Increasing the bus speed and adding delays might have 'fixed' it.
Steve


My Other Hobby: Engineer Turned Baker

#3 smarcus3

smarcus3

    Advanced Member

  • Members
  • PipPipPip
  • 134 posts

Posted 12 April 2012 - 06:23 AM

Its better but still gets messed up within 5 minutes of running.
Steve


My Other Hobby: Engineer Turned Baker

#4 hanzibal

hanzibal

    Advanced Member

  • Members
  • PipPipPip
  • 1287 posts
  • LocationSweden

Posted 12 April 2012 - 07:02 AM

For anyone to help you with this you must supply at least some information of what you are trying to do, what parts you're interfacing with, what your code looks like, etc. ...or we could add a special subforum called "thinking out loud" :P

#5 smarcus3

smarcus3

    Advanced Member

  • Members
  • PipPipPip
  • 134 posts

Posted 12 April 2012 - 07:22 AM

I may have finally got the delays done. I am going to let it run over night.
Steve


My Other Hobby: Engineer Turned Baker

#6 smarcus3

smarcus3

    Advanced Member

  • Members
  • PipPipPip
  • 134 posts

Posted 12 April 2012 - 07:23 AM

For anyone to help you with this you must supply at least some information of what you are trying to do, what parts you're interfacing with, what your code looks like, etc.

...or we could add a special subforum called "thinking out loud" :P


Sometimes a guy just has to vent you know.
Steve


My Other Hobby: Engineer Turned Baker

#7 Stefan

Stefan

    Moderator

  • Members
  • PipPipPip
  • 1965 posts
  • LocationBreda, the Netherlands

Posted 12 April 2012 - 07:41 AM

Hi :) If you're vented well, and found the solution, would you like to share it, so if someone else gets the same problem, he doesn't need to vent as well? ;)
"Fact that I'm a moderator doesn't make me an expert in things." Stefan, the eternal newb!
My .NETMF projects: .NETMF Toolbox / Gadgeteer Light / Some PCB designs

#8 smarcus3

smarcus3

    Advanced Member

  • Members
  • PipPipPip
  • 134 posts

Posted 12 April 2012 - 03:22 PM

It still is a problem. It is better than the default configuration but far from perfect. After 5 - 10 minutes the output messes up and the display become garbled then blank. Ill post my code soon.
Steve


My Other Hobby: Engineer Turned Baker

#9 smarcus3

smarcus3

    Advanced Member

  • Members
  • PipPipPip
  • 134 posts

Posted 12 April 2012 - 03:30 PM

Attached is my solution. I am still testing it but it now has run the display for > 15 minutes without becoming garbled. Most applicable code is below. I will update the run time as the day goes on.

RUNTIME: 20 minutes.

If anyone would like the additional functions just PM me or post a message on the thread.

Here is my I2CBus class for my microliquid library. I increased the bus speed to 1000. If the number is set to the default 0,0 I get garbled messages. This still happens at 0,300. I am not exactly sure what this does but it does seem to help.

public class I2CBus : IDisposable
    {
        /// <summary>Aggregated device for this driver</summary>
        private I2CDevice Device;

        /// <summary>Creates a new <see cref="I2CBus"/> instance </summary>
        /// <remarks>
        /// At this time the .NET Micro Framework only supports a single I2C bus. 
        /// Therefore, creating more than one I2CBus instance will generate an
        /// exception. 
        /// </remarks>
        /// <exception cref="InvalidOperationException">An I2CBus instance already
        /// exists and it's Dispose() method has not been called</exception>
        public I2CBus()
        {
            this.Device = new I2CDevice(new I2CDevice.Configuration(0, 1000));
        }


My function which writes to the LCD

public static void updateLCDDisplay(clFinance Stock, Lcd lcd) //needs LCD in it
        {
            /* THIS IS A HELPER FUNCTION WHICH IS CALLED TO UPDATE THE DISPLAY OF THE LCD DISPLAY WITH THE 
             * INPUTTED STOCK. THIS FUNCTION KEEPS THE MAIN FUNCTION CLEAN.
             * 
             *                  //     LCD Display     //
             *
             *                  //        LAYOUT       // 
             *                  //  STOCK NAME   LAST  //
             *                  //    CHANGE PERCENT   //
             *                  //   DAILY  -  CHANGE  //
             *                  //    MARKET VALUE     //
             * 
             */ 
            
            //20 x 4 size LCD

            

            string formattedStockName = "";
            string formattedMarketValue = "";
            string formattedDailyChange = "";
            string formattedChange = "";
            string formattedPercChange = "";
            string formattedLast = "";


            //Stock Name
            //if (Stock.GetStockName().Length > 20)
                formattedStockName = Stock.GetTicker();
            //else
            //    formattedStockName = Stock.GetStockName();

            //Change
            if (Stock.GetChange() > 0)
                formattedChange = "$" + setDecimalPlaceOfString(Stock.GetChange(), 2, true);
            else
                formattedChange = "-$" + setDecimalPlaceOfString(Stock.GetChange() * -1,2, true);
            
            //Percent
            formattedPercChange = setDecimalPlaceOfString(Stock.GetPercentChange(), 2, true) +"%";

            //Daily Change
            if (Stock.GetDailyChange() > 0)
                formattedDailyChange = "$" + setDecimalPlaceOfString(Stock.GetDailyChange(),2, true);
            else
                formattedDailyChange = "-$" + setDecimalPlaceOfString(Stock.GetDailyChange() * -1, 2, true);
            
            //Market Value
            if (Stock.GetMarketValue() > 0)
                formattedMarketValue = "$" + setDecimalPlaceOfString(Stock.GetMarketValue(), 2,true);
            else
                formattedMarketValue = "-$" + setDecimalPlaceOfString(Stock.GetMarketValue() * -1, 2,true);

            //Last
            formattedLast = "$" + setDecimalPlaceOfString(Stock.GetLastPrice(), 2, true);

            lcd.Clear();
            //lcd.SetCursorPosition(0, 0);
            Thread.Sleep(1000);
            
            //Row 1
            lcd.SetCursorPosition(0,0);
            Thread.Sleep(1000);
            lcd.Write(removeIllegalCharacters(centerString(formattedStockName, 20)));
            Thread.Sleep(1000);
            //Row 2
            lcd.SetCursorPosition(0, 1);
            Thread.Sleep(1000);
            lcd.Write(removeIllegalCharacters(centerString(formattedChange, formattedPercChange, 20)));
            Thread.Sleep(1000);
            //Row 3
            lcd.SetCursorPosition(0, 2);
            Thread.Sleep(1000);
            lcd.Write(removeIllegalCharacters(centerString(formattedDailyChange, 20)));
            Thread.Sleep(1000);
            //Row 4
            lcd.SetCursorPosition(0, 3);
            Thread.Sleep(1000);
            lcd.Write(removeIllegalCharacters(centerString(formattedMarketValue, 20)));
            Thread.Sleep(1000);

            //TESTING CODE
            
            //string temp2 = center2String(formattedStockName,formattedLast, 20);
            //string temp3 = center2String(formattedChange, formattedPercChange, 20);
            //string temp1 = center1String(formattedDailyChange, 20);
            //string temp = center1String(formattedMarketValue, 20);

        }


My function to make sure Illegal characters aren't sent to the LCD

public static string removeIllegalCharacters(string inputString)
        {
            int asciiValue;
            for (int i = 0; i < inputString.Length - 1; i++)
            {
                asciiValue = (int) inputString[i];

                if (asciiValue == 45 || asciiValue == 36 || asciiValue == 46 || asciiValue == 37 || (asciiValue >= 48 && asciiValue <= 57) ||
                    (asciiValue >= 97 && asciiValue <= 122) || (asciiValue >= 65 && asciiValue <= 90) || asciiValue == 32 || asciiValue == 44)
                {
                    //This character is fine.
                    //Move on to the next
                }
                else
                {
                    return "ILLEGAL CHAR FOUND";
                }
            }
            return inputString;
        }

Function to center LCD Text

public static string centerString(string sourceString1, string sourceString2, int formattedStringLength)
        {
            /*THIS FUNCTION TAKES IN TWO STRINGS AND CENTERS IT IN THE OUTPUTTED STRING. THIS RETURNED STRING'S 
             * LENGTH IS DETERMINED BY THE INTEGER, FORMATTEDSTRINGLENGTH.
             * 
             * INPUTS:  SOURCESTRING1           ---> STRING CONTAINING A STRING NEEDED TO BE CENTERED
             *          SOURCESTRING2           ---> STRING CONTAINING A STRING NEEDED TO BE CENTERED
             *          FORMATTEDSTRINGLENGTH   ---> INTEGER SPECIFING THE LENGTH OF THE RETURNED STRING
             * 
             * OUTPUT:  A STRING THE LENGTH OF FORMATTEDSTRINGLEGTH THAT CENTERS THE SOURCESTRINGS
             */ 
            string formattedString = "";

            if (sourceString1.Length + sourceString2.Length < formattedStringLength)
            {
                int diference = (int) (formattedStringLength - sourceString1.Length -  sourceString2.Length)/3;

                //only 2 spaces
                if (diference == 0 && formattedStringLength - sourceString1.Length - sourceString2.Length >= 2)
                {
                    diference = 1;
                }

                //only 1 space
                if (diference == 0 && formattedStringLength - sourceString1.Length - sourceString2.Length == 1)
                    return sourceString1 + " " + sourceString2;
                
                for (int i = 0; i < formattedStringLength; i++)
                {
                    if (i < diference)
                        formattedString = formattedString + " ";
                    else if (i < sourceString1.Length + diference)
                    {
                        formattedString = formattedString + sourceString1;
                        i = diference + sourceString1.Length - 1;
                    }
                    else if (i < sourceString1.Length + 2 * diference)
                    {
                        formattedString = formattedString + " ";
                    }
                    else if (i < sourceString1.Length + sourceString2.Length + 2 * diference)
                    {
                        formattedString = formattedString + sourceString2;
                        i = sourceString1.Length + sourceString2.Length + 2 * diference - 1;
                    }
                    else
                        formattedString = formattedString + " ";
                }

                return formattedString;
            }
            else
                return sourceString1 + sourceString2;
        }
        public static string centerString(string sourceString, int formattedStringLength)
        {
            /*THIS FUNCTION TAKES IN ONE STRINGS AND CENTERS IT IN THE OUTPUTTED STRING. THIS RETURNED STRING'S 
             * LENGTH IS DETERMINED BY THE INTEGER, FORMATTEDSTRINGLENGTH.
             * 
             * INPUTS:  SOURCESTRING1           ---> STRING CONTAINING A STRING NEEDED TO BE CENTERED
             *          FORMATTEDSTRINGLENGTH   ---> INTEGER SPECIFING THE LENGTH OF THE RETURNED STRING
             * 
             * OUTPUT:  A STRING THE LENGTH OF FORMATTEDSTRINGLEGTH THAT CENTERS THE SOURCESTRING
             */ 
            string formattedString = "";

            if (sourceString.Length < formattedStringLength)
            {
                int diference = formattedStringLength - sourceString.Length;
                for (int i = 0; i < formattedStringLength; i++)
                {
                    if (i < (int) diference/2)
                        formattedString = formattedString + " ";
                    else if (i < sourceString.Length + (int) diference/2)
                    {
                        formattedString = formattedString + sourceString;
                        i = (int)diference / 2 + sourceString.Length - 1;
                    }
                    else
                        formattedString = formattedString + " ";
                }

                return formattedString = formattedString.Substring(0,formattedStringLength);
            }
            else
                //forces it to be the right length
                return sourceString.Substring(0, formattedStringLength);
        }

Steve


My Other Hobby: Engineer Turned Baker

#10 smarcus3

smarcus3

    Advanced Member

  • Members
  • PipPipPip
  • 134 posts

Posted 12 April 2012 - 06:48 PM

This code is better but in the end it still freezes up. Trying something else. Ill keep you all updated.
Steve


My Other Hobby: Engineer Turned Baker

#11 smarcus3

smarcus3

    Advanced Member

  • Members
  • PipPipPip
  • 134 posts

Posted 12 April 2012 - 07:11 PM

Writing to the lcd char by char might have fixed it.
Steve


My Other Hobby: Engineer Turned Baker

#12 smarcus3

smarcus3

    Advanced Member

  • Members
  • PipPipPip
  • 134 posts

Posted 12 April 2012 - 08:43 PM

Here is a char by char writing function. This function def helps but it is not perfect. If anyone has a suggestion on how to fix it or improve the implementation of i2c PLEASE let me know.

public static void wrtieCharbyCharLCD(Lcd lcd, string inputString, int cursorStartRow)
        {
            //Needs a string the width of the LCD.

            lcd.SetCursorPosition(0, cursorStartRow); //COLUMN ROW
            for (int i = 0; i < clConstants.LCDWIDTH; i++)
            {
                if (inputString[i].ToString() != " ")
                {
                    Thread.Sleep(clConstants.DELAYBETWEENLCDWRITEMS);
                    lcd.SetCursorPosition(i, cursorStartRow);
                    lcd.Write(inputString[i].ToString());
                }
            }
        }

Steve


My Other Hobby: Engineer Turned Baker




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users

home    hardware    projects    downloads    community    where to buy    contact Copyright © 2016 Wilderness Labs Inc.  |  Legal   |   CC BY-SA
This webpage is licensed under a Creative Commons Attribution-ShareAlike License.