Você está na página 1de 7

1 /******************************************************************************

2 *
3 *
4 *******************************************************************************/
5
6
7
8 #include <stdint.h>
9
10 //------------------------------ Definitions ---------------------------------//
11
12 #define SSD1306_I2C_ADDRESS 0x7A
13
14 #if !defined SSD1306_128_32 && !defined SSD1306_96_16
15 #define SSD1306_128_64
16 #endif
17 #if defined SSD1306_128_32 && defined SSD1306_96_16
18 #error "Only one SSD1306 display can be specified at once"
19 #endif
20
21 #if defined SSD1306_128_64
22 #define SSD1306_LCDWIDTH 128
23 #define SSD1306_LCDHEIGHT 64
24 #endif
25 #if defined SSD1306_128_32
26 #define SSD1306_LCDWIDTH 128
27 #define SSD1306_LCDHEIGHT 32
28 #endif
29 #if defined SSD1306_96_16
30 #define SSD1306_LCDWIDTH 96
31 #define SSD1306_LCDHEIGHT 16
32 #endif
33
34 #define SSD1306_SETCONTRAST 0x81
35 #define SSD1306_DISPLAYALLON_RESUME 0xA4
36 #define SSD1306_DISPLAYALLON 0xA5
37 #define SSD1306_NORMALDISPLAY 0xA6
38 #define SSD1306_INVERTDISPLAY_ 0xA7
39 #define SSD1306_DISPLAYOFF 0xAE
40 #define SSD1306_DISPLAYON 0xAF
41 #define SSD1306_SETDISPLAYOFFSET 0xD3
42 #define SSD1306_SETCOMPINS 0xDA
43 #define SSD1306_SETVCOMDETECT 0xDB
44 #define SSD1306_SETDISPLAYCLOCKDIV 0xD5
45 #define SSD1306_SETPRECHARGE 0xD9
46 #define SSD1306_SETMULTIPLEX 0xA8
47 #define SSD1306_SETLOWCOLUMN 0x00
48 #define SSD1306_SETHIGHCOLUMN 0x10
49 #define SSD1306_SETSTARTLINE 0x40
50 #define SSD1306_MEMORYMODE 0x20
51 #define SSD1306_COLUMNADDR 0x21
52 #define SSD1306_PAGEADDR 0x22
53 #define SSD1306_COMSCANINC 0xC0
54 #define SSD1306_COMSCANDEC 0xC8
55 #define SSD1306_SEGREMAP 0xA0
56 #define SSD1306_CHARGEPUMP 0x8D
57 #define SSD1306_EXTERNALVCC 0x01
58 #define SSD1306_SWITCHCAPVCC 0x02
59
60 // Scrolling #defines
61 #define SSD1306_ACTIVATE_SCROLL 0x2F
62 #define SSD1306_DEACTIVATE_SCROLL 0x2E
63 #define SSD1306_SET_VERTICAL_SCROLL_AREA 0xA3
64 #define SSD1306_RIGHT_HORIZONTAL_SCROLL 0x26
65 #define SSD1306_LEFT_HORIZONTAL_SCROLL 0x27
66 #define SSD1306_VERTICAL_AND_RIGHT_HORIZONTAL_SCROLL 0x29
67 #define SSD1306_VERTICAL_AND_LEFT_HORIZONTAL_SCROLL 0x2A
68
69
70 uint8_t _i2caddr, _vccstate, x_pos = 1, y_pos = 1;
71
72 //--------------------------------------------------------------------------//
73
74 int1 wrap = TRUE;
75
76 const char Font[] = {
77 0x00, 0x00, 0x00, 0x00, 0x00,
78 0x00, 0x00, 0x5F, 0x00, 0x00,
79 0x00, 0x07, 0x00, 0x07, 0x00,
80 0x14, 0x7F, 0x14, 0x7F, 0x14,
81 0x24, 0x2A, 0x7F, 0x2A, 0x12,
82 0x23, 0x13, 0x08, 0x64, 0x62,
83 0x36, 0x49, 0x56, 0x20, 0x50,
84 0x00, 0x08, 0x07, 0x03, 0x00,
85 0x00, 0x1C, 0x22, 0x41, 0x00,
86 0x00, 0x41, 0x22, 0x1C, 0x00,
87 0x2A, 0x1C, 0x7F, 0x1C, 0x2A,
88 0x08, 0x08, 0x3E, 0x08, 0x08,
89 0x00, 0x80, 0x70, 0x30, 0x00,
90 0x08, 0x08, 0x08, 0x08, 0x08,
91 0x00, 0x00, 0x60, 0x60, 0x00,
92 0x20, 0x10, 0x08, 0x04, 0x02,
93 0x3E, 0x51, 0x49, 0x45, 0x3E,
94 0x00, 0x42, 0x7F, 0x40, 0x00,
95 0x72, 0x49, 0x49, 0x49, 0x46,
96 0x21, 0x41, 0x49, 0x4D, 0x33,
97 0x18, 0x14, 0x12, 0x7F, 0x10,
98 0x27, 0x45, 0x45, 0x45, 0x39,
99 0x3C, 0x4A, 0x49, 0x49, 0x31,
100 0x41, 0x21, 0x11, 0x09, 0x07,
101 0x36, 0x49, 0x49, 0x49, 0x36,
102 0x46, 0x49, 0x49, 0x29, 0x1E,
103 0x00, 0x00, 0x14, 0x00, 0x00,
104 0x00, 0x40, 0x34, 0x00, 0x00,
105 0x00, 0x08, 0x14, 0x22, 0x41,
106 0x14, 0x14, 0x14, 0x14, 0x14,
107 0x00, 0x41, 0x22, 0x14, 0x08,
108 0x02, 0x01, 0x59, 0x09, 0x06,
109 0x3E, 0x41, 0x5D, 0x59, 0x4E,
110 0x7C, 0x12, 0x11, 0x12, 0x7C,
111 0x7F, 0x49, 0x49, 0x49, 0x36,
112 0x3E, 0x41, 0x41, 0x41, 0x22,
113 0x7F, 0x41, 0x41, 0x41, 0x3E,
114 0x7F, 0x49, 0x49, 0x49, 0x41,
115 0x7F, 0x09, 0x09, 0x09, 0x01,
116 0x3E, 0x41, 0x41, 0x51, 0x73,
117 0x7F, 0x08, 0x08, 0x08, 0x7F,
118 0x00, 0x41, 0x7F, 0x41, 0x00,
119 0x20, 0x40, 0x41, 0x3F, 0x01,
120 0x7F, 0x08, 0x14, 0x22, 0x41,
121 0x7F, 0x40, 0x40, 0x40, 0x40,
122 0x7F, 0x02, 0x1C, 0x02, 0x7F,
123 0x7F, 0x04, 0x08, 0x10, 0x7F,
124 0x3E, 0x41, 0x41, 0x41, 0x3E,
125 0x7F, 0x09, 0x09, 0x09, 0x06,
126 0x3E, 0x41, 0x51, 0x21, 0x5E,
127 0x7F, 0x09, 0x19, 0x29, 0x46
128 };
129 const char Font2[] = {
130 0x26, 0x49, 0x49, 0x49, 0x32,
131 0x03, 0x01, 0x7F, 0x01, 0x03,
132 0x3F, 0x40, 0x40, 0x40, 0x3F,
133 0x1F, 0x20, 0x40, 0x20, 0x1F,
134 0x3F, 0x40, 0x38, 0x40, 0x3F,
135 0x63, 0x14, 0x08, 0x14, 0x63,
136 0x03, 0x04, 0x78, 0x04, 0x03,
137 0x61, 0x59, 0x49, 0x4D, 0x43,
138 0x00, 0x7F, 0x41, 0x41, 0x41,
139 0x02, 0x04, 0x08, 0x10, 0x20,
140 0x00, 0x41, 0x41, 0x41, 0x7F,
141 0x04, 0x02, 0x01, 0x02, 0x04,
142 0x40, 0x40, 0x40, 0x40, 0x40,
143 0x00, 0x03, 0x07, 0x08, 0x00,
144 0x20, 0x54, 0x54, 0x78, 0x40,
145 0x7F, 0x28, 0x44, 0x44, 0x38,
146 0x38, 0x44, 0x44, 0x44, 0x28,
147 0x38, 0x44, 0x44, 0x28, 0x7F,
148 0x38, 0x54, 0x54, 0x54, 0x18,
149 0x00, 0x08, 0x7E, 0x09, 0x02,
150 0x18, 0xA4, 0xA4, 0x9C, 0x78,
151 0x7F, 0x08, 0x04, 0x04, 0x78,
152 0x00, 0x44, 0x7D, 0x40, 0x00,
153 0x20, 0x40, 0x40, 0x3D, 0x00,
154 0x7F, 0x10, 0x28, 0x44, 0x00,
155 0x00, 0x41, 0x7F, 0x40, 0x00,
156 0x7C, 0x04, 0x78, 0x04, 0x78,
157 0x7C, 0x08, 0x04, 0x04, 0x78,
158 0x38, 0x44, 0x44, 0x44, 0x38,
159 0xFC, 0x18, 0x24, 0x24, 0x18,
160 0x18, 0x24, 0x24, 0x18, 0xFC,
161 0x7C, 0x08, 0x04, 0x04, 0x08,
162 0x48, 0x54, 0x54, 0x54, 0x24,
163 0x04, 0x04, 0x3F, 0x44, 0x24,
164 0x3C, 0x40, 0x40, 0x20, 0x7C,
165 0x1C, 0x20, 0x40, 0x20, 0x1C,
166 0x3C, 0x40, 0x30, 0x40, 0x3C,
167 0x44, 0x28, 0x10, 0x28, 0x44,
168 0x4C, 0x90, 0x90, 0x90, 0x7C,
169 0x44, 0x64, 0x54, 0x4C, 0x44,
170 0x00, 0x08, 0x36, 0x41, 0x00,
171 0x00, 0x00, 0x77, 0x00, 0x00,
172 0x00, 0x41, 0x36, 0x08, 0x00,
173 0x02, 0x01, 0x02, 0x04, 0x02
174 };
175
176
177 void ssd1306_command(uint8_t c) {
178 uint8_t control = 0x00; // Co = 0, D/C = 0
179 I2C_Start(SSD1306_STREAM);
180 I2C_Write(SSD1306_STREAM, _i2caddr);
181 I2C_Write(SSD1306_STREAM, control);
182 I2C_Write(SSD1306_STREAM, c);
183 I2C_Stop(SSD1306_STREAM);
184 }
185
186 void SSD1306_Init(uint8_t vccstate = SSD1306_SWITCHCAPVCC, uint8_t i2caddr =
SSD1306_I2C_ADDRESS) {
187 _vccstate = vccstate;
188 _i2caddr = i2caddr;
189 #ifdef SSD1306_RST
190 output_low(SSD1306_RST);
191 output_drive(SSD1306_RST);
192 delay_ms(10);
193 output_high(SSD1306_RST);
194 #endif
195 // Init sequence
196 ssd1306_command(SSD1306_DISPLAYOFF); // 0xAE
197 ssd1306_command(SSD1306_SETDISPLAYCLOCKDIV); // 0xD5
198 ssd1306_command(0x80); // the suggested ratio 0x80
199
200 ssd1306_command(SSD1306_SETMULTIPLEX); // 0xA8
201 ssd1306_command(SSD1306_LCDHEIGHT - 1);
202
203 ssd1306_command(SSD1306_SETDISPLAYOFFSET); // 0xD3
204 ssd1306_command(0x0); // no offset
205 ssd1306_command(SSD1306_SETSTARTLINE | 0x0); // line #0
206 ssd1306_command(SSD1306_CHARGEPUMP); // 0x8D
207 if (vccstate == SSD1306_EXTERNALVCC)
208 { ssd1306_command(0x10); }
209 else
210 { ssd1306_command(0x14); }
211 ssd1306_command(SSD1306_MEMORYMODE); // 0x20
212 ssd1306_command(0x00); // 0x0 act like ks0108
213 ssd1306_command(SSD1306_SEGREMAP | 0x1);
214 ssd1306_command(SSD1306_COMSCANDEC);
215
216 #if defined SSD1306_128_32
217 ssd1306_command(SSD1306_SETCOMPINS); // 0xDA
218 ssd1306_command(0x02);
219 ssd1306_command(SSD1306_SETCONTRAST); // 0x81
220 ssd1306_command(0x8F);
221
222 #elif defined SSD1306_128_64
223 ssd1306_command(SSD1306_SETCOMPINS); // 0xDA
224 ssd1306_command(0x12);
225 ssd1306_command(SSD1306_SETCONTRAST); // 0x81
226 if (vccstate == SSD1306_EXTERNALVCC)
227 { ssd1306_command(0x9F); }
228 else
229 { ssd1306_command(0xCF); }
230
231 #elif defined SSD1306_96_16
232 ssd1306_command(SSD1306_SETCOMPINS); // 0xDA
233 ssd1306_command(0x2); //ada x12
234 ssd1306_command(SSD1306_SETCONTRAST); // 0x81
235 if (vccstate == SSD1306_EXTERNALVCC)
236 { ssd1306_command(0x10); }
237 else
238 { ssd1306_command(0xAF); }
239
240 #endif
241
242 ssd1306_command(SSD1306_SETPRECHARGE); // 0xd9
243 if (vccstate == SSD1306_EXTERNALVCC)
244 { ssd1306_command(0x22); }
245 else
246 { ssd1306_command(0xF1); }
247 ssd1306_command(SSD1306_SETVCOMDETECT); // 0xDB
248 ssd1306_command(0x40);
249 ssd1306_command(SSD1306_DISPLAYALLON_RESUME); // 0xA4
250 ssd1306_command(SSD1306_NORMALDISPLAY); // 0xA6
251
252 ssd1306_command(SSD1306_DEACTIVATE_SCROLL);
253
254 ssd1306_command(SSD1306_DISPLAYON);//--turn on oled panel
255 }
256
257 void SSD1306_StartScrollRight(uint8_t start, uint8_t stop) {
258 ssd1306_command(SSD1306_RIGHT_HORIZONTAL_SCROLL);
259 ssd1306_command(0X00);
260 ssd1306_command(start); // start page
261 ssd1306_command(0X00);
262 ssd1306_command(stop); // end page
263 ssd1306_command(0X00);
264 ssd1306_command(0XFF);
265 ssd1306_command(SSD1306_ACTIVATE_SCROLL);
266 }
267
268 void SSD1306_StartScrollLeft(uint8_t start, uint8_t stop) {
269 ssd1306_command(SSD1306_LEFT_HORIZONTAL_SCROLL);
270 ssd1306_command(0X00);
271 ssd1306_command(start);
272 ssd1306_command(0X00);
273 ssd1306_command(stop);
274 ssd1306_command(0X00);
275 ssd1306_command(0XFF);
276 ssd1306_command(SSD1306_ACTIVATE_SCROLL);
277 }
278
279 void SSD1306_StartScrollDiagRight(uint8_t start, uint8_t stop) {
280 ssd1306_command(SSD1306_SET_VERTICAL_SCROLL_AREA);
281 ssd1306_command(0X00);
282 ssd1306_command(SSD1306_LCDHEIGHT);
283 ssd1306_command(SSD1306_VERTICAL_AND_RIGHT_HORIZONTAL_SCROLL);
284 ssd1306_command(0X00);
285 ssd1306_command(start);
286 ssd1306_command(0X00);
287 ssd1306_command(stop);
288 ssd1306_command(0X01);
289 ssd1306_command(SSD1306_ACTIVATE_SCROLL);
290 }
291
292 void SSD1306_StartScrollDiagLeft(uint8_t start, uint8_t stop) {
293 ssd1306_command(SSD1306_SET_VERTICAL_SCROLL_AREA);
294 ssd1306_command(0X00);
295 ssd1306_command(SSD1306_LCDHEIGHT);
296 ssd1306_command(SSD1306_VERTICAL_AND_LEFT_HORIZONTAL_SCROLL);
297 ssd1306_command(0X00);
298 ssd1306_command(start);
299 ssd1306_command(0X00);
300 ssd1306_command(stop);
301 ssd1306_command(0X01);
302 ssd1306_command(SSD1306_ACTIVATE_SCROLL);
303 }
304
305 void SSD1306_StopScroll(void) {
306 ssd1306_command(SSD1306_DEACTIVATE_SCROLL);
307 }
308
309 void SSD1306_Dim(int1 dim) {
310 uint8_t contrast;
311 if (dim)
312 contrast = 0; // Dimmed display
313 else {
314 if (_vccstate == SSD1306_EXTERNALVCC)
315 contrast = 0x9F;
316 else
317 contrast = 0xCF;
318 }
319 // the range of contrast to too small to be really useful
320 // it is useful to dim the display
321 ssd1306_command(SSD1306_SETCONTRAST);
322 ssd1306_command(contrast);
323 }
324
325 void SSD1306_SetTextWrap(int1 w) {
326 wrap = w;
327 }
328
329 void SSD1306_InvertDisplay(int1 i) {
330 if (i)
331 ssd1306_command(SSD1306_INVERTDISPLAY_);
332 else
333 ssd1306_command(SSD1306_NORMALDISPLAY);
334 }
335
336 void SSD1306_GotoXY(uint8_t x, uint8_t y) {
337 if((x > 21) || y > 8)
338 return;
339 x_pos = x;
340 y_pos = y;
341 }
342
343 void SSD1306_PutC(uint8_t c) {
344 uint8_t font_c;
345 if((c < ' ') || (c > '~'))
346 c = '?';
347 ssd1306_command(SSD1306_COLUMNADDR);
348 ssd1306_command(6 * (x_pos - 1));
349 ssd1306_command(6 * (x_pos - 1) + 4); // Column end address (127 = reset)
350
351 ssd1306_command(SSD1306_PAGEADDR);
352 ssd1306_command(y_pos - 1); // Page start address (0 = reset)
353 ssd1306_command(y_pos - 1); // Page end address
354
355 I2C_Start(SSD1306_STREAM);
356 I2C_Write(SSD1306_STREAM, _i2caddr);
357 I2C_Write(SSD1306_STREAM, 0x40);
358
359 for(uint8_t i = 0; i < 5; i++ ) {
360 if(c < 'S')
361 font_c = font[(c - 32) * 5 + i];
362 else
363 font_c = font2[(c - 'S') * 5 + i];
364
365 I2C_Write(SSD1306_STREAM, font_c);
366 }
367 I2C_Stop(SSD1306_STREAM);
368
369 x_pos = x_pos % 21 + 1;
370 if (wrap && (x_pos == 1))
371 y_pos = y_pos % 8 + 1;
372
373 }
374
375 void SSD1306_PutCustomC(char *c) {
376 uint8_t line;
377 ssd1306_command(SSD1306_COLUMNADDR);
378 ssd1306_command(6 * (x_pos - 1));
379 ssd1306_command(6 * (x_pos - 1) + 4); // Column end address (127 = reset)
380
381 ssd1306_command(SSD1306_PAGEADDR);
382 ssd1306_command(y_pos - 1); // Page start address (0 = reset)
383 ssd1306_command(y_pos - 1); // Page end address
384
385 I2C_Start(SSD1306_STREAM);
386 I2C_Write(SSD1306_STREAM, _i2caddr);
387 I2C_Write(SSD1306_STREAM, 0x40);
388
389 for(uint8_t i = 0; i < 5; i++ ) {
390 line = c[i];
391 I2C_Write(SSD1306_STREAM, line);
392 }
393 I2C_Stop(SSD1306_STREAM);
394
395 x_pos = x_pos % 21 + 1;
396 if (wrap && (x_pos == 1))
397 y_pos = y_pos % 8 + 1;
398
399 }
400
401 void SSD1306_ClearDisplay() {
402
403 ssd1306_command(SSD1306_COLUMNADDR);
404 ssd1306_command(0); // Column start address
405 #if defined SSD1306_128_64 || defined SSD1306_128_32
406 ssd1306_command(127); // Column end address
407 #else
408 ssd1306_command(95); // Column end address
409 #endif
410
411 ssd1306_command(SSD1306_PAGEADDR);
412 ssd1306_command(0); // Page start address (0 = reset)
413 #if defined SSD1306_128_64
414 ssd1306_command(7); // Page end address
415 #elif defined SSD1306_128_32
416 ssd1306_command(3); // Page end address
417 #elif defined SSD1306_96_16
418 ssd1306_command(1); // Page end address
419 #endif
420
421 I2C_Start(SSD1306_STREAM);
422 I2C_Write(SSD1306_STREAM, _i2caddr);
423 I2C_Write(SSD1306_STREAM, 0x40);
424
425 for(uint16_t i = 0; i < SSD1306_LCDHEIGHT * SSD1306_LCDWIDTH / 8; i++ )
426 I2C_Write(SSD1306_STREAM, 0);
427
428 I2C_Stop(SSD1306_STREAM);
429
430 }
431
432 void SSD1306_FillScreen() {
433
434 ssd1306_command(SSD1306_COLUMNADDR);
435 ssd1306_command(0); // Column start address
436 #if defined SSD1306_128_64 || defined SSD1306_128_32
437 ssd1306_command(127); // Column end address
438 #else
439 ssd1306_command(95); // Column end address
440 #endif
441
442 ssd1306_command(SSD1306_PAGEADDR);
443 ssd1306_command(0); // Page start address (0 = reset)
444 #if defined SSD1306_128_64
445 ssd1306_command(7); // Page end address
446 #elif defined SSD1306_128_32
447 ssd1306_command(3); // Page end address
448 #elif defined SSD1306_96_16
449 ssd1306_command(1); // Page end address
450 #endif
451
452 I2C_Start(SSD1306_STREAM);
453 I2C_Write(SSD1306_STREAM, _i2caddr);
454 I2C_Write(SSD1306_STREAM, 0x40);
455
456 for(uint16_t i = 0; i < SSD1306_LCDHEIGHT * SSD1306_LCDWIDTH / 8; i++ )
457 I2C_Write(SSD1306_STREAM, 0xFF);
458
459 I2C_Stop(SSD1306_STREAM);
460
461 }
462

Você também pode gostar