User | Revision | Line number | New contents of line |
tonydbeck |
0:1cfbf6b8d116
|
1
|
#include "gLCD.h"
|
tonydbeck |
0:1cfbf6b8d116
|
2
|
#include "mbed.h"
|
tonydbeck |
0:1cfbf6b8d116
|
3
|
|
tonydbeck |
0:1cfbf6b8d116
|
4
|
gLCD::gLCD(PinName rst, PinName rs, PinName e, PinName d0, PinName d1,
|
tonydbeck |
0:1cfbf6b8d116
|
5
|
PinName d2, PinName d3, PinName d4, PinName d5, PinName d6, PinName d7, PinName led)
|
tonydbeck |
0:1cfbf6b8d116
|
6
|
: _rst(rst), _rs(rs), _e(e), _d(d0, d1, d2, d3, d4, d5, d6, d7), _led(led){
|
tonydbeck |
0:1cfbf6b8d116
|
7
|
|
tonydbeck |
0:1cfbf6b8d116
|
8
|
_rst = 0; // Put display driver into Reset mode
|
tonydbeck |
0:1cfbf6b8d116
|
9
|
_e = 1;
|
tonydbeck |
0:1cfbf6b8d116
|
10
|
_rs = 0; // Put display controller in command mode
|
tonydbeck |
0:1cfbf6b8d116
|
11
|
|
tonydbeck |
0:1cfbf6b8d116
|
12
|
wait(0.015); // Wait 15ms to ensure powered up
|
tonydbeck |
0:1cfbf6b8d116
|
13
|
|
tonydbeck |
0:1cfbf6b8d116
|
14
|
_rst = 1; // Switch reset off
|
tonydbeck |
0:1cfbf6b8d116
|
15
|
|
tonydbeck |
0:1cfbf6b8d116
|
16
|
|
tonydbeck |
0:1cfbf6b8d116
|
17
|
/* ADC Select
|
tonydbeck |
0:1cfbf6b8d116
|
18
|
Horizontal direction
|
tonydbeck |
0:1cfbf6b8d116
|
19
|
0xA0 = SEG1->SEG132 (left to right)
|
tonydbeck |
0:1cfbf6b8d116
|
20
|
0xA1 = SEG132->SEG1 (right to left) */
|
tonydbeck |
0:1cfbf6b8d116
|
21
|
writeCommand(0xA0); // 0xA0 = SEG1->SEG132 (left to right)
|
tonydbeck |
0:1cfbf6b8d116
|
22
|
|
tonydbeck |
0:1cfbf6b8d116
|
23
|
/* SHL Select
|
tonydbeck |
0:1cfbf6b8d116
|
24
|
Vertical direction
|
tonydbeck |
0:1cfbf6b8d116
|
25
|
0xC0 = COM1->COM64 (top to bottom)
|
tonydbeck |
0:1cfbf6b8d116
|
26
|
0xC8 = COM64->COM1 (bottom to top) */
|
tonydbeck |
0:1cfbf6b8d116
|
27
|
writeCommand(0xC0); // 0xC0 = COM1->COM64
|
tonydbeck |
0:1cfbf6b8d116
|
28
|
|
tonydbeck |
0:1cfbf6b8d116
|
29
|
/* LCD bias select
|
tonydbeck |
0:1cfbf6b8d116
|
30
|
Screen in use: Displaytech 64128E
|
tonydbeck |
0:1cfbf6b8d116
|
31
|
Display duty ratio: 1/65
|
tonydbeck |
0:1cfbf6b8d116
|
32
|
Display bias: 1/9
|
tonydbeck |
0:1cfbf6b8d116
|
33
|
0xA2 = 1/5 or 1/6 or 1/7 bias
|
tonydbeck |
0:1cfbf6b8d116
|
34
|
0xA3 = 1/6 or 1/8 or 1/9 bias
|
tonydbeck |
0:1cfbf6b8d116
|
35
|
Note: at 1/6 bias use 0xA2 for 1/49 duty ratio
|
tonydbeck |
0:1cfbf6b8d116
|
36
|
or 0xA3 for 1/33 duty ratio */
|
tonydbeck |
0:1cfbf6b8d116
|
37
|
writeCommand(0xA3); // 0xA3 = 1/9 bias
|
tonydbeck |
0:1cfbf6b8d116
|
38
|
|
tonydbeck |
0:1cfbf6b8d116
|
39
|
/*Power control
|
tonydbeck |
0:1cfbf6b8d116
|
40
|
Displaytech screen uses built-in power circuits
|
tonydbeck |
0:1cfbf6b8d116
|
41
|
Switch on in order as specified in data sheet
|
tonydbeck |
0:1cfbf6b8d116
|
42
|
wait 1ms between each command
|
tonydbeck |
0:1cfbf6b8d116
|
43
|
1st - Voltage converter ON = 0x2C
|
tonydbeck |
0:1cfbf6b8d116
|
44
|
2nd - Voltage regulator ON = 0x2E
|
tonydbeck |
0:1cfbf6b8d116
|
45
|
3rd - Voltage follower ON = 0x2F */
|
tonydbeck |
0:1cfbf6b8d116
|
46
|
writeCommand(0x2C); //0x2C = Voltage converter ON
|
tonydbeck |
0:1cfbf6b8d116
|
47
|
wait(0.001);
|
tonydbeck |
0:1cfbf6b8d116
|
48
|
writeCommand(0x2E); //0x2E = Voltage regulator ON
|
tonydbeck |
0:1cfbf6b8d116
|
49
|
wait(0.001);
|
tonydbeck |
0:1cfbf6b8d116
|
50
|
writeCommand(0x2F); //0x2F = Voltage follower ON
|
tonydbeck |
0:1cfbf6b8d116
|
51
|
|
tonydbeck |
0:1cfbf6b8d116
|
52
|
/*Regulator resistor select
|
tonydbeck |
0:1cfbf6b8d116
|
53
|
Sets the internal resistance ratio used in the internal voltage regulator
|
tonydbeck |
0:1cfbf6b8d116
|
54
|
Refer to datasheet p.42
|
tonydbeck |
0:1cfbf6b8d116
|
55
|
This works as a corse contrast control
|
tonydbeck |
0:1cfbf6b8d116
|
56
|
0x20 = 1.9
|
tonydbeck |
0:1cfbf6b8d116
|
57
|
0x21 = 2.19
|
tonydbeck |
0:1cfbf6b8d116
|
58
|
0x22 = 2.55
|
tonydbeck |
0:1cfbf6b8d116
|
59
|
0x23 = 3.02
|
tonydbeck |
0:1cfbf6b8d116
|
60
|
0x24 = 3.61
|
tonydbeck |
0:1cfbf6b8d116
|
61
|
0x25 = 4.35
|
tonydbeck |
0:1cfbf6b8d116
|
62
|
0x26 = 5.29
|
tonydbeck |
0:1cfbf6b8d116
|
63
|
0x27 = 6.48 */
|
tonydbeck |
0:1cfbf6b8d116
|
64
|
writeCommand(0x26);
|
tonydbeck |
0:1cfbf6b8d116
|
65
|
|
tonydbeck |
0:1cfbf6b8d116
|
66
|
/*Set reference voltage register
|
tonydbeck |
0:1cfbf6b8d116
|
67
|
Used as a fine contrast control
|
tonydbeck |
0:1cfbf6b8d116
|
68
|
0x81 = Enter voltage register set mode
|
tonydbeck |
0:1cfbf6b8d116
|
69
|
0x00 to 0x3F = 0 to 63 */
|
tonydbeck |
0:1cfbf6b8d116
|
70
|
writeCommand(0x81); //0x81 = Enter voltage register set mode
|
tonydbeck |
0:1cfbf6b8d116
|
71
|
writeCommand(0x46); //0x30 = Set ref voltage to 30
|
tonydbeck |
0:1cfbf6b8d116
|
72
|
|
tonydbeck |
0:1cfbf6b8d116
|
73
|
/*Initial display line
|
tonydbeck |
0:1cfbf6b8d116
|
74
|
Specify DDRAM line for COM1
|
tonydbeck |
0:1cfbf6b8d116
|
75
|
0x40 + display line */
|
tonydbeck |
0:1cfbf6b8d116
|
76
|
writeCommand(0x40); //Set initial line to 0
|
tonydbeck |
0:1cfbf6b8d116
|
77
|
|
tonydbeck |
0:1cfbf6b8d116
|
78
|
/*Set page address
|
tonydbeck |
0:1cfbf6b8d116
|
79
|
Sets the initial page address to write to
|
tonydbeck |
0:1cfbf6b8d116
|
80
|
0xB0 + page address 0 to 8 */
|
tonydbeck |
0:1cfbf6b8d116
|
81
|
writeCommand(0xB0); //Initial page set to 0
|
tonydbeck |
0:1cfbf6b8d116
|
82
|
|
tonydbeck |
0:1cfbf6b8d116
|
83
|
/*Set column address
|
tonydbeck |
0:1cfbf6b8d116
|
84
|
Sets the initial column to write to
|
tonydbeck |
0:1cfbf6b8d116
|
85
|
for LSB (b3-b0) 0x00 + first nibble
|
tonydbeck |
0:1cfbf6b8d116
|
86
|
for MSB (b7-b4) 0x10 + second nibble
|
tonydbeck |
0:1cfbf6b8d116
|
87
|
0x00 to 0x83 = column 0 to 131 */
|
tonydbeck |
0:1cfbf6b8d116
|
88
|
writeCommand(0x00); //Sets LSB to 0
|
tonydbeck |
0:1cfbf6b8d116
|
89
|
writeCommand(0x10); //Sets MSB to 0 - column is now set to 0
|
tonydbeck |
0:1cfbf6b8d116
|
90
|
|
tonydbeck |
0:1cfbf6b8d116
|
91
|
/*Reverse display
|
tonydbeck |
0:1cfbf6b8d116
|
92
|
Selects either a normal display or a reverse display
|
tonydbeck |
0:1cfbf6b8d116
|
93
|
0xA6 = normal
|
tonydbeck |
0:1cfbf6b8d116
|
94
|
0xA7 = reverse */
|
tonydbeck |
0:1cfbf6b8d116
|
95
|
writeCommand(0xA6); //Sets display to normal
|
tonydbeck |
0:1cfbf6b8d116
|
96
|
|
tonydbeck |
0:1cfbf6b8d116
|
97
|
/*Set static indicator
|
tonydbeck |
0:1cfbf6b8d116
|
98
|
Sets up a static indicator on the display
|
tonydbeck |
0:1cfbf6b8d116
|
99
|
See datasheet p.42
|
tonydbeck |
0:1cfbf6b8d116
|
100
|
This is a 2 instruction cycle
|
tonydbeck |
0:1cfbf6b8d116
|
101
|
0xAC = static indicator ON
|
tonydbeck |
0:1cfbf6b8d116
|
102
|
0xAD = static indicator OFF
|
tonydbeck |
0:1cfbf6b8d116
|
103
|
Next instruction to set indicator type:
|
tonydbeck |
0:1cfbf6b8d116
|
104
|
0x00 = OFF
|
tonydbeck |
0:1cfbf6b8d116
|
105
|
0x01 = ON - 1 second blinking
|
tonydbeck |
0:1cfbf6b8d116
|
106
|
0x02 = ON - 0.5 second blinking
|
tonydbeck |
0:1cfbf6b8d116
|
107
|
0x03 = ON - always ON */
|
tonydbeck |
0:1cfbf6b8d116
|
108
|
writeCommand(0xAD); //Static indicator OFF
|
tonydbeck |
0:1cfbf6b8d116
|
109
|
writeCommand(0x00); //OFF - 0.5 second blinking
|
tonydbeck |
0:1cfbf6b8d116
|
110
|
|
tonydbeck |
0:1cfbf6b8d116
|
111
|
/*Display ON/OFF
|
tonydbeck |
0:1cfbf6b8d116
|
112
|
Switched the display to on or off
|
tonydbeck |
0:1cfbf6b8d116
|
113
|
0xAE = Display OFF
|
tonydbeck |
0:1cfbf6b8d116
|
114
|
0xAF = Display ON */
|
tonydbeck |
0:1cfbf6b8d116
|
115
|
writeCommand(0xAF);
|
tonydbeck |
0:1cfbf6b8d116
|
116
|
|
tonydbeck |
0:1cfbf6b8d116
|
117
|
|
tonydbeck |
0:1cfbf6b8d116
|
118
|
|
tonydbeck |
0:1cfbf6b8d116
|
119
|
|
tonydbeck |
0:1cfbf6b8d116
|
120
|
|
tonydbeck |
0:1cfbf6b8d116
|
121
|
|
tonydbeck |
0:1cfbf6b8d116
|
122
|
}
|
tonydbeck |
0:1cfbf6b8d116
|
123
|
|
tonydbeck |
0:1cfbf6b8d116
|
124
|
|
tonydbeck |
0:1cfbf6b8d116
|
125
|
|
tonydbeck |
0:1cfbf6b8d116
|
126
|
void gLCD::writeCommand(int command)
|
tonydbeck |
0:1cfbf6b8d116
|
127
|
{
|
tonydbeck |
0:1cfbf6b8d116
|
128
|
_rs = 0;
|
tonydbeck |
0:1cfbf6b8d116
|
129
|
wait(0.000040f);
|
tonydbeck |
0:1cfbf6b8d116
|
130
|
writeByte(command);
|
tonydbeck |
0:1cfbf6b8d116
|
131
|
}
|
tonydbeck |
0:1cfbf6b8d116
|
132
|
|
tonydbeck |
0:1cfbf6b8d116
|
133
|
void gLCD::writeData(int data)
|
tonydbeck |
0:1cfbf6b8d116
|
134
|
{
|
tonydbeck |
0:1cfbf6b8d116
|
135
|
_rs = 1;
|
tonydbeck |
0:1cfbf6b8d116
|
136
|
writeByte(data);
|
tonydbeck |
0:1cfbf6b8d116
|
137
|
}
|
tonydbeck |
0:1cfbf6b8d116
|
138
|
|
tonydbeck |
0:1cfbf6b8d116
|
139
|
void gLCD::writeByte(int value)
|
tonydbeck |
0:1cfbf6b8d116
|
140
|
{
|
tonydbeck |
0:1cfbf6b8d116
|
141
|
_led = 1;
|
tonydbeck |
0:1cfbf6b8d116
|
142
|
_e = 1;
|
tonydbeck |
0:1cfbf6b8d116
|
143
|
wait(0.000040f); //Allow time for display
|
tonydbeck |
0:1cfbf6b8d116
|
144
|
_d = value;
|
tonydbeck |
0:1cfbf6b8d116
|
145
|
wait(0.000040f); //Allow time for display
|
tonydbeck |
0:1cfbf6b8d116
|
146
|
_e = 0; //Load value to display - display driver registers on falling edge of e
|
tonydbeck |
0:1cfbf6b8d116
|
147
|
_led = 0;
|
tonydbeck |
0:1cfbf6b8d116
|
148
|
|
tonydbeck |
0:1cfbf6b8d116
|
149
|
}
|
tonydbeck |
0:1cfbf6b8d116
|
150
|
|
tonydbeck |
0:1cfbf6b8d116
|
151
|
void gLCD::locate(int page, int column)
|
tonydbeck |
0:1cfbf6b8d116
|
152
|
{
|
tonydbeck |
0:1cfbf6b8d116
|
153
|
_column = column;
|
tonydbeck |
0:1cfbf6b8d116
|
154
|
_page = page;
|
tonydbeck |
0:1cfbf6b8d116
|
155
|
writeCommand(0xB0 + page);
|
tonydbeck |
0:1cfbf6b8d116
|
156
|
writeCommand(0x00 + (column & 0x0f));
|
tonydbeck |
0:1cfbf6b8d116
|
157
|
writeCommand(0x10 + ((column >> 4) & 0x0f));
|
tonydbeck |
0:1cfbf6b8d116
|
158
|
}
|
tonydbeck |
0:1cfbf6b8d116
|
159
|
|
tonydbeck |
0:1cfbf6b8d116
|
160
|
void gLCD::locateChar(int page, int column)
|
tonydbeck |
0:1cfbf6b8d116
|
161
|
{
|
tonydbeck |
0:1cfbf6b8d116
|
162
|
column = ((column*6)+1);
|
tonydbeck |
0:1cfbf6b8d116
|
163
|
_column = column;
|
tonydbeck |
0:1cfbf6b8d116
|
164
|
_page = page;
|
tonydbeck |
0:1cfbf6b8d116
|
165
|
writeCommand(0xB0 + page);
|
tonydbeck |
0:1cfbf6b8d116
|
166
|
writeCommand(0x00 + (column & 0x0f));
|
tonydbeck |
0:1cfbf6b8d116
|
167
|
writeCommand(0x10 + ((column >> 4) & 0x0f));
|
tonydbeck |
0:1cfbf6b8d116
|
168
|
}
|
tonydbeck |
0:1cfbf6b8d116
|
169
|
|
tonydbeck |
0:1cfbf6b8d116
|
170
|
void gLCD::clearScreen()
|
tonydbeck |
0:1cfbf6b8d116
|
171
|
{
|
tonydbeck |
0:1cfbf6b8d116
|
172
|
for(int a = 0; a < 8; a++)
|
tonydbeck |
0:1cfbf6b8d116
|
173
|
{
|
tonydbeck |
0:1cfbf6b8d116
|
174
|
locate(a,0);
|
tonydbeck |
0:1cfbf6b8d116
|
175
|
for(int b = 0; b <128; b++)
|
tonydbeck |
0:1cfbf6b8d116
|
176
|
{
|
tonydbeck |
0:1cfbf6b8d116
|
177
|
writeData(0x00);
|
tonydbeck |
0:1cfbf6b8d116
|
178
|
}
|
tonydbeck |
0:1cfbf6b8d116
|
179
|
}
|
tonydbeck |
0:1cfbf6b8d116
|
180
|
locate(0,0);
|
tonydbeck |
0:1cfbf6b8d116
|
181
|
}
|
tonydbeck |
0:1cfbf6b8d116
|
182
|
|
tonydbeck |
0:1cfbf6b8d116
|
183
|
void gLCD::writeChar5x8(int p, int col, int c)
|
tonydbeck |
0:1cfbf6b8d116
|
184
|
{
|
tonydbeck |
0:1cfbf6b8d116
|
185
|
const int *ptrFont5x8 = font5x8;
|
tonydbeck |
0:1cfbf6b8d116
|
186
|
locate(p,col);
|
tonydbeck |
0:1cfbf6b8d116
|
187
|
ptrFont5x8+= ((c-32)*6);
|
tonydbeck |
0:1cfbf6b8d116
|
188
|
|
tonydbeck |
0:1cfbf6b8d116
|
189
|
for(int a=0; a < 6; a++)
|
tonydbeck |
0:1cfbf6b8d116
|
190
|
{
|
tonydbeck |
0:1cfbf6b8d116
|
191
|
|
tonydbeck |
0:1cfbf6b8d116
|
192
|
writeData(*ptrFont5x8);
|
tonydbeck |
0:1cfbf6b8d116
|
193
|
|
tonydbeck |
0:1cfbf6b8d116
|
194
|
ptrFont5x8++;
|
tonydbeck |
0:1cfbf6b8d116
|
195
|
}
|
tonydbeck |
0:1cfbf6b8d116
|
196
|
|
tonydbeck |
0:1cfbf6b8d116
|
197
|
}
|
tonydbeck |
0:1cfbf6b8d116
|
198
|
|
tonydbeck |
0:1cfbf6b8d116
|
199
|
|
tonydbeck |
0:1cfbf6b8d116
|
200
|
|
tonydbeck |
0:1cfbf6b8d116
|
201
|
int gLCD::_putc(int value)
|
tonydbeck |
0:1cfbf6b8d116
|
202
|
{
|
tonydbeck |
0:1cfbf6b8d116
|
203
|
|
tonydbeck |
0:1cfbf6b8d116
|
204
|
|
tonydbeck |
0:1cfbf6b8d116
|
205
|
|
tonydbeck |
0:1cfbf6b8d116
|
206
|
writeChar5x8(_page, _column, value);
|
tonydbeck |
0:1cfbf6b8d116
|
207
|
_column+=6;
|
tonydbeck |
0:1cfbf6b8d116
|
208
|
|
tonydbeck |
0:1cfbf6b8d116
|
209
|
|
tonydbeck |
0:1cfbf6b8d116
|
210
|
return value;
|
tonydbeck |
0:1cfbf6b8d116
|
211
|
}
|
tonydbeck |
0:1cfbf6b8d116
|
212
|
|
tonydbeck |
0:1cfbf6b8d116
|
213
|
int gLCD::_getc() {
|
tonydbeck |
0:1cfbf6b8d116
|
214
|
return -1;
|
tonydbeck |
0:1cfbf6b8d116
|
215
|
}
|
tonydbeck |
0:1cfbf6b8d116
|
216
|
|
tonydbeck |
0:1cfbf6b8d116
|
217
|
|
tonydbeck |
0:1cfbf6b8d116
|
218
|
void gLCD::writeBitmap(const int *ptrBitmap)
|
tonydbeck |
0:1cfbf6b8d116
|
219
|
{
|
tonydbeck |
0:1cfbf6b8d116
|
220
|
|
tonydbeck |
0:1cfbf6b8d116
|
221
|
|
tonydbeck |
0:1cfbf6b8d116
|
222
|
|
tonydbeck |
0:1cfbf6b8d116
|
223
|
for(int a=0; a < 8; a++)
|
tonydbeck |
0:1cfbf6b8d116
|
224
|
{
|
tonydbeck |
0:1cfbf6b8d116
|
225
|
locate(a,0);
|
tonydbeck |
0:1cfbf6b8d116
|
226
|
|
tonydbeck |
0:1cfbf6b8d116
|
227
|
for(int b=0; b < 128; b++)
|
tonydbeck |
0:1cfbf6b8d116
|
228
|
{
|
tonydbeck |
0:1cfbf6b8d116
|
229
|
writeData(*ptrBitmap);
|
tonydbeck |
0:1cfbf6b8d116
|
230
|
ptrBitmap+=8;
|
tonydbeck |
0:1cfbf6b8d116
|
231
|
|
tonydbeck |
0:1cfbf6b8d116
|
232
|
|
tonydbeck |
0:1cfbf6b8d116
|
233
|
}
|
tonydbeck |
0:1cfbf6b8d116
|
234
|
ptrBitmap-=1023;
|
tonydbeck |
0:1cfbf6b8d116
|
235
|
}
|
tonydbeck |
0:1cfbf6b8d116
|
236
|
|
tonydbeck |
0:1cfbf6b8d116
|
237
|
} |