Electronics – Tutorials, Tips and more …

If we hear, we forget; if we see, we remember; if we do, we understand – Confucius

CodeVisionAVR Tip: How to display formatted numbers on LCD

Posted by Omkar on July 14, 2009

Generally at some point of time we all need to display something more than “hello world” onto LCD. CodeVisionAVR(cvavr) provides functions like lcd_putsf() and lcd_puts() for displaying strings on LCD. However these function cannot be used directly to display something like : “Vin = 10V” or may be “Value = 0000A01F”. In short these functions do not provide the functionality of formatted output, as provided by printf() function. How to implement this functionality is objective of this post. Fortunately there is very simple way to get this thing done …

Understanding lcd_puts() function:

This is standard library function provided by cvavr for printing string stored in RAM on LCD display. Syntax of this function is :

void lcd_puts(char *str)

str is the pointer to the buffer containing null terminated string. (All strings in C are always NULL terminated). This function will print everything contained in the given string buffer. So if we can somehow get the formatted string ready made, we can simply pass it to this function and get things printed on LCD.

The sprintf() function:

This is standard C library function. Its prototype is provided in stdio.h header file. When I say standard C library function, then it means that functionality of this function will be provided by ANY C compiler (supporting standard C library) existing in the world. It can be WinAVR, IAR C compiler, Imagecraft C compiler, RealView compiler for ARM, TI’s C compiler for DSPs, etc. So you can use the technique described here for any C compiler with suitable modifications.

Syntax of sprintf() function is as follows :

void sprintf(char *str, char flash *fmtstr [ , arg1, arg2, …])

This function will place the formatted text into given string buffer (str). So when you write :

i = 10;
sprintf(strbuff,"Value of i = %d (dec) or %X (hex)",i,i);

Then after executing this statement strbuff will contain following string:

“Value of i = 10 (dec) or A (hex)”

Now you can print this string on LCD by passing strbuff to lcd_puts() function :


Thats it !!

You should refer documentation of the cvavr for knowing the formatting features provided by sprintf() function. Since embedded systems have limited resources, cvavr provides different implementations of this function. In these implementations certain formatting features have been removed to reduce program size. To change the implementation of sprintf(), go to Menu Project>Configure and select tab C Compiler. In this tab look at the (s)printf features drop down list and select the desired functionality. By deafult this setting will be set to int, width. For this implementation, following conversion type characters are supported: ‘c’, ‘s’, ‘p’, ‘i’, ‘d’, ‘u’, ‘x’, ‘X’, ‘%’, no width or precision specifiers are supported, only the ‘+’ and ‘ ‘ flags are supported, no input size modifiers are supported.

For more details on other options, refer cvavr help. The more features are selected, larger is the code size generated for the printf and sprintf functions.

- Remember to choose size of string buffer array (i.e. char *str) appropriately. If the size is less than expected final formatted string, sprintf() function will continue to write sequentially in memory, till all characters are written. This will result in application crash or will corrupt values of other variables in SRAM.

- Generally LCDs can accommodate very small number of characters. So make sure that you have enough space to print the desired string on LCD. If there is no sufficient space left for printing further, then clear the LCD using lcd_clear() function.


To print formatted output on LCD, use the code similar to the one shown below :

char strbuff[20];
sprintf(strbuff, “Value of x = %d”, x);

About these ads

24 Responses to “CodeVisionAVR Tip: How to display formatted numbers on LCD”

  1. 0867532 said

    i hope you can help me.
    look i have 3 varibles & char:
    char time[12];
    int sec = 0;
    int min = 0;
    int hrs = 0;


    while (1) {
    . . .
    sprintf(time, ” %d : %d : %d”,hrs,min,sec);
    but controller just restarts every sec.
    if i print only sec – it works.:(
    as you so cleaver, help plz.


    • Omkar said

      Looks like it could be buffer overflow problem. Increase the size of time[] array to 15 or more. If you consider the maximum number “12 : 59 : 59 ” It is 13 character string. One more extra will be needed for NULL termination. Total 14 characters. So your array size is not sufficient and thus it is causing overflow and screwing up the execution.


  2. mani said

    hi.. i want to display 2 or 3 digit nos. on LCD.
    I tried your method and it worked fine till i tested it in proteus as a simulation..
    but when i tested it on hardware comprising 8051 and 16×2 LCD it did not display anything.

    After long time i figured out that if if delete the function sprintf() from the code the LCD works fine.

    so the doubt i have is whether the function usage also depends on the LCD being used?

    I have also tried using itoa() function but the compiler shows error even if i include stdlib.h .

    all i want to do is when i press a key on keypad the the microcontroller should count up from 0 to 200 and display it on LCD. As we can send data as characters only to display on LCD and dont know how to display 2 or 3 digit nos. easily.

    Kindly help.


    • Omkar said

      Which compiler are you using ?

      The code should work on real LCD as well. Is your real LCD model and the proteus models are different ? Are you using standard 16×2 LCD in your project ?


  3. dh said

    how to print custom characters on the lcd using code vision avr ?


  4. Maryam said

    any idea why my LCD comes on but does not show any character? Am I missing anything?


  5. […] with the CodeVisionAVR C Compiler CodeVisionAVR User's Manual CodeVisionAVR C Compiler Tutorial CodeVisionAVR Tip: How to display formatted numbers on LCD AVR : Tutorial 1: Introduction to AVR AVR : Tutorial 2 : AVR – Input / Output Mega16 […]


  6. sigit said

    how to clear line lcd.?


  7. santy said

    PLZZZ help me to display float number on LCD is there is %f for that ???
    plz tell me..


    • Omkar said

      u can use %f in codevisionavr. See its help.


    • nishantdawn said

      inlude this heder file

      declare a float
      float f

      declare a char
      char c[10]

      there is an inbuilt commend in cvavr ftoa u can se it like this

      now ur float is converted into a variable string c

      now print it


      • nishantdawn said

        inlude this heder file

        declare a float
        float f

        declare a char
        char c[10]

        there is an inbuilt commend in cvavr ftoa u can se it like this

        now ur float is converted into a variable string c

        now print it


  8. Kuthsav said

    Hi sir,
    Can u please send me the code for finding temperature using ATmega32L and LM35.
    AM using codevisionAVR C compiler.


  9. acceph said

    Hi all, especially to Omkar, Nice article!!

    I am developing a graphic LCD system now, and I use many sprintf() function.
    the format is like this :
    I use this line inside an interupt to update the time periodically.
    I think there is no mistake on syntax or any other thing. BUT, some of my global variable are truncated!! I don’t know the reason, Have you ever meet this kind of problem?

    Because I have check that truncated variable before and after the bold sprintf() line.
    Before is no problem, but after those line, the global variable already truncated, and untill now I still couldn’t fix it, I hope I will find answer here..

    anyway, thank’s for your kindly help


    • Omkar said

      Which compiler you are using ? If you are getting unexpected global variable truncation/overwriting then you should increase the program’s data stack size. Generally compiler uses some default data stack size (e.g. CVAVR uses 256 bytes). If your global variables exceed this space, then they will be overwritten by some other activity in your program. Try increasing data stack size. You will have to refer your compiler’s manuals.


  10. roozbeh said

    hi i need to program lcd for stylize 8 bit with codevision
    because in codevision we only program lcd for stylize 4
    do i must write separate library for that?


  11. jatin said

    hello, its jatin.
    I have done LCD interfacing on Code vision AVR on PORT B.
    Now I want to interface another LCD on the same atmega micro-controller. So how to address both the LCD.? Or if I want to end particular data on ONE LCD and some other data on Second LCD.?


    • Omkar said

      You can employ following scheme :
      PB0 -RS of both LCDs
      PB1 -RD of both LCDs
      PB2 – A input of EXOR gate
      PB3 – B input of EXOR gate, EN of LCD2
      PB4 -D0 of both LCDs
      PB5 -D1 of both LCDs
      PB6 -D2 of both LCDs
      PB7 -D3 of both LCDs

      output of EXOR gate – Connect to EN of LCD1.

      Idea is like this : cvavr controlls the PB2 to enable the LCD when u call LCD functions. Now at this time you keep PB3 ZERO. So that output of EXOR gate follows the PB2 and LCD1 operates in normal fashion. Now whenever you want to write text on LCD2, simply make PB3 high and call usual LCD functions. These functions will make PB2 high, but since PB3 is also HIGH it will force result in ZERO at the output of EXOR gate and hence will disable the LCD1. Once you are done with displaying text on LCD2, again make PB3 ZERO to resume writing on LCD1.

      EXOR : 74HC86 > Quad two input EXOR gate.

      This is just a concept, I have not tested it. But I think it should work without any problem.


  12. nikhil said

    found urs article intresting but can we use it in AVR studio,,,…….if not how it can b done in AVR using AVR studio software..


  13. pranav said

    for the next article can u write about the the encoder-decoder circuit for micro-mouse …… which will help to calculate the turning of the bot…


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Electronics - Tutorials, Tips and more ...

If we hear, we forget; if we see, we remember; if we do, we understand - Confucius

Orange thought

Written sculptures

Mix Bhel for the Soul

(: - xxx Smile Please xxx - :)

Ashish speaks

Just another WordPress.com weblog


Get every new post delivered to your Inbox.

Join 295 other followers

%d bloggers like this: