/*#define D4 PORTD0
#define D5 PORTD1
#define D6 PORTD2
#define D7 PORTD3*/
#define F_CPU 8000000UL //Define CPU Frequency e.g. here 8MHz
#include <avr/io.h> //Include AVR std. library file
#include <util/delay.h> // Include Delay header file
#define LCD_Dir DDRB //Define LCD data port direction
#define LCD_Port PORTB // Define LCD data port
#define RS PB0 /* Define Register Select pin */
#define EN PB1 /* Define Enable signal pin */
void LCD_Command( unsigned char cmnd )
{
LCD_Port = (LCD_Port & 0x0F) | (cmnd & 0xF0); /* Sending upper nibble of the command*/
LCD_Port &= ~ (1<<RS); /* RS=0, command reg. */
LCD_Port |= (1<<EN); /* Enable pulse */
_delay_us(1);
LCD_Port &= ~ (1<<EN);
_delay_us(200);
LCD_Port = (LCD_Port & 0x0F) | (cmnd << 4); /* Sending lower nibble of the cpmmand*/
LCD_Port |= (1<<EN);
_delay_us(1);
LCD_Port &= ~ (1<<EN);
_delay_ms(2);
}
void LCD_Char( unsigned char data )
{
LCD_Port = (LCD_Port & 0x0F) | (data & 0xF0);/* 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 & 0x0F) | (data << 4); /* 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 */
LCD_Command(0x28); /* 2 line, 5*7 matrix in 4-bit mode */
LCD_Command(0x0c); /* Display on cursor off*/
LCD_Command(0x06); /* Increment cursor (shift cursor to right)*/
LCD_Command(0x01); /* Clear display screen*/
_delay_ms(2);
}
void LCD_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_Char (str[i]);
_delay_ms(2);
}
}
void LCD_string(char *s, int LCD_line, int LCD_cursor_position)
{
if (LCD_line == 1)
LCD_Command(0x80 + LCD_cursor_position);
else
LCD_Command(0xc0 + LCD_cursor_position);
while (*s)
LCD_Char(*s++); // ++ is equivalent to increment
}
void LCD_cursor_blink(void) //displays LCD blinking cursor
{
LCD_Command(0x0F);
}
void LCD_string_Float(float LCD_number, int LCD_line, int LCD_cursor_position)
{
char buffer_LCD[13];
dtostre(LCD_number, buffer_LCD, 5, 4);
LCD_string(buffer_LCD, LCD_line, LCD_cursor_position);
}
void LCD_string_Int(int LCD_number, int LCD_line, int LCD_cursor_position)
{
char buffer_LCD[10];
itoa(LCD_number, buffer_LCD, 10);
LCD_string(buffer_LCD, LCD_line, LCD_cursor_position);
}
void LCD_Clear()
{
LCD_Command (0x01); /* Clear display */
_delay_ms(2);
LCD_Command (0x80); /* Cursor at home position */
}
void LCD_ShiftLeft(void)
{
/*for(int i=0;i<LCD_Char;i++)
{
LCD_Command(0x18);
_delay_ms(100);
}*/
int i=0;
while(i<LCD_String){
i++;
LCD_Command(0x18);
_delay_ms(100);
}
}
void LCD_ShiftRight(void)
{
int i=0;
for(int i=0;i<LCD_String;i++)
{
LCD_Command(0x1c);
_delay_ms(100);
}
}
int LCD_lencount(){
int len =0;
for(int i=0;i<LCD_String;i++){
len = len + 1;
}
return len;
}
int main()
{
LCD_Init(); /* Initialization of LCD*/
/*LCD_String("ECE Microcontroller");
// Write string on 1st line of LCD
LCD_Command(0xC0); //Go to 2nd line
LCD_String("4-bit Mode"); // Write string on 2nd line
LCD_ShiftLeft();_delay_ms(10);LCD_Clear();*/
/*LCD_String("ECE Microcontroller");
// Write string on 1st line of LCD
LCD_Command(0xC0); // Go to 2nd line
LCD_String("4-bit Mode"); // Write string on 2nd line
LCD_ShiftRight();_delay_ms(10);LCD_Clear();*/
while(1);
}