LCD in 4-bit mode
We have seen that there are 8 general purpose input output (GPIO) lines in the LCD but while using it in 4-bit mode MSB of any data or commnad is sent first over 4 bits and then 4 LSB sent by shifting the data byte 4 times left ( shift your data to rest of the pin inside the LCD) .
Now lets understand the operation by example .
Complete your circuit first as shown in figure below
only 4 pins (higher bits ) of the lcd are connected to the PORTA (pin4-to –pin7). And control pins reset(RS),read/write(R/W) and enable pins are same as 8-bit mode of the lcd which I have written previously.
Now write the code below and observe the result first .
Ok you have successfully completed the result . lets understand the code . while writing the code there are some steps
i).Initialize your LCD with some command that LCD should be operated on 4-bit mode .that I have written above .
- ii) Give some command to the LCD so that it can understand what you have intend to do.
iii) Finally send the data to the LCD with nibble by nibble (ie 4-bit=1 nibble).and shift the data to the lower pins.
Lets starts with header files
The avr/io is a avr standard file , delay file is used to delay the operation .and the inttypes.h file is integer type conversion file , furthermore the inttypes.h file is a C header file that is part of the C standard library and API. It was added with the 1999 version of the ISO C standard (known as C99). It includes thestdint.h header and defines a number of macros for using it with the printf and scanf family of functions, as well as functions for working with the int_max-t type.
Now declare some functions separately for easy programming .
void lcd_init(); //for initializing the lcd with proper command
void dis_cmd(char);//this function takes character and send the command to the lcd
void dis_data(char); // this function is send the data to the lcd and shift to the lower bit of the lcd
void lcdcmd(char); // this function take character from the initialization part of the lcd and send them out to lcd
void lcddata(char); // finally the data has been displayed to the lcd using this function .
ok I will explain this much only . all the definition of the fuction that are created to this programme are much more simple if you have understand the previous 8-bit mode of the lcd . see the data sheet of the LCD then you will know which bit need to be set or reset to display correctly to the display .try to understand the code your self to become a good programmer . you need to explore more about it if you are really stuck there . the code is extremely simple written in C language.
Header file for LCD
As we have describe the header file for the button previously . we can make our own library by putting all basic functions separately to the H-file . But our header file is very concise we cannot use it to other family of the avr like atmega328p or atmega-8 etc . it doesnot mean we cannot write a standard file for the LCD to operate for any microcontroller of AVR family but there is a standard lcd header file you can find it on Atmel website which is originally written for the all microcontroller and all modes of operation . you better you would use / learn to use the standard library .below is the link for the LCD header file .