OLED_Driver.c
9.07 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
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
/******************************************************************************
***************************Intermediate driver layer***************************
* | file : OLED_Driver.c
* | version : V1.0
* | date : 2017-11-09
* | function : SSD1327 Drive function
note:
Image scanning:
Please use progressive scanning to generate images or fonts
******************************************************************************/
#include "OLED_Driver.h"
#include <stdio.h>
COLOR Buffer[OLED_WIDTH / 2 * OLED_HEIGHT];
OLED_DIS sOLED_DIS;
/*******************************************************************************
function:
Hardware reset
*******************************************************************************/
static void OLED_Reset(void)
{
OLED_RST = 1;
//PC12 = 1;
Driver_Delay_ms(100);
OLED_RST = 0;
//PC12 = 0;
Driver_Delay_ms(100);
OLED_RST = 1;
//PC12 = 1;
Driver_Delay_ms(100);
}
/*******************************************************************************
function:
Write register address and data
*******************************************************************************/
void OLED_WriteReg(uint8_t Reg)
{
#if USE_SPI_4W
OLED_DC = 0;
OLED_CS = 0;
SPI4W_Write_Byte(Reg);
OLED_CS = 1;
#elif USE_IIC
I2C_Write_Byte(Reg,IIC_CMD);
#endif
}
void OLED_WriteData(uint8_t Data)
{
#if USE_SPI_4W
OLED_DC = 1;
OLED_CS = 0;
SPI4W_Write_Byte(Data);
OLED_CS = 1;
#elif USE_IIC
I2C_Write_Byte(Data,IIC_RAM);
#endif
}
/*******************************************************************************
function:
Common register initialization
*******************************************************************************/
static void OLED_InitReg(void)
{
OLED_WriteReg(0xae);//--turn off oled panel
OLED_WriteReg(0x15); // set column address
OLED_WriteReg(0x00); // start column 0
OLED_WriteReg(0x7f); // end column 127
OLED_WriteReg(0x75); // set row address
OLED_WriteReg(0x00); // start row 0
OLED_WriteReg(0x7f); // end row 127
OLED_WriteReg(0x81); // set contrast control
OLED_WriteReg(0x80);
OLED_WriteReg(0xa0); // gment remap
OLED_WriteReg(0x51); //51
OLED_WriteReg(0xa1); // start line
OLED_WriteReg(0x00);
OLED_WriteReg(0xa2); // display offset
OLED_WriteReg(0x00);
OLED_WriteReg(0xa4); // rmal display
OLED_WriteReg(0xa8); // set multiplex ratio
OLED_WriteReg(0x7f);
OLED_WriteReg(0xb1); // set phase leghth
OLED_WriteReg(0xf1);
OLED_WriteReg(0xb3); // set dclk
OLED_WriteReg(0x00); //80Hz:0xc1 90Hz:0xe1 100Hz:0x00 110Hz:0x30 120Hz:0x50 130Hz:0x70 01
OLED_WriteReg(0xab); //
OLED_WriteReg(0x01); //
OLED_WriteReg(0xb6); // set phase leghth
OLED_WriteReg(0x0f);
OLED_WriteReg(0xbe);
OLED_WriteReg(0x0f);
OLED_WriteReg(0xbc);
OLED_WriteReg(0x08);
OLED_WriteReg(0xd5);
OLED_WriteReg(0x62);
OLED_WriteReg(0xfd);
OLED_WriteReg(0x12);
}
/********************************************************************************
function: Set the display scan and color transfer modes
parameter:
Scan_dir : Scan direction
Colorchose : RGB or GBR color format
********************************************************************************/
void OLED_SetGramScanWay(OLED_SCAN_DIR Scan_dir)
{
//Get the screen scan direction
sOLED_DIS.OLED_Scan_Dir = Scan_dir;
//Get GRAM and OLED width and height
if(Scan_dir == L2R_U2D || Scan_dir == L2R_D2U || Scan_dir == R2L_U2D || Scan_dir == R2L_D2U) {
sOLED_DIS.OLED_Dis_Column = OLED_WIDTH;
sOLED_DIS.OLED_Dis_Page = OLED_HEIGHT;
sOLED_DIS.OLED_X_Adjust = OLED_X;
sOLED_DIS.OLED_Y_Adjust = OLED_Y;
} else {
sOLED_DIS.OLED_Dis_Column = OLED_HEIGHT;
sOLED_DIS.OLED_Dis_Page = OLED_WIDTH;
sOLED_DIS.OLED_X_Adjust = OLED_Y;
sOLED_DIS.OLED_Y_Adjust = OLED_X;
}
}
/********************************************************************************
function:
initialization
********************************************************************************/
void OLED_Init(OLED_SCAN_DIR OLED_ScanDir)
{
//Hardware reset
OLED_Reset();
//Set the initialization register
OLED_InitReg();
//Set the display scan and color transfer modes
OLED_SetGramScanWay(OLED_ScanDir);
Driver_Delay_ms(200);
//Turn on the OLED display
OLED_WriteReg(0xAF);
}
/********************************************************************************
function: Set the display point(Xpoint, Ypoint)
parameter:
xStart : X direction Start coordinates
xEnd : X direction end coordinates
********************************************************************************/
void OLED_SetCursor(POINT Xpoint, POINT Ypoint)
{
if((Xpoint > sOLED_DIS.OLED_Dis_Column) || (Ypoint > sOLED_DIS.OLED_Dis_Page))
return;
OLED_WriteReg(0x15);
OLED_WriteReg(Xpoint);
OLED_WriteReg(Xpoint);
OLED_WriteReg(0x75);
OLED_WriteReg(Ypoint);
OLED_WriteReg(Ypoint);
}
/********************************************************************************
function: Set the display Window(Xstart, Ystart, Xend, Yend)
parameter:
xStart : X direction Start coordinates
Ystart : Y direction Start coordinates
Xend : X direction end coordinates
Yend : Y direction end coordinates
********************************************************************************/
void OLED_SetWindow(POINT Xstart, POINT Ystart, POINT Xend, POINT Yend)
{
if((Xstart > sOLED_DIS.OLED_Dis_Column) || (Ystart > sOLED_DIS.OLED_Dis_Page) ||
(Xend > sOLED_DIS.OLED_Dis_Column) || (Yend > sOLED_DIS.OLED_Dis_Page))
return;
OLED_WriteReg(0x15);
OLED_WriteReg(Xstart);
OLED_WriteReg(Xend - 1);
OLED_WriteReg(0x75);
OLED_WriteReg(Ystart);
OLED_WriteReg(Yend - 1);
}
/********************************************************************************
function: Set show color
parameter:
Color : Set show color,16-bit depth
********************************************************************************/
//static void OLED_SetColor(LENGTH Dis_Width, LENGTH Dis_Height, COLOR Color ){
void OLED_SetColor(POINT Xpoint, POINT Ypoint, COLOR Color)
{
if(Xpoint > sOLED_DIS.OLED_Dis_Column || Ypoint > sOLED_DIS.OLED_Dis_Page) {
return;
}
//1 byte control two points
if(Xpoint % 2 == 0) {
Buffer[Xpoint / 2 + Ypoint * 64] = (Color << 4) | Buffer[Xpoint / 2 + Ypoint * 64];
} else {
Buffer[Xpoint / 2 + Ypoint * 64] = (Color & 0x0f) | Buffer[Xpoint / 2 + Ypoint * 64];
}
}
/********************************************************************************
function:
Clear screen
********************************************************************************/
void OLED_Clear(COLOR Color)
{
unsigned int i,m;
//OLED_SetWindow(0, 0, sOLED_DIS.OLED_Dis_Column, sOLED_DIS.OLED_Dis_Page);
for(i = 0; i < sOLED_DIS.OLED_Dis_Page; i++) {
for(m = 0; m < (sOLED_DIS.OLED_Dis_Column / 2); m++) {
Buffer[i * (sOLED_DIS.OLED_Dis_Column / 2) + m] = Color | (Color << 4);
}
}
}
/********************************************************************************
function: Update all memory to LCD
********************************************************************************/
void OLED_Display(void)
{
uint16_t page, Column;
COLOR *pBuf = (COLOR *)Buffer;
OLED_SetWindow(0, 0, sOLED_DIS.OLED_Dis_Column, sOLED_DIS.OLED_Dis_Page);
//write data
for (page = 0; page < sOLED_DIS.OLED_Dis_Page; page++) {
for(Column = 0; Column < sOLED_DIS.OLED_Dis_Column / 2; Column++ ) {
OLED_WriteData(*pBuf);
pBuf++;
}
}
}
/********************************************************************************
function:
Clear Window
********************************************************************************/
void OLED_ClearWindow(POINT Xstart, POINT Ystart, POINT Xend, POINT Yend, COLOR Color)
{
uint16_t i,m, Xpoint, Ypoint;
Xpoint = (Xend - Xstart) / 2;
Ypoint = Yend - Ystart;
uint16_t Num = Xstart + Ystart * (sOLED_DIS.OLED_Dis_Column / 2);
for(i = 0; i < Ypoint; i++) {
for(m = 0; m < Xpoint; m++) {
Buffer[Num + m] = 0x00;
}
Num = Xstart + (Ystart + i + 1) * (sOLED_DIS.OLED_Dis_Column / 2);
}
}
/********************************************************************************
function: Update Window memory to LCD
********************************************************************************/
void OLED_DisWindow(POINT Xstart, POINT Ystart, POINT Xend, POINT Yend)
{
uint16_t page, Column, Xpoint, Ypoint;
Xpoint = (Xend - Xstart) / 2;
Ypoint = Yend - Ystart;
OLED_SetWindow(Xstart, Ystart, Xend, Yend);
//write data
COLOR *pBuf = (COLOR *)Buffer + Xstart + Ystart * (sOLED_DIS.OLED_Dis_Column / 2);
for (page = 0; page < Ypoint; page++) {
for(Column = 0; Column < Xpoint; Column++ ) {
OLED_WriteData(*pBuf);
pBuf++;
}
pBuf = (COLOR *)Buffer + Xstart + (Ystart + page + 1) * (sOLED_DIS.OLED_Dis_Column / 2);
}
}