Using an LCD with the ATmega8 microcontroller in 8-bit mode involves connecting the 8 data lines of the LCD to 8 GPIO pins of the ATmega8.
This mode allows for straightforward communication between the microcontroller and the LCD but requires more I/O pins.
Ensure that both the ATmega8 and the LCD are powered with a stable 5V supply.
Connect a potentiometer to V0 for contrast control.
Initialize the LCD in 8-bit mode by sending specific commands to the LCD. This includes setting the display size, font, and the entry mode.
Set the DDR (Data Direction Register) of the port connected to the data pins of the LCD as output.
Create functions to send commands and data to the LCD.
Use the RS pin to select whether you are sending a command (RS=0) or data (RS=1).
Toggle the E pin to signal the LCD to accept the data or command.
You can create custom characters by writing to the CGRAM (Character Generator RAM) of the LCD.
Write text to the DDRAM (Display Data RAM) of the LCD to display it on the screen.
#include <avr/io.h>
#include <util/delay.h>
#define LCD_DATA_PORT PORTD
#define LCD_DATA_DDR DDRD
#define LCD_CONTROL_PORT PORTB
#define LCD_CONTROL_DDR DDRB
#define RS PB0
#define EN PB1
void LCD_Command(unsigned char cmd) {
LCD_DATA_PORT = cmd;
LCD_CONTROL_PORT &= ~(1 << RS); // RS = 0 for command
LCD_CONTROL_PORT |= (1 << EN); // EN = 1 for H-to-L pulse
_delay_ms(1);
LCD_CONTROL_PORT &= ~(1 << EN); // EN = 0 to complete the H-to-L pulse
_delay_ms(3);
}
void LCD_Char(unsigned char char_data) {
LCD_DATA_PORT = char_data;
LCD_CONTROL_PORT |= (1 << RS); // RS = 1 for data
LCD_CONTROL_PORT |= (1 << EN); // EN = 1 for H-to-L pulse
_delay_ms(1);
LCD_CONTROL_PORT &= ~(1 << EN); // EN = 0 to complete the H-to-L pulse
_delay_ms(1);
}
void LCD_Init() {
LCD_DATA_DDR = 0xFF; // Make PORTD as output
LCD_CONTROL_DDR |= (1 << RS) | (1 << EN); // Make RS and EN as output
_delay_ms(20); // LCD Power ON delay
// Initialization sequence
LCD_Command(0x38); // Function set: 8-bit, 2 Line, 5x7 Dots
LCD_Command(0x0E); // Display on, Cursor blinking
LCD_Command(0x01); // Clear display
LCD_Command(0x06); // Entry mode, auto increment with no shift
}
void LCD_String(char *str) {
int i;
for(i=0; str[i]!=0; i++) {
LCD_Char(str[i]);
}
}
void LCD_String_xy(char row, char pos, char *str) {
if (row == 0 && pos < 16) {
LCD_Command((pos & 0x0F) | 0x80); // Command for 1st row and required position
} else if (row == 1 && pos < 16) {
LCD_Command((pos & 0x0F) | 0xC0); // Command for 2nd row and required position
}
LCD_String(str); // Print string
}
int main(void) {
LCD_Init(); // Initialize LCD
LCD_String_xy(0, 0, "Hello World!"); // Print string on 1st row
LCD_String_xy(1, 0, "ProgPtr.com"); // Print string on 2nd row
while(1) {
// Your application code
}
}
This code initializes the LCD in 8-bit mode and defines functions to send commands and data to the LCD.
It then displays the string "Hello, World!" on the first line and "ATmega8" on the second line of the LCD.
The LCD_String_xy function allows you to specify the row and position where the string starts.
Remember, you may need to adjust the delay times depending on your specific LCD's speed and the clock speed of your ATmega8.
Interfacing an LCD in 8-bit mode with ATmega8 involves setting up a circuit with a parallel data connection and writing a program to control the LCD. This mode is straightforward in terms of the programming logic but uses more I/O pins of the microcontroller. This setup is ideal for applications where the display of text or custom characters is required and sufficient I/O pins are available.
Search