#define F_CPU 8000000UL
#include <avr/io.h> /* Include AVR std. library file */
#include <util/delay.h> /* Include inbuilt defined Delay header file */
#define RS PB3 /* Define Register Select (data reg./command reg.) signal pin */
#define EN PB2 /* Define Enable signal pin */
void lcd_sending_command( unsigned char cmnd )
{
PORTB = (cmnd & 0xF0); /* sending upper nibble */
PORTB &= ~ (1<<RS); /* RS=0, command reg. */
PORTB |= (1<<EN); /* Enable pulse */
_delay_us(1);
PORTB &= ~ (1<<EN);
PORTB = (cmnd << 4); /* sending lower nibble */
PORTB |= (1<<EN);
_delay_us(1);
PORTB &= ~ (1<<EN);
_delay_ms(2);
}
void lcd_sending_data( unsigned char data )
{
PORTB = (data & 0xF0); /* sending upper nibble */
PORTB |= (1<<RS); /* RS=1, data reg. */
PORTB|= (1<<EN);
_delay_us(1);
PORTB &= ~ (1<<EN);
PORTB = (data << 4); /* sending lower nibble */
PORTB |= (1<<RS);
PORTB |= (1<<EN);
_delay_us(1);
PORTB &= ~ (1<<EN);
_delay_ms(2);
}
void lcd_initialization (void) /* LCD Initialize function */
{
DDRB = 0xFF; /* Make LCD command port direction as o/p */
_delay_ms(20); /* LCD Power ON delay always >15ms */
lcd_sending_command(0x33); /* Ensures display is in 8-bit mode */
lcd_sending_command(0x32); /* send for 4 bit initialization of LCD */
lcd_sending_command(0x28); /* Use 2 line and initialize 5*7 matrix in (4-bit mode)*/
lcd_sending_command(0x0f); /* Display on, cursor on and blink cursor*/
lcd_sending_command(0x01); /* Clear display screen*/
lcd_sending_command(0x06); /* Clear display screen*/
lcd_sending_command(0x02); /* Clear display screen*/
_delay_ms(2);
}
void lcd_display_string (char *str) /* Send string to LCD function */
{
int i;
for(i=0;str[i]!=0;i++) /* Send each char of string till the NULL */
{
lcd_sending_data (str[i]);
}
}
int main()
{
int i;
lcd_initialization(); /* Initialization of LCD*/
lcd_display_string("ECE2022"); /* Write string on 1st line of LCD*/
lcd_sending_command(0xc0); /* Go to 2nd line*/
lcd_display_string("4-bit, by Louis WY LIU!");
while(1) {
for(i=0; i<16; i++) {_delay_ms(500); lcd_sending_command(0x18);}
for(i=0; i<16; i++) {_delay_ms(500); lcd_sending_command(0x1c);}
}
}