(10 intermediate revisions by the same user not shown)
Line 1: Line 1:
 
[[category: LPC2148 Tutorials]]
 
[[category: LPC2148 Tutorials]]
In this tutorial we are going to see how to interface a 2x16 LCD with PIC16f877a in 4-bit mode.
+
In this tutorial, we are going to see how to interface a 2x16 LCD with LPC2148 in 4-bit mode.
As per the name the 2x16 has 2 lines with 16 chars on each lines. It supports all the ascii chars and is basically used for displaying the alpha numeric characters. Here each character is displayed in a matrix of 5x7 pixels.
+
As per the name the 2x16 has 2 lines with 16 chars on each line. It supports all the ASCII chars and is basically used for displaying the alphanumeric characters. Here each character is displayed in a matrix of 5x7 pixels.
Apart from alpha numeric chars it also provides the provision to display the custom characters by creating the pattern.
+
Apart from alphanumeric chars it also provides the provision to display the custom characters by creating the pattern.
  
  
Line 25: Line 25:
 
| 6|| EN || Enable. Falling edge triggered
 
| 6|| EN || Enable. Falling edge triggered
 
|-
 
|-
| 7 || D0 || Data Bit 0 (Not used in 4-bit operation)
+
| 7 || D0 || Data Bit 0  
 
|-         
 
|-         
| 8 || D1 || Data Bit 1 (Not used in 4-bit operation)
+
| 8 || D1 || Data Bit 1  
 
|-         
 
|-         
| 9 || D2 || Data Bit 2 (Not used in 4-bit operation)
+
| 9 || D2 || Data Bit 2  
 
|-
 
|-
| 10 || D3 || Data Bit 3 (Not used in 4-bit operation)  
+
| 10 || D3 || Data Bit 3)  
 
|-
 
|-
 
| 11 || D4 || Data Bit 4
 
| 11 || D4 || Data Bit 4
Line 49: Line 49:
  
 
Apart from the voltage supply connections the important pins from the programming perspective are the data lines(8-bit Data bus), Register select, Read/Write and Enable pin.<br><br>   
 
Apart from the voltage supply connections the important pins from the programming perspective are the data lines(8-bit Data bus), Register select, Read/Write and Enable pin.<br><br>   
<b>Data Bus:</b> As shown in the above figure and table, an alpha numeric lcd has a 8-bit data bus referenced as D0-D7.
+
<b>Data Bus:</b> As shown in the above figure and table, an alphanumeric LCD has an 8-bit data bus referenced as D0-D7.
As it is a 8-bit data bus, we can send the data/cmd to LCD in bytes. It also provides the provision to send the the data/cmd in chunks of 4-bit, which is used when there are limited number of GPIO lines on the microcontroller.<br><br>   
+
As it is an 8-bit data bus, we can send the data/cmd to LCD in bytes. It also provides the provision to send the data/cmd in chunks of 4-bit, which is used when there are limited number of GPIO lines on the microcontroller.<br><br>   
 
<b>Register Select(RS):</b> The LCD has two register namely a Data register and Command register. Any data that needs to be displayed on the LCD has to be written to the data register of LCD. Command can be issued to LCD by writing it to Command register of LCD.
 
<b>Register Select(RS):</b> The LCD has two register namely a Data register and Command register. Any data that needs to be displayed on the LCD has to be written to the data register of LCD. Command can be issued to LCD by writing it to Command register of LCD.
 
This signal is used to differentiate the data/cmd received by the LCD.<br>  
 
This signal is used to differentiate the data/cmd received by the LCD.<br>  
Line 58: Line 58:
 
For write operation the RW should be <b>LOW</b> and for read operation the R/W should be <b>HIGH</b>.<br><br>   
 
For write operation the RW should be <b>LOW</b> and for read operation the R/W should be <b>HIGH</b>.<br><br>   
 
<b>Enable(EN):</b> This pin is used to send the enable trigger to LCD.
 
<b>Enable(EN):</b> This pin is used to send the enable trigger to LCD.
After sending the data/cmd, Selecting the data/cmd register, Selecting the Write operation. A HIGH-to-LOW pulse has to be send on this enable pin which will latch the info into the LCD register and triggers the LCD to act accordingly.
+
After sending the data/cmd, Selecting the data/cmd register, Selecting the Write operation. An HIGH-to-LOW pulse has to be sent on this enable pin which will latch the info into the LCD register and triggers the LCD to act accordingly.
 
<br/>
 
<br/>
 
<br/>
 
<br/>
Line 64: Line 64:
 
=Schematic=
 
=Schematic=
 
Below schematic shows the minimum connection required for interfacing the LCD with the microcontroller.<br>
 
Below schematic shows the minimum connection required for interfacing the LCD with the microcontroller.<br>
As we are interfacing the LCD in 4-bit mode, only the higher 4 data lines are used as data bus.
+
As we are interfacing the LCD in 4-bit mode, only the higher 4 data lines are used as the data bus.
 
<br><br>
 
<br><br>
  
 
=Port Connection=
 
=Port Connection=
 
This section shows how to configure the GPIO for interfacing the LCD.<br>
 
This section shows how to configure the GPIO for interfacing the LCD.<br>
The below configuration is as per the above schematic. You can connect the LCD to any of the PORT pins available on your boards and update this section accordingly
+
Below table shows the LCD pins connection.
 +
{| class="table table-striped table-hover table-condensed table-bordered"
 +
|-class="info"
 +
!RS || RW || EN || D0  || D1  || D2  || D3  || D4 || D5 || D6 || D7
 +
|-
 +
|P1_16  || P1_17 || P1_18 || P_NC || P_NC || P_NC || P_NC || P1_20 || P1_21 || P1_22 || P1_23
 +
|}
 +
*P_NC: Pin Not Connected
 +
<br>
 +
The below configuration is as per the above table. You can connect the LCD to any of the PORT pins available on your boards and update this section accordingly
 
<html>
 
<html>
 
<script src="https://gist.github.com/SaheblalBagwan/6de869a221357ec2d41bb379a23cc89d.js"></script>
 
<script src="https://gist.github.com/SaheblalBagwan/6de869a221357ec2d41bb379a23cc89d.js"></script>
Line 75: Line 84:
  
 
=LCD Operation=
 
=LCD Operation=
In this section we are going to see how to send the data/cmd to the LCD along with the timing diagrams.
+
In this section, we are going to see how to send the data/cmd to the LCD along with the timing diagrams.
First lets see the timing diagram for sending the data and the command signals(RS,RW,EN), accordingly we write the algorithm and finally the code.
+
First let's see the timing diagram for sending the data and the command signals(RS,RW,EN), accordingly, we write the algorithm and finally the code.
  
 
===Timing Diagram===
 
===Timing Diagram===
Line 92: Line 101:
 
*step4: Send a High-to-Low pulse on Enable PIN with some delay_us.
 
*step4: Send a High-to-Low pulse on Enable PIN with some delay_us.
 
<html>
 
<html>
<script src="https://gist.github.com/SaheblalBagwan/a1d184abe71193a035ea6778f9d9214b.js"></script>
+
<script src="https://gist.github.com/Amritach/6e4b08f06568b82fcd64.js"></script>
 
</html>
 
</html>
 
<br><br>
 
<br><br>
Line 104: Line 113:
  
 
<html>
 
<html>
<script src="https://gist.github.com/SaheblalBagwan/c8b9677f9e1851014933d996951bbe22.js"></script>
+
<script src="https://gist.github.com/Amritach/6a137d1bf6a27cefb8de.js"></script>
 
</html>
 
</html>
 
<br><br>
 
<br><br>
 
=Hardware Connections=
 
[[File:LCD 16X2 4bit bb.png|none]]
 
  
 
= Code Examples =
 
= Code Examples =
 
Here is the complete code for displaying the data on 2x16 LCD in 4-bit mode.
 
Here is the complete code for displaying the data on 2x16 LCD in 4-bit mode.
 
<html>
 
<html>
<script src="https://gist.github.com/SaheblalBagwan/9ebcab47701b9faa928f4e54615af779.js"></script>
+
<script src="https://gist.github.com/SaheblalBagwan/a3e14b104c29ccf40f5143e71e1b485e.js"></script>
 
</html>
 
</html>
 
<br><br>
 
<br><br>
  
 
=Using Explore Embedded Libraries :=
 
=Using Explore Embedded Libraries :=
In the above tutorial we just discussed how to interface 2x16Lcd in 4-bit mode.<br>
+
In the above tutorial, we just discussed how to interface 2x16Lcd in 4-bit mode.<br>
Once you know the working of lcd, you can directly use the ExploreEmbedded libraries to play around with your LCD.<br>
+
Once you know the working of LCD, you can directly use the ExploreEmbedded libraries to play around with your LCD.<br>
For that you need to include the lcd.c/lcd.h and the associated files(delay/stdutils).<br>  
+
For that you need to include the lcd.c/LCD.h and the associated files(delay/stdutils).<br>  
 
After including these files, the only thing you got to do is to configure the PORTs in lcd.h as per your hardware connection.<br>
 
After including these files, the only thing you got to do is to configure the PORTs in lcd.h as per your hardware connection.<br>
 
The below sample code shows how to use the already available LCD functions.
 
The below sample code shows how to use the already available LCD functions.
==LCD 1x16==
 
<html>
 
<script src="https://gist.github.com/sharanago/1c42e5b6253260bd8ead3d061d7a627a.js"></script>
 
</html>
 
 
 
==LCD 2x16==
 
==LCD 2x16==
 
<html>
 
<html>
<script src="https://gist.github.com/sharanago/bf69e20dc277fda3f562779fbe2acbfe.js"></script>
+
<script src="https://gist.github.com/SaheblalBagwan/f95f1e2d1d9245c8d220c3bd28274ecd.js"></script>
 
</html>
 
</html>
  
 
==LCD 4x20==
 
==LCD 4x20==
 
<html>
 
<html>
<script src="https://gist.github.com/sharanago/5a15fb1ad85c3b91271ceaa453385410.js"></script>
+
<script src="https://gist.github.com/SaheblalBagwan/bafd89b3a1e7c69d98c7d55375ce4404.js"></script>
 
</html>
 
</html>
  
Line 142: Line 143:
  
 
= Downloads=
 
= Downloads=
Download the complete project folder from the below link: <br>
+
Download the complete project folder from [https://github.com/ExploreEmbedded/LPC2148_Stick this link]
[https://github.com/ExploreEmbedded/Pic16f877a_ExploreUltraPicDevKit/archive/master.zip Hardware design Files and Code Library]
+
<br><br>
  
  
Have a opinion, suggestion , question or feedback about the article let it out here!
+
Have an opinion, suggestion , question or feedback about the article let it out here!
 
{{DISQUS}}
 
{{DISQUS}}

Latest revision as of 13:11, 24 July 2016

In this tutorial, we are going to see how to interface a 2x16 LCD with LPC2148 in 4-bit mode. As per the name the 2x16 has 2 lines with 16 chars on each line. It supports all the ASCII chars and is basically used for displaying the alphanumeric characters. Here each character is displayed in a matrix of 5x7 pixels. Apart from alphanumeric chars it also provides the provision to display the custom characters by creating the pattern.


LCD UNIT

Let us look at a pin diagram of a commercially available LCD like JHD162 which uses a HD44780 controller and then describe its operation. Pic16f877aLcdInterface.png

Pin Number Symbol Pin Function
1 VSS Ground
2 VCC +5v
3 VEE Contrast adjustment (VO)
4 RS Register Select. 0:Command, 1: Data
5 R/W Read/Write, R/W=0: Write & R/W=1: Read
6 EN Enable. Falling edge triggered
7 D0 Data Bit 0
8 D1 Data Bit 1
9 D2 Data Bit 2
10 D3 Data Bit 3)
11 D4 Data Bit 4
12 D5 Data Bit 5
13 D6 Data Bit 6
14 D7 Data Bit 7/Busy Flag
15 A/LED+ Back-light Anode(+)
16 K/LED- Back-Light Cathode(-)



Apart from the voltage supply connections the important pins from the programming perspective are the data lines(8-bit Data bus), Register select, Read/Write and Enable pin.

Data Bus: As shown in the above figure and table, an alphanumeric LCD has an 8-bit data bus referenced as D0-D7. As it is an 8-bit data bus, we can send the data/cmd to LCD in bytes. It also provides the provision to send the data/cmd in chunks of 4-bit, which is used when there are limited number of GPIO lines on the microcontroller.

Register Select(RS): The LCD has two register namely a Data register and Command register. Any data that needs to be displayed on the LCD has to be written to the data register of LCD. Command can be issued to LCD by writing it to Command register of LCD. This signal is used to differentiate the data/cmd received by the LCD.
If the RS signal is LOW then the LCD interprets the 8-bit info as Command and writes it Command register and performs the action as per the command.
If the RS signal is HIGH then the LCD interprets the 8-bit info as data and copies it to data register. After that the LCD decodes the data for generating the 5x7 pattern and finally displays on the LCD.

Read/Write(RW): This signal is used to write the data/cmd to LCD and reads the busy flag of LCD. For write operation the RW should be LOW and for read operation the R/W should be HIGH.

Enable(EN): This pin is used to send the enable trigger to LCD. After sending the data/cmd, Selecting the data/cmd register, Selecting the Write operation. An HIGH-to-LOW pulse has to be sent on this enable pin which will latch the info into the LCD register and triggers the LCD to act accordingly.

Schematic

Below schematic shows the minimum connection required for interfacing the LCD with the microcontroller.
As we are interfacing the LCD in 4-bit mode, only the higher 4 data lines are used as the data bus.

Port Connection

This section shows how to configure the GPIO for interfacing the LCD.
Below table shows the LCD pins connection.

RS RW EN D0 D1 D2 D3 D4 D5 D6 D7
P1_16 P1_17 P1_18 P_NC P_NC P_NC P_NC P1_20 P1_21 P1_22 P1_23
  • P_NC: Pin Not Connected


The below configuration is as per the above table. You can connect the LCD to any of the PORT pins available on your boards and update this section accordingly

LCD Operation

In this section, we are going to see how to send the data/cmd to the LCD along with the timing diagrams. First let's see the timing diagram for sending the data and the command signals(RS,RW,EN), accordingly, we write the algorithm and finally the code.

Timing Diagram

The below image shows the timing diagram for sending the data to the LCD.
As shown in the timing diagram the data is written after sending the RS and RW signals. It is still ok to send the data before these signals.
The only important thing is the data should be available on the databus before generating the High-to-Low pulse on EN pin. figure: command write


Steps for Sending Command:

  • step1: Send the I/P command to LCD.
  • step2: Select the Control Register by making RS low.
  • step3: Select Write operation making RW low.
  • step4: Send a High-to-Low pulse on Enable PIN with some delay_us.



Steps for Sending Data:

  • step1: Send the character to LCD.
  • step2: Select the Data Register by making RS high.
  • step3: Select Write operation making RW low.
  • step4: Send a High-to-Low pulse on Enable PIN with some delay_us.

The timings are similar as above only change is that RS is made high for selecting Data register.



Code Examples

Here is the complete code for displaying the data on 2x16 LCD in 4-bit mode.

Using Explore Embedded Libraries :

In the above tutorial, we just discussed how to interface 2x16Lcd in 4-bit mode.
Once you know the working of LCD, you can directly use the ExploreEmbedded libraries to play around with your LCD.
For that you need to include the lcd.c/LCD.h and the associated files(delay/stdutils).
After including these files, the only thing you got to do is to configure the PORTs in lcd.h as per your hardware connection.
The below sample code shows how to use the already available LCD functions.

LCD 2x16

LCD 4x20

01LCD 4bit.png

Downloads

Download the complete project folder from this link


Have an opinion, suggestion , question or feedback about the article let it out here!