LCD programming

Thread Starter

andrew24

Joined Aug 20, 2008
76
Hallo! i'm trying to send a text string to LCD for the first time. Could you check my code for any errors? Thanks :)

Rich (BB code):
/LCD DATA BUS CONNECTED TO PORTD , 8 bit mode
// RS connected to PB0, R/W to PB1, E to PB2

#include <avr/io.h>
#include <util/delay.h>
#include <string.h>


void LCD_WAIT(void)
 {
 
 DDRD=0; //Set all pins as inputs for reading busy flag
 PORTB=0x00; // It's a read command
 
 // checking if the LCD  is still busy
 while(!(PIND7&0b100000)){   
_delay_loop_2(10);
}
}


void LCD_SEND_COMMAND(void)
 {
 
 DDRD=0xFF; //Set all pins as outputs for sending instruction
 PORTB|=(1<<PORTB1);; // It's a write command
 
 PORTB|=(1<<PORTB2); //E=1
 _delay_loop_2(1000); 
 PORTB&=0b1111011; // E=0
 _delay_loop_2(1000);
 
 LCD_WAIT();

 DDRD=0XFF; //SET PORTD PINS AS OUTPUT
 DDRB=0XFF; //SET PORTB PINS AS OUTPUT- FOR E,RW,RS
 
 }
 
 

 void LCD_INIT(void)
 {
 
 // FUNCTION SET
 
 PORTD=0x38; //8 BIT INTERFACE,2 LINES,5*7 FONT
 LCD_SEND_COMMAND();

 
 // DISPLAY ON/OFF CONTROL
 
 PORTD=0x0E; //DISPLAY ON, CURSOR ON, BLINK OFF
 LCD_SEND_COMMAND();
 
 
 // DISPLAY ON/OFF CONTROL
 
 PORTD=0x06; // AUTOMATIC CURSOS POSITION INCREMENT
 LCD_SEND_COMMAND();
 
 // CLEAR DISPLAY
 PORTD=0x01; //CLAER DISPLAY & RETURN CURSOR HOME
 LCD_SEND_COMMAND();
 }
 
 

void LCD_WRITE(char TEXT[])
 {
 unsigned char i;

PORTB|=(1<<PORTB0);

 for(i=0;(i<32)&&(TEXT!='\0');i++)
 {
 PORTD=TEXT;
 LCD_SEND_COMMAND();
 
 }
 }
 

int main(void){
 
 LCD_INIT();
 LCD_WRITE("Andrew");
 }
 
Last edited:

hgmjr

Joined Jan 28, 2005
9,027
One comment I would make is that your function "main" is defined to return an integer. I am surprised that you are able to get the program to compile without a return(0); statement.

hgmjr
 

hgmjr

Joined Jan 28, 2005
9,027
My comments are based on the assumption that your LCD display is based on the Hitachi HD44780 LCD driver.

My experience with LCD programming is that they are very picky about the initialization process. I seem to recall that a delay is required following power up to allow the LCD driver to complete its internal power up sequence. I don't see any delay being provided.

hgmjr
 

Thread Starter

andrew24

Joined Aug 20, 2008
76
Yes,it's HD4478 :) should I provide a delay right after this line? " void LCD_INIT(void){ "
thanks for correcting my mistakes!
 
Last edited:

hgmjr

Joined Jan 28, 2005
9,027
You may want to consider using the LCD.h and LCD.c routines that are included in the AVRSTUDIO/WINAVR package to bootstrap your LCD interface.

hgmjr
 

Thread Starter

andrew24

Joined Aug 20, 2008
76
I'd rather write my own routine, i think the understanding comes when one succesfully makes something by himself :) However, I connected the LCD, loaded the code to the microcontroller, but nothing happened.. One strange thing is that if i connect LCD's VCC and VDD and also backlight's anode to the power supply there is no backlight, but if i leave VCC floating, the there is a backlight..
 

Thread Starter

andrew24

Joined Aug 20, 2008
76
I will try that. Is there any simple way to scheck if the LCD is still stil alive? When connected to the power supply how it should behave? i found it to be very hot after my programming atempts :)
 

AlexR

Joined Jan 16, 2008
732
The initialisation process for an LCD display is quite involved and easy to get wrong.
This document tells you all you need to know. http://fab.cba.mit.edu/classes/MIT/863.05/classes/11_14/44780.pdf
Take a look at pages 45/46 for the initialisation sequence and pay close attention to the time delays between instructions.

I have found that when you power up an LCD without initialising it, it comes up in 1 line mode with the top line line showing all solid blocks but I suspect it could come up in any state.
 

AlexR

Joined Jan 16, 2008
732
The honest answer is I don't know but I suspect that the first 2 times it is for the HD44780 chip reset sequence and the third time is to put it into 8 bit mode.

In any case that data sheet is produced by Hitachi (the manufactures of the HD44780 chip) and if you follow their recommendations everything works as it should.
 

hgmjr

Joined Jan 28, 2005
9,027
I have programmed a couple of LCD modules and it is just as alexr has indicated. It is easy to err in performing the initialization sequence and if you don't get it right the LCD will not operate. The delays that are recommended in between the commands during the power up sequence are critical minimum delay times. They are needed for the HD44780 chip to take care of its internal set of registers. If you shorten these delays or leave them out altogether, the LCD will not operate or its operation will be irradic.

Since this is your first LCD programming experience, I would recommend that you start by using the built-in function to verify that your LCD display is not damaged since you have stated that it is getting hot. Keep in mind that programming an LCD module is a challenge even for the seasoned programmer. Once you have convinced yourself that the LCD module is good, then you can try your hand at writing your own LCD interface program secure in the knowledge that if it does not work then you need to debug your code.

hgmjr
 

Thread Starter

andrew24

Joined Aug 20, 2008
76
Thanks for all that helped me! I finaly made it to display my name.
However there is one bug: when i send instruction to clear the screen and return the cursor home, and then try to display some text, lcd displays nothing, just blinks cursor. When i commented the clear screen instruction the text was displayed, but but screen is unclear from previos tests.
How can i clear screen everytime i send something new to lcd, and at the same time display a text?
thanks, Andrew
 

hgmjr

Joined Jan 28, 2005
9,027
Thanks for all that helped me! I finaly made it to display my name.
However there is one bug: when i send instruction to clear the screen and return the cursor home, and then try to display some text, lcd displays nothing, just blinks cursor. When i commented the clear screen instruction the text was displayed, but but screen is unclear from previos tests.
How can i clear screen everytime i send something new to lcd, and at the same time display a text?
thanks, Andrew
A bit of a guess here but I suspect that you may not be giving the LCD time enough to clear its memory and return to readiness to except the next command you send it. I would recommend that you look into implementing the code needed to test the "busy" flag that is available from the LCD. This should eliminate your problem. If you test the "busy" flag and wait for LCD to return to the "not busy" state, things will work more consistently.

hgmjr
 

Thread Starter

andrew24

Joined Aug 20, 2008
76
You were right, that helped me! I have another question, about how to send a variable, or a number to an lcd. i wrote a function for that, but it doesn't work corectly: it was meant to split a number for e.g 245 to a separate digits (2,4,5) and assignd an ASCII value for them.

void LCD_WRITE_CHAR(unsigned int charr[])
{
unsigned char i;

for(i=0;i<sizeof(charr)-1;i++)
{

PORTB|=0x01; //RS low for writting data
_delay_loop_1(1400/1000); // WAIT 140 ns

if (charr==0) PORTD=48;
else if (charr==1) PORTD=49;
else if (charr==2) PORTD=50;
else if (charr==3) PORTD=51;
else if (charr==4) PORTD=52;
else if (charr==5) PORTD=53;
else if (charr==6) PORTD=54;
else if (charr==7) PORTD=55;
else if (charr==8) PORTD=56;
else if (charr==9) PORTD=57;

PORTB|=0x04; // Set E high
_delay_loop_1(80/1000); // Wait 80ns
PORTB=0x00; // Set E low
_delay_loop_1(10/1000); // wait 10 ns

}
}
 
Last edited:
Top