/
st7565.c
251 lines (192 loc) · 6.01 KB
/
st7565.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
#include <htc.h>
#include "st7565-config.h"
#include "st7565.h"
#include "delay.h"
/** Global buffer to hold the current screen contents. */
// This has to be kept here because the width & height are set in
// st7565-config.h
unsigned char glcd_buffer[SCREEN_WIDTH * SCREEN_HEIGHT / 8];
#ifdef ST7565_DIRTY_PAGES
unsigned char glcd_dirty_pages;
#endif
void glcd_pixel(unsigned char x, unsigned char y, unsigned char colour) {
if (x > SCREEN_WIDTH || y > SCREEN_HEIGHT) return;
// Real screen coordinates are 0-63, not 1-64.
x -= 1;
y -= 1;
unsigned short array_pos = x + ((y / 8) * 128);
#ifdef ST7565_DIRTY_PAGES
#warning ** ST7565_DIRTY_PAGES enabled, only changed pages will be written to the GLCD **
glcd_dirty_pages |= 1 << (array_pos / 128);
#endif
if (colour) {
glcd_buffer[array_pos] |= 1 << (y % 8);
} else {
glcd_buffer[array_pos] &= 0xFF ^ 1 << (y % 8);
}
}
void glcd_blank() {
// Reset the internal buffer
for (int n = 1; n <= (SCREEN_WIDTH * SCREEN_HEIGHT / 8) - 1; n++) {
glcd_buffer[n] = 0;
}
// Clear the actual screen
for (int y = 0; y < 8; y++) {
glcd_command(GLCD_CMD_SET_PAGE | y);
// Reset column to 0 (the left side)
glcd_command(GLCD_CMD_COLUMN_LOWER);
glcd_command(GLCD_CMD_COLUMN_UPPER);
// We iterate to 132 as the internal buffer is 65*132, not
// 64*124.
for (int x = 0; x < 132; x++) {
glcd_data(0x00);
}
}
}
void glcd_refresh() {
for (int y = 0; y < 8; y++) {
#ifdef ST7565_DIRTY_PAGES
// Only copy this page if it is marked as "dirty"
if (!(glcd_dirty_pages & (1 << y))) continue;
#endif
glcd_command(GLCD_CMD_SET_PAGE | y);
// Reset column to the left side. The internal memory of the
// screen is 132*64, we need to account for this if the display
// is flipped.
//
// Some screens seem to map the internal memory to the screen
// pixels differently, the ST7565_REVERSE define allows this to
// be controlled if necessary.
#ifdef ST7565_REVERSE
if (!glcd_flipped) {
#else
if (glcd_flipped) {
#endif
glcd_command(GLCD_CMD_COLUMN_LOWER | 4);
} else {
glcd_command(GLCD_CMD_COLUMN_LOWER);
}
glcd_command(GLCD_CMD_COLUMN_UPPER);
for (int x = 0; x < 128; x++) {
glcd_data(glcd_buffer[y * 128 + x]);
}
}
#ifdef ST7565_DIRTY_PAGES
// All pages have now been updated, reset the indicator.
glcd_dirty_pages = 0;
#endif
}
void glcd_init() {
// Select the chip
GLCD_CS1 = 0;
GLCD_RESET = 0;
// Datasheet says "wait for power to stabilise" but gives
// no specific time!
DelayMs(50);
GLCD_RESET = 1;
// Datasheet says max 1ms here
//DelayMs(1);
// Set LCD bias to 1/9th
glcd_command(GLCD_CMD_BIAS_9);
// Horizontal output direction (ADC segment driver selection)
glcd_command(GLCD_CMD_HORIZONTAL_NORMAL);
// Vertical output direction (common output mode selection)
glcd_command(GLCD_CMD_VERTICAL_REVERSE);
// The screen is the "normal" way up
glcd_flipped = 0;
// Set internal resistor. A suitable middle value is used as
// the default.
glcd_command(GLCD_CMD_RESISTOR | 0x3);
// Power control setting (datasheet step 7)
// Note: Skipping straight to 0x7 works with my hardware.
// glcd_command(GLCD_CMD_POWER_CONTROL | 0x4);
// DelayMs(50);
// glcd_command(GLCD_CMD_POWER_CONTROL | 0x6);
// DelayMs(50);
glcd_command(GLCD_CMD_POWER_CONTROL | 0x7);
// DelayMs(10);
// Volume set (brightness control). A middle value is used here
// also.
glcd_command(GLCD_CMD_VOLUME_MODE);
glcd_command(31);
// Reset start position to the top
glcd_command(GLCD_CMD_DISPLAY_START);
// Turn the display on
glcd_command(GLCD_CMD_DISPLAY_ON);
// Unselect the chip
GLCD_CS1 = 1;
}
void glcd_data(unsigned char data) {
// A0 is high for display data
GLCD_A0 = 1;
// Select the chip
GLCD_CS1 = 0;
for (int n = 0; n < 8; n++) {
if (data & 0x80) {
GLCD_SDA = 1;
} else {
GLCD_SDA = 0;
}
// Pulse SCL
GLCD_SCL = 1;
GLCD_SCL = 0;
data <<= 1;
}
// Unselect the chip
GLCD_CS1 = 1;
}
void glcd_command(char command) {
// A0 is low for command data
GLCD_A0 = 0;
// Select the chip
GLCD_CS1 = 0;
for (int n = 0; n < 8; n++) {
if (command & 0x80) {
GLCD_SDA = 1;
} else {
GLCD_SDA = 0;
}
// Pulse SCL
GLCD_SCL = 1;
GLCD_SCL = 0;
command <<= 1;
}
// Unselect the chip
GLCD_CS1 = 1;
}
void glcd_flip_screen(unsigned char flip) {
if (flip) {
glcd_command(GLCD_CMD_HORIZONTAL_NORMAL);
glcd_command(GLCD_CMD_VERTICAL_REVERSE);
glcd_flipped = 0;
} else {
glcd_command(GLCD_CMD_HORIZONTAL_REVERSE);
glcd_command(GLCD_CMD_VERTICAL_NORMAL);
glcd_flipped = 1;
}
}
void glcd_inverse_screen(unsigned char inverse) {
if (inverse) {
glcd_command(GLCD_CMD_DISPLAY_REVERSE);
} else {
glcd_command(GLCD_CMD_DISPLAY_NORMAL);
}
}
void glcd_test_card() {
unsigned char p = 0xF0;
for (int n = 1; n <= (SCREEN_WIDTH * SCREEN_HEIGHT / 8); n++) {
glcd_buffer[n - 1] = p;
if (n % 4 == 0) {
unsigned char q = p;
p = p << 4;
p |= q >> 4;
}
}
glcd_refresh();
}
void glcd_contrast(char resistor_ratio, char contrast) {
if (resistor_ratio > 7 || contrast > 63) return;
glcd_command(GLCD_CMD_RESISTOR | resistor_ratio);
glcd_command(GLCD_CMD_VOLUME_MODE);
glcd_command(contrast);
}