When you write to the LCD the cursor stays at the end of last string you wrote.. in this case at the end of "Than you". Next time you write to the LCD the text is written starting from the place where you left the cursor.. in this case "Thank youThank you". You need to clear the LCD before each write. Clearing the LCD also moves the cursor at the beginning. Initializing the LCD clears it and moves the cursor at the beginnin, but also does some other stuff which are unnecessary.
The LCD_clear() function can be named differently.. check your LCD library for correct function.
Thank you so much sir.
I got the output correctly.
However while building the program i got 3 warnings . Does it matters ?
I have posted the warnings along with the code below. Please let me know the changes if possible.
../Test3-lcd.c:37: warning: implicit declaration of function 'LCD_write_string'
../Test3-lcd.c:89: warning: conflicting types for 'LCD_write_string'
../Test3-lcd.c:37: warning: previous implicit declaration of 'LCD_write_string' was here
Code
//LCD Program/*
//LCD DATA port----PORT B
//signal port------PORT D
// rs-------PD0
// rw-------PD1
// en-------PD2 */
#define F_CPU 16000000UL
#include<avr/io.h>
#include<util/delay.h>
#define LCD_DATA PORTB //LCD data port
#define ctrl PORTD
#define en PD2 // enable signal
#define rw PD1 // read/write signal
#define rs PD0 // register select signal
void LCD_cmd(unsigned char cmd);
void init_LCD(void);
void LCD_write(unsigned char data);
int main()
{
DDRA = 0b00000000; //configure portA as input
DDRC= 0xff; //Configure portC as output
DDRB=0xff; // Configure PORTB as output for data bus
DDRD=0x07; //Configure PORTD as output for control lines
init_LCD(); // initialization of LCD
while(1)
{
if(bit_is_clear(PINA,0)) //Check high on pin0 of portC
{
PORTC&=~(1<<PINC0); // turn off buzzer
_delay_ms(50); // delay of 50 mili seconds
LCD_cmd(0x01); // clear LCD
LCD_write_string("Thank you");
}
else
{
PORTC|= 1<<PINC0; //Turn on buzzer
_delay_ms(50); // delay of 50 mili seconds
LCD_cmd(0x01); // clear LCD
LCD_write_string("Buckle up");
}
}
return 0;
}
void init_LCD(void)
{
LCD_cmd(0x38); // initialization of 16X2 LCD in 8bit mode
_delay_ms(1);
LCD_cmd(0x01); // clear LCD
_delay_ms(1);
LCD_cmd(0x0E); // cursor ON
_delay_ms(1);
LCD_cmd(0x80); // ---8 go to first line and --0 is for 0th position
_delay_ms(1);
return;
}
void LCD_cmd(unsigned char cmd)
{
LCD_DATA=cmd;
ctrl =(0<<rs)|(0<<rw)|(1<<en);
_delay_ms(1);
ctrl =(0<<rs)|(0<<rw)|(0<<en);
_delay_ms(50);
return;
}
void LCD_write(unsigned char data)
{
LCD_DATA= data;
ctrl = (1<<rs)|(0<<rw)|(1<<en);
_delay_ms(1);
ctrl = (1<<rs)|(0<<rw)|(0<<en);
_delay_ms(50);
return ;
}
void LCD_write_string(unsigned char *str) //store address value of the string in pointer *str
{
int i=0;
while(str
!='\0') // loop will go on till the NULL character in the string
{
LCD_write(str); // sending data on LCD byte by byte
i++;
}
return;
}