-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathKS0108.c
208 lines (186 loc) · 6.84 KB
/
KS0108.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
//-------------------------------------------------------------------------------------------------
// Universal KS0108 driver library
// (c) Rados³aw Kwiecieñ, [email protected]
// Modified by Stephen Gordon
//-------------------------------------------------------------------------------------------------
#include <xc.h>
#include "KS0108.h"
#include "dispbuf-config.h"
#ifdef USE_CORETIMER
#include "peripheral/coretimer/plib_coretimer.h"
#define DelayUs(x) CORETIMER_DelayUs(x)
#define DelayMs(x) CORETIMER_DelayMs(x)
#else
#include "delay.h"
#endif
//-------------------------------------------------------------------------------------------------
unsigned char screen_x, screen_y;
unsigned char KS0108Buf[DISPBUF_SCREEN_WIDTH*DISPBUF_SCREEN_HEIGHT / 8];
//-------------------------------------------------------------------------------------------------
//
//-------------------------------------------------------------------------------------------------
void KS0108_Initialize(void) {
unsigned char i;
KS0108_InitializePorts();
for (i = 0; i < 2; i++)
KS0108_WriteCommand((DISPLAY_ON_CMD | DISPLAY_ON), i);
}
//-------------------------------------------------------------------------------------------------
//
//-------------------------------------------------------------------------------------------------
void KS0108_GoTo(unsigned char x, unsigned char y) {
unsigned char i;
screen_x = x;
screen_y = y;
for (i = 0; i < DISPBUF_SCREEN_WIDTH / 64; i++) {
KS0108_WriteCommand(DISPLAY_SET_Y | 0, i);
KS0108_WriteCommand(DISPLAY_SET_X | y, i);
KS0108_WriteCommand(DISPLAY_START_LINE | 0, i);
}
KS0108_WriteCommand(DISPLAY_SET_Y | (x % 64), (x / 64));
KS0108_WriteCommand(DISPLAY_SET_X | y, (x / 64));
}
//-------------------------------------------------------------------------------------------------
//
//-------------------------------------------------------------------------------------------------
void KS0108_ClearScreen(void) {
unsigned char i, j;
for (j = 0; j < DISPBUF_SCREEN_HEIGHT / 8; j++) {
KS0108_GoTo(0, j);
for (i = 0; i < DISPBUF_SCREEN_WIDTH; i++)
KS0108_WriteData(0x00);
}
}
inline void KS0108_Delay_EH(void) {
asm("nop");
DelayUs(KS0108_EN_H);
}
inline void KS0108_Delay_EL(void) {
asm("nop");
DelayUs(KS0108_EN_L);
}
//-------------------------------------------------------------------------------------------------
//
//-------------------------------------------------------------------------------------------------
void KS0108_InitializePorts(void) {
KS0108_CTRL_DIRN_OUT();
KS0108_CS1_HIGH();
KS0108_CS2_HIGH();
KS0108_RST_HIGH();
}
//-------------------------------------------------------------------------------------------------
// Enable Controller (0-2)
//-------------------------------------------------------------------------------------------------
void KS0108_EnableController(unsigned char controller) {
KS0108_Delay_EH();
switch (controller) {
#ifdef KS0108_SWAP_SIDES
case 0: KS0108_CS1_LOW();
break;
case 1: KS0108_CS2_LOW();
break;
#else
case 0: KS0108_CS2_LOW();
break;
case 1: KS0108_CS1_LOW();
break;
#endif
}
KS0108_Delay_EH();
}
//-------------------------------------------------------------------------------------------------
// Disable Controller (0-2)
//-------------------------------------------------------------------------------------------------
void KS0108_DisableController(unsigned char controller) {
KS0108_Delay_EL();
switch (controller) {
#ifdef KS0108_SWAP_SIDES
case 0: KS0108_CS1_HIGH();
break;
case 1: KS0108_CS2_HIGH();
break;
#else
case 0: KS0108_CS2_HIGH();
break;
case 1: KS0108_CS1_HIGH();
break;
#endif
}
KS0108_Delay_EL();
}
//-------------------------------------------------------------------------------------------------
// Read Status from specified controller (0-2)
//-------------------------------------------------------------------------------------------------
unsigned char KS0108_ReadStatus(unsigned char controller) {
unsigned char status;
KS0108_DATA_DIRN_IN();
KS0108_RW_HIGH();
KS0108_RS_LOW();
KS0108_EnableController(controller);
KS0108_E_HIGH();
DelayUs(KS0108_EN_H);
status = KS0108_DATA_INPUT();
KS0108_E_LOW();
KS0108_DisableController(controller);
return status;
}
//-------------------------------------------------------------------------------------------------
// Write command to specified controller
//-------------------------------------------------------------------------------------------------
void KS0108_WriteCommand(unsigned char commandToWrite, unsigned char controller) {
while (KS0108_ReadStatus(controller) & DISPLAY_STATUS_BUSY);
KS0108_DATA_DIRN_OUT();
KS0108_RW_LOW();
KS0108_RS_LOW();
KS0108_EnableController(controller);
KS0108_DATA_OUTPUT(commandToWrite);
KS0108_E_HIGH();
KS0108_Delay_EL();
KS0108_E_LOW();
KS0108_DisableController(controller);
}
//-------------------------------------------------------------------------------------------------
// Read data from current position
//-------------------------------------------------------------------------------------------------
unsigned char KS0108_ReadData(void) {
unsigned char data;
while (KS0108_ReadStatus(screen_x / 64) & DISPLAY_STATUS_BUSY);
KS0108_DATA_DIRN_IN();
KS0108_RW_HIGH();
KS0108_RS_HIGH();
KS0108_EnableController(screen_x / 64);
KS0108_E_HIGH();
data = KS0108_DATA_INPUT();
KS0108_E_LOW();
KS0108_DisableController(screen_x / 64);
screen_x++;
return data;
}
//-------------------------------------------------------------------------------------------------
// Write data to current position
//-------------------------------------------------------------------------------------------------
void KS0108_WriteData(unsigned char dataToWrite) {
while (KS0108_ReadStatus(screen_x / 64) & DISPLAY_STATUS_BUSY);
KS0108_DATA_DIRN_OUT();
KS0108_RW_LOW();
KS0108_RS_HIGH();
KS0108_DATA_OUTPUT(dataToWrite);
KS0108_EnableController(screen_x / 64);
KS0108_E_HIGH();
KS0108_Delay_EL();
KS0108_E_LOW();
KS0108_DisableController(screen_x / 64);
screen_x++;
KS0108_Delay_EL();
}
//-------------------------------------------------------------------------------------------------
//
//-------------------------------------------------------------------------------------------------
void KS0108_WriteBuf(unsigned char * bmp, unsigned char x, unsigned char y, unsigned char dx, unsigned char dy) {
unsigned char i, j;
for (j = 0; j < dy / 8; j++) {
KS0108_GoTo(x, y + j);
for (i = 0; i < dx; i++)
KS0108_WriteData(*bmp++);
}
}