-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathssd1306xled.c
executable file
·374 lines (307 loc) · 8.45 KB
/
ssd1306xled.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
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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
/*
* SSD1306xLED - Drivers for SSD1306 controlled dot matrix OLED/PLED 128x64 displays
*
* @file: ssd1306xled.c
* @created: 2014-08-12
* @author: Neven Boyanov
*
* Source code available at: https://bitbucket.org/tinusaur/ssd1306xled
*
*/
// ----------------------------------------------------------------------------
#include <stdlib.h>
#include <avr/io.h>
#include <util/delay.h>
#include <avr/pgmspace.h>
#include "ssd1306xled.h"
#include "font6x8.h"
#include "font8X16.h"
//#include "font16x16cn.h"
// ----------------------------------------------------------------------------
#define DIGITAL_WRITE_HIGH(PORT) (PORTB |= _BV(PORT))
#define DIGITAL_WRITE_LOW(PORT) (PORTB &= ~(_BV(PORT)))
// ----------------------------------------------------------------------------
// Some code based on "IIC_wtihout_ACK" by http://www.14blog.com/archives/1358
const uint8_t ssd1306_init_sequence [] PROGMEM = { // Initialization Sequence
0xAE, // Display OFF (sleep mode)
0x20, 0b00, // Set Memory Addressing Mode
// 00=Horizontal Addressing Mode; 01=Vertical Addressing Mode;
// 10=Page Addressing Mode (RESET); 11=Invalid
0xB0, // Set Page Start Address for Page Addressing Mode, 0-7
0xC8, // Set COM Output Scan Direction
0x00, // ---set low column address
0x10, // ---set high column address
0x40, // --set start line address
0x81, 0x7F, // Set contrast control register
0xA1, // Set Segment Re-map. A0=address mapped; A1=address 127 mapped.
0xA6, // Set display mode. A6=Normal; A7=Inverse
0xA8, 0x3F, // Set multiplex ratio(1 to 64)
0xA4, // Output RAM to Display
// 0xA4=Output follows RAM content; 0xA5,Output ignores RAM content
0xD3, 0x00, // Set display offset. 00 = no offset
0xD5, // --set display clock divide ratio/oscillator frequency
0xF0, // --set divide ratio
0xD9, 0x22, // Set pre-charge period
0xDA, 0x12, // Set com pins hardware configuration
0xDB, // --set vcomh
0x20, // 0x20,0.77xVcc
0x8D, 0x14, // Set DC-DC enable
0xAF // Display ON in normal mode
};
void ssd1306_init(void)
{
DDRB |= _BV(SSD1306_SDA) | _BV(SSD1306_SCL); // Set port as output
PORTB |= _BV(SSD1306_SDA) | _BV(SSD1306_SCL); //set to high state
for (uint8_t i = 0; i < sizeof (ssd1306_init_sequence); ++i)
{
ssd1306_send_command(pgm_read_byte(&ssd1306_init_sequence[i]));
}
}
void ssd1306_xfer_start(void)
{
DIGITAL_WRITE_HIGH(SSD1306_SCL); // Set to HIGH
DIGITAL_WRITE_HIGH(SSD1306_SDA); // Set to HIGH
DIGITAL_WRITE_LOW(SSD1306_SDA); // Set to LOW
DIGITAL_WRITE_LOW(SSD1306_SCL); // Set to LOW
}
void ssd1306_xfer_stop(void)
{
DIGITAL_WRITE_LOW(SSD1306_SCL); // Set to LOW
DIGITAL_WRITE_LOW(SSD1306_SDA); // Set to LOW
DIGITAL_WRITE_HIGH(SSD1306_SCL); // Set to HIGH
DIGITAL_WRITE_HIGH(SSD1306_SDA); // Set to HIGH
}
void ssd1306_send_byte(uint8_t byte)
{
for(uint8_t i=0; i<8; ++i)
{
((byte << i) & 0x80) ? DIGITAL_WRITE_HIGH(SSD1306_SDA) : DIGITAL_WRITE_LOW(SSD1306_SDA);
DIGITAL_WRITE_HIGH(SSD1306_SCL);
DIGITAL_WRITE_LOW(SSD1306_SCL);
}
DIGITAL_WRITE_HIGH(SSD1306_SDA);
DIGITAL_WRITE_HIGH(SSD1306_SCL);
DIGITAL_WRITE_LOW(SSD1306_SCL);
}
void ssd1306_send_command(uint8_t command)
{
ssd1306_xfer_start();
ssd1306_send_byte(SSD1306_SA); // Slave address, SA0=0
ssd1306_send_byte(0x00); // write command
ssd1306_send_byte(command);
ssd1306_xfer_stop();
}
void ssd1306_send_data_start(void)
{
ssd1306_xfer_start();
ssd1306_send_byte(SSD1306_SA);
ssd1306_send_byte(0x40); //write data
}
void ssd1306_send_data_stop(void)
{
ssd1306_xfer_stop();
}
void ssd1306_setpos(uint8_t x, uint8_t y)
{
ssd1306_xfer_start();
ssd1306_send_byte(SSD1306_SA); //Slave address,SA0=0
ssd1306_send_byte(0x00); //write command
ssd1306_send_byte(0xb0 + y);
ssd1306_send_byte( ( (x & 0xf0) >> 4) | 0x10); // |0x10
ssd1306_send_byte( (x & 0x0f) | 0x01); // |0x01
ssd1306_xfer_stop();
}
void ssd1306_fillscreen(uint8_t fill_Data)
{
for(uint8_t m=0; m<8; ++m)
{
ssd1306_send_command(0xb0 | m); //page0-page7
ssd1306_send_command(0x00); //low column start address
ssd1306_send_command(0x10); //high column start address
ssd1306_send_data_start();
for(uint8_t n=0; n<128; ++n)
{
ssd1306_send_byte(fill_Data);
}
ssd1306_send_data_stop();
}
}
void ssd1306_bar(uint8_t x, uint8_t y, uint8_t v)
{
ssd1306_setpos(x, y);
ssd1306_send_data_start();
v >>= 1;
uint8_t i=0;
for(; i<v; ++i)
{
ssd1306_send_byte(7);
}
for(; i<128; ++i)
{
ssd1306_send_byte(0);
}
ssd1306_send_data_stop();
}
void ssd1306_dbar(uint8_t x, uint8_t y, uint8_t v)
{
ssd1306_setpos(x, y);
ssd1306_send_data_start();
v >>= 2;
uint8_t i=0;
for(; i<(64-v); ++i)
{
ssd1306_send_byte(0);
}
for(; i<(64+v); ++i)
{
ssd1306_send_byte(7);
}
for(; i<128; ++i)
{
ssd1306_send_byte(0);
}
ssd1306_send_data_stop();
}
void ssd1306_char_f6x8(uint8_t x, uint8_t y, const char* p)
{
for(; *p; ++p)
{
if(x > 126)
{
x=0;
++y;
}
ssd1306_setpos(x,y);
ssd1306_send_data_start();
uint8_t c = *p - 32;
for(uint8_t i=0; i<6; ++i)
{
ssd1306_send_byte( pgm_read_byte(&ssd1306xled_font6x8[c*6 + i]) );
}
ssd1306_send_data_stop();
x += 6;
}
}
void ssd1306_char_f8x16(uint8_t x, uint8_t y, const char* p)
{
for(uint8_t i=0; *p; ++p)
{
uint8_t c = *p - 32;
if (x>120)
{
x=0;
y++;
}
ssd1306_setpos(x,y);
ssd1306_send_data_start();
for(i=0; i<8; ++i)
{
ssd1306_send_byte(pgm_read_byte(&ssd1306xled_font8X16[c*16+i]));
}
ssd1306_send_data_stop();
ssd1306_setpos(x, y+1);
ssd1306_send_data_start();
for(i=0; i<8; ++i)
{
ssd1306_send_byte(pgm_read_byte(&ssd1306xled_font8X16[c*16+i+8]));
}
ssd1306_send_data_stop();
x+=8;
}
}
void ssd1306_char_f16x32(uint8_t x, uint8_t y,const char* p)
{
for(uint8_t i=0; *p; ++p)
{
uint8_t c = *p - 32;
if (x>120)
{
x=0;
y++;
}
for(uint8_t k=0; k<2; ++k)
{
ssd1306_setpos(x, y+k);
ssd1306_send_data_start();
for(i=0; i<8; ++i)
{
uint8_t b = pgm_read_byte( &ssd1306xled_font8X16[c*16+i] );
uint8_t h = 0;
for(uint8_t z=0; z<4; ++z)
{
if(b & (1 << (z+(k*4))))
{
h |= (3 << (z*2));
}
}
ssd1306_send_byte(h);
ssd1306_send_byte(h);
}
ssd1306_send_data_stop();
}
for(uint8_t k=0; k<2; ++k)
{
ssd1306_setpos(x, y+k+2);
ssd1306_send_data_start();
for(i=0; i<8; ++i)
{
uint8_t b = pgm_read_byte( &ssd1306xled_font8X16[c*16+i+8] );
uint8_t h = 0;
for(uint8_t z=0; z<4; ++z)
{
if(b & (1 << (z+(k*4))))
{
h |= (3 << (z*2));
}
}
ssd1306_send_byte(h);
ssd1306_send_byte(h);
}
ssd1306_send_data_stop();
}
x+=16;
}
}
/*
void ssd1306_char_f16x16(uint8_t x, uint8_t y, uint8_t N)
{
uint8_t wm=0;
unsigned int adder = 32 * N;
ssd1306_setpos(x , y);
ssd1306_send_data_start();
for(wm = 0; wm < 16; wm++)
{
//ssd1306_send_byte(pgm_read_byte(&ssd1306xled_font8X16cn[adder]));
adder += 1;
}
ssd1306_send_data_stop();
ssd1306_setpos(x,y + 1);
ssd1306_send_data_start();
for(wm = 0;wm < 16;wm++)
{
//ssd1306_send_byte(pgm_read_byte(&ssd1306xled_font8X16cn[adder]));
adder += 1;
}
ssd1306_send_data_stop();
}
*/
void ssd1306_draw_bmp(uint8_t x0, uint8_t y0, uint8_t x1, uint8_t y1, const uint8_t bitmap[])
{
unsigned int j = 0;
uint8_t x,y;
if(y1%8==0)
y=y1/8;
else
y=y1/8+1;
for(y=y0;y<y1;y++)
{
ssd1306_setpos(x0,y);
ssd1306_send_data_start();
for(x=x0;x<x1;x++)
{
ssd1306_send_byte(pgm_read_byte(&bitmap[j++]));
}
ssd1306_send_data_stop();
}
}
// ----------------------------------------------------------------------------