#include <avr/io.h>
#include <util/delay.h>
#define FOSC 16000000
#define LCD_PRT PORTD //PD4-PD7
#define LCD_DDR DDRD
#define LCD_PIN PIND
#define LCD_RS 0
#define LCD_RW 1
#define LCD_EN 2
void delay_us(int d)
{
_delay_us(d);
}
void delay_ms(int d)
{
_delay_ms(d);
}
void lcdCommand( unsigned char cmnd)
{
LCD_PRT = (LCD_PRT & 0x0f) | (cmnd & 0xf0); //send high nibbles
LCD_PRT &= ~ (1<<LCD_RS);
LCD_PRT &= ~ (1<<LCD_RW);
LCD_PRT |= (1<<LCD_EN);
delay_us(1);
LCD_PRT &= ~(1<<LCD_EN);
delay_us(20);
LCD_PRT = (LCD_PRT & 0x0f) | (cmnd <<4); //send low nibbles
LCD_PRT |= (1<<LCD_EN);
delay_us(1);
LCD_PRT &= ~(1<<LCD_EN);
}
void lcdData( unsigned char data)
{
LCD_PRT = (LCD_PRT & 0x0f) | (data & 0xf0); //send high nibbles
LCD_PRT |= (1<<LCD_RS);
LCD_PRT &= ~ (1<<LCD_RW);
LCD_PRT |= (1<<LCD_EN);
delay_us(1);
LCD_PRT &= ~(1<<LCD_EN);
LCD_PRT = (LCD_PRT & 0x0f) | (data <<4); //send low nibbles
LCD_PRT |= (1<<LCD_EN);
delay_us(1);
LCD_PRT &= ~(1<<LCD_EN);
}
void lcd_init(void)
{
LCD_DDR=0xff;
LCD_PRT &=~(1<<LCD_EN);
delay_us(2000);
lcdCommand(0x28);
delay_us(100);
lcdCommand(0x0c);
delay_us(100);
lcdCommand(0x01);
delay_us(2000);
lcdCommand(0x06);
delay_us(100);
}
void lcd_gotoxy(unsigned char x, unsigned char y)
{
unsigned char firstCharAdr[] = {0x80, 0xC0, 0x90, 0xD0};//16x4 Lcd display addresses
lcdCommand (firstCharAdr[y-1] + x-1);
delay_us(100);
}
void lcd_print (char * str)
{
unsigned char i = 0;
while ( str[i] !=0)
{
lcdData(str[i]);
i++;
}
}
int main (void)
{
lcd_init();
lcd_gotoxy(1,1);
lcd_print ("Welcome");
lcd_gotoxy(1,2);
lcd_print ("to");
lcd_gotoxy(1,3);
lcd_print ("the");
lcd_gotoxy(1,4);
lcd_print ("world");
while(1);
return 0;
}