Help with display/print function for LCD

Discussion in 'Embedded Systems and Microcontrollers' started by chimera, Dec 17, 2011.

  1. chimera

    Thread Starter Member

    Oct 21, 2010
    122
    2
    Hello. This is my first post in the programmers section. I have recently gotten into micro controllers and am having a lot of fun. I am using the MSP430 Launchpad. Its cheap and very good bang for the buck.

    I have successfully interfaced a 16x2 LCD (Hitachi compatible HD4478U). I had a buddy of mine help me out with the code. However, given my limited knowlege in C ++ programing, this is what code snippet I have for printing data to the LCD (this is from an .h file):
    Code ( (Unknown Language)):
    1.  
    2. void Print(char *Text)
    3. {
    4.     char *c;
    5.     c=Text;
    6.     if(!Text) return;
    7.     for( c = Text; *c != '\0'; ++c)
    8.     {
    9.         SendByte(*c, TRUE);
    10.     }
    11. }
    12.  
    So in the main program, I would do something like: Print ("AllaboutCircuits"); and it would display this on the screen. However, I want to display data result from a calculation. For sake of simplicity, lets assume the result I am trying to display is 345 (decimal value). So I would do something like this:

    int temp=0;
    int test=345;
    a=test/100; /*saves 3 in a
    temp=test%100 /*saves 45 in temp
    b=temp/10; /*saves 4 in b
    c=temp%10; /*saves 5 in c

    How should I go about modifying the Print code snippet to able to send the 345 to the LCD without actually using typing in "345" using Print function.

    I need to learn this so that I can eventually display a voltage from a potentiometer interfacing the micro controller using A-to-D conversion. I know its a long post, but please bear with me; I am not so good in programming but trying to better my self :) Thanks for looking!!!
     
    Last edited: Dec 17, 2011
  2. spinnaker

    AAC Fanatic!

    Oct 29, 2009
    4,887
    1,015
    This is an extremely basic question. it is probably the second type of program you would write in C. The first being printing "Hello World".

    Before you go one step further I suggest you get a tutorial on C and study it.

    The function you want to lookup is sprintf. It allows you to print a formatted string to a string buffer then send that to the LCD.


    And from the looks of it this is C and not C++.
     
    chimera likes this.
  3. chimera

    Thread Starter Member

    Oct 21, 2010
    122
    2
    thanks for the reply. so based on what u have said.. I will and have been studying up on C but in the mean time.. how do u think I should go about changing the code snippet. In this way, it'll guide me thru some syntax and I can see what they mean.

    Thanks :)
     
  4. hgmjr

    Moderator

    Jan 28, 2005
    9,030
    214
    After you have parsed the 3-digit number into its individual digits, you can then add 0x30 (hex) to each number to convert it to its ASCII equivalent. You can then send them to the display one character at a time.

    hgmjr
     
    chimera likes this.
  5. chimera

    Thread Starter Member

    Oct 21, 2010
    122
    2
    Code ( (Unknown Language)):
    1.  
    2.  
    3. #include <msp430g2231.h>
    4. #include "lcd.h"
    5.  
    6. int a;
    7.  
    8. void main(void) {
    9.     WDTCTL = WDTPW + WDTHOLD;
    10.     P1DIR=0xF9;                // microcontroller
    11.     P1OUT=0;            // microcontroller
    12.     LCD_init();        // Initialize LCD
    13.     clear();    // Clear LCD
    14.     a=9;
    15.    
    16.     Print (a+0x30);
    17.    
    18.  
    19.  **** by the way..print function is:
    20. void Print(char *Text)
    21. {
    22.     char *c;
    23.     c=Text;
    24.     if(!Text) return;
    25.     for( c = Text; *c != '\0'; ++c)
    26.     {
    27.         SendByte(*c, TRUE);
    28.     }
    29. }
    30.  

    It gives me an error saying:
    argument of type "int" is incompatible with parameter of type "char *" LCD_TEST test.c
     
    Last edited: Dec 17, 2011
  6. spinnaker

    AAC Fanatic!

    Oct 29, 2009
    4,887
    1,015
    Why go through all of that when the OP can simply use sprintf? I can see if OP wanted avoid floating point and use integer math. But the OP is already using FP anyway,


    http://www.cplusplus.com/reference/clibrary/cstdio/sprintf/


    Code ( (Unknown Language)):
    1.  
    2.  
    3. int a = 9;
    4. // For integers
    5. sprintf(buffer,"The value is %i",a);
    6. Print(buffer);
    7.  
    8.  
    9. // For floating point assuming your compiler supports %f
    10. float C = 1.23;
    11. sprintf(buffer,"The value is %f.2:",C);
    12. Print(buffer);
    13.  
    14.  
    15. //For floating point if compiler does not support %f
    16.     sprintf(string,"The value is  %2d.%02d: ",(int)c ,(int)((fBatteryVolts - (int)c) *100));
    17.  
    18.  
    19.  
    20.  
    21.  
     
    chimera likes this.
  7. hgmjr

    Moderator

    Jan 28, 2005
    9,030
    214
    Declare "a" as a char.


    hgmjr
     
    chimera likes this.
  8. chimera

    Thread Starter Member

    Oct 21, 2010
    122
    2
    Guys, thank you for all your efforts; I have been reading on documentations but my issue still has not gotten resolved:

    I want to pass an integer to the LCD that results from a calculation. Lets assume that integer is 3. Now, the Print function that I use to write to the LCD only accepts characters which I pass to it through the main program (Example: Print (" Hello World" ). The function is:

    Code ( (Unknown Language)):
    1.  
    2. void Print(char *Text)
    3. {
    4.     char *c;
    5.     c=Text;
    6.     if(!Text) return;
    7.     for( c = Text; *c != '\0'; ++c)
    8.     {
    9.         SendByte(*c, TRUE);
    10.     }
    11. }
    12.  
    Now, how should I convert that integer to character or rather in which ever type the Print function can accept. The other .h files included are: #include <stdio.h>
    #include <stdlib.h>

    Please help me. I dont know how to solve this issue. Thank you so very much
     
  9. spinnaker

    AAC Fanatic!

    Oct 29, 2009
    4,887
    1,015
    I gave you the answer. Why do you want to make it so difficult for yourself? Try the sprintf method.
     
    chimera likes this.
  10. chimera

    Thread Starter Member

    Oct 21, 2010
    122
    2
    Hey it works!!

    However, the value of a isant showing up. Its blank. This the code that I am using:

    Code ( (Unknown Language)):
    1.  
    2.  
    3. #include <msp430g2231.h>
    4. #include <stdio.h>
    5. //#include <stdlib.h>
    6. #include "lcd.h"
    7.  
    8. void main(void) {
    9.     WDTCTL = WDTPW + WDTHOLD;
    10.     P1DIR=0xF9;                // microcontroller
    11.     P1OUT=0;            // microcontroller
    12.     LCD_init();        // Initialize LCD
    13.     clear();    // Clear LCD    
    14.  
    15.     int a = 9;
    16.     char buffer[20];
    17.     sprintf(buffer,"The value is %i",a);
    18.     Print(buffer);
    19. }
    20.  
     
  11. hgmjr

    Moderator

    Jan 28, 2005
    9,030
    214
    You need to add 0x30 hex to the integer value to convert it to ASCII.

    hgmjr
     
    chimera likes this.
  12. chimera

    Thread Starter Member

    Oct 21, 2010
    122
    2
    This is how I am entering the hex (0x30 or '0'). The text shows up..meaning that the code is working. But the value of a doesnt. Should I change the a to a char?

    Code ( (Unknown Language)):
    1.  
    2. int a = 9;
    3.     char buffer[20];
    4.     sprintf(buffer,"The value is %i",(a+'0'));
    5.     Print(buffer);
    6.  
     
  13. hgmjr

    Moderator

    Jan 28, 2005
    9,030
    214
    Try casting it to char.

    hgmjr
     
    chimera likes this.
  14. chimera

    Thread Starter Member

    Oct 21, 2010
    122
    2
    IT WORKS!! YAY :D :D :D
    Code ( (Unknown Language)):
    1.  
    2. #include <msp430g2231.h>
    3. #include <stdio.h>
    4. #include "lcd.h"
    5.  
    6. int a;
    7. char buffer[];
    8. void main(void) {
    9.     WDTCTL = WDTPW + WDTHOLD;
    10.     P1DIR=0xF9;                // microcontroller
    11.     P1OUT=0;            // microcontroller
    12.     LCD_init();        // Initialize LCD
    13.     clear();    // Clear LCD
    14.     a=9+0x30;
    15.     sprintf(buffer,"The value is %c",a);
    16.     Print(buffer);
    17. }
    18.  

    The issue was %c-- I was entering %i---which was signed integer. It was luck of the draw. I went on 43oh.com/forum and there..someone had just posted format specifiers:
    There are 7 format specifiers:
    %c - Character
    %s - String
    %i - signed Integer (16 bit)
    %u - Unsigned integer (16 bit)
    %l - signed Long (32 bit)
    %n - uNsigned loNg (32 bit

    Finally I am one more step closer to my goal!! Thanks everyone for getting me close..step by step!
     
  15. spinnaker

    AAC Fanatic!

    Oct 29, 2009
    4,887
    1,015
    You are trying to add a character to an integer. That does not work in C.

    Why not just print the number to a buffer and be done with it?

    sprintf(buffer,"%i", a);

    If you insist on doing it the hard way


    int a = 9;
    char buffer[20];
    buffer[ 0] = 30+a;
    buffer[1] = 0;
    Print(buffer);
     
  16. hgmjr

    Moderator

    Jan 28, 2005
    9,030
    214
    The best part of this whole exercise is that you were industrious enough to uncover the final solution for yourself.

    Well done,
    hgmjr
     
  17. spinnaker

    AAC Fanatic!

    Oct 29, 2009
    4,887
    1,015

    Ok so now do this with a=9489.

    You will find it is not so simple.
     
  18. thatoneguy

    AAC Fanatic!

    Feb 19, 2009
    6,357
    718
    What compiler are you using?

    There should be a dectostr() or numtostr() function that takes a number argument and returns a string pointer. The pointer (*x) is a number holding the base memory address of the first character in the string to be sent.

    Look in your compiler's help file under "String Functions" to find what that function is called in your compiler. Most all compilers have some version of it.
     
  19. chimera

    Thread Starter Member

    Oct 21, 2010
    122
    2
    Finally it works. LOL.. i'll leave the hard version for the time when im better versed with C. I will for sure say that I need to read up alot of C programming. This is hard stuff. New respect.

    BTW.. how does one determine the buffer size. I left mine to be determined by the number of arguments in my array. Henceforth the char buffer [];

    THANK YOU SPINNAKER and HGMJR.. you guys rock! :)
     
  20. chimera

    Thread Starter Member

    Oct 21, 2010
    122
    2

    hahah.. true.. all I am seeing is 'A' on the screen. Why is that and How can I deal with that? By parsing the values?
     
Loading...