on Tuesday, 29 October 2013

Are you worried whether your 16x2 Lcd is working or not? Are you worried whether it is burned or working well? Here is the solution, just follow the below steps to find your solution...

FOLLOW THESE STEPS :
>> Connect the pin vcc pin to 5v

>>Connect pin Vee and gnd pin to ground

>> Connect Led+ to 5v

>>Connect led- pin to ground


After you made these connections then Lcd look somewhat like this below  i.e the lower line will be brighten..




 
If your Lcd is not working well, then both the lines will be bright shown below






Important Note: Most Lcds can withstand upto 9v( but not all), and dont worry if u
applied 9v without knowing it.. Do above steps with 5v only...

Tutorial(with code) on  interface lcd with microcontroller then check  " Interfacing 16x2 lcd to Microcontroller "


AUTHOR: YOUSTRON SIC










There are many display devices which can be interfaced with microcontroller for output operations. One among them  is  16x2 LCD display which is the most common output device. We dont use LCD library functions provided by mikroC compiler but we create our own functional modules for them.
 WHAT YOU WILL LEARN FROM THIS PROJECT?

1. Pin configuration of 16x2Lcd
2. Control and Display commands
3. Displaying data on the LCD
3. Passing string to a function


AIM:dAim of the project is to display our required string on the Lcd display.


EXPLANATION:
16x2 Lcd indicates it can display 16 characters per line and totally consists of 2 lines.
Top view of Lcd is shown below. You can notice there are 16pins at the bottom of the Lcd.

lcd 16x2



PIN NUMBER
PIN NAME
PURPOSE
1
VSS
Ground
2
VDD
Power supply
3
VEE
Contrast i.e We connect this pin to pot to adjust the contrast. If we connect this pin to ground then LCD will be at its highest brightness
4
RS
Register select if RS=0 --> Instruction reg, if Rs=1--> Data register
5
R/W
Read/Write, if R/W=0--> Write, R/W=1--> Read
6
E
Enable
7
DB0
Data pin
8
DB1
Data pin
9
DB2
Data pin
10
DB3
Data pin
11
DB4
Data pin
12
DB5
Data pin
13
DB6
Data pin
14
DB7
Data pin
15
LED-ve
Lcd back light –ve pin, connected to ground
16
LED  +ve
Lcd back light +ve pin, connected to power source +ve

DB0 to DB7:


Pins DB0 to DB7 are data pins  i.e whatever we want to display/commands on the LCD we send through these pins. We will connect these pins with  port pins of a microcontroller and we send corresponding ASCII values. 
There is a difference between 'Command data' and 'display data'. Command data is used to give any command to LCD to perform it, example clearing display, cursor on, cursor blinking, start cursor at the first line, etc. Whereas display data is our desired data which has to be shown on the LCD screen , example hello , welcome etc.
Every command have predefined ASCII value, lets see most common commands below


COMMAND
ASCII VALUE
Clear Display Screen
0x01
Configure LCD as  2lines 5x7 matrix
0X38
Display ON cursor blinking
0X0E
Force cursor to start at first line first place
0X80
Force cursor to start at 2nd line
0XC0
Shift entire display right
0x1C


If we want to clear the display then we should send 0x01 to the data pins i.e DB0 to DB7. Similarly if we want to 'configure our lcd as 2lines' then we need to send 0x38. 
In the same way if we want to display any data on the lcd we send through these data pins. 

Suppose i want to show '8' on the lcd screen then we need to send its ascii value 0x38. 
Now you might get doubt that the ascii value of 8 and the command value of 'configure lcd' are both the same i.e 0x38 then which one will be performed by our lcd??? 
Your doubt is genuine, to solve these confusion for LCD there is a pin called as "RS" i.e  4th pin(look at the above pin table)  if RS=0  then Instruction reg will be selected and  if Rs=1 then Data register will be selected. If we want to give any command to LCD we make RS pin 0 and if we want to display any data we make RS to 1. 

R/W pin i.e 5th pin is used to read the data from the LCD or writing data to the LCD , in general we make R/W=0 because we perform write operation in most cases.


Every time when we send data whether it is command data or display data we need to make E pin high and then low i.e E=1 some delay then E=0
NOTE: While we are sending string we need to send each letter one after the other. For example we need to send "electronics" then first we need to send 'e' ,then 'l' , then 'e' and so on.... 
For this purpose we use Array, we save string(which has to be displayed on lcd) in an array and then we will send the each character until we encounter '\0' which is the last letter in an array.

TO SEND COMMAND DATA (IN POINTS): 
1. port=command (i.e send Ascii data to port pins i.e db0 to db7)
2. Rs=0 (Instruction register select)
3. R/W=0 (Write)
4. E=1 (E high)
5. delay
6. E=0 (E low)

TO SEND DISPLAY DATA(IN POINTS):
1. port=display data
2. RS=1 (data register)
3. R/W=0 (write)
4. E=1 (E high)
5. Delay
6. E=0 (E low)

TO INITIALIZE THE LCD:
1.Send command 0x38 (configuring LCD as 2 line 5x7 matrix)
2.Send command 0x0E (Display on, Cursor blinking)
3.Send command 0x01 (Clear Display Screen)

CIRCUIT DIAGRAM:
16x2 lcd tutorial

All other connections are same i.e pin 13, 14 =crystal, 12,31=gnd, 1,11,32=power +5v.
Note PORTC connected to DATA PINS of LCD
 RS=portd.f0
 R/W=portd.f1
 E=portd.f2

PROGRAM:
//COMPILER: MIKROC FOR PIC
//MICRO CONTROLLER: PIC18F452
//CRYSTAL FREQ: 4MHZ
//Function to send command data:
//Below function will send the command to the Lcd.
void send_cmdd(unsigned char commands)// variable 'Commands' holds the command which has to be send
{
 portc=commands;//look 1st point from above 'To send command data in points'
 portd.f0=0;//point 2 i.e Rs=0
 portd.f1=0;// point 3 i.e R/W=0
 portd.f2=1;//point4 i.e E=1
 delay_ms(2);//point5 i.e delay
 portd.f2=0;//point6 i.e E=0
 }

//Function to send Display data:
//Below function will send data string which has to be displayed on the lcd
void send_data(unsigned char *String)// display data will saved in 'string'
{
unsigned char i=0;                                                                                                                      

 while(String[i]!='\0')//repeat loop until last letter of the array
   {
       portc = String[i];// send first character of the string to data pins of lcd
       portd.f0 = 1;//point 2 of "To send Display data in points:" i.e Rs=1
       portd.f1 = 0; //point 3 i.e R/W=0
       portd.f2 = 1; //point 4 i.e E=1
      delay_ms(2); //point 5 i.e delay
       portd.f2 = 0; //point 6 i.e E=0
       i++;// increment the i value so that next chracter can be sent
    }
}

//Function to Initialize LCD:
 void Lcd_startup( )
{
  send_cmdd(0x38); //Config lcd as 2line 5x7 matrix
  send_cmdd(0x01); // Clear display Screen
  send_cmdd(0x0E);// Display ON cursor blinking
}

//Main Function
void main()
{
  TRISD=0X0;// Assign portd as output
  TRISC=0X0;// Portc as output
  PORTD=0;
  PORTC=0;
  Lcd_startup();//calling startup function to initialize
  send_data("YOUSTRON");//calling function name 'send_data' to send string "YOUSTRON"
  delay_ms(500);// normal delay of 500ms
  send_cmdd(0xC0);// force the cursor to start from 2nd line, this is a command
                               //so we are using command function 'send_cmdd()'
  delay_ms(100);
  send_data("SIC");//cursor at 2nd line, we are sending data "SIC" to display using send_data() function
}

Verify the above code using proteus isis then proceed with the real hardware, if you dont know how to use proteus then check simple tutorial "Guide to Proteus Simulator for Projects"


If you have a doubt whether your LCD is working properly or not then read my next post " How to check whether 16x2 LCD working properly or not" (updated soon)



AUTHOR: YOUSTRON SIC