# Help with display/print function for LCD

#### chimera

Joined Oct 21, 2010
122
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):
Rich (BB code):
void Print(char *Text)
{
char *c;
c=Text;
if(!Text) return;
for( c = Text; *c != '\0'; ++c)
{
SendByte(*c, TRUE);
}
}
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:

#### spinnaker

Joined Oct 29, 2009
7,835
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):
Rich (BB code):
void Print(char *Text)
{
char *c;
c=Text;
if(!Text) return;
for( c = Text; *c != '\0'; ++c)
{
SendByte(*c, TRUE);
}
}
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!!!
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

Joined Oct 21, 2010
122
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

#### hgmjr

Joined Jan 28, 2005
9,029
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

Joined Oct 21, 2010
122
Rich (BB code):
#include <msp430g2231.h>
#include "lcd.h"

int a;

void main(void) {
WDTCTL = WDTPW + WDTHOLD;
P1DIR=0xF9;                // microcontroller
P1OUT=0;            // microcontroller
LCD_init();        // Initialize LCD
clear();    // Clear LCD
a=9;

Print (a+0x30);

**** by the way..print function is:
void Print(char *Text)
{
char *c;
c=Text;
if(!Text) return;
for( c = Text; *c != '\0'; ++c)
{
SendByte(*c, TRUE);
}
}

It gives me an error saying:
argument of type "int" is incompatible with parameter of type "char *" LCD_TEST test.c

Last edited:

#### spinnaker

Joined Oct 29, 2009
7,835
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
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/

Rich (BB code):
int a = 9;
// For integers
sprintf(buffer,"The value is %i",a);
Print(buffer);

// For floating point assuming your compiler supports %f
float C = 1.23;
sprintf(buffer,"The value is %f.2:",C);
Print(buffer);

//For floating point if compiler does not support %f
sprintf(string,"The value is  %2d.%02d: ",(int)c ,(int)((fBatteryVolts - (int)c) *100));

#### hgmjr

Joined Jan 28, 2005
9,029
Declare "a" as a char.

hgmjr

#### chimera

Joined Oct 21, 2010
122
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:

Rich (BB code):
void Print(char *Text)
{
char *c;
c=Text;
if(!Text) return;
for( c = Text; *c != '\0'; ++c)
{
SendByte(*c, TRUE);
}
}
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>

#### spinnaker

Joined Oct 29, 2009
7,835
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:

Rich (BB code):
void Print(char *Text)
{
char *c;
c=Text;
if(!Text) return;
for( c = Text; *c != '\0'; ++c)
{
SendByte(*c, TRUE);
}
}
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>

I gave you the answer. Why do you want to make it so difficult for yourself? Try the sprintf method.

#### chimera

Joined Oct 21, 2010
122
Hey it works!!

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

Rich (BB code):
#include <msp430g2231.h>
#include <stdio.h>
//#include <stdlib.h>
#include "lcd.h"

void main(void) {
WDTCTL = WDTPW + WDTHOLD;
P1DIR=0xF9;                // microcontroller
P1OUT=0;            // microcontroller
LCD_init();        // Initialize LCD
clear();    // Clear LCD

int a = 9;
char buffer[20];
sprintf(buffer,"The value is %i",a);
Print(buffer);
}

#### hgmjr

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

hgmjr

#### chimera

Joined Oct 21, 2010
122
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?

Rich (BB code):
int a = 9;
char buffer[20];
sprintf(buffer,"The value is %i",(a+'0'));
Print(buffer);

#### hgmjr

Joined Jan 28, 2005
9,029
Try casting it to char.

hgmjr

#### chimera

Joined Oct 21, 2010
122
IT WORKS!! YAY
Rich (BB code):
#include <msp430g2231.h>
#include <stdio.h>
#include "lcd.h"

int a;
char buffer[];
void main(void) {
WDTCTL = WDTPW + WDTHOLD;
P1DIR=0xF9;                // microcontroller
P1OUT=0;            // microcontroller
LCD_init();        // Initialize LCD
clear();    // Clear LCD
a=9+0x30;
sprintf(buffer,"The value is %c",a);
Print(buffer);
}

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!

#### spinnaker

Joined Oct 29, 2009
7,835
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?

Rich (BB code):
int a = 9;
char buffer[20];
sprintf(buffer,"The value is %i",(a+'0'));
Print(buffer);
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);

#### hgmjr

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

Well done,
hgmjr

#### spinnaker

Joined Oct 29, 2009
7,835
IT WORKS!! YAY
Rich (BB code):
#include <msp430g2231.h>
#include <stdio.h>
#include "lcd.h"

int a;
char buffer[];
void main(void) {
WDTCTL = WDTPW + WDTHOLD;
P1DIR=0xF9;                // microcontroller
P1OUT=0;            // microcontroller
LCD_init();        // Initialize LCD
clear();    // Clear LCD
a=9+0x30;
sprintf(buffer,"The value is %c",a);
Print(buffer);
}
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!

Ok so now do this with a=9489.

You will find it is not so simple.

#### thatoneguy

Joined Feb 19, 2009
6,359
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.

#### chimera

Joined Oct 21, 2010
122
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!