Thursday, May 12, 2011

CodeVisionAVR Tip: How to display formatted numbers on LCD

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 :
lcd_puts(strbuff);

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.

NOTE:
- 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.




Summary:

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);
lcd_puts(strbuff);
—–
—–

No comments:

Post a Comment

Terima kasih atas komentar yang anda sampaikan , sehingga dapat menambah wawasan saya sebagai penulis dan membuat blog ini semakin berguna banyak orang