-
Notifications
You must be signed in to change notification settings - Fork 0
/
lcd.c
371 lines (333 loc) · 7.53 KB
/
lcd.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
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
/*
* File: lcd.c
* Author: 477grp8
*
* Created on April 28, 2015, 9:27 PM
*/
#include <xc.h>
#include "lcd.h"
#include "delay.h"
void writeEN(){
LCDCE = 0; //enable LCD chip
LCDWR = 0; //enable write to LCD
}
void writeDIS(){
LCDCE = 1; //chip enable high
LCDWR = 1; //stop writing
}
void LCDStat(){
delayNs(2); //TODO Change later to 1, leaving it slow while I'm working
}
void sendByte( char b ){
DB = b;
writeEN();
asm("nop");
writeDIS();
}
void sendCommand( char cmd ){
LCDStat();
LCDCD = 1;
sendByte(cmd);
}
void sendData( char data ){
LCDStat();
LCDCD = 0;
sendByte(data);
}
void setMode(){
sendCommand( 0x83 );
}
void setGraphicHome(){
sendData( 0x00 );
sendData( 0x08 );
sendCommand( 0x42 );
}
void setGraphicArea(){
sendData( 0x14 );
sendData( 0x00 );
sendCommand( 0x43 );
}
void setTextHome(){
sendData( 0x00 );
sendData( 0x00 );
sendCommand( 0x40 );
}
void setTextArea(){
sendData( 0x14 );
sendData( 0x00 );
sendCommand( 0x41 );
}
void setCGRom(){
sendData( 0x03 );
sendData( 0x00 );
sendCommand( 0x22 );
}
void setAddrPointer( char first , char second ){
sendData( first );
sendData( second );
sendCommand( 0x24 );
}
void DAWSet(){ sendCommand( 0xB0 ); }
void DARSet(){ sendCommand( 0xB1 ); }
void DAReset(){
sendCommand(0xB2);
}
void TestMessage(){
int i;
setAddrPointer( 0x00 , 0x00 );
DAWSet();
for(i=8;i>0;i=i--){
LCDStat();
sendData(0x28);LCDStat();
sendData(0x45);LCDStat();
sendData(0x59);LCDStat();
sendData(0x00);LCDStat();
sendData(0x2C);LCDStat();
sendData(0x4F);LCDStat();
sendData(0x4F);LCDStat();
sendData(0x4B);LCDStat();
sendData(0x01);LCDStat();
sendData(0x00);LCDStat();
sendData(0x00);LCDStat();
sendData(0x00);LCDStat();
sendData(0x00);LCDStat();
sendData(0x00);LCDStat();
sendData(0x00);LCDStat();
sendData(0x00);LCDStat();
sendData(0x00);LCDStat();
sendData(0x00);LCDStat();
sendData(0x00);LCDStat();
sendData(0x00);LCDStat();
sendData(0x21);LCDStat();
sendData(0x00);LCDStat();
sendData(0x34);LCDStat();
sendData(0x48);LCDStat();
sendData(0x49);LCDStat();
sendData(0x4E);LCDStat();
sendData(0x47);LCDStat();
sendData(0x01);LCDStat();
sendData(0x00);LCDStat();
sendData(0x00);LCDStat();
sendData(0x00);LCDStat();
sendData(0x00);LCDStat();
sendData(0x00);LCDStat();
sendData(0x00);LCDStat();
sendData(0x00);LCDStat();
sendData(0x00);LCDStat();
sendData(0x00);LCDStat();
sendData(0x00);LCDStat();
sendData(0x00);LCDStat();
sendData(0x00);
}
}
void clearMemory(int n){
//clear 20 cols by 16 rows: 320 addresses
int i;
DAWSet();
for(i=n;i>0;i--){
sendData(0x00);LCDStat();
}
DAReset();
}
void clearGraphics(){
setAddrPointer( 0x00 , 0x00 );
clearMemory(120);
}
void clearText(){
setAddrPointer( 0x00 , 0x00 );
clearMemory(120);
}
void clearDisplay(){
setAddrPointer( 0x00 , 0x00 );
clearMemory(320);
}
void WriteDisplay(){
setAddrPointer( 0x00 , 0x00 );
TestMessage();
}
void setCursorPattern(){
sendCommand( 0xA0 );
}
void setCursorPointer( char col , char row ){
sendData(col);
sendData(row);
sendCommand( 0x21 );
}
void setDisplayMode(){
sendCommand(0x94);
}
void PrintInt (int val){
DAWSet();
sendData(val+16);
LCDStat();
DAReset();
}
void PrintChar (char val){
int i = val;
i = i - 32;
DAWSet();
sendData(i);
LCDStat();
DAReset();
}
void PrintString(char* string){
int i = 0;
char next;
next = string[i];
while(next != '\0'){
PrintChar(next);
i++;
next = string[i];
}
}
void PrintFloat(float val)
{
//always prints 5 places
int i = val*100;
int pflag = 0;
int thous = i/10000;
int hund = i/1000 - thous*10;
int tenth = i/100 - thous*100 - hund*10;
int dectenth = i/10 - thous*1000 - hund*100 - tenth*10;
int dechund = i/1 - thous*10000 - hund*1000 - tenth*100 - dectenth*10;
if(thous && !pflag){
PrintInt(thous);
pflag = 1;
}
else{
PrintString(" ");
}
if(hund){
PrintInt(hund);
pflag = 1;
}
else{
PrintString(" ");
}
PrintInt(tenth);
PrintString(".");
PrintInt(dectenth);
PrintInt(dechund);
}
void PrintEmptyLine(){
// 20 spaces per line
PrintString(" ");
}
void PrintMenu(char *menu, int select){
clearDisplay();
setAddrPointer( 0x00 , 0x00 );
size_t line=0, chs = 0, i=0;
if(select==line){ PrintChar('>'); }
else{ PrintChar(' '); }
chs++;
while(menu[i]!='\0'){
if(menu[i]=='\n'){
while(chs++<20){
PrintChar(' ');
}
if(++line == select){
PrintChar('>');
}
else{ PrintChar(' '); }
chs = 1;
}
else{
chs++;
PrintChar(menu[i]);
}
i++;
}
PrintEmptyLine();
PrintEmptyLine();
PrintEmptyLine();
PrintEmptyLine();
PrintEmptyLine();
}
void PrintStockInfo(char* ticker, float value, float open, float close, float predict){
int negflag;
float change = value - open;
if(change < 0){
negflag = 1;
change = open - value;
}
else{
negflag = 0;
}
setAddrPointer( 0x00 , 0x00 );
PrintString(" Stock Report ");
PrintEmptyLine();
PrintEmptyLine();
PrintString(" Ticker: ");
PrintString(ticker);
int i=0;
while(ticker[i++] != '\0'){}
while(i++ <= 5){ PrintString(" "); }
PrintString(" ");
PrintEmptyLine();
PrintString(" Value: ");
PrintFloat(value);
PrintString(" ");
PrintString(" Open: ");
PrintFloat(open);
PrintString(" ");
PrintString(" Close: ");
PrintFloat(close);
PrintString(" ");
//PrintEmptyLine();
PrintString(" Change:");
if(negflag){
PrintString("-");
}
else{ PrintString("+"); }
PrintFloat(change);
PrintString(" ");
PrintString(" Predict: ");
PrintFloat(predict);
PrintString(" ");
PrintEmptyLine();
PrintString(" ^ Menu ");
PrintString(" <Prev *Print Next> ");
PrintString(" v Menu ");
PrintEmptyLine();
}
void initLCD(){
//initialize test values
LATF = 0;
LATE = 0;
TRISF = 0;
TRISE = 0;
//Open Drain Controls
ODCE = 0x00;
ODCF = 0x00;
//set LCD control output enables
TRISFbits.TRISF0 = 0; //RF0 output
TRISFbits.TRISF5 = 0; //RF5 output
TRISDbits.TRISD0 = 0; //RD0 output
TRISDbits.TRISD5 = 0; //RD5 output
TRISDbits.TRISD9 = 0;//RD9 output
TRISDbits.TRISD10 = 0; //RD10 output
TRISDbits.TRISD11 = 0; //RD11 output
//LCD Init
LCDHALT = 1;
LCDCE = 1;
LCDCD = 1;
LCDRD = 1;
LCDWR = 1;
LCDRST = 0;
delayMs(50);
LCDRST = 1;
delayMs(50);
setTextHome(); //map text memory
setTextArea(); //map
setGraphicHome();
setGraphicArea();
setMode();
setCursorPattern();
setDisplayMode(); //works
clearDisplay(); //why isn't the display clearing
setAddrPointer( 0x00 , 0x00 );
//PrintStockInfo("ATVI", 122.72,2.81, 22.8);
delayMs(50);
LCDCE = 0; //enable chip
delayMs(5000);
}