-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLCD.c
More file actions
255 lines (237 loc) · 6.74 KB
/
LCD.c
File metadata and controls
255 lines (237 loc) · 6.74 KB
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
#include "xc.h"
#define FCY 4000000UL //FCY = FOSC / 2 unsigned long (UL)
// PC24FJ128GA204 = 8MHz internal oscillator
// must be defined before importing <libpic30.h>
#include <libpic30.h>
#include <string.h>
#define DEGREE_ALIAS 0x002B // is the '+' sign acting in place of the ° symbol
#include "LCD.h"
void LCD_ClearCommand() {
/*
* This function clears the RC8 bit for Command Mode, Sets E (RC9), and Sets RC0
* to complete the clear command
*/
//clear display
LATC = 0x0201; //0000 0010 0000 0001
__delay_ms(1);
LATCbits.LATC9 = 0; // toggle E pin
__delay_ms(1);
}
//address can only be a number between $20 - $2F OR $40 - $4F
void LCD_SetDisplayAddressCommand(int address) {
/*
* This function clears the RC8 bit for Command Mode & Sets E (RC9)
* RC7 must be set; RC0 - RC6 is used to set the address to a value between
* $20-2F OR $40-$4F
* @param: address: must be between $20-2F or $40-4F
*/
if (0x0020 > address || address > 0x004F)
return;
LATC = address; // sets the LCD display address
LATC |= 0x0280; // 0000 0010 1000 0000 Required configuration
__delay_ms(1);
LATCbits.LATC9 = 0; // toggle E pin
__delay_ms(1);
}
void LCD_Init() {
/*
* This function clears the RC8 bit for Command Mode & Sets E (RC9), Reference the LCD
* documentation for the listing of commands
*/
//display on command
LATC = 0x020F; // 0000 0010 0000 1111
__delay_ms(1);
LATCbits.LATC9 = 0; // toggle E pin
__delay_ms(1);
//set function command
LATC = 0x023F; // 0000 0010 0011 1111
__delay_ms(1);
LATCbits.LATC9 = 0; // toggle E pin
__delay_ms(1);
}
void LCD_PrintString(char *str) {
/*
* This function takes a char[] (null terminated) and outputs each char in
* the array to the LCD controller. Special characters require a different
* method. RS needs to be set for data transfer
* @param: str: the string to be output on the LCD
*/
char ch;
int i = 0;
for (; i < strlen(str); i++) {
ch = str[i];
if (ch == DEGREE_ALIAS) {
//it is the ° alias symbol, so print it manually
LCD_PrintChar(ch);
} else {
LATC = ch;
LATC |= 0x0300; // 0000 0011 ---- ---- RS set for Data Mode
__delay_ms(1);
LATCbits.LATC9 = 0; // toggle E pin
// delay?
}
}
}
void LCD_PrintChar(char ch) {
/*
* This function takes a char (mainly special chars like the degree symbol)
* and outputs it to the LCD controller. This has to be done by hard-coding
* the hex value of the char to the LCD data bus
* RS needs to be set for data transfer
* @param: ch: the char to be output to the LCD
*/
int charHexValue;
//degree symbol
if (ch == DEGREE_ALIAS) {
charHexValue = 0x00DF;
} else {
// or something to make sure the value has a valid hex value
charHexValue = ch;
}
LATC = charHexValue;
LATC |= 0x0300; // 0000 0011 ---- ---- RS and E set
__delay_ms(1);
LATCbits.LATC9 = 0; // toggle E
__delay_ms(1);
}
void LCD_PrintInteger(int num) {
if (num / 1000 >= 1) {
switch (num / 1000) {
case 1:
LCD_PrintChar('1');
break;
case 2:
LCD_PrintChar('2');
break;
case 3:
LCD_PrintChar('3');
break;
case 4:
LCD_PrintChar('4');
break;
case 5:
LCD_PrintChar('5');
break;
case 6:
LCD_PrintChar('6');
break;
case 7:
LCD_PrintChar('7');
break;
case 8:
LCD_PrintChar('8');
break;
case 9:
LCD_PrintChar('9');
break;
}
if ((num / 100) % 10 == 0)
LCD_PrintChar('0');
if ((num / 10) % 100 == 0)
LCD_PrintChar('0');
LCD_PrintInteger(num % 1000);
} else if (num / 100 >= 1) {
switch (num / 100) {
case 1:
LCD_PrintChar('1');
break;
case 2:
LCD_PrintChar('2');
break;
case 3:
LCD_PrintChar('3');
break;
case 4:
LCD_PrintChar('4');
break;
case 5:
LCD_PrintChar('5');
break;
case 6:
LCD_PrintChar('6');
break;
case 7:
LCD_PrintChar('7');
break;
case 8:
LCD_PrintChar('8');
break;
case 9:
LCD_PrintChar('9');
break;
}
if ((num / 10) % 10 == 0)
LCD_PrintChar('0');
LCD_PrintInteger(num % 100);
} else if (num / 10 >= 1) {
switch (num / 10) {
case 1:
LCD_PrintChar('1');
break;
case 2:
LCD_PrintChar('2');
break;
case 3:
LCD_PrintChar('3');
break;
case 4:
LCD_PrintChar('4');
break;
case 5:
LCD_PrintChar('5');
break;
case 6:
LCD_PrintChar('6');
break;
case 7:
LCD_PrintChar('7');
break;
case 8:
LCD_PrintChar('8');
break;
case 9:
LCD_PrintChar('9');
break;
}
LCD_PrintInteger(num % 10);
} else {
switch (num) {
case 0:
LCD_PrintChar('0');
break;
case 1:
LCD_PrintChar('1');
break;
case 2:
LCD_PrintChar('2');
break;
case 3:
LCD_PrintChar('3');
break;
case 4:
LCD_PrintChar('4');
break;
case 5:
LCD_PrintChar('5');
break;
case 6:
LCD_PrintChar('6');
break;
case 7:
LCD_PrintChar('7');
break;
case 8:
LCD_PrintChar('8');
break;
case 9:
LCD_PrintChar('9');
break;
}
}
}
void LCD_PrintFloat(float num) {
LCD_PrintInteger(num);
LCD_PrintChar('.');
int floatingPoint = ((num - (int)num) * 100);
LCD_PrintInteger(floatingPoint);
}