Friday 6 April 2012

LCD interfacing to Atmega16

        Introduction:

Here I'll discuss how to interface LCD display to microcontrollers. There are different types of LCD diplay available in market, but the most commonly used character based LCD display is Hitachi's HD44780 controller or other which are compatible with HD44580.

         PINs Of  LCD Display: 

 This is a 16x2 LCD, means it has to lines and each line can display 16 characters. Most LCDs have 14 Pins and some LCDs have 2 controller has 16 Pins (two pins are extra in both for back-light
LED connections). Pin description is shown:





Fixed voltage pins:

           Pin1(Vss/Gnd), Pin2(Vcc), Pin3(Vee, connected to a pot) are dc lines, they are directly connected to voltage sources and to ground, they are not controlled by the microcontroller. Click here to see the diagram. 

Control Pins:

         Pin4(RS), Pin(R/W), Pin6(EN) are control pins, I have connected these pins to PD4, PD5, PD6 respectedly,click here to see the diagram.
        EN: A negative edge of clock pulse must be  given to this pin to make the LCD work each time you write a dataport(Pin7 to Pin14). How you will give an negative edge of clock pusle here? First SET the pin then RESET the pin. 
        For R/W and RS see above picture.

Backlight Control Pins:

         There are two extra pins (LED+ and LED-) to turn on the backlight of LCD display. Click here to see the diagram.

Data Pins:

         Pin7 to Pin14 are data pins. They are connected to portB of the microcontroller. Here Pin7 is LSB and Pin14 is MSB. Click here to see the diagram.

Basic Instructions For LCD Display:

No.
Instruction
Hex
Decimal
1
Function Set: 8-bit, 1 Line, 5x7 Dots
0x30
48
2
Function Set: 8-bit, 2 Line, 5x7 Dots
0x38
56
3
Function Set: 4-bit, 1 Line, 5x7 Dots
0x20
32
4
Function Set: 4-bit, 2 Line, 5x7 Dots
0x28
40
5
Entry Mode
0x06
6
6
Display off Cursor off
(clearing display without clearing DDRAM content)
0x08
8
7
Display on Cursor on
0x0E
14
8
Display on Cursor off
0x0C
12
9
Display on Cursor blinking
0x0F
15
10
Shift entire display left
0x18
24
12
Shift entire display right
0x1C
30
13
Move cursor left by one character
0x10
16
14
Move cursor right by one character
0x14
20
15
Clear Display (also clear DDRAM content)
0x01
1
16
Set DDRAM address or cursor position on display
0x80+add
128+add
17
Set CGRAM address or set pointer to CGRAM location
0x40+add
64+add

Sending Commands to LCD

To send commands we simply need to select the command register. Following are the
steps:
  1. Move data to LCD port. 
  2. select command register.
  3.  select write operation. 
  4. send enable signal.
  5.  wait for LCD to process the command
I have writen a subroutine to perform step2,3,4 and 5 in wrcomm() function

code is:
int wrcomm(void)

{

commport &= ~(1 << rs); //Setting RS = 0, selecting command register
commport &= ~(1 << wr); //Setting RW = 0
commport |= (1 << en); //EN = 1
commport &= ~(1 << en); //EN = 0, thus giving high to low pulse on Enable pin

_delay_ms(100);        //wait for a moment.

return 1;

}

  

Sending Data to LCD

To send data we simply need to select the data register. Everything is same as
the command routine. Following are the steps:
  1. Move data to LCD port.
  2. select data register.
  3. select write operation.
  4. send enable signal.
  5. wait for LCD to process the data
I have writen a subroutine wrdata();

Code is:
 
int wrdata(void)

{

commport |= (1 << rs); //Setting RS = 1, selecting data register
commport &= ~(1 << wr); //Setting RW = 0
commport |= (1 << en); //EN = 1
commport &= ~(1 << en); //EN = 0, thus giving high to low pulse on Enable pin
_delay_ms(100) ; //wait for the process to complete

return 1;

}


Initializing LCD:

  1. Choose a function set, write down the corresponding command value to the register, I have chosen function 2, 8bit, 2 lines, 5x7 dots.
  2. Choose any of Instruction set no 6,7,8,9. I have chosen no9
  3. Clear the LCD
  4. Entry  mode
The funcion is LCD_init()
Code is:
 int LCD_init()

{

dataport = 0x38; //initialize LCD 2 lines, 5x7 matrix
wrcomm(); //Right the command byte to command register

dataport = 0x0F; //Display on cursor blinking command
wrcomm(); //Right the command byte to command register

dataport = 0x01; //clear LCD
wrcomm(); //Right the command byte to command register

dataport = 0x06; //auto increment
wrcomm(); //Right the command byte to command register

return 1;

}  


Sending String to LCD:  

Remember that this types of LCDs are capable of displaying only character value.
If you want to display a digit say 5 and you write
dataport = 5;
wrdata();
 But you will see some unwanted character. Actually this is a character whose ASCII value is 5. To display 5 you have to write it like:
dataport ='5';
wrdata();
                             or
dataport=5+48; // 48 is ASCII value of zero
wrdata();                                                                                                                                            

But how to display a whole string?
Write down a function, send the base address of the string array as function argument, remember that the type of the function argument should be pointer tpe so that it can hold the address.
here is my code:

int LCD_SendData_string(unsigned char *var)
{
while(*var)
{
dataport=(*var++);
wrdata();
}
return 1;
}

void main()
{
..........
.........
........
unsigned char j[]='"Now Temp:";
LCD_SendData_string(unsigned char *j)
....................
............
.......


I'll publish the whole code with Temperature sensing and displaying it on LCD tutorial,

9 comments:

  1. good work dear..congrats...I am very excited to see ur work...

    ReplyDelete
  2. an very systematic code.... thank you

    ReplyDelete
  3. thanks you its very gud to write to code

    ReplyDelete
  4. Sir can you please define the commport and dataport...

    ReplyDelete
  5. Sir if you accidentally reversed the connection for the lcd flex cable would it damage the display?Thanks!

    ReplyDelete
  6. Hi there to everybody, it’s my first go to see of this web site; this weblog consists of awesome and in fact good stuff for visitors. Hurrah, that’s what I was exploring for, what stuff! Existing here at this blog, thanks admin of this web site. You can also visit LED Display Screen for more zoomvisual.com.sg related information and knowledge.

    ReplyDelete
  7. Hi there to everybody, it’s my first go to see of this web site; this weblog consists of awesome and in fact good stuff for visitors. Hurrah, that’s what I was exploring for, what stuff! Existing here at this blog, thanks admin of this web site. You can also visit LED Display Screen for more zoomvisual.com.sg related information and knowledge.

    ReplyDelete