how to sprintf char array to show it on lcd?!

Go To Last Post
15 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hello everyone,

i want to show a string on lcd 2*16, i used this:

#include <alcd.h>
#include <string.h>
#include <delay.h>
#include <stdio.h>

char s[] = "Hello";

void main(void)
{

while (1)
    { 

sprintf(lcdBuffer,"str:%s",s);
        lcd_puts(lcdBuffer);
        delay_ms(100);

   }

}

but nothing is displayed!

also i used:

sprintf(lcdBuffer,"str:%p","string");
        lcd_puts(lcdBuffer);
        delay_ms(100);

and it works! so i used:

char str[5]="hello";

sprintf(lcdBuffer,"str:%p",str);
        lcd_puts(lcdBuffer);
        delay_ms(100);

but it's not working and some Unclear characters are displayed! sad

Last Edited: Mon. Sep 18, 2017 - 07:20 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

The first question is: if it's already a char array, why do you need sprintf() at all? Surely, you can just "print" it directly?

Have you tried that?

 

#include <alcd.h>
#include <string.h>
#include <delay.h>
#include <stdio.h>
char s[] = "Hello";
void main(void)
{
    while (1)
    { 
        sprintf( lcdBuffer, "str:%s", s );
        lcd_puts(lcdBuffer);
        delay_ms(100);
   }
}

You haven't shown the definition of lcdBuffer

 

Have you used a simulator or debugger to step through the code & see what's happening?

 

    sprintf( lcdBuffer, "str:%p", "string" );
    lcd_puts(lcdBuffer);
    delay_ms(100);

%p should print the address of the string - is that what you saw?

 

 

How to properly post source code: http://www.avrfreaks.net/comment...

 

And don't be afraid to use spaces - spacing out your parameter lists makes them much easier to read!

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Why on earth are you using %p ?!? %p is to print a pointer address and will usually (implementation defined) show hex digits.

 

Like Andy says, in your first code why are you even using sprintf(). Surely you just:

        lcd_puts(s);

?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

It is always wise to post a complete program.   If it is short,  paste it to a CODE window.   If it is very long,  attach the file.

 

A normal structure would be:

#include <alcd.h>
#include <string.h>
#include <delay.h>
#include <stdio.h>

char s[] = "Hello";

void main(void)
{
    char lcdBuffer[20];     //somewhere to put the formatted message
    lcd_init(16);           //initialise the LCD
    while (1)
    {
        sprintf( lcdBuffer, "str:%s", s );
        lcd_puts(lcdBuffer);
        delay_ms(100);
   }
}

Most programs consist of this structure:

#include <alcd.h>        //include any library header files
...
char s[] = "Hello";      //declare/define any global variables
...
void main(void)
{
    char lcdBuffer[20];  // e.g. declare local variables
    ...
    init_lcd(16);        // e.g. setup() phase. Initialise library.
    ...
    while (1)            // e.g. loop phase
    {
        sprintf(...);    // e.g. actual repeated code
        ...
   }
}

It looks like a Codevision program.   The Wizard will generate the structure for you.   But it is as easy to write it yourself.   Note that a lot of CodeWizard initialisation is redundant i.e. SFR registers will already be in a known "Reset state"

 

Your example program shows how you do not need to add any AVR specific low-level statements.    The LCD pins were configured in the GUI.

 

David.

 

p.s.  You can make CV printf() directly to an LCD just like the UART.   i.e.  without the separate sprintf() and lcd_puts()

This would be later exercse.

Last Edited: Mon. Sep 18, 2017 - 08:58 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

thanks all,

my problem solved!

i was trying to combine array like this:

sprintf(lcd_Buffer,"1:%s\n",numbers[0]);
                lcd_puts(lcd_Buffer);

(number is number[10][11])

on lcd i just get this=> 1:

there was nothing at the end of that and i don't know why??!! indecision

but after some try,it solved and, it's showing me the numbers and i don't know why??!indecision

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

IF you want  help with code, you need to show the actual code that you want help with!

 

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

We don't know what numbers[] is so this can only be a guess but I can't help thinking that an array called numbers[] might actually contain, um, numbers. If that means something like int or long or even signed char then you would need a printf() format specificer that matches the type. Perhaps start with %d or %u or something?

 

I have no idea why you started this thread talking about %s and sprintf() for strings if it really is some kind of integer values you are talking about? One can be output directly using an LCD print_string() type routine while the other does need some kind of inetger to ASCII string conversion such as offered by sprintf() but to use it you have to get the character after % correct. You can't just make wild guesses at it with things like 's' or 'p' or whatever.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

It seems an excellent idea.   God invented printf() and it was extremely good.

 

It is straightforward to format your output nicely for humans.    You either retarget directly to printf or use sprintf() and a string output function like lcd_puts().

 

Using the C++ facilities of Arduino is really unpleasant.    Most LCD libraries do not provide formatting.

Anyway,  Codevision is a C Compiler.    So printf/sprintf is the natural choice.

 

David.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

david.prentice wrote:
It is straightforward to format your output nicely for humans.  

But you do have to RTFM and understand the formatting options and how to choose & specify them.

 

They should all be in your 'C' textbook.

 

Here's a quick reference: http://www.cplusplus.com/reference/cstdio/printf/

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 1
char str[5]="hello";

This is not right, you need one extra char to hold the string termination /0. It's better to let the compiler decide the size:

char str[]="hello";

like you did in the first block of code.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

david.prentice wrote:
lcd_init(16); //initialise the LCD

+1.  It looks like Codevision.  Does alcd do an automatic init?  If so, how is the screen width determined.

 

Given that, character LCDs take a lot longer to do post-reset processing than an AVR.  So I always have a delay after setting port directions before attempting init.

 

alcd knows what pins are used?  Is this part of Wizard setup?

 

 

You can put lipstick on a pig, but it is still a pig.

I've never met a pig I didn't like, as long as you have some salt and pepper.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

When you configure any peripheral in the GUI, macros are generated.
So when the library is compiled, it uses these macros to see which port pins to wiggle.
.
"alcd.h" has been available for years.
"lcd.h" required you to put inline .equ statements in your C source code.
.
David.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

david.prentice wrote:
When you configure any peripheral in the GUI, macros are generated.

I see that in the docs.  [as you can tell I'm not an alcd user]  However, I see no notes in the Help about any "automatic" lcd_init() or equivalent.  So something still doesn't make sense to me as there are no lcd_init() in OP's code -- so how did he get anything?!?

You can put lipstick on a pig, but it is still a pig.

I've never met a pig I didn't like, as long as you have some salt and pepper.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

She would not get anything. Just black squares. There is no secret init.
.
I suggested that she had omitted the init function.
Of course, without complete code this is just speculation.
.
But otherwise the OP's program structure is fine. I suspect that she is an experienced PC programmer. e.g. only necessary headers were included.
.
It takes a little while to get used to Embedded programming.
.
David.

Last Edited: Mon. Sep 18, 2017 - 07:21 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

david.prentice wrote:
I suggested that she had omitted the init function. Of course, without complete code this is just speculation.
Mohsen.j wrote:
also i used:
Mohsen.j wrote:
and it works!

Thus my confusion.  As you said, we aren't seeing the complete code?

 

http://www.avrfreaks.net/comment...

theusch wrote:
Post a complete test program that demonstrates the symptoms. Tell AVR model, and clock speed. Tell language, toolchain, version, and optimization settings. Tell what you expect to happen, and tell what >>is<< happening.

You can put lipstick on a pig, but it is still a pig.

I've never met a pig I didn't like, as long as you have some salt and pepper.