void port_init();
void LCD_init();
void out_ctrl_inst();
void lcd_ctrl(char);
void out_data(char);
void LCD_write(char*);
void write_data(char);
void set_cursor(char, char);
void clear_display();
void setup()
{
port_init();
LCD_init();
clear_display();
// Set cursor to 5th position (0-indexed) on the first row and write "Welcome"
set_cursor(0, 5);
LCD_write("Thennal");
clear_display();
// Set cursor to 1st position (0-indexed) on the second row and write "To ECEN Academy"
set_cursor(1, 1);
LCD_write("Welcome's You!");
}
void loop()
{
// Empty loop
}
void port_init()
{
volatile char* LCD_port = (volatile char*)0x30;
*LCD_port = 0xFF; // Set LCD data port as output
volatile char* LCD_CTRL_port = (volatile char*)0x107;
*LCD_CTRL_port = 0x03; // Set LCD control port as output
}
void LCD_init()
{
out_data(0x38); // 8-bit 2-line 5x8 dot matrix
out_ctrl_inst();
out_data(0x0F); // Turn on display and cursor
out_ctrl_inst();
out_data(0x06); // Cursor shift right
out_ctrl_inst();
// Add a delay after initialization to allow the LCD to stabilize
delay(50);
}
void out_ctrl_inst()
{
lcd_ctrl(0x01); // En-1, RS-0 (Instruction)
delay(10);
lcd_ctrl(0x00); // En-0
delay(10);
}
void lcd_ctrl(char cmd)
{
volatile char* LCD_CTRL_CMD = (volatile char*)0x108;
*LCD_CTRL_CMD = cmd;
}
void out_data(char data)
{
volatile char* LCD_DATA = (volatile char*)0x31;
*LCD_DATA = data;
}
void LCD_write(char* ptr)
{
while (*ptr != 0)
{
write_data(*ptr); // Write each character to the LCD
ptr++;
delay(10); // Add a slight delay between each character
}
}
void write_data(char data)
{
out_data(data); // Send data to the LCD
lcd_ctrl(0x02); // RS = 1 (data mode), RW = 0
delay(10);
lcd_ctrl(0x03); // Enable = 1
delay(10);
lcd_ctrl(0x02); // Enable = 0
delay(10);
}
void set_cursor(char row, char column)
{
// Base addresses for the first and second lines of the LCD
char address;
if (row == 0)
address = 0x80 + column; // First line starts at 0x80
else if (row == 1)
address = 0xC0 + column; // Second line starts at 0xC0
out_data(address); // Send the calculated address
out_ctrl_inst();
// Add a delay to ensure cursor is set before writing data
delay(10);
}
void clear_display()
{
out_data(0x01); // Clear display
out_ctrl_inst();
}