-
Notifications
You must be signed in to change notification settings - Fork 0
/
LCD_code.c
207 lines (175 loc) · 3.44 KB
/
LCD_code.c
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
#include "constants.h"
#include <xc.h>
void writeLCDnibble(char nibble)
{
if (nibble & 1)
{ LCD_DB4 = 1;
}else{
LCD_DB4 = 0;
}
if (nibble & 2)
{ LCD_DB5 = 1;
}else{
LCD_DB5 = 0;
}
if (nibble & 4)
{ LCD_DB6 = 1;
}else{
LCD_DB6 = 0;
}
if (nibble & 8)
{ LCD_DB7 = 1;
}else{
LCD_DB7 = 0;
}
}
void pulse_en(void)
{
_delay(625); // 100 µs
LCD_EN = 1;
_delay(625); // 100 µs
LCD_EN = 0;
_delay(625); // 100 µs
}
void pulse_en_long(void)
{
_delay(12500); // 2000 µs
LCD_EN = 1;
_delay(12500); // 2000 µs
LCD_EN = 0;
_delay(12500); // 2000 µs
}
void LCD_cmd( char command )
{
LCD_RS = 0; // LCD R/W = 0
// Send upper nibble
char toPort;
toPort = (command >> 4) & 0x0F;
writeLCDnibble(toPort);
pulse_en_long();
// Send lower nibble
toPort = command & 0x0F;
writeLCDnibble(toPort);
pulse_en_long();
}
void LCD_char( char command )
{
LCD_RS = 1; // LCD R/W = 1
// Send upper nibble
char toPort;
toPort = (command >> 4) & 0x0F;
writeLCDnibble(toPort);
pulse_en();
// Send lower nibble
toPort = command & 0x0F;
writeLCDnibble(toPort);
pulse_en();
}
void initialize_LCD(void)
{
_delay(625000); // 100 ms
writeLCDnibble(0x3);
pulse_en();
_delay(125000); // 20 ms
writeLCDnibble(0x3);
pulse_en();
_delay(6250); // 1 ms
writeLCDnibble(0x3);
pulse_en();
writeLCDnibble(0x2);
pulse_en();
LCD_cmd(0x28); // function set
LCD_cmd(0x01); // clear display
_delay(31250); // 5 ms
LCD_cmd(0x06); // Set entry mode to increment, display freeze
LCD_cmd(0x0F); // Put display on, cursor on and blinking on
LCD_cmd(0x0C); // Put display on, cursor off and blinking off
}
void LCD_clear(void)
{
LCD_cmd(0x01);
}
void LCD_home(void)
{
LCD_cmd(0x02);
}
void LCD_second_row(void)
{
LCD_cmd(0xC0);
}
void LCD_display_cursor(void)
{
LCD_cmd(0x0E);
}
void LCD_remove_cursor(void)
{
LCD_cmd(0x0C);
}
void LCD_decrement_cursor(void)
{
LCD_cmd(0x10);
}
void LCD_increment_cursor(void)
{
LCD_cmd(0x14);
}
void LCD_write_string(const char *string)
{
while( *string != '\0')
{
if(*(string)=='\n')
{ LCD_second_row();
}else if ( *string == 'º')
{
LCD_char(0xDF);
}else{
LCD_char(*string);
}
string++;
}
}
void LCD_write_number(unsigned long input)
{
int buffer[10];
for(char i = 0; i<10; i++)
{
buffer[i] = input % 10;
input = (input - buffer[i]) /10;
}
int startWriting = 0;
for(char j = 0; j<10; j++)
{
int towrite = buffer[9-j];
if( towrite != 0 || startWriting)
{
LCD_char(towrite+0x30);
startWriting = 1;
}
if(j == 9 && !startWriting)
{ LCD_char(0x30); }
}
}
/*
void LCD_write_number_decimal(long whole, long decimal)
{
int buffer[10];
for(char i = 0; i<10; i++)
{
buffer[i] = whole % 10;
whole = (whole - buffer[i]) /10;
}
int startWriting = 0;
for(char j = 0; j<10; j++)
{
int towrite = buffer[9-j];
if( towrite != 0 || startWriting)
{
LCD_char(towrite+0x30);
startWriting = 1;
}
if(j == 9 && !startWriting)
{ LCD_char(0x30); }
LCD_char(0x2D);
}
}
*/