16 X 2 LCD is most popular in Embedded projects .16 is the number of Columns & 2 is the no. of Rows.Generally it needs 8 bits to display the data as the Data bus is of 8 bits.
In this post the LCD is used in 4 bit mode ,splitting the data Byte in Nibbles.So we can save 4 data port pins of the PIC microcontroller.
Complete datasheet of this LCD is available here .
The Data or command is sent in nibble form (1 nibble= 4 bit) in the 4-bit mode. The higher nibble is sent first followed by the lower nibble. The function of RS, RW and EN pins remains similar to 8-bit mode.
The pin out of LCD is as follows:
1. Ground
2. VCC (+3.3 to +5V)
3. Display Contrast adjustment (VO)
4. Register Select (RS). RS=0: Command, RS=1: Data
5. Read/Write (R/W). R/W=0: Write, R/W=1: Read
6. Clock (Enable). Falling edge triggered
7,8,9,10 Bit 0 to bit3 (Not used in 4-bit operation)
11. D4 Bit 4
12. D5 Bit 5
13. D6 Bit 6
14. D7 Bit 7
15.Backlight Anode (+)
16.Backlight Cathode (-)
Click on Window –> PIC Memory Views –> Configuration bits & set the config bits as FOSC = HS ,WDTE =OFF ,LVP = OFF ,etc.. as seen below
Click on “Generate Source code to output “ button seen at the bottom.
You can copy & paste the Configuration code on to the Editor Pane.
#include “lcd.h” statement indicates that the lcd.h file resides inside the project folder.Previously you should’ve downloaded this file & stored it inside the LCD.X folder.Otherwise you’ll get red error squiggles against this statement.
2. VCC (+3.3 to +5V)
3. Display Contrast adjustment (VO)
4. Register Select (RS). RS=0: Command, RS=1: Data
5. Read/Write (R/W). R/W=0: Write, R/W=1: Read
6. Clock (Enable). Falling edge triggered
7,8,9,10 Bit 0 to bit3 (Not used in 4-bit operation)
11. D4 Bit 4
12. D5 Bit 5
13. D6 Bit 6
14. D7 Bit 7
15.Backlight Anode (+)
16.Backlight Cathode (-)
For interfacing LCD with microcontroller we need system-bus (data-bus, address-bus and control-bus).In our case for 4 bit mode the data pins (D4-D7) are the ‘data and address’ bus while the 3 control pins(RS, R/W and E) are the control bus
These displays generally have a 16-wire interface, of which 11 IO lines from the PIC micro are needed. 8 of these lines are for DATA.
As we use LCD in 4 BIT Mode , only 4 lines are actually needed.
The Read/Write R/W pin is pulled to Ground making the LCD write only.
Dropping the 4 Data lines & the R/W line , we need only 6 I/O lines (
4 data lines
D4-D7 along with RS and Enable) f
or basic operation
To start with create a Folder say inside F:\myprojects to store all our work.
Open the MPLABX IDE & click on File –> New Project
Choose Project –> Microchip Embedded –> Standalone Project click on Next & select the Device as PIC16F877A
Select Tool as PICKIT2 & Compiler as XC8
Provide project name as LCD & project location as F:\myprojects ( we’ve created earlier).
click Finish
Inside FILE PANE you can see the default File folders created by the IDE.
For the XC compiler to understand the LCD hardware , a Header file is used.Download this file & store it inside the LCD.X folder.
Copy & paste the lcd.h header file into the LCD.X folder.
Right click on Header Files –> Add Existing Item & browse to the location where you’ve stored the lcd.h header file & select it.
Now lcd.h will appear under Header Files folder
Now Right click on Source Files –> New –> C Main File
Provide a file name , or leave it to default name “newmain” .
Select the extension as .c from the drop down.
You can see newmain.c file created inside the LCD.X project folder.
Click on Finish to see the newmain.c file under Source Files folder .The LCD.h file is seen under Header Files folder.
Delete the generated code for now .
Click on “Generate Source code to output “ button seen at the bottom.
You can copy & paste the Configuration code on to the Editor Pane.
Or you can enter the following config. code in a single line as :
————————————————–
#include <xc.h>
#pragma config FOSC=HS , WDTE=OFF , LVP =OFF , PERTE=OFF
—————————————————
As we use an external crystal of 20 MHz on demo board , we declare FOSC as HS & other settings WatchDog, Low Voltage Programming ,etc . as OFF
Below the #include <xc.h> statement define the crystal value as
#define _XTAL_FREQ 20000000
20 followed by 6 zeroes represents 20 MHz value.
PORT D pins are used to connect with the LCD.
We need only 4 pins for DATA , as we use the LCD in 4 bit mode. Pins RD4 to RD7 are used for data bits D4 to D7.
RS (Register Select) is connected to pin RD2.
When RS = 0 , D4 to D7 value is interpreted as COMMAND by the LCD.
When RS = 1 , D4 to D7 value is interpreted as DATA by the LCD.
To initialize & set the cursor location we set this RS as 0 (Command mode) & for displaying Data we set this RS to 1.
LCD enable pin is connected to RD3.
R/W READ/WRITE pin of LCD is connected to GND as we use the LCD to display DATA.
All these connections are declared using the #define preprocessor statements.
———————————————–
#define _XTAL_FREQ 20000000
#define RS RD2
#define EN RD3
#define D4 RD4
#define D5 RD5
#define D6 RD6
#define D7 RD7
// BEGIN CONFIG
#pragma config FOSC = HS,WDTE=OFF ,PWRTE = OFF,BOREN = OFF, LVP = OFF
//END CONFIG
#include <xc.h>
#define EN RD3
#define D4 RD4
#define D5 RD5
#define D6 RD6
#define D7 RD7
// BEGIN CONFIG
#pragma config FOSC = HS,WDTE=OFF ,PWRTE = OFF,BOREN = OFF, LVP = OFF
//END CONFIG
#include <xc.h>
#include "lcd.h";
void main()
{
unsigned int a;
TRISD = 0x00;
Lcd_Init();
while(1)
{
Lcd_Clear();
Lcd_Set_Cursor(1,1);
Lcd_Write_String("LCD Library for");
Lcd_Set_Cursor(2,1);
Lcd_Write_String("MPLAB XC8");
__delay_ms(2000);
Lcd_Clear();
Lcd_Set_Cursor(1,1);
Lcd_Write_String("www.egtechprojects.com");
__delay_ms(2000);
Lcd_Clear();
Lcd_Set_Cursor(1,1);
Lcd_Write_String("EG Technologies");
{
unsigned int a;
TRISD = 0x00;
Lcd_Init();
while(1)
{
Lcd_Clear();
Lcd_Set_Cursor(1,1);
Lcd_Write_String("LCD Library for");
Lcd_Set_Cursor(2,1);
Lcd_Write_String("MPLAB XC8");
__delay_ms(2000);
Lcd_Clear();
Lcd_Set_Cursor(1,1);
Lcd_Write_String("www.egtechprojects.com");
__delay_ms(2000);
Lcd_Clear();
Lcd_Set_Cursor(1,1);
Lcd_Write_String("EG Technologies");
for(a=0;a<15;a++)
{
__delay_ms(1000);
Lcd_Shift_Left();
}
{
__delay_ms(1000);
Lcd_Shift_Left();
}
for(a=0;a<15;a++)
{
__delay_ms(1000);
Lcd_Shift_Right();
}
{
__delay_ms(1000);
Lcd_Shift_Right();
}
Lcd_Clear();
Lcd_Set_Cursor(2,1);
Lcd_Write_Char(‘S’);
Lcd_Write_Char(‘E’);
__delay_ms(2000);
}
}
Lcd_Set_Cursor(2,1);
Lcd_Write_Char(‘S’);
Lcd_Write_Char(‘E’);
__delay_ms(2000);
}
}
————————————————————-
After feeding in the code as above ,
Click on Run –> Build Project
If all the syntax are correct you get BUILD SUCCESSFUL.
Finally click on Run –> Run Main Project or press F6 to execute the program & see result on the LCD.
No comments:
Post a Comment