#include<avr/io.h>
#include<util/delay.h>
#define F_CPU 16000000UL
#define LCD_Dir DDRB /* Define LCD data port direction */
#define LCD_Port PORTB /* Define LCD data port */
#define RS PB5 /* Define Register Select pin */
#define EN PB4 /* Define Enable signal pin */
#define out PORTD
void check()
{
out|=1<<7;
_delay_ms(100);
out&=~(1<<7);
_delay_ms(100);
}
void LCD_Command( unsigned char cmnd )
{
check();
_delay_ms(1);
LCD_Port = (LCD_Port & 0xF0) | (cmnd >> 4); /* sending upper nibble */
LCD_Port &= ~ (1<<RS); /* RS=0, command reg. */
LCD_Port |= (1<<EN); /* Enable pulse */
_delay_us(1);
LCD_Port &= ~ (1<<EN);
_delay_ms(200);
LCD_Port = (LCD_Port & 0xF0) | (cmnd & 0x0F); /* sending lower nibble */
LCD_Port |= (1<<EN);
_delay_us(1);
LCD_Port &= ~ (1<<EN);
_delay_ms(2);
}
void LCD_Char( unsigned char cmnd )
{
check();
_delay_ms(1);
LCD_Port = (LCD_Port & 0xF0) | (cmnd >> 4); /* sending upper nibble */
LCD_Port |= (1<<RS); /* RS=1, data reg. */
LCD_Port|= (1<<EN);
_delay_us(1);
LCD_Port &= ~ (1<<EN);
_delay_us(200);
LCD_Port = (LCD_Port & 0xF0) | (cmnd & 0x0F); /* sending lower nibble */
LCD_Port |= (1<<EN);
_delay_us(1);
LCD_Port &= ~ (1<<EN);
_delay_ms(2);
}
void LCD_Init (void) /* LCD Initialize function */
{
LCD_Dir = 0xFF; /* Make LCD port direction as o/p */
_delay_ms(20); /* LCD Power ON delay always >15ms */
LCD_Command(0x02); /* send for 4 bit initialization of LCD */
_delay_ms(5);
LCD_Command(0x28); /* 2 line, 5*7 matrix in 4-bit mode */
_delay_ms(5);
LCD_Command(0x0c); /* Display on cursor off*/
_delay_ms(5);
_delay_ms(5);
LCD_Command(0x06); /* Increment cursor (shift cursor to right)*/
_delay_ms(1);
LCD_Command(0x0E); // Display OFF, Cursor ON
_delay_ms(5);
LCD_Command(0x83);
LCD_Command(0x01); /* Clear display screen*/
_delay_ms(20);
_delay_ms(2);
}
void LCD_String_xy (char row, char pos, char *str) /* Send string to LCD with xy position */
{
if (row == 0 && pos<16)
LCD_Command((pos & 0x0F)|0x80); /* Command of first row and required position<16 */
else if (row == 1 && pos<16)
LCD_Command((pos & 0x0F)|0xC0); /* Command of first row and required position<16 */
LCD_String(str); /* Call LCD string function */
}
void LCD_String (char *str) /* Send string to LCD function */
{
_delay_ms(1);
int i;
for(i=0;str[i]!=0;i++) /* Send each char of string till the NULL */
{
_delay_ms(1);
LCD_Char (str[i]);
}
}
int main()
{
DDRD=0X80;
PORTD=0x00;
LCD_Init(); /* Initialization of LCD*/
_delay_ms(1);
PORTB=0X00;
while(1)
{
LCD_Char('h');
LCD_String("ElectronicWINGS"); /* Write string on 1st line of LCD*/
_delay_ms(1);
LCD_Command(0xC0); /* Go to 2nd line*/
_delay_ms(1);
LCD_String("Hello World"); /* Write string on 2nd line*/
// while(1);
check();
}
return 0;
}