-
Notifications
You must be signed in to change notification settings - Fork 2
/
lcd.h
197 lines (179 loc) · 3.61 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
#define eS_PORTB0 14
#define eS_PORTB1 15
#define eS_PORTD0 2
#define eS_PORTD1 3
#define eS_PORTD2 4
#define eS_PORTD3 5
#define eS_PORTD4 6
#define eS_PORTD5 11
#define eS_PORTD6 12
#define eS_PORTD7 13
#define D4 eS_PORTD4
#define D5 eS_PORTD5
#define D6 eS_PORTD6
#define D7 eS_PORTD7
#define RS eS_PORTB0
#define EN eS_PORTB1
#include <util/delay.h>
void pinChange(int a, int b)
{
if(b == 0)
{
if(a == eS_PORTB0)
PORTB &= ~(1<<PB0);
else if(a == eS_PORTB1)
PORTB &= ~(1<<PB1);
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_PORTB0)
PORTB |= (1<<PB0);
else if(a == eS_PORTB1)
PORTB |= (1<<PB1);
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 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_Set_Cursor(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_Write_Char(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_Write_String(char *a)
{
int i;
for(i=0;a[i]!='\0';i++)
Lcd4_Write_Char(a[i]);
}
void Lcd4_Shift_Right()
{
Lcd4_Cmd(0x01);
Lcd4_Cmd(0x0C);
}
void Lcd4_Shift_Left()
{
Lcd4_Cmd(0x01);
Lcd4_Cmd(0x08);
}
//End LCD 4 Bit Interfacing Functions
void Lcd4_Write_Integer(int x) // Takes an integer as input and displays it
{ // value of integer should be in between
char buffer[8]; // the range of "int",
itoa(x,buffer,10); // else it'll print garbage values.
Lcd4_Write_String(buffer); // It use Send_A_String() for displaying.
}