-
Notifications
You must be signed in to change notification settings - Fork 0
/
nokia5110.c
427 lines (394 loc) · 11.8 KB
/
nokia5110.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
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
/*
* nokia5110.c
*
* Created: 2015-06-09 21:07:02
* version: 2017-04-17
* Author: uwezi
*/
#include <stdlib.h>
#include <util/atomic.h>
#include <avr/pgmspace.h>
#include <string.h>
#include "nokia5110.h"
#ifndef F_CPU
#define F_CPU 20000000UL // save to assume the fastest clock
#endif
#include <util/delay.h>
/*
** constants/macros (thanks Peter Fleury)
*/
#if defined(__AVR_ATmega64__) || defined(__AVR_ATmega128__)
/* on ATmega64/128 PINF is on port 0x00 and not 0x60 */
#define PIN(x) ( &PORTF==&(x) ? _SFR_IO8(0x00) : (*(&x - 2)) )
#else
#define PIN(x) (*(&x - 2)) /* address of input register of port x */
#endif
//
// macros for the software SPI
//
#define SetRST RST_PORT |= (1 << RST)
#define ClearRST RST_PORT &=~(1 << RST)
#define SetSCE SCE_PORT |= (1 << SCE)
#define ClearSCE SCE_PORT &=~(1 << SCE)
#define SetDC DC_PORT |= (1 << DC)
#define ClearDC DC_PORT &=~(1 << DC)
#define SetSD SD_PORT |= (1 << SD)
#define ClearSD SD_PORT &=~(1 << SD)
#define SetSCL SCL_PORT |= (1 << SCL)
#define ClearSCL SCL_PORT &=~(1 << SCL)
// 6x8 font
// LSB is top
// MSB is bottom
//
static const uint8_t smallFont[][6] PROGMEM =
#include "font_6x8_iso8859_1.h"
//
// define a local copy of the display memory
//
uint8_t framebuffer[NOKIASIZEX*NOKIASIZEY/8];
/*--------------------------------------------------------------------------------------------------
Name : NOKIA_writeCommand
Description : Sends command to display controller.
Argument(s) : command -> command to be sent
Return value : None.
--------------------------------------------------------------------------------------------------*/
void NOKIA_writeCommand (uint8_t command )
{
uint8_t i;
// the bit-banging routines may not be interrupted!
ATOMIC_BLOCK(ATOMIC_RESTORESTATE)
{
ClearSCE; //enable LCD
ClearDC; // set LCD into command mode
ClearSCL;
for (i=0; i<8; i++)
{
if (command & 0b10000000)
{
SetSD;
}
else
{
ClearSD;
}
SetSCL; // minimum 100 ns
ClearSCL; // minimum 100 ns
command <<= 1;
}
SetSCE; // disable LCD
}
}
/*--------------------------------------------------------------------------------------------------
Name : NOKIA_writeData
Description : Sends data to display controller.
Argument(s) : data -> data to be sent
Return value : None.
--------------------------------------------------------------------------------------------------*/
void NOKIA_writeData (uint8_t data )
{
uint8_t i;
// the bit-banging routines may not be interrupted!
ATOMIC_BLOCK(ATOMIC_RESTORESTATE)
{
ClearSCE; // enable LCD
SetDC; // set LCD in data mode
ClearSCL;
#ifdef NOKIAROTATE
for (i=0; i<8; i++)
{
if (data & 0b00000001)
{
SetSD;
}
else
{
ClearSD;
}
SetSCL; // minimum 100 ns
ClearSCL; // minimum 100 ns
data >>= 1;
}
#else
for (i=0; i<8; i++)
{
if (data & 0b10000000)
{
SetSD;
}
else
{
ClearSD;
}
SetSCL; // minimum 100 ns
ClearSCL; // minimum 100 ns
data <<= 1;
}
#endif
SetSCE; // disable LCD
}
}
/*--------------------------------------------------------------------------------------------------
Name : NOKIA_gotoXY
Description : Sets cursor location to xy location corresponding to basic font.
Argument(s) : x - range: 0 to 84
y -> range: 0 to 5
Return value : None.
--------------------------------------------------------------------------------------------------*/
void NOKIA_gotoXY ( uint8_t x, uint8_t y )
{
NOKIA_writeCommand (0x80 | x); //column
NOKIA_writeCommand (0x40 | y); //row
}
/*--------------------------------------------------------------------------------------------------
Name : NOKIA_clearbuffer
Description : Clears the framebuffer but does not transfer to LCD.
Argument(s) : None.
Return value : None.
--------------------------------------------------------------------------------------------------*/
void NOKIA_clearbuffer(void)
{
memset(framebuffer, 0x00, NOKIASIZEX*NOKIASIZEY/8);
}
/*--------------------------------------------------------------------------------------------------
Name : NOKIA_update
Description : transfers the local framebuffer to the display
Argument(s) : None.
Return value : None.
--------------------------------------------------------------------------------------------------*/
void NOKIA_update (void)
{
uint16_t i;
NOKIA_gotoXY(0,0); // start with (0,0) position
for(i=0; i<(NOKIASIZEX*NOKIASIZEY/8); i++)
{
#ifdef NOKIAROTATE
NOKIA_writeData(framebuffer[NOKIASIZEX*NOKIASIZEY/8-i-1]);
#else
NOKIA_writeData(framebuffer[i]);
#endif
}
NOKIA_gotoXY(0,0); // bring the XY position back to (0,0)
}
/*--------------------------------------------------------------------------------------------------
Name : NOKIA_clear
Description : Clears the display
Argument(s) : None.
Return value : None.
--------------------------------------------------------------------------------------------------*/
void NOKIA_clear ( void )
{
NOKIA_clearbuffer();
NOKIA_update();
}
/*--------------------------------------------------------------------------------------------------
Name : NOKIA_init
Description : LCD controller initialization.
Argument(s) : contrast value VOP
Return value : None.
--------------------------------------------------------------------------------------------------*/
void NOKIA_init (uint8_t vop)
{
DDR(SCE_PORT) |= (1 << SCE);
DDR(RST_PORT) |= (1 << RST);
DDR(SCL_PORT) |= (1 << SCL);
DDR(DC_PORT) |= (1 << DC);
DDR(SD_PORT) |= (1 << SD);
_delay_ms(100);
// the bit-banging routines may not be interrupted!
ATOMIC_BLOCK(ATOMIC_RESTORESTATE)
{
ClearSCE; // Enable LCD
ClearRST; // reset LCD
_delay_ms(100);
SetRST;
SetSCE; //disable LCD
}
NOKIA_writeCommand( 0x21 ); // LCD Extended Commands.
NOKIA_writeCommand( 0x80 | vop ); // Set LCD Vop (Contrast).
NOKIA_writeCommand( 0x04 ); // Set Temp coefficent.
NOKIA_writeCommand( 0x13 ); // LCD bias mode 1:48.
NOKIA_writeCommand( 0x20 ); // LCD Standard Commands, Horizontal addressing mode.
NOKIA_writeCommand( 0x0c ); // LCD in normal mode.
NOKIA_clear();
}
/*--------------------------------------------------------------------------------------------------
Name : NOKIA_setVop
Description : Sets the contrast voltage
Argument(s) : contrast value VOP 0 to 127
Return value : None.
--------------------------------------------------------------------------------------------------*/
void NOKIA_setVop(uint8_t vop)
{
NOKIA_writeCommand( 0x21 ); // LCD Extended Commands.
NOKIA_writeCommand( 0x80 | vop ); // Set LCD Vop (Contrast).
NOKIA_writeCommand( 0x20 ); // LCD Standard Commands, Horizontal addressing mode.
NOKIA_writeCommand( 0x0c ); // LCD in normal mode.
}
/*--------------------------------------------------------------------------------------------------
Name : NOKIA_setpixel
Description : Sets the pixel at xy location
Argument(s) : x - range: 0 to 83
y - range: 0 to 47
Return value : None.
--------------------------------------------------------------------------------------------------*/
void NOKIA_setpixel(uint8_t x, uint8_t y)
{
if ((x < NOKIASIZEX) && (y < NOKIASIZEY))
{
framebuffer[(uint16_t) x+NOKIASIZEX*(y/8)] |= (1 << (y % 8));
}
}
/*--------------------------------------------------------------------------------------------------
Name : NOKIA_clearpixel
Description : Clears the pixel at xy location
Argument(s) : x - range: 0 to 83
y - range: 0 to 47
Return value : None.
--------------------------------------------------------------------------------------------------*/
void NOKIA_clearpixel(uint8_t x, uint8_t y)
{
if ((x < NOKIASIZEX) && (y < NOKIASIZEY))
{
framebuffer[(uint16_t) x+NOKIASIZEX*(y/8)] &= ~(1 << (y % 8));
}
}
/*--------------------------------------------------------------------------------------------------
Name : NOKIA_putchar
Description : puts a single character onto LCD
Argument(s) : x - range: 0 to 83
y - range: 0 to 47
ch - character
attr - attribute 0-normal, 1-inverse, 2-underline
Return value : None.
--------------------------------------------------------------------------------------------------*/
void NOKIA_putchar(uint8_t x0, uint8_t y0, char ch, uint8_t attr)
{
uint8_t yd, ym, i, fontbyte;
uint16_t m;
yd = y0/8;
ym = y0%8;
for (i=0; i<6; i++)
{
fontbyte = pgm_read_byte(&smallFont[(uint8_t)ch][i]);
switch (attr)
{
case 0:
break;
case 1:
fontbyte ^= 0xff;
break;
case 2:
fontbyte |= 0b10000000;
break;
}
if ((x0+i)<NOKIASIZEX)
{
m = (uint16_t) x0+i+NOKIASIZEX*(yd);
framebuffer[m] &= ~(0xff << ym);
framebuffer[m] |= (fontbyte << ym);
if ((y0<(NOKIASIZEY-8)) && (ym != 0))
{
m = (uint16_t) x0+i+NOKIASIZEX*(yd+1);
framebuffer[m] &= ~(0xff >> (8-ym));
framebuffer[m] |= (fontbyte >> (8-ym));
}
}
}
}
/*--------------------------------------------------------------------------------------------------
Name : NOKIA_print
Description : prints a string
Argument(s) : x - range: 0 to 83
y - range: 0 to 47
*ch - pointer t string
attr - attribute 0-normal, 1-inverse, 2-underline
Return value : None.
--------------------------------------------------------------------------------------------------*/
void NOKIA_print(uint8_t x, uint8_t y, char *ch,uint8_t attr)
{
while (*ch)
{
NOKIA_putchar(x, y, *ch, attr);
ch++;
x += 6;
}
}
void NOKIA_print_p(uint8_t x, uint8_t y, const char *ch,uint8_t attr)
{
char c;
while ((c = pgm_read_byte(ch)))
{
NOKIA_putchar(x, y, c, attr);
ch++;
x += 6;
}
}
/*--------------------------------------------------------------------------------------------------
Name : NOKIA_scroll
Description : softscrolls the framebuffer
Argument(s) : dy - range: 0 to +/-47
Return value : None.
--------------------------------------------------------------------------------------------------*/
void NOKIA_scroll(int8_t dy)
{
int8_t y1;
uint8_t x, y, dy1, dy8, b1, b2;
if (dy>0)
{
dy8 = dy/8;
dy1 = dy%8;
for (x=0; x<NOKIASIZEX; x++)
{
for (y=0; y<(NOKIASIZEY/8); y++)
{
y1=y+dy8;
if (y1<(NOKIASIZEY/8))
{
b1 = framebuffer[x + NOKIASIZEX*y1];
}
else
{
b1=0;
}
if ((y1+1)<(NOKIASIZEY/8))
{
b2 = framebuffer[x + NOKIASIZEX*(y1+1)];
}
else
{
b2=0;
}
framebuffer[x + NOKIASIZEX*(y)] = (b1 >> dy1) | (b2 << (8-dy1));
}
}
}
else
{
dy8 = abs(dy)/8;
dy1 = abs(dy)%8;
for (x=0; x<NOKIASIZEX; x++)
{
for (y=0; y<(NOKIASIZEY/8); y++)
{
y1=(NOKIASIZEY/8)-y-dy8-1;
if (y1>=0)
{
b1 = framebuffer[x + NOKIASIZEX*y1];
}
else
{
b1=0;
}
if ((y1-1)>=0)
{
b2 = framebuffer[x + NOKIASIZEX*(y1-1)];
}
else
{
b2=0;
}
framebuffer[x + NOKIASIZEX*((NOKIASIZEY/8)-y-1)] = (b1 << dy1) | (b2 >> (8-dy1));
}
}
}
}