Friday, 3 August 2012

LCD (2x16) interface to Atmega 16

In this tutorial we are going to interface a 2x16 LCD to Atmega 16 microcontroller and simulate it using proteus software and explain how to display variables on LCD in an easy way.
The editor we are going to use is codevision avr compiler.



1-open the editor and select the wizard as shown in the following picture

 2- select the chip tab and choose your microcontroller here we are using Atmega 16 so we select it , then select the frequecny for your chip in this example ( 8 Mhz)
3-then select LCD tab and choose the PORT you want to connect  LCD to (example : PORTC).
   then choose the type of your LCD where 16 is the LCD 2x16 character
4- now you have finished initialization of your code, to see your code select file > generate,save and exit.
    then save your file (the program will ask you to save three files save them in the same folder with the same name) . after you save your files your code will be displayed .
( NOTE : the underlined word determines which port the lcd is connected to and since we choose PORTC , the number 0x15 is written where 0x15 is the address of PORTC in the I/O registers of Atmega 16 e.g. if want PORTA >> 0x1B , PORTB >> 0x18 , PORTD >> 0x12 )
5- at the end of the code you will find while (1) where we place the main program of the microcontroller. Now we have to give a look at the lcd functions.

LCD Functions

1- lcd_clear( ) :

clears the LCD and sets the printing character position at row 0 and column 0. (char position will be explained in the next function )

2- lcd_gotoxy(x, y)

sets the current display position at column x and row y. The row and column numbering starts
from 0. 
for example : lcd_gotoxy(11,1) will set the pointer at the location arrowed in the following figure i.e. the next character will be displaye at row #1 and column #11

3- lcd_putchar(c)

displays the character c at the current display position.(used to display variables)

4- lcd_puts(char *str)

displays at the current display position the string str, located in RAM.

5- lcd_putsf(char flash *str)

displays at the current display position the string str, located in FLASH.(used to display strings i.e. words)
for examlpe : lcd_putsf( "Hello World!!!") >> the string Hello World!!! will be displayed as it's i.e. any thing between double qoutes will be displayed as it's.


As shown in the following figure :

 Displaying Variable

for more details on how to display variable on lcd click here
assume you are displaying the temperature of a room at some time it's 30 Celsius and later it's 28 Celsius how can you display the varying temperature .

Here we go :

assume the variable you want display has a max value 999 i.e. it's three digits and stored in the variable x.
define three variables(char) : h,t,u     >>  where h stands for hubdreds , t for tenth and u for units.

Then :
h=x/100;             // assume x=245     >>>>> 245/100=2
lcd_putchar(h+0x30);     // to display 2 you must send it's ASCII to the LCD which is 0x32
t=(x-h*100)/10;              //245-200=45   >>>> 45/10=4;
lcd_putchar(t+0x30);       // send 0x34
u=(x-h*100-t*10);              //245-200=45   >>>45-40=5  
lcd_putchar(u+0x30);         // send 0x35
Here is a simple program where there are 8 switches connected to PORTA represent our variable which can take a value from 0 to 255 and we display this value on the LCD.
Download HERE  
Download this tutorial in PDF HERE

Do you like this post? Please link back to this article by copying one of the codes below.

URL: HTML link code: BB (forum) link code:

1 comment:

  1. Thanx for ur post nd ur article, today I just tell u about a biggest retailar in electronics, ya @Sargam Electronics is the biggest Retail Chain in Electronics in Delhi. Sargam deals with big brands like Samsung, Sony, Lg, Toshiba, Sansui, Hitachi, Voltas, Godrej, Azure, Nikon, Whirlpool, Dell, HP, Acer and many more brands. @Sargam Electronics offers you to buy many electronics products like laptop,mobile,camera,washing machine,refrigerator,air conditionar and multiple electronics products at huge discount.