Tuesday, July 14, 2009

The 1,2,3 in interfacing LCD with PIC (Software: Part 1)

This article is written assuming user has knowledge on hardware part.

There are few basic things which we have to bear in mind.

  1. 3 control lines:
    1. Register Select (RS)
    2. Read/Write (R/W)
    3. Enable (E)
  2. Simple instruction set you might need:
    1. Initializing – determine cursor’s position and characteristic.


Table 1: The command control codes

For more information, please refer to http://www.cytron.com.my/viewProduct.php?pid=FjswICgDMQkFLhMRHy0XHVI/kU2m3Az9XsdY/Nuz3hY=&store=

Next, we move on to the next part where we initialize the LCD and start displaying some characters. Hence, to simplify, the objectives of the following step are:

1. Initialize LCD

2. Display few characters

3. Determine location to display the characters

The first step to do is to initialize the LCD. As mentioned earlier, the 3 controls line must be set prior to the initialization. RS is set to ‘0’ so that the LCD is in command mode. Then, set the initialization bits (D7-D0) according to Table 1. The next step is to toggle the E pin. When E bit is being toggled, it enables the chip to accept data. The R/W pin is being connected to ground in this case as we do not require data to be read from the LCD module.


To perform the mentioned steps in details:

// clear screen

rs = 0;

PORTD = 0b00000001;

E = 1;

delay (300);

E = 0;

delay (300);


If you’re on the right track, you should see nothing on your LCD. If there are boxes on the first line, adjust the contrast so that it is semi-visible. This is the step to clear screen (refer to Table 1). Next, we set the cursor’s position and characteristic.


// initialize cursor

PORTD = 0b00001111;

E = 1;
delay (300);

E = 0;

delay (300);


After performing this code, you should be able to see your cursor at the first position of the LCD. The cursor is also blinking and underlined.


(NOTE: Remember to initialize your PORTD as output port and a while loop at the end of the code so that the program does not repeats itself when loaded.)


Now, you are ready to complete the second task, which is to display some characters. The steps to input characters are similar to initializing the LCD, however, they differ by the configuration of RS bit. When displaying characters, the RS must be set to ‘1’ to enable data transfer. As for the characters, they are identified using ASCII code. You can always get the code online. Just google it! In this tutorial, we will use only few characters, they are: ‘A’, ‘B’ and ‘C’.


// function set (refer table 1)

rs = 0;

PORTD = 0b00110100;

E = 1;

delay (300);

E = 0;

delay (300);

// write in characters

rs = 1;

PORTD = 0x41; //’A’

E = 1;

delay (300);

E = 0;

delay (300);

PORTD = 0x42; //’B’

E = 1;

delay (300);

E = 0;

delay (300);

PORTD = 0x43; // ‘C’

E = 1;

delay (300);

E = 0;

delay (300);


Now that you’re equipped with skills to key in characters and initializing the LCD, we can move on to determine the location of the cursor.


To test with the cursor location, we set our LCD into two lines mode, to have a wider range for manipulation.


Next, we will have to set the location we want our cursor to be at. For instance, we want to set the cursor to address 00001000. We set the display address command, 0b10001000.


// set the function (2 lines)

rs = 0;

PORTD = 0x38;

E = 1;

delay (300);

E = 0;

delay (300);

PORTD = 0x88; // move cursor to ninth location

E = 1;

delay (300);

E = 0;

delay (300);


Well, I guess that’s all for the basic of LCD which you need to know before advancing into more sophisticated program.

1 comment:

kaizen boy said...

...
how u expect people to read ur blog if u write these things ah?

-.-'''