(24 intermediate revisions by 6 users not shown)
Line 1: Line 1:
 
[[category: LPC1768 Tutorials]]
 
[[category: LPC1768 Tutorials]]
 
 
=Objective=
 
 
In this tutorial we are going to see how to interface a 2x16 LCD with LPC1768 in 8-bit mode.
 
In this tutorial we are going to see how to interface a 2x16 LCD with LPC1768 in 8-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 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.
Line 9: Line 6:
  
 
=LCD UNIT=
 
=LCD UNIT=
{{Box|type=l_green_light|text=
 
 
 
Let us look at a pin diagram of a commercially available LCD like '''JHD162''' which uses a '''HD44780''' controller and then describe its operation.
 
Let us look at a pin diagram of a commercially available LCD like '''JHD162''' which uses a '''HD44780''' controller and then describe its operation.
[[File:LCD_output.PNG|thumbnail|fig LCD display ]]
+
[[FILE:Pic16f877aLcdInterface.png]]
[[File:LCD Char 5x7 Matrix.jpg|thumbnail]]
+
[[File:PIN_Diagram.PNG]]
+
 
+
 
+
All the pins are identically to the lcd internal controller discussed above
+
 
+
}}
+
  
{|class="wikitable "  
+
{| class="table table-striped table-hover table-condensed table-bordered"
 +
|-class="info"
 +
| Pin Number || Symbol || Pin Function
 
|-
 
|-
! PIN NUMBER !! FUNCTION
+
|1 || VSS ||Ground
 
|-
 
|-
|1 || Ground
+
| 2|| VCC || +5v
 
|-
 
|-
| 2|| VCC
+
| 3 || VEE || Contrast adjustment (VO)
 
|-
 
|-
| 3 || Contrast adjustment (VO)
+
| 4 || RS || Register Select. 0:Command, 1: Data
 
|-
 
|-
| 4 || Register Select (RS). RS=0: Command, RS=1: Data
+
| 5 || R/W || Read/Write, R/W=0: Write & R/W=1: Read
 
|-
 
|-
| 5 || Read/Write (R/W). R/W=0: Write, R/W=1: Read
+
| 6|| EN || Enable. Falling edge triggered
 
|-
 
|-
| 6|| Clock (Enable). Falling edge triggered
+
| 7 || D0 || Data Bit 0
 +
|-       
 +
| 8 || D1 || Data Bit 1
 +
|-       
 +
| 9 || D2 || Data Bit 2
 
|-
 
|-
| 7 || Bit 0 (Not used in 4-bit operation)
+
| 10 || D3 || Data Bit 3
 
|-
 
|-
| 8 || Bit 1 (Not used in 4-bit operation)
+
| 11 || D4 || Data Bit 4
 +
|-        
 +
| 12 || D5 || Data Bit 5
 +
|-         
 +
| 13 || D6 || Data Bit 6
 +
|-         
 +
| 14 || D7 || Data Bit 7/Busy Flag
 
|-
 
|-
| 9 || Bit 2 (Not used in 4-bit operation)
+
| 15 || A/LED+ || Back-light Anode(+)
 
|-
 
|-
| 10 || Bit 3 (Not used in 4-bit operation)
+
| 16 || K/LED- || Back-Light Cathode(-)  
|-
+
| 11 || Bit 4
+
|-
+
| 12 || Bit 5
+
|-
+
| 13 || Bit 6
+
|-
+
|  14|| Bit 7
+
|-
+
|  15|| Back-light Anode(+)
+
|-
+
| 16 || Back-Light Cathode(-)  
+
 
|}
 
|}
<br/>
+
<br><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>   
 
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> <br>As shown in the above figure and table, an alpha numeric lcd has a 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 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>   
<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> <br>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>  
 
If the RS signal is <b>LOW</b> then the LCD interprets the 8-bit info as <b>Command</b> and writes it <b>Command register</b> and performs the action as per the command.<br>   
 
If the RS signal is <b>LOW</b> then the LCD interprets the 8-bit info as <b>Command</b> and writes it <b>Command register</b> and performs the action as per the command.<br>   
 
If the RS signal is <b>HIGH</b> then the LCD interprets the 8-bit info as <b>data</b> and copies it to <b>data register</b>. After that the LCD decodes the data for generating the 5x7 pattern and finally displays on the LCD.<br><br>   
 
If the RS signal is <b>HIGH</b> then the LCD interprets the 8-bit info as <b>data</b> and copies it to <b>data register</b>. After that the LCD decodes the data for generating the 5x7 pattern and finally displays on the LCD.<br><br>   
<b>Read/Write(RW):</b> This signal is used to write the data/cmd to LCD and reads the  busy flag of LCD.
+
<b>Read/Write(RW):</b> <br>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 <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> <br>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. 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.
 
<br/>
 
<br/>
 
<br/>
 
<br/>
 +
 +
=Hardware Connections=
 +
<div class ="img-responsive">
 +
[[File:LCD 8Bit bb.jpg|680px]]
 +
</div>
 +
 
=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 8-bit mode, all the 8 data lines are used as data bus.
 
As we are interfacing the LCD in 8-bit mode, all the 8 data lines are used as data bus.
  
 +
<html>
 +
<img src=https://www.exploreembedded.com/wiki/images/c/c9/LPC1768_8bitLCD.PNG class="img-responsive">
 +
</html>
  
 
<br/>
 
<br/>
Line 84: Line 80:
 
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
 
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
<syntaxhighlight>
+
<html>
/* Configure the data bus and Control bus as per the hardware connection */
+
<script src="https://gist.github.com/Amritach/8a28293e7c8d8cef5aa8.js"></script>
#define LcdDataBusPort      LPC_GPIO1->FIOPIN
+
</html>
#define LcdControlBusPort  LPC_GPIO2->FIOPIN
+
 
+
#define LcdDataBusDirnReg  LPC_GPIO1->FIODIR
+
#define LcdCtrlBusDirnReg      LPC_GPIO2->FIODIR
+
 
+
 
+
#define LCD_D0    20
+
#define LCD_D1    21
+
#define LCD_D2    22
+
#define LCD_D3    23
+
#define LCD_D4    24
+
#define LCD_D5    25
+
#define LCD_D6    26
+
#define LCD_D7    27
+
 
+
#define LCD_RS    0
+
#define LCD_RW    1
+
#define LCD_EN    2
+
</syntaxhighlight>
+
 
<br/>
 
<br/>
 
<br/>
 
<br/>
Line 111: Line 88:
 
=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 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.
  
 
===Timing Diagram===
 
===Timing Diagram===
Line 122: Line 99:
  
 
===Steps for Sending Command:===  
 
===Steps for Sending Command:===  
Below are the steps for sending the command
 
 
*step1: Send the I/P command to LCD.
 
*step1: Send the I/P command to LCD.
 
*step2: Select the Control Register by making RS low.
 
*step2: Select the Control Register by making RS low.
 
*step3: Select Write operation making RW low.
 
*step3: Select Write operation making RW low.
 
*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.
<syntaxhighlight>
+
<html>
/* Function to send the command to LCD */
+
<script src="https://gist.github.com/Amritach/a2be754e2f1cc8206293.js"></script>
void Lcd_CmdWrite(char cmd)
+
</html>
{
+
    sendByte(cmd);                        //Send the command
+
    LcdControlBusPort &= ~(1<<LCD_RS); // Send LOW pulse on RS pin for selecting Command register
+
    LcdControlBusPort &= ~(1<<LCD_RW); // Send LOW pulse on RW pin for Write operation
+
    LcdControlBusPort |= (1<<LCD_EN);  // Generate a High-to-low pulse on EN pin
+
    delay(1000);
+
    LcdControlBusPort &= ~(1<<LCD_EN);
+
 
+
    delay(10000);
+
}
+
</syntaxhighlight>
+
  
 
===Steps for Sending Data:===  
 
===Steps for Sending Data:===  
Line 148: Line 113:
 
*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.
 
The timings are similar as above only change is that '''RS''' is made high for selecting Data register.
 
The timings are similar as above only change is that '''RS''' is made high for selecting Data register.
<syntaxhighlight>
+
<html>
/* Function to send the data to LCD */
+
<script src="https://gist.github.com/Amritach/523b84f22b9d85b27fbf.js"></script>
void Lcd_DataWrite(char dat)
+
</html>
{
+
    sendByte(dat);                    //Send the data
+
    LcdControlBusPort |= (1<<LCD_RS);  // Send HIGH pulse on RS pin for selecting data register
+
    LcdControlBusPort &= ~(1<<LCD_RW); // Send LOW pulse on RW pin for Write operation
+
    LcdControlBusPort |= (1<<LCD_EN);  // Generate a High-to-low pulse on EN pin
+
    delay(1000);
+
    LcdControlBusPort &= ~(1<<LCD_EN);
+
  
    delay(10000);
+
=Code Example=
}
+
===Example 1===
</syntaxhighlight>
+
Here is the complete code for displaying the data on 2x16 LCD in 8-bit mode.
 +
<html>
 +
<script src="https://gist.github.com/Amritach/8abaf223a2162b17d2af.js"></script>
 +
</html>
  
=Code=
+
===Using Explore Embedded Libraries :===
Here is the complete code for displaying the data on 2x16 LCD in 4-bit mode.
+
In the above tutorial we just discussed how to interface 2x16Lcd in 8-bit mode.<br>
<syntaxhighlight>
+
Once you know the working of lcd, you can directly use the ExploreEmbedded libraries to play around with your LCD.<br>
#include<lpc17xx.h>
+
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>
 +
The below sample code shows how to use the already available LCD functions.<br>
  
/* Configure the data bus and Control bus as per the hardware connection */
+
Refer this link for more info on LCD libraries.
#define LcdDataBusPort      LPC_GPIO1->FIOPIN
+
#define LcdControlBusPort  LPC_GPIO2->FIOPIN
+
  
#define LcdDataBusDirnReg  LPC_GPIO1->FIODIR
+
<html>
#define LcdCtrlBusDirnReg      LPC_GPIO2->FIODIR
+
<script src="https://gist.github.com/Amritach/f6623a13490f16d1ff6a.js"></script>
 
+
</html>
 
+
#define LCD_D0    20
+
#define LCD_D1    21
+
#define LCD_D2    22
+
#define LCD_D3    23
+
#define LCD_D4    24
+
#define LCD_D5    25
+
#define LCD_D6    26
+
#define LCD_D7    27
+
 
+
#define LCD_RS      0
+
#define LCD_RW    1
+
#define LCD_EN    2
+
 
+
 
+
/* Masks for configuring the DataBus and Control Bus direction */
+
#define PortDataBusConfig  ((1<<LCD_D0)|(1<<LCD_D1)|(1<<LCD_D2)|(1<<LCD_D3)|(1<<LCD_D4)|(1<<LCD_D5)|(1<<LCD_D6)|(1<<LCD_D7))
+
#define PortCtrlBusConfig  ((1<<LCD_RS)|(1<<LCD_RW)|(1<<LCD_EN))
+
#define LCD_dataBusMask    ((1<<LCD_D0)|(1<<LCD_D1)|(1<<LCD_D2)|(1<<LCD_D3)|(1<<LCD_D4)|(1<<LCD_D5)|(1<<LCD_D6)|(1<<LCD_D7))
+
 
+
 
+
 
+
 
+
/* local function to generate some delay */
+
void delay(int cnt)
+
{
+
    int i;
+
    for(i=0;i<cnt;i++);
+
}
+
 
+
 
+
 
+
 
+
 
+
/* Function send the a nibble on the Data bus (LCD_D4 to LCD_D7) */
+
void sendByte(char byte)
+
{
+
    LcdDataBusPort&=~(LCD_dataBusMask);                  // Clear previous data
+
    LcdDataBusPort|= (((byte >>0x00) & 0x01) << LCD_D0);
+
    LcdDataBusPort|= (((byte >>0x01) & 0x01) << LCD_D1);
+
    LcdDataBusPort|= (((byte >>0x02) & 0x01) << LCD_D2);
+
    LcdDataBusPort|= (((byte >>0x03) & 0x01) << LCD_D3);
+
    LcdDataBusPort|= (((byte >>0x04) & 0x01) << LCD_D4);
+
    LcdDataBusPort|= (((byte >>0x05) & 0x01) << LCD_D5);
+
    LcdDataBusPort|= (((byte >>0x06) & 0x01) << LCD_D6);
+
    LcdDataBusPort|= (((byte >>0x07) & 0x01) << LCD_D7);
+
}
+
 
+
 
+
/* Function to send the command to LCD. As it is 4bit mode, a byte of data is sent in two 4-bit nibbles */
+
void Lcd_CmdWrite(char cmd)
+
{
+
    sendByte(cmd);  //Send higher nibble
+
    LcdControlBusPort &= ~(1<<LCD_RS); // Send LOW pulse on RS pin for selecting Command register
+
    LcdControlBusPort &= ~(1<<LCD_RW); // Send LOW pulse on RW pin for Write operation
+
    LcdControlBusPort |= (1<<LCD_EN);  // Generate a High-to-low pulse on EN pin
+
    delay(1000);
+
    LcdControlBusPort &= ~(1<<LCD_EN);
+
 
+
    delay(10000);
+
}
+
 
+
 
+
 
+
void Lcd_DataWrite(char cmd)
+
{
+
    sendByte(cmd);  //Send higher nibble
+
    LcdControlBusPort |= (1<<LCD_RS);  // Send HIGH pulse on RS pin for selecting data register
+
    LcdControlBusPort &= ~(1<<LCD_RW); // Send LOW pulse on RW pin for Write operation
+
    LcdControlBusPort |= (1<<LCD_EN);  // Generate a High-to-low pulse on EN pin
+
    delay(1000);
+
    LcdControlBusPort &= ~(1<<LCD_EN);
+
 
+
    delay(10000);
+
}
+
 
+
 
+
 
+
void main()
+
{
+
    char i,a[]={"Good morning!"};
+
    SystemInit();                    //Clock and PLL configuration
+
 
+
    LcdDataBusDirnReg = PortDataBusConfig;    // Configure all the LCD pins as output
+
    LcdCtrlBusDirnReg = PortCtrlBusConfig;
+
                                           
+
    Lcd_CmdWrite(0x38);                      // enable 5x7 mode for chars
+
    Lcd_CmdWrite(0x01);                      // Display OFF, Cursor ON
+
    Lcd_CmdWrite(0x0E);                    // Clear Display
+
    Lcd_CmdWrite(0x80);                      // Move the cursor to beginning of first line
+
 
+
 
+
    Lcd_DataWrite('H');
+
    Lcd_DataWrite('e');
+
    Lcd_DataWrite('l');
+
    Lcd_DataWrite('l');
+
    Lcd_DataWrite('o');
+
    Lcd_DataWrite(' ');
+
    Lcd_DataWrite('w');
+
    Lcd_DataWrite('o');
+
    Lcd_DataWrite('r');
+
    Lcd_DataWrite('l');
+
    Lcd_DataWrite('d');
+
 
+
    Lcd_CmdWrite(0xc0);
+
    for(i=0;a[i]!=0;i++)
+
    {
+
        Lcd_DataWrite(a[i]);
+
    }
+
 
+
    while(1);
+
}
+
 
+
</syntaxhighlight>
+
  
 
= Downloads=
 
= Downloads=
 +
https://codeload.github.com/ExploreEmbedded/Explore-Cortex-M3-LPC1768-Stick-DVB-14001/zip/master
  
 
{{#widget:Facebook_Like_Box|profile=https://www.facebook.com/ExploreEmbedded}}
 
  
 
Have a opinion, suggestion , question or feedback about the article let it out here!
 
Have a opinion, suggestion , question or feedback about the article let it out here!
 
{{DISQUS}}
 
{{DISQUS}}

Latest revision as of 08:49, 30 July 2016

In this tutorial we are going to see how to interface a 2x16 LCD with LPC1768 in 8-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. Apart from alpha numeric chars it also provides the provision to display the custom characters by creating the pattern. Scope of this tutorial is to show how to display the alpha numeric chars on LCD, Generating and displaying the custom chars will be discussed in subsequent tutorials.

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 alpha numeric lcd has a 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.

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. 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.

Hardware Connections

LCD 8Bit bb.jpg

Schematic

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



Port Connection

This section shows how to configure the GPIO for interfacing the LCD.
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

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 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.

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 Example

Example 1

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

Using Explore Embedded Libraries :

In the above tutorial we just discussed how to interface 2x16Lcd in 8-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.

Refer this link for more info on LCD libraries.

Downloads

https://codeload.github.com/ExploreEmbedded/Explore-Cortex-M3-LPC1768-Stick-DVB-14001/zip/master


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