Senin, 20 Agustus 2012

Displaying variables on LCD

variables on lcd
LCD is very important when you use micro-controllers.It helps you to know what is happening inside micro-controller.When you use analog sensors such as temperature or humidity sensors and you want to display their values.The value is not constant and keep changing by time (variable).Then how you are going to display this value.

In this simple tutorial  we are going to display variables on LCD using ATmega16 and avr codevision compiler.

The idea is to convert the variable into a string and to do so there is a library called stdlib.h ,which must be included into your program, used to convert a variable of any type to string.This library containing many functions but we are going to explain functions used to display variables :

1- itoa(int n,char *str) :
converts the integer n to characters in string str. i.e. this function convert the int variable n (variable you want to display) to a string and this is an example on how to use it  :
/* 1st you have to define an array of character whose number of elements is max number of digits you are going to display on the lcd i.e. if you are going to display a variable whose max value is 9999 i.e 4 digits then the array will have 4 elements only */
char buffer [4];
/* then convert your variable into a string and each digit in the string is stored in array buffer */
itoa(n,buffer);
// then display your variable using lcd_puts function :
lcd_puts(buffer);

2- ltoa(long int n, char *str) :
converts the long integer n to characters in string str.The same as itoa but for variables with large value ( long ).

3- ftoa(float n, unsigned char decimals, char *str) :
converts the floating point number n to characters in string str. 
The number is represented with a specified number of decimals. 
It's used whe you want to display decimal value where deciamls is the number of digits you want after the deciaml point.
// example :
#include <stdlib.h>
char buffer[8];
x=240.2468;
ftoa(x,4,buffer);     //to display 4 digits after the decimal point
lcd_puts(buffer);


4- ftoe(float n, unsigned char decimals, char *str) :
converts the floating point number n to characters in string str.
The number is represented as a mantissa with a specified number of decimals and an integer power of 10 exponent (e.g. 12.35e-5).
i.e. display your variable in exponential format as shown.
#include <stdlib.h>
char buffer[8];
x=0.0000000245;
ftoe(x,2,buffer);
lcd_puts(buffer);


Download this tutorial in PDF HERE

0 komentar:

Posting Komentar