16x2 lcd help

OP #3524263
Rate this post
useful
not useful
Hey,I was working on atmega 8 and tried to use a 16x2lcd with it but lcd 
is only showing blocks and nothing else. I want to know whether 
something is wrong with my code or with my connections.Here is my code..
1
#define F_CPU 1000000UL
2

3
#include <avr/io.h>
4
#include <util/delay.h>
5

6
#define LCD_DATA PORTB                //LCD data port
7
#define ctrl PORTD
8
#define en PD2                         // enable signal
9
#define rw PD1                       // read/write signal
10
#define rs PD0                     // register select signal
11

12

13

14
int main()
15
{
16
DDRB=0xff;                                  // setting the port B
17
DDRD=0x07;                                // setting for port D
18
init_LCD();                                 // initialization of LCD
19
_delay_ms(50);                        // delay of 50 mili seconds
20
LCD_write_string("“hello world”");                      // function to print string on LCD
21
return 0;
22
}
23

24
void init_LCD(void)
25
{
26
LCD_cmd(0x38);                            // initialization of 16X2 LCD in 8bit mode
27
_delay_ms(1);
28
LCD_cmd(0x01);                                 // clear LCD
29
_delay_ms(1);
30
LCD_cmd(0x0E);                        // cursor ON
31
_delay_ms(1);
32
LCD_cmd(0x80);                     // —8 go to first line and –0 is for 0th position
33
_delay_ms(1);
34
return;
35
}
36

37
void LCD_cmd(unsigned char cmd)
38
{
39
LCD_DATA=cmd;
40
ctrl =(0<<rs)|(0<<rw)|(1<<en);
41
_delay_ms(1);
42
ctrl =(0<<rs)|(0<<rw)|(0<<en);
43
_delay_ms(50);
44
return;
45
}
46

47
void LCD_write(unsigned char data)
48
{
49
LCD_DATA= data;
50
ctrl = (1<<rs)|(0<<rw)|(1<<en);
51
_delay_ms(1);
52
ctrl = (1<<rs)|(0<<rw)|(0<<en);
53
_delay_ms(50);
54
return ;
55
}
56

57
void LCD_write_string(unsigned char *str)             //store address value of the string in pointer *str
58
{
59
int i=0;
60
while(str[i]!='\0')                               // loop will go on till the NULL character in the string
61
{
62
LCD_write(str[i]);                            // sending data on LCD byte by byte
63
i++;
64
}
65
return;
66
}
Guest #3524677
Rate this post
useful
not useful
RTFM regarding correct initialization. If in doubt use the Fleury 
LCD-lib. Reinventing the wheel sometimes is a PITA.

Which type of LCD are you using? They pretend to be compatible but not 
all are. What kind of connection between CPU and LCD? One row of black 
boxes or two? Have you tried adjusting the contrast voltage?
Guest #3524748
Rate this post
useful
not useful
What kind of controller does this display use? HD44780(-compatible)?

Ajay R. wrote:
[...]
> init_LCD();                           // initialization of LCD
> _delay_ms(50);                        // delay of 50 mili seconds
[...]

I think the delay has to be before the initialization of the display, 
just after the power was applied.

Steve

Reply

Please log in before posting.

or

Log in with Google account

Registration is free and takes only a minute.

Register now