Home »
Embedded Systems
AVR | Print HELLO WORLD on the 16x2 LCD Display
In this tutorial, we will learn a simple program, about how to print HELLO WORLD on the LCD. As this is our first LCD program, I would try to explain you every step in a more detailed way.
By Suryaveer Singh Last updated : May 12, 2023
AVR | Print HELLO WORLD on the 16x2 LCD Display
We would learn the connection to the LCD first as the connections is a bit complex and here we are using an 8-bit LCD.
Simulation
Simulation Explanation
-
Search these devices from the device selection menu:
- ATmega16
- LM016L (It’s a 16*2 Alphanumeric LCD)
- One Power Terminal
- One Ground terminal
- Connect the power terminal from the VDD and VEE of the LCD.
- Connect the Enable pin (E) and VSS pin to the ground terminal.
- Double click on the Power terminal and write +5V in its properties.
- Connect the R/S and E pin to the PC0 and PC1 of the ATmega16.
- The next step would be to debug the HEX file in the ATmega16.
Now after all the connections are made we will move forward to the coding section. As the coding for the LCD is bit longer so I won’t be attaching the screenshots from the Atmel Studio.
C Program to Print HELLO WORLD on 6x2 LCD Display using AVR
#include <avr/io.h>
#define F_CPU 1000000
#include <util/delay.h>
#define RS0
#define EN1
void lcd_comm (char);
void lcd_data(char);
void lcd_init (void);
int main(void)
{
lcd_init();
lcd_data('H');
lcd_data('E');
lcd_data('L');
lcd_data('L');
lcd_data('O');
lcd_comm(20);
lcd_data('W');
lcd_data('O');
lcd_data('R');
lcd_data('L');
lcd_data('D');
while(1)
{
}
}
void lcd_comm(char x){
PORTD = x;
PORTC &= ~(1<<RS);
PORTC |= 1<<EN;
_delay_ms(5);
PORTC &= ~(1<<EN);
}
void lcd_data(char x){
PORTD = x;
PORTC |= 1<<RS;
PORTC |= 1<<EN;
_delay_ms(5);
PORTC &= ~(1<<EN);
}
void lcd_init(void){
DDRD = 0xFF;
DDRC = 0x03;
lcd_comm(0x38);
lcd_comm(0x06);
lcd_comm(0x0E);
lcd_comm(0x01);
lcd_comm(0x80);
}
Explanation
- Firstly we have included all the header file that is required basically
- At the initial condition, we have defined EN=0 and RS=1.
- Next we have defined certain functions lcd_comm(char), lcd_data(char) and lcd_init(void).
- Inside the int main(void) we have written the alphabets that need to be print in the screen.
- Also here lcd_comm(20); is the command given to the LCD to create space between the two words.
- Inside the void lcd_comm(char x) we have taken the variable as char x, which we have assigned to PORTC.
- In the next step we have masked the initial value of RS which was initially 1, and here we have made it 0.
- Next, we have made our Enable Pin high and then low by giving the time delay of 5ms in between.
- Again for the next function, we would be giving the data to LCD through this.
- We have taken a variable x, and assigned to PORTD, again made RS pin 0 and also have done similarly the Enable pin high and then low by providing the time delay of 5ms.
- In this function lcd_init(void) we have written all the commands that are required for the LCD at the beginning.
- For more detail of every command, you can check the last article that I have written about the LCD.
- Also, the DDRD=0xFF indicates all the data pins connected to the PORTD, and DDRC=0x03 is for the connection of the ENABLE Pin and R/S pin we connected to PORTC.
Build the program and after this debug the Hex file in the simulation software design that we have created earlier and click on the RUN button and your HELLO WORLD will appear in the Screen.