Wednesday, 28 January 2015

How to interface 20x4 LCD Diaplay using 8051/AT89c51 with 4 data lines

20x4 LCD Interfacing Using 8051/89c51 with 4-data lines

Here I am writing program to interface 20x4 LCD using 8051/AT89c51 Microcontroller with using only 4 data lines. For interfacing a LCD you must have some idea about LCD Pins. The LCD which is normally available in market is of 16-pin.LCD pin description is explained below:-

PIN NUMBER
SYMBOL
FUNCTION
1.
VSS
GROUND
2.
VDD
+5V
3.
VEE
Contrast Adjustment
4.
RS
Register Selection Signal
5.
R/W
H/L , Read/write Signal
6.
E
H->L,  Enable Signal
7.
D0
H/L ,Data Bus Line
8.
D1
H/L ,Data Bus Line
9.
D2
H/L ,Data Bus Line
10.
D3
H/L ,Data Bus Line
11.
D4
H/L ,Data Bus Line
12.
D5
H/L ,Data Bus Line
13.
D6
H/L ,Data Bus Line
14.
D7
H/L ,Data Bus Line
15.
A/VEE
Backlight Anode
16.
K
Backlight cathode

As per the pin details of LCD explained in above table the important pin used to display character on LCD are RS,R/W,E and data lines (D0-D7) .Through RS pin we can select the LCD data register or LCD command register. If RS pin is HIGH then data register is selected or if RS pin is LOW then command register is selected. R/W is for selecting read or write operation if R/W pin is HIGH then read operation is selected if R/W pin is LOW then write operation is selected. Enable pin is to enabling data reception. Pin D0 to D7 are data lines through which LCD received command or data in 8-bit format.

Now to interfacing LCD on 4 data lines i have connected higher 4-bit of Port1 (i.e. P1.4 – P1.7) to higher the 4 data lines (i.e. D4-D7) you can see Connection diagram used to interface LCD using 4 data lines is given in below image:-



Now you can see in the connection diagram that RS is connected to P2.0 and R/W pin is ground for Write operation and Enable (E) pin is connected to P2.1.Also considered that higher 4 data lines are connected to higher 4 bit of port 1.
In this Program I have print my name (VAIBHAV SINGHAL) in first line and my profession   (i.e. FIRMWARE DEVELOPER) in second line and my blog url in 3rd and 4th line (i.e www.embeddedsolutions4u.blogspot.com). The main file of program is shown in below image:-


In the image of above program you can see I have first initialized the LCD by sending commands 0x02 to reset LCD,0x28 for 2 lines and 5x7 matrix and also for selecting LCD in 4- bit mode,0x0C to ON the display,0x01 to clear LCD screen and 0x06 for cursor increment. After initializing LCD i have write command 0x80 to start writing from 1 position of first line and send my name then send command 0xC0 to write data from 1 position of second line i have wrote my profession then after I have send command 0x94 to write data from 1 position of third line and I have print www.embeddedsolution then after I have send command 0xD4 to write data from  1 position of fourth line and I have print s4u.blogspot.com.
Now in above program I have used to functions one to write command and the other is to send string on LCD. Definition of both function is given in image below:-


In above image you can see both functions. In “LCD_write_command” function register firstly I have selected the command register by putting RS pin LOW and then after I have pull low higher 4 bit of port1 which is connected to data lines and then I have put higher 4 bit of data and then enables data reception by putting Enable pin high for very short time after that I have put zero to data lines and then after ihave send the lower 4 bit to data lines.
 In “LCD_Send_String” function I have send complete string to LCD by using “LCD_write_data” function. Full details of this function is given in image below:- 


The “LCD_write_data” function is same as of “LCD_write_command” function except that I have select data register in this function by putting RS pin high.           
This is all about to write characters on 20x4 LCD by using 4 data lines. After installing program in 8051/89c51 the data which’ll show on 20x4 LCD is given in below image :-


Download Link

To download complete code of 20x4 LCD interfacing using 8051/89c51 with 4-data lines click here.if you are unable to download then please share you email id and your requirement in comment section.



Tuesday, 27 January 2015

How to Display custom character on 16x2 LCD.

Displaying Custom character on 16x2 LCD

Here I am explaining how to display custom character on 16x2 LCD display. In alphanumeric 16x2 LCD display 64 bytes of CG (character generator) RAM is available to create custom character. Address of CG RAM is starts from 64 and ends to 127. Each character consists 8 bit so maximum 8 custom characters can be stored in CG RAM.

Now in my code firstly I have generated 8 custom characters and put it into CG RAM of LCD .Then after I have display these 8 characters starting from 1 position of first line with single space between each character.

Main Program to display custom character on 16x2 LCD with 5x7 pixels is given in images below :-



In above image you can see that I have created a array which contains 56 digit. So here first 7 digits is for first custom character which is a Bell then next 7 digits is for second custom character which is Heart then next 7 digits is for third custom character which is Smile then next 7 digits is for fourth custom character which is Symbol of Music then next 28 digits is for 4 Hindi Characters .You will say how I will explain in detail but first look on image given below which contain the main code:-


The code shown in image above is complete code of creating 8 custom character and display all these on 16x2 LCD.
So Here I will only explain how I create my first character Bell and display it on LCD the rest you’ll automatically understand.

Creating Custom Character:-

In above code after initializing LCD I have used a For loop for creating 8 custom character in that loop when we enter first time we’ll send a command 64 on LCD which is base address of CG RAM. Then I have send first 7 data using loop which are 4,14,14,14,31,0,4 to create Bell.

How this data creating Bell is a question? Answer of this question you will found below:-

16   8     4    2     1





    





























  
The above Figure shows the 5x7 pixel of LCD Block .Which pixel we wants to put ON we can do it with code. First I have send data 4 which is to ON 3rd  pixel of first row and then I have send 14,14,14 which put ON 2,3,4 pixel of 2,3,4 row and then I have send 31 to ON all 5 pixels of 5th row then 0 to OFF all 5 pixels of 6th row then after 4 to ON 3rd  pixel of seventh row. In this manner the picture which comes out is of Bell. So in this manner you can generate your own custom character.

Displaying Custom Character :-

After Creating all 8 characters I have display all these character using a for loop .In that loop first I have send command of start_add which is 0x80 to display character from 1 position of first line of 16x2 LCD. Then I have display the custom character which I have put in CG RAM for Ex I have put Bell on address 64-71. To characters which we have put in CG RAM can be display by sending data 0 for location 64, 1 for location 72 and …………….7 for location 120.

Through the above code the character that I have created is given in Image below:-


Read This :-
Before Reading this post First read How to display character on 16x2 LCD from my earlier post.

Download Link:-

To Download Complete code please click here. If you are unable to download this code then please give your email id in comment section.

Wednesday, 14 January 2015

How to Interface 7 Segment with 8051/AT89c51

Interfacing 7 segment display using 8051/89c51

Sample code for how to interface a 7 segment display using 8051/AT89c51 is here. For interfacing a 7 segment you must have some idea about 7 segment display.7 segment display is of two types:-

1.      Common Cathode
2.      Common anode

In Common cathode type of display its common pin should be connected to ground (0 level) so that we can ON its rest of LED’s by putting high value (1 level or +5V) on its pin.
In Common anode type of display its common pin should be connected to Logic High (1 level or +5V) so that we can ON its rest of LED’s by putting Low value (0 level or ground) on its pin.
Here I have write the program using Common cathode 7 segment display. You can see the connection diagram in image below:-


     


In the above connection diagram I have connected the P2 to 7 segment display. For details description you can see the below table:-

Controller Pin
7 segment PIN
P2.0
a
P2.1
b
P2.2
c
P2.3
d
P2.4
e
P2.5
f
P2.6
g
P2.7
Common

So if you wants to ON Led “a” then you can send high signal to PIN P2.0.In this program display show digit from 0 to 9 repeatedly with 1 sec delay. Now you can see the main program in image below:-



In main code firstly I have put zero on PORT2 to off 7 segment display. Then after when I enter in infinite loop I have called a function “display_digit()” to display digit from 0 to 9 on 7 segment display. Because the value of variable i is a zero so digit 0 is delay first on 7 segment then after I have give 1 sec delay and incremented variable to display 1. In this manner digit gets incremented after every 1 second and displayed. But when value of i becomes greater than 9 then its again become zero because of if condition.
Explanation of “display_digit()” function for displaying digit from 0 to 9 is given in below  image:-



In above image you can see the complete “display_digit()” function which I have used to display digit from 0 to 9. I am explaining how I have display digit 0. To display digit zero I have send the 0x3f value to PORT2 to ON Led a,b,c,d,e and f but LED g is off. So you can see in very first image of 7-segment display if LED a,b,c,d,e,f will ON then digit zero will display. This is how you can see the other values also to display digit from 1 to 9.

 Download Links
For downloading complete code of 7-segment display you can click here. You can also download supported datasheets from Download tab.