Hey..I am trying my hands on lcd 16x2 and somehow i got it to work.I'm
trying to display an output on 16x2 which is dependent on sum input on
other pins but cant get it to work.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 | DDRC=0;
|
19 | PORTC=0b0000000;
|
20 |
|
21 | init_LCD(); // initialization of LCD
|
22 | _delay_ms(50); // delay of 50 mili seconds
|
23 | LCD_write_string("“hello world”"); // function to print string on LCD
|
24 | if(PINC==0b0000001){
|
25 | _delay_ms(50);
|
26 | LCD_cmd(0xc0);
|
27 | _delay_ms(1);
|
28 | LCD_write_string("THIS IS WORKING ");}
|
29 | return 0;
|
30 | }
|
31 |
|
32 | void init_LCD(void)
|
33 | {
|
34 | LCD_cmd(0x38); // initialization of 16X2 LCD in 8bit mode
|
35 | _delay_ms(1);
|
36 | LCD_cmd(0x01); // clear LCD
|
37 | _delay_ms(1);
|
38 | LCD_cmd(0x0E); // cursor ON
|
39 | _delay_ms(1);
|
40 | LCD_cmd(0x80); // —8 go to first line and –0 is for 0th position
|
41 | _delay_ms(1);
|
42 | return;
|
43 | }
|
44 |
|
45 | void LCD_cmd(unsigned char cmd)
|
46 | {
|
47 | LCD_DATA=cmd;
|
48 | ctrl =(0<<rs)|(0<<rw)|(1<<en);
|
49 | _delay_ms(1);
|
50 | ctrl =(0<<rs)|(0<<rw)|(0<<en);
|
51 | _delay_ms(50);
|
52 | return;
|
53 | }
|
54 |
|
55 | void LCD_write(unsigned char data)
|
56 | {
|
57 | LCD_DATA= data;
|
58 | ctrl = (1<<rs)|(0<<rw)|(1<<en);
|
59 | _delay_ms(1);
|
60 | ctrl = (1<<rs)|(0<<rw)|(0<<en);
|
61 | _delay_ms(50);
|
62 | return ;
|
63 | }
|
64 |
|
65 | void LCD_write_string(unsigned char *str) //store address value of the string in pointer *str
|
66 | {
|
67 | int i=0;
|
68 | while(str[i]!='\0') // loop will go on till the NULL character in the string
|
69 | {
|
70 | LCD_write(str[i]); // sending data on LCD byte by byte
|
71 | i++;
|
72 | }
|
73 | return;
|
74 | }
|
The first line ie HELLO WORLD is working fine but there is no display in
the second line.