I want to use lcd in 4 bit mode using atmega8, and i am facing problems in that... Someone help me to sort out this problem.
I am using 16x2 character LCD and my pin configurations are,
Code C - [expand ] 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
LCD_DATA > PORTD ( PD4, PD5, PD6, PD7)
Rs > PC5
E > PC4
my code is/ /****************************************************************************************************/ /
#define F_CPU 4000000UL
#include<avr/io.h>
#include<util/delay.h>
#define DATA PORTD
//#define RS PC5
//#define E PC4
void lcd_init( void )
{
lcd_cmd( 0x28 ) ;
_delay_ms( 25 ) ;
lcd_cmd( 0x01 ) ;
_delay_ms( 25 ) ;
lcd_cmd( 0x0E ) ;
_delay_ms( 25 ) ;
lcd_cmd( 0x06 ) ;
_delay_ms( 25 ) ;
}
void lcd_cmd( unsigned int cmd)
{
DATA= cmd & 0xF0 ;
PORTC= ( 0 << PC5) ; //|(1<<PC4);
PORTC= ( 1 << PC4) ;
_delay_ms( 25 ) ;
PORTC= ( 0 << PC4) ;
DATA= ( ( cmd<< 4 ) & 0xF0 ) ;
PORTC= ( 0 << PC5) ; //|(1<<PC4);
PORTC= ( 1 << PC4) ;
_delay_ms( 25 ) ;
PORTC= ( 0 << PC4) ;
return ;
}
void lcd_data1( unsigned char data1)
{
DATA= data1 & 0xF0 ;
PORTC= ( 1 << PC5) ; //|(1<<PC4);
PORTC= ( 1 << PC4) ;
_delay_ms( 25 ) ;
PORTC= ( 0 << PC4) ;
DATA= ( ( data1<< 4 ) & 0xF0 ) ;
PORTC= ( 1 << PC5) ; //|(1<<PC4);
PORTC= ( 1 << PC4) ;
_delay_ms( 25 ) ;
PORTC= ( 0 << PC4) ;
return ;
}
int main( void )
{
DDRD= 0xFF ;
DDRC= 0x30 ;
_delay_ms( 2 ) ;
lcd_init( ) ;
while ( 1 )
{
_delay_ms( 2 ) ;
lcd_data1( 'B' ) ;
}
return 0 ;
}
Last edited by a moderator: Nov 30, 2012