NerdKits - electronics education for a digital generation

You are not logged in. [log in]

NEW: Learning electronics? Ask your questions on the new Electronics Questions & Answers site hosted by CircuitLab.

Support Forum » Displaying String variables onto the LCD.

June 15, 2010
by Jer
Jer's Avatar

How do I print a string variable onto the LCD?

I have been researching it but have not been able to figure it out yet.

June 15, 2010
by hevans
(NerdKits Staff)

hevans's Avatar

Hi Jer,

If the string variable is in program memory you can use the lcd_write_string() function include in libnerdkits.

If you have a string in RAM (and you have enough room to include stdio.h) you can use the fputs() function in stdio.h. fputs() takes a pointer to a string, and a stream to print to. You will have to setup the LCD as a stream the way we do in the NerdKits Guide, and then just use the LCD stream. The fputs() function is documented in the stdio.h avr-libc documentation. There is also a variant of this function for strings in program space.

You can also use formatted strings, which we covered in our [prinf and scanf tutorial])(http://www.nerdkits.com/videos/printf_and_scanf/). Let me know if you have any trouble.

Humberto

June 15, 2010
by Jer
Jer's Avatar

Thanks again Humberto!

June 15, 2010
by Jer
Jer's Avatar

So, will this work?

main() {

 char mystr[10] = "hello world";
 .
 .
 .

 fputs_P(mystr, &lcd_stream)

}

And Also, I would like to do something like this:

// 2 dim char array. 6 chars X 10 Lines. char mystr[10][6] = {"Row 1 of 10","Row 2 of 10","Row 3 of 10", ... "Row 10 of 10"};

fputs_P(mystr[LineX], &lcd_stream);

Will this work? It couldn't be that simple, could it?

June 16, 2010
by hevans
(NerdKits Staff)

hevans's Avatar

Hi Jer,

Right now you have your strings defined in RAM. This will work if you remove the "-j .text" flag to avr-objcopy in your makefile. Otherwise statically initialized strings have nowhere to exist.

Now assuming that your strings are in RAM, you need to use the fputs() function, not the fputs_P() function. The fputs_P() funtion is for strings that are in program space. Otherwise, it is that simple.

Let us know if it works.

Humberto

June 16, 2010
by Jer
Jer's Avatar

I tried both:

 fputs_P(mystr, &lcd_stream);

 fputs(mystr, &lcd_stream);

neather work right.

Can you please make an example program?

June 16, 2010
by hevans
(NerdKits Staff)

hevans's Avatar

Hi Jer,

I put together the following example program, which worked on my NerdKit here. As I noted above I had to remove the '-j .text' from my makefile to make the static array initialization work. Hope it helps. Let me know if there is something you are not getting that I can clear up for you.

#define F_CPU 14745600

#include <stdio.h>

#include <avr/io.h>
#include <avr/interrupt.h>
#include <avr/pgmspace.h>
#include <inttypes.h>

#include "../libnerdkits/delay.h"
#include "../libnerdkits/lcd.h"

int main() {
  // LED as output
  lcd_init();
  FILE lcd_stream = FDEV_SETUP_STREAM(lcd_putchar, 0, _FDEV_SETUP_WRITE);
  lcd_home();

  char foo[10] = "My Array";
  fputs(foo,&lcd_stream);

  while(1){

  }

}
June 16, 2010
by Jer
Jer's Avatar

I also would like to know how to display a selected string form a 2 dim char array?

// string array 4 chars wide by 20 rows. char foo[20][4] = {"ABC","DEF","GHI","JKL","MNO","PQR"...};// is this the correct way to make a 2D string array?

fputs(foo[row],&lcd_stream);// Is this correct to display


row = 0 prints out "ABC"

row = 1 prints out "DEF"

row = 2 prints out "GHI"

and so on.

1) These strings are static so were would be the best place to put them and how do I put them there? 2) Could you also give me an example of non-static displaing of string vars.

PS Thanks for the code I will try it as soon as I get back home from work tonight!

Thank again!!

June 16, 2010
by Jer
Jer's Avatar

Ok, Is your first code example using the array as static? I thought it was only static if the key word 'const' was used to declare the array.

I need a clearer understanding of putting things in ram vs. flash or any other options? What are the other options and what is the syntax.

June 16, 2010
by hevans
(NerdKits Staff)

hevans's Avatar

Hi Jer,

Your 2d array looks right. You pretty much seem to have the right understanding here.

It is generally better practice to store array in program space (mostly because you have so much more of it). Basically you just need to use the PGM_P data type which is a pointer to a string in program space, then make sure you wrap your string in PSTR()

  PGM_P bar = PSTR("myString");

You would then print it with fputs_P.

2d arrays in program memory get tricky. There is a good explanation of 2d arrays of Data in Program Space in the avr-libc documentation. Documentation for PGM_P and the PSTR() macro are in the pgmspace.h documentation. Notice that they are both just macros that make sure your strings get put in program space.

Humberto

Post a Reply

Please log in to post a reply.

Did you know that you can control 120 LEDs with just 17 microcontroller pins? Learn more...