-
Notifications
You must be signed in to change notification settings - Fork 3
/
LiquidCrystal.h
437 lines (402 loc) · 8.35 KB
/
LiquidCrystal.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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
//Library for ATmega 16
//LCD Functions
#define eS_PORTA0 0
#define eS_PORTA1 1
#define eS_PORTA2 2
#define eS_PORTA3 3
#define eS_PORTA4 4
#define eS_PORTA5 5
#define eS_PORTA6 6
#define eS_PORTA7 7
#define eS_PORTB0 10
#define eS_PORTB1 11
#define eS_PORTB2 12
#define eS_PORTB3 13
#define eS_PORTB4 14
#define eS_PORTB5 15
#define eS_PORTB6 16
#define eS_PORTB7 17
#define eS_PORTC0 20
#define eS_PORTC1 21
#define eS_PORTC2 22
#define eS_PORTC3 23
#define eS_PORTC4 24
#define eS_PORTC5 25
#define eS_PORTC6 26
#define eS_PORTC7 27
#define eS_PORTD0 30
#define eS_PORTD1 31
#define eS_PORTD2 32
#define eS_PORTD3 33
#define eS_PORTD4 34
#define eS_PORTD5 35
#define eS_PORTD6 36
#define eS_PORTD7 37
#ifndef D0
#define D0 eS_PORTA0
#define D1 eS_PORTA1
#define D2 eS_PORTA2
#define D3 eS_PORTA3
#endif
#include<util/delay.h>
void pinChange(int , int )
void Lcd8_Port(char )
void Lcd8_Cmd(char )
void Lcd8_Clear()
void Lcd8_SetCursor(char , char )
void Lcd8_Init()
void Lcd8_WriteChar(char )
void Lcd8_WriteString(char *)
void Lcd8_ShiftRight()
void Lcd8_ShiftLeft()
void Lcd4_Port(char )
void Lcd4_Cmd(char )
void Lcd4_Clear()
void Lcd4_SetCursor(char , char )
void Lcd4_Init()
void Lcd4_WriteChar(char )
void Lcd4_WriteString(char *)
void Lcd4_ShiftRight()
void Lcd4_ShiftLeft()
void pinChange(int a, int b)
{
if(b == 0)
{
if(a == eS_PORTA0)
PORTA &= ~(1<<PA0);
else if(a == eS_PORTA1)
PORTA &= ~(1<<PA1);
else if(a == eS_PORTA2)
PORTA &= ~(1<<PA2);
else if(a == eS_PORTA3)
PORTA &= ~(1<<PA3);
else if(a == eS_PORTA4)
PORTA &= ~(1<<PA4);
else if(a == eS_PORTA5)
PORTA &= ~(1<<PA5);
else if(a == eS_PORTA6)
PORTA &= ~(1<<PA6);
else if(a == eS_PORTA7)
PORTA &= ~(1<<PA7);
else if(a == eS_PORTB0)
PORTB &= ~(1<<PB0);
else if(a == eS_PORTB1)
PORTB &= ~(1<<PB1);
else if(a == eS_PORTB2)
PORTB &= ~(1<<PB2);
else if(a == eS_PORTB3)
PORTB &= ~(1<<PB3);
else if(a == eS_PORTB4)
PORTB &= ~(1<<PB4);
else if(a == eS_PORTB5)
PORTB &= ~(1<<PB5);
else if(a == eS_PORTB6)
PORTB &= ~(1<<PB6);
else if(a == eS_PORTB7)
PORTB &= ~(1<<PB7);
else if(a == eS_PORTC0)
PORTC &= ~(1<<PC0);
else if(a == eS_PORTC1)
PORTC &= ~(1<<PC1);
else if(a == eS_PORTC2)
PORTC &= ~(1<<PC2);
else if(a == eS_PORTC3)
PORTC &= ~(1<<PC3);
else if(a == eS_PORTC4)
PORTC &= ~(1<<PC4);
else if(a == eS_PORTC5)
PORTC &= ~(1<<PC5);
else if(a == eS_PORTC6)
PORTC &= ~(1<<PC6);
else if(a == eS_PORTC7)
PORTC &= ~(1<<PC7);
else if(a == eS_PORTD0)
PORTD &= ~(1<<PD0);
else if(a == eS_PORTD1)
PORTD &= ~(1<<PD1);
else if(a == eS_PORTD2)
PORTD &= ~(1<<PD2);
else if(a == eS_PORTD3)
PORTD &= ~(1<<PD3);
else if(a == eS_PORTD4)
PORTD &= ~(1<<PD4);
else if(a == eS_PORTD5)
PORTD &= ~(1<<PD5);
else if(a == eS_PORTD6)
PORTD &= ~(1<<PD6);
else if(a == eS_PORTD7)
PORTD &= ~(1<<PD7);
}
else
{
if(a == eS_PORTA0)
PORTA |= (1<<PA0);
else if(a == eS_PORTA1)
PORTA |= (1<<PA1);
else if(a == eS_PORTA2)
PORTA |= (1<<PA2);
else if(a == eS_PORTA3)
PORTA |= (1<<PA3);
else if(a == eS_PORTA4)
PORTA |= (1<<PA4);
else if(a == eS_PORTA5)
PORTA |= (1<<PA5);
else if(a == eS_PORTA6)
PORTA |= (1<<PA6);
else if(a == eS_PORTA7)
PORTA |= (1<<PA7);
else if(a == eS_PORTB0)
PORTB |= (1<<PB0);
else if(a == eS_PORTB1)
PORTB |= (1<<PB1);
else if(a == eS_PORTB2)
PORTB |= (1<<PB2);
else if(a == eS_PORTB3)
PORTB |= (1<<PB3);
else if(a == eS_PORTB4)
PORTB |= (1<<PB4);
else if(a == eS_PORTB5)
PORTB |= (1<<PB5);
else if(a == eS_PORTB6)
PORTB |= (1<<PB6);
else if(a == eS_PORTB7)
PORTB |= (1<<PB7);
else if(a == eS_PORTC0)
PORTC |= (1<<PC0);
else if(a == eS_PORTC1)
PORTC |= (1<<PC1);
else if(a == eS_PORTC2)
PORTC |= (1<<PC2);
else if(a == eS_PORTC3)
PORTC |= (1<<PC3);
else if(a == eS_PORTC4)
PORTC |= (1<<PC4);
else if(a == eS_PORTC5)
PORTC |= (1<<PC5);
else if(a == eS_PORTC6)
PORTC |= (1<<PC6);
else if(a == eS_PORTC7)
PORTC |= (1<<PC7);
else if(a == eS_PORTD0)
PORTD |= (1<<PD0);
else if(a == eS_PORTD1)
PORTD |= (1<<PD1);
else if(a == eS_PORTD2)
PORTD |= (1<<PD2);
else if(a == eS_PORTD3)
PORTD |= (1<<PD3);
else if(a == eS_PORTD4)
PORTD |= (1<<PD4);
else if(a == eS_PORTD5)
PORTD |= (1<<PD5);
else if(a == eS_PORTD6)
PORTD |= (1<<PD6);
else if(a == eS_PORTD7)
PORTD |= (1<<PD7);
}
}
//LCD 8 Bit Interfacing Functions
void Lcd8_Port(char a)
{
if(a & 1)
pinChange(D0,1);
else
pinChange(D0,0);
if(a & 2)
pinChange(D1,1);
else
pinChange(D1,0);
if(a & 4)
pinChange(D2,1);
else
pinChange(D2,0);
if(a & 8)
pinChange(D3,1);
else
pinChange(D3,0);
if(a & 16)
pinChange(D4,1);
else
pinChange(D4,0);
if(a & 32)
pinChange(D5,1);
else
pinChange(D5,0);
if(a & 64)
pinChange(D6,1);
else
pinChange(D6,0);
if(a & 128)
pinChange(D7,1);
else
pinChange(D7,0);
}
void Lcd8_Cmd(char a)
{
pinChange(RS,0); // => RS = 0
Lcd8_Port(a); //Data transfer
pinChange(EN,1); // => E = 1
_delay_ms(1);
pinChange(EN,0); // => E = 0
_delay_ms(1);
}
void Lcd8_Clear()
{
Lcd8_Cmd(1);
}
void Lcd8_SetCursor(char a, char b)
{
if(a == 1)
Lcd8_Cmd(0x80 + b);
else if(a == 2)
Lcd8_Cmd(0xC0 + b);
}
void Lcd8_Init()
{
pinChange(RS,0);
pinChange(EN,0);
_delay_ms(20);
///////////// Reset process from datasheet /////////
Lcd8_Cmd(0x30);
_delay_ms(5);
Lcd8_Cmd(0x30);
_delay_ms(1);
Lcd8_Cmd(0x30);
_delay_ms(10);
/////////////////////////////////////////////////////
Lcd8_Cmd(0x38); //function set
Lcd8_Cmd(0x0C); //display on,cursor off,blink off
Lcd8_Cmd(0x01); //clear display
Lcd8_Cmd(0x06); //entry mode, set increment
}
void Lcd8_WriteChar(char a)
{
pinChange(RS,1); // => RS = 1
Lcd8_Port(a); //Data transfer
pinChange(EN,1); // => E = 1
_delay_ms(1);
pinChange(EN,0); // => E = 04
_delay_ms(1);
}
void Lcd8_WriteString(char *a)
{
int i;
for(i=0;a[i]!='\0';i++)
Lcd8_Write_Char(a[i]);
}
void Lcd8_ShiftRight()
{
Lcd8_Cmd(0x1C);
}
void Lcd8_ShiftLeft()
{
Lcd8_Cmd(0x18);
}
//End LCD 8 Bit Interfacing Functions
//LCD 4 Bit Interfacing Functions
void Lcd4_Port(char a)
{
if(a & 1)
pinChange(D4,1);
else
pinChange(D4,0);
if(a & 2)
pinChange(D5,1);
else
pinChange(D5,0);
if(a & 4)
pinChange(D6,1);
else
pinChange(D6,0);
if(a & 8)
pinChange(D7,1);
else
pinChange(D7,0);
}
void Lcd4_Cmd(char a)
{
pinChange(RS,0); // => RS = 0
Lcd4_Port(a);
pinChange(EN,1); // => E = 1
_delay_ms(1);
pinChange(EN,0); // => E = 0
_delay_ms(1);
}
void Lcd4_Clear()
{
Lcd4_Cmd(0);
Lcd4_Cmd(1);
}
void Lcd4_SetCursor(char a, char b)
{
char temp,z,y;
if(a == 1)
{
temp = 0x80 + b;
z = temp>>4;
y = (0x80+b) & 0x0F;
Lcd4_Cmd(z);
Lcd4_Cmd(y);
}
else if(a == 2)
{
temp = 0xC0 + b;
z = temp>>4;
y = (0xC0+b) & 0x0F;
Lcd4_Cmd(z);
Lcd4_Cmd(y);
}
}
void Lcd4_Init()
{
Lcd4_Port(0x00);
_delay_ms(20);
///////////// Reset process from datasheet /////////
Lcd4_Cmd(0x03);
_delay_ms(5);
Lcd4_Cmd(0x03);
_delay_ms(11);
Lcd4_Cmd(0x03);
/////////////////////////////////////////////////////
Lcd4_Cmd(0x02);
Lcd4_Cmd(0x02);
Lcd4_Cmd(0x08);
Lcd4_Cmd(0x00);
Lcd4_Cmd(0x0C);
Lcd4_Cmd(0x00);
Lcd4_Cmd(0x06);
}
void Lcd4_WriteChar(char a)
{
char temp,y;
temp = a&0x0F;
y = a&0xF0;
pinChange(RS,1); // => RS = 1
Lcd4_Port(y>>4); //Data transfer
pinChange(EN,1);
_delay_ms(1);
pinChange(EN,0);
_delay_ms(1);
Lcd4_Port(temp);
pinChange(EN,1);
_delay_ms(1);
pinChange(EN,0);
_delay_ms(1);
}
void Lcd4_WriteString(char *a)
{
int i;
for(i=0;a[i]!='\0';i++)
Lcd4_Write_Char(a[i]);
}
void Lcd4_ShiftRight()
{
Lcd4_Cmd(0x01);
Lcd4_Cmd(0x0C);
}
void Lcd4_ShiftLeft()
{
Lcd4_Cmd(0x01);
Lcd4_Cmd(0x08);
}
//End LCD 4 Bit Interfacing Functions