Circuit Diagram of Interfacing 16X2 LCD to AVR Microcontroller:
Circuit Explanation:
- Well this is not different from the way interfacing the LCD to 8051 or PIC microcontroller. The crystal oscillator will provide the clock to the microcontroller. The capacitors connected to the crystal will act as filters and help the crystal to resonate and oscillates to its parallel resonate mode.
- The potentiometer which is connected to the pin 3 and pin2 of LCD will help to adjust the contrast of the LCD. The 4, 5 and 6 pins of LCD that is Register select, Read/write and Enable pins are connected to the PD0, PD1 and PD2 pins of Atmega16. The data pins of LCD are connected to the pins of 33 to 40 pins of Atmega16.
Programming ATMEGA16 for Interfacing with 16X2 LCD:
It very important how the data is send
to the LCD and how the command is send to the LCD, suppose if you are
sending data to the LCD, then you have to make the ENABLE pin of 16×2
LCD pin to low before sending the data, when you think the data you want
send is ready make the ENABLE pin again high that is 1 in coding
language. If you make ENABLE pin high then only LCD will work.
Just by making the ENABLE pin high will
not work, you have make REGISTER SELECT pin (RS pin) also high so that
LCD will accept that it a normal data which has to be displayed on the
screen of LCD, if you forgot to make RS pin high it eventually think
that user is sending it a command and make it self ready to act
according to the command like making cursor to move, clearing the data
on the LCD, changing the cursor position etc.
Last but not least another pin you need
to worry of read/write pin, we all know that for any device the basic
functionality start with read and write, reading the data and writing
the data is main and important function for any peripheral or system.
here in the LCD while sending the data for displaying you have to make
the R/W pin low, so that LCD will under stand that data should be
written on the LCD screen and act accordingly.
Just sending the data and displaying it
will not complete the task; arrangement of data in understandable way is
the important and crucial task for the programmer. You can arrange the
data in the LCD or making the LCD to work according to your wish, can be
done by sending the commands or special functions to the LCD, you may
think that what type of commands are needed to work for LCD, commands
for cursor position, increasing or decreasing the contrast, making the
cursor to change line like from first line to second line etc. To send a
command to the LCD you need to make pins high and low just like sending
the data. For sending the command you need to make the ENABLE PIN high,
REGISTER SELECT pin (RS pin) low that is 0 in programmer terms, and
read/write pin (R/W pin) high, you need to remember this configuration
for sending the command.
Different commands and there hexadecimal code generally used by the programmer while displaying the data.
If you want to display content in one line in 5x7 matrix | ||
If you want to display content in two line in 5x7 matrix | ||
If you display 4 bit data in one line in 5x7 matrix | ||
If you display 4 bit data in two line in 5x7 matrix | ||
entry mode | ||
To clear the display without clearing the ram content | ||
Making the cursor on and also display on | ||
Making the cursor off and also display off | ||
Displaying the data on cursor blinking | ||
Shifting complete display data to left side | ||
Shifting complete display data to right side | ||
Moving cursor to one place or one character left | ||
Moving cursor to one place or one character RIGHT | ||
Clearing the complete display including RAM DATA | ||
Set DDRAM address on cursor position |
If we want to talk in brief for displaying data in LCD
- E=1; enable pin should be high
- RS=1; Register select should be high
- R/W=0; Read/Write pin should be low.
For sending command to LCD
- E=1; enable pin should be high
- RS=0; Register select should be low
- R/W=1; Read/Write pin should be high.
When
you are passing a string, its better use a string pointer and increment
the pointer, if you are incrementing a pointer it will automatically go
the next address of the variable in which you can store your character
which you wanted to display. See the below example.
void write_string(unsigned char *str) //store address value of the string in pointer *str
{
int i=0;
while(strng[i]!=’\0′) // loop will go on till the NULL character in the string
{
lcd_write(strng[i]);// sending data on LCD byte by byte
i++;
}
return;
}
Code for Interfacing the LCD to ATMEGA16:
#include<avr/io.h>#include<util/delay.h>
LCD DATA port----PORT B
signal port------PORT D
rs-------PD0
rw-------PD1
en-------PD2
*/
#define LCD_DATA PORTB //LCD data port
#define ctrl PORTD
#define en PD2 // enable signal
#define rw PD1 // read/write signal
#define rs PD0 // register select signal
#define ctrl PORTD
#define en PD2 // enable signal
#define rw PD1 // read/write signal
#define rs PD0 // register select signal
void LCD_cmd(unsigned char cmd);
void init_LCD(void);
void LCD_write(unsigned char data);
void init_LCD(void);
void LCD_write(unsigned char data);
int main()
{
DDRB=0xff; // setting the port B
DDRD=0×07; // setting for port D
init_LCD(); // initialization of LCD
_delay_ms(50); // delay of 50 mili seconds
LCD_write_string(“hello world”); // function to print string on LCD
return 0;
}
{
DDRB=0xff; // setting the port B
DDRD=0×07; // setting for port D
init_LCD(); // initialization of LCD
_delay_ms(50); // delay of 50 mili seconds
LCD_write_string(“hello world”); // function to print string on LCD
return 0;
}
void init_LCD(void)
{
LCD_cmd(0×38); // initialization of 16X2 LCD in 8bit mode
_delay_ms(1);
LCD_cmd(0×01); // clear LCD
_delay_ms(1);
LCD_cmd(0x0E); // cursor ON
_delay_ms(1);
LCD_cmd(0×80); // —8 go to first line and –0 is for 0th position
_delay_ms(1);
return;
}
{
LCD_cmd(0×38); // initialization of 16X2 LCD in 8bit mode
_delay_ms(1);
LCD_cmd(0×01); // clear LCD
_delay_ms(1);
LCD_cmd(0x0E); // cursor ON
_delay_ms(1);
LCD_cmd(0×80); // —8 go to first line and –0 is for 0th position
_delay_ms(1);
return;
}
void LCD_cmd(unsigned char cmd)
{
LCD_DATA=cmd;
ctrl =(0<<rs)|(0<<rw)|(1<<en);
_delay_ms(1);
ctrl =(0<<rs)|(0<<rw)|(0<<en);
_delay_ms(50);
return;
}
{
LCD_DATA=cmd;
ctrl =(0<<rs)|(0<<rw)|(1<<en);
_delay_ms(1);
ctrl =(0<<rs)|(0<<rw)|(0<<en);
_delay_ms(50);
return;
}
void LCD_write(unsigned char data)
{
LCD_DATA= data;
ctrl = (1<<rs)|(0<<rw)|(1<<en);
_delay_ms(1);
ctrl = (1<<rs)|(0<<rw)|(0<<en);
_delay_ms(50);
return ;
}
{
LCD_DATA= data;
ctrl = (1<<rs)|(0<<rw)|(1<<en);
_delay_ms(1);
ctrl = (1<<rs)|(0<<rw)|(0<<en);
_delay_ms(50);
return ;
}
void LCD_write_string(unsigned char *str) //store address value of the string in pointer *str
{
int i=0;
while(str[i]!=’\0′) // loop will go on till the NULL character in the string
{
LCD_write(str[i]); // sending data on LCD byte by byte
i++;
}
return;
}
{
int i=0;
while(str[i]!=’\0′) // loop will go on till the NULL character in the string
{
LCD_write(str[i]); // sending data on LCD byte by byte
i++;
}
return;
}
No comments:
Post a Comment