-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlcd.h
223 lines (189 loc) · 4.22 KB
/
lcd.h
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
#define RS 0
#define RW 1
#define EN 2
#define lcd_port PORTC
#define sbit(reg,bit) reg |= (1<<bit) // Macro defined for Setting a bit of any register.
#define cbit(reg,bit) reg &= ~(1<<bit) // Macro defined for Clearing a bit of any register.
void init_ports();
void lcd_reset();
void lcd_init();
void lcd_wr_command(unsigned char);
void lcd_wr_char(char);
void lcd_line1();
void lcd_line2();
void lcd_string(char*);
unsigned int temp;
unsigned int unit;
unsigned int tens;
unsigned int hundred;
unsigned int thousand;
unsigned int million;
/*//Function to configure LCD port
void lcd_port_config (void)
{
DDRC = DDRC | 0xF7; //all the LCD pin's direction set as output
PORTC = PORTC & 0x80; // all the LCD pins are set to logic 0 except PORTC 7
}*/
//Function to Reset LCD
void lcd_set_4bit()
{
_delay_ms(1);
cbit(lcd_port,RS); //RS=0 --- Command Input
cbit(lcd_port,RW); //RW=0 --- Writing to LCD
lcd_port = 0x30; //Sending 3
sbit(lcd_port,EN); //Set Enable Pin
_delay_ms(5); //Delay
cbit(lcd_port,EN); //Clear Enable Pin
_delay_ms(1);
cbit(lcd_port,RS); //RS=0 --- Command Input
cbit(lcd_port,RW); //RW=0 --- Writing to LCD
lcd_port = 0x30; //Sending 3
sbit(lcd_port,EN); //Set Enable Pin
_delay_ms(5); //Delay
cbit(lcd_port,EN); //Clear Enable Pin
_delay_ms(1);
cbit(lcd_port,RS); //RS=0 --- Command Input
cbit(lcd_port,RW); //RW=0 --- Writing to LCD
lcd_port = 0x30; //Sending 3
sbit(lcd_port,EN); //Set Enable Pin
_delay_ms(5); //Delay
cbit(lcd_port,EN); //Clear Enable Pin
_delay_ms(1);
cbit(lcd_port,RS); //RS=0 --- Command Input
cbit(lcd_port,RW); //RW=0 --- Writing to LCD
lcd_port = 0x20; //Sending 2 to initialise LCD 4-bit mode
sbit(lcd_port,EN); //Set Enable Pin
_delay_ms(5); //Delay
cbit(lcd_port,EN); //Clear Enable Pin
}
//Function to Initialize LCD
void lcd_init()
{
lcd_set_4bit();
_delay_ms(1);
lcd_wr_command(0x28); //LCD 4-bit mode and 2 lines.
lcd_wr_command(0x01);
lcd_wr_command(0x06);
lcd_wr_command(0x0E);
lcd_wr_command(0x80);
}
//Function to Write Command on LCD
void lcd_wr_command(unsigned char cmd)
{
unsigned char temp;
temp = cmd;
temp = temp & 0xF0;
lcd_port &= 0x0F;
lcd_port |= temp;
cbit(lcd_port,RS);
cbit(lcd_port,RW);
sbit(lcd_port,EN);
_delay_ms(5);
cbit(lcd_port,EN);
cmd = cmd & 0x0F;
cmd = cmd<<4;
lcd_port &= 0x0F;
lcd_port |= cmd;
cbit(lcd_port,RS);
cbit(lcd_port,RW);
sbit(lcd_port,EN);
_delay_ms(5);
cbit(lcd_port,EN);
}
//Function to Write Data on LCD
void lcd_wr_char(char letter)
{
char temp;
temp = letter;
temp = (temp & 0xF0);
lcd_port &= 0x0F;
lcd_port |= temp;
sbit(lcd_port,RS);
cbit(lcd_port,RW);
sbit(lcd_port,EN);
_delay_ms(5);
cbit(lcd_port,EN);
letter = letter & 0x0F;
letter = letter<<4;
lcd_port &= 0x0F;
lcd_port |= letter;
sbit(lcd_port,RS);
cbit(lcd_port,RW);
sbit(lcd_port,EN);
_delay_ms(5);
cbit(lcd_port,EN);
}
//Function to bring cursor at home position
void lcd_home()
{
lcd_wr_command(0x80);
}
//Function to Print String on LCD
void lcd_string(char *str)
{
while(*str != '\0')
{
lcd_wr_char(*str);
str++;
}
}
//Position the LCD cursor at "row", "column".
void lcd_cursor (char row, char column)
{
switch (row) {
case 1: lcd_wr_command (0x80 + column - 1); break;
case 2: lcd_wr_command (0xc0 + column - 1); break;
case 3: lcd_wr_command (0x94 + column - 1); break;
case 4: lcd_wr_command (0xd4 + column - 1); break;
default: break;
}
}
//Function To Print Any input value upto the desired digit on LCD
void lcd_print (char row, char coloumn, unsigned int value, int digits)
{
unsigned char flag=0;
if(row==0||coloumn==0)
{
lcd_home();
}
else
{
lcd_cursor(row,coloumn);
}
if(digits==5 || flag==1)
{
million=value/10000+48;
lcd_wr_char(million);
flag=1;
}
if(digits==4 || flag==1)
{
temp = value/1000;
thousand = temp%10 + 48;
lcd_wr_char(thousand);
flag=1;
}
if(digits==3 || flag==1)
{
temp = value/100;
hundred = temp%10 + 48;
lcd_wr_char(hundred);
flag=1;
}
if(digits==2 || flag==1)
{
temp = value/10;
tens = temp%10 + 48;
lcd_wr_char(tens);
flag=1;
}
if(digits==1 || flag==1)
{
unit = value%10 + 48;
lcd_wr_char(unit);
}
if(digits>5)
{
lcd_wr_char('E');
}
}