Tuesday, July 06, 2010

LCD Interfacing

LCD Interfacing with AVR

Now we need to interface an LCD to our microcontroller so that we can display messages, outputs, etc. Sometimes using an LCD becomes almost inevitable for debugging and calibrating the sensors (discussed later). We will use the 16x2 LCD, which means it has two rows of 16 characters each. Hence in total we can display 32 characters.





Cell coordinates : (x,y)

0,0 1,0 2,0 3,0 4,0 5,0 6,0 7,0 8,0 9,0 10,0 11,0 12,0 13,0 14,0 15,0
0,1 1,1 2,1 3,1 4,1 5,1 6,1 7,1 8,1 9,1 10,1 11,1 12,1 13,1 14,1 15,1

avr cvavr lcd connections

Circuit Connection

There are 16 pins in an LCD; See reverse side of the LCD for the PIN configuration.
The connections have to be made as shown in image.


Settings in CVAVR

When we connect an LCD to Atmega16, one full PORT is dedicated to it, denoted by PORT-X in the figure. To enable LCD interfacing in the microcontroller, just click on the LCD tab in the Code Wizard and select the PORT at which you want to connect the LCD. We will select PORTC. Also select the number of characters per line in your LCD. This is 16 in our case. Code Wizard now shows you the complete list of connections which you will have to make in order to interface the LCD. These are nothing but the same as in the above figure for general PORT-X.

cvavr avr lcd


As you can see, there are some special connections other than those to uC, Vcc and gnd. These are general LCD settings. Pin 3 (VO) is for the LCD contrast, ground it through a <1kω>

Printing Functions

Now once the connections have been made, we are ready to display something on our screen. Displaying our name would be great to start with. Some of the general LCD functions which you must know are:

lcd_clear()

Clears the lcd. Remember! Call this function before the while(1) loop, otherwise you won’t be able to see anything!

lcd_gotoxy(x,y)

Place the cursor at coordinates (x,y) and start writing from there. The first coordinate is (0,0). Hence, x ranges from 0 to 15 and y from 0 to 1 in our LCD. Suppose you want to display something starting from the 5th character in second line, then the function would be

lcd_gotoxy(5,1);

lcd_putchar(char c)

To display a single character.

Ex,
lcd_putchar(‘H’);

lcd_putsf(constant string)

To display a constant string.

Ex,

lcd_putsf(“IIT Kanpur”);

lcd_puts(char_array)

To display a variable string, which is nothing but an array of characters (data type char) in C language. e.g., You have an array char c[10] which keeps on changing. Then to display it, the function would be called as

lcd_puts(c);

Now we have seen that only characters or strings (constant or variable) can be displayed on the LCD. But quite often we have to display values of numeric variables, which is not possible directly. Hence we need to first convert that numeric value to a string and then display it. For e.g., if we have a variable of type integer, say int k, and we need to display the value of k (which changes every now and then, 200 now and 250 after a second... and so on). For this, we use the C functions itoa() and ftoa(), but remember to include the header file stdlib.h to use these C functions.

itoa(int val, char arr[])

It stores the value of integer val in the character array arr. E.g., we have already defined

int i and char c[20], then

itoa(i,c);
lcd_puts(c);

Similarly we have

ftoa(float val, char decimal_places, char arr[])

It stores the value of floating variable f in the character array arr with the number of decimal places as specified by second parameter. E.g., we have already defined float f and char c[20], then

ftoa(f,4,c); // till 4 decimal places
lcd_puts(c);

Now we are ready to display anything we want on our LCD. Just try out something which you would like to see glowing on it ! I know you are going to print your name first :-)

No comments:

Post a Comment