Arduino-LCD
LCD library for Arduino
PCD8544.h
Go to the documentation of this file.
1 
19 #ifndef PCD8544_H
20 #define PCD8544_H
21 
22 #include "LCD.h"
23 #include "GPIO.h"
24 #include "SRPO.h"
25 
26 #ifndef CHARBITS
27 #define CHARBITS 8
28 #endif
29 
58 template<BOARD::pin_t SCE_PIN,
59  BOARD::pin_t DC_PIN,
60  BOARD::pin_t SDIN_PIN,
61  BOARD::pin_t SCLK_PIN>
62 class PCD8544 : public LCD::Device {
63 public:
65  static const uint8_t FONT_WIDTH = 6;
66  static const uint8_t FONT_HEIGHT = 8;
67  static const uint8_t WIDTH = 84 / FONT_WIDTH;
68  static const uint8_t HEIGHT = 48 / FONT_HEIGHT;
69 
76  PCD8544(const uint8_t* font = NULL) :
77  LCD::Device()
78  {
79  static const uint8_t default_font[] PROGMEM = {
80  0x00, 0x00, 0x00, 0x00, 0x00,
81  0x00, 0x00, 0x5F, 0x00, 0x00,
82  0x00, 0x07, 0x00, 0x07, 0x00,
83  0x14, 0x7F, 0x14, 0x7F, 0x14,
84  0x24, 0x2A, 0x7F, 0x2A, 0x12,
85  0x23, 0x13, 0x08, 0x64, 0x62,
86  0x36, 0x49, 0x56, 0x20, 0x50,
87  0x00, 0x08, 0x07, 0x03, 0x00,
88  0x00, 0x1C, 0x22, 0x41, 0x00,
89  0x00, 0x41, 0x22, 0x1C, 0x00,
90  0x2A, 0x1C, 0x7F, 0x1C, 0x2A,
91  0x08, 0x08, 0x3E, 0x08, 0x08,
92  0x00, 0x80, 0x70, 0x30, 0x00,
93  0x08, 0x08, 0x08, 0x08, 0x08,
94  0x00, 0x00, 0x60, 0x60, 0x00,
95  0x20, 0x10, 0x08, 0x04, 0x02,
96  0x3E, 0x51, 0x49, 0x45, 0x3E,
97  0x00, 0x42, 0x7F, 0x40, 0x00,
98  0x72, 0x49, 0x49, 0x49, 0x46,
99  0x21, 0x41, 0x49, 0x4D, 0x33,
100  0x18, 0x14, 0x12, 0x7F, 0x10,
101  0x27, 0x45, 0x45, 0x45, 0x39,
102  0x3C, 0x4A, 0x49, 0x49, 0x31,
103  0x41, 0x21, 0x11, 0x09, 0x07,
104  0x36, 0x49, 0x49, 0x49, 0x36,
105  0x46, 0x49, 0x49, 0x29, 0x1E,
106  0x00, 0x00, 0x14, 0x00, 0x00,
107  0x00, 0x40, 0x34, 0x00, 0x00,
108  0x00, 0x08, 0x14, 0x22, 0x41,
109  0x14, 0x14, 0x14, 0x14, 0x14,
110  0x00, 0x41, 0x22, 0x14, 0x08,
111  0x02, 0x01, 0x59, 0x09, 0x06,
112  0x3E, 0x41, 0x5D, 0x59, 0x4E,
113  0x7C, 0x12, 0x11, 0x12, 0x7C,
114  0x7F, 0x49, 0x49, 0x49, 0x36,
115  0x3E, 0x41, 0x41, 0x41, 0x22,
116  0x7F, 0x41, 0x41, 0x41, 0x3E,
117  0x7F, 0x49, 0x49, 0x49, 0x41,
118  0x7F, 0x09, 0x09, 0x09, 0x01,
119  0x3E, 0x41, 0x41, 0x51, 0x73,
120  0x7F, 0x08, 0x08, 0x08, 0x7F,
121  0x00, 0x41, 0x7F, 0x41, 0x00,
122  0x20, 0x40, 0x41, 0x3F, 0x01,
123  0x7F, 0x08, 0x14, 0x22, 0x41,
124  0x7F, 0x40, 0x40, 0x40, 0x40,
125  0x7F, 0x02, 0x1C, 0x02, 0x7F,
126  0x7F, 0x04, 0x08, 0x10, 0x7F,
127  0x3E, 0x41, 0x41, 0x41, 0x3E,
128  0x7F, 0x09, 0x09, 0x09, 0x06,
129  0x3E, 0x41, 0x51, 0x21, 0x5E,
130  0x7F, 0x09, 0x19, 0x29, 0x46,
131  0x26, 0x49, 0x49, 0x49, 0x32,
132  0x03, 0x01, 0x7F, 0x01, 0x03,
133  0x3F, 0x40, 0x40, 0x40, 0x3F,
134  0x1F, 0x20, 0x40, 0x20, 0x1F,
135  0x3F, 0x40, 0x38, 0x40, 0x3F,
136  0x63, 0x14, 0x08, 0x14, 0x63,
137  0x03, 0x04, 0x78, 0x04, 0x03,
138  0x61, 0x59, 0x49, 0x4D, 0x43,
139  0x00, 0x7F, 0x41, 0x41, 0x41,
140  0x02, 0x04, 0x08, 0x10, 0x20,
141  0x00, 0x41, 0x41, 0x41, 0x7F,
142  0x04, 0x02, 0x01, 0x02, 0x04,
143  0x40, 0x40, 0x40, 0x40, 0x40,
144  0x00, 0x03, 0x07, 0x08, 0x00,
145  0x20, 0x54, 0x54, 0x78, 0x40,
146  0x7F, 0x28, 0x44, 0x44, 0x38,
147  0x38, 0x44, 0x44, 0x44, 0x28,
148  0x38, 0x44, 0x44, 0x28, 0x7F,
149  0x38, 0x54, 0x54, 0x54, 0x18,
150  0x00, 0x08, 0x7E, 0x09, 0x02,
151  0x18, 0xA4, 0xA4, 0x9C, 0x78,
152  0x7F, 0x08, 0x04, 0x04, 0x78,
153  0x00, 0x44, 0x7D, 0x40, 0x00,
154  0x20, 0x40, 0x40, 0x3D, 0x00,
155  0x7F, 0x10, 0x28, 0x44, 0x00,
156  0x00, 0x41, 0x7F, 0x40, 0x00,
157  0x7C, 0x04, 0x78, 0x04, 0x78,
158  0x7C, 0x08, 0x04, 0x04, 0x78,
159  0x38, 0x44, 0x44, 0x44, 0x38,
160  0xFC, 0x18, 0x24, 0x24, 0x18,
161  0x18, 0x24, 0x24, 0x18, 0xFC,
162  0x7C, 0x08, 0x04, 0x04, 0x08,
163  0x48, 0x54, 0x54, 0x54, 0x24,
164  0x04, 0x04, 0x3F, 0x44, 0x24,
165  0x3C, 0x40, 0x40, 0x20, 0x7C,
166  0x1C, 0x20, 0x40, 0x20, 0x1C,
167  0x3C, 0x40, 0x30, 0x40, 0x3C,
168  0x44, 0x28, 0x10, 0x28, 0x44,
169  0x4C, 0x90, 0x90, 0x90, 0x7C,
170  0x44, 0x64, 0x54, 0x4C, 0x44,
171  0x00, 0x08, 0x36, 0x41, 0x00,
172  0x00, 0x00, 0x77, 0x00, 0x00,
173  0x00, 0x41, 0x36, 0x08, 0x00,
174  0x02, 0x01, 0x02, 0x04, 0x02,
175  0x3C, 0x26, 0x23, 0x26, 0x3C,
176  };
177  m_font = font != NULL ? font : default_font;
178  m_sce.output();
179  m_dc.output();
180  }
181 
187  virtual bool begin()
188  {
189  // Display setup script
190  static const uint8_t script[] PROGMEM = {
192  SET_VOP | 0x3f,
193  SET_TEMP_COEFF | 0x00,
194  SET_BIAS_SYS | 0x02,
197  };
198 
199  // Initiate display setting
200  m_sce.high();
201  write_command_P(script, sizeof(script));
203  display_clear();
204  backlight_on();
205  return (true);
206  }
207 
213  virtual bool end()
214  {
215  display_clear();
217  backlight_off();
218  return (true);
219  }
220 
226  virtual void display_contrast(uint8_t level)
227  {
229  write_command(SET_VOP | (level & VOP_MASK));
231  }
232 
237  virtual void display_on()
238  {
240  }
241 
246  virtual void display_off()
247  {
249  }
250 
255  virtual void display_normal()
256  {
258  }
259 
264  virtual void display_inverse()
265  {
267  }
268 
273  virtual void display_clear()
274  {
275  cursor_home();
276  write_data(BACKGROUND, WIDTH * FONT_WIDTH * HEIGHT);
277  cursor_home();
278  }
279 
286  virtual void cursor_set(uint8_t x, uint8_t y)
287  {
288  if (x >= WIDTH) x = 0;
289  if (y >= HEIGHT) y = 0;
290  write_command(SET_X_ADDR | ((x * FONT_WIDTH) & X_ADDR_MASK));
292  m_x = x;
293  m_y = y;
294  }
295 
304  virtual size_t write(uint8_t c)
305  {
306  // Check for special characters
307  if (c < ' ') {
308  switch (c) {
309  case '\a': // Check for special character: alert
310  m_mode = ~m_mode;
311  return (1);
312  case '\b': // Check for special character: back-space
313  cursor_set(m_x - 1, m_y);
314  write_data(BACKGROUND, FONT_WIDTH);
315  return (1);
316  case '\f': // Check for special character: form-feed
317  display_clear();
318  return (1);
319  case '\n': // Check for line-feed: clear new line
320  cursor_set(0, m_y + 1);
321  write_data(BACKGROUND, FONT_WIDTH * WIDTH);
322  case '\r': // Carriage-return: move to start of line
323  cursor_set(0, m_y);
324  return (1);
325  case '\t': // Check for horizontal tab
326  {
327  uint8_t x = m_x + m_tab - (m_x % m_tab);
328  uint8_t y = m_y + (x >= WIDTH);
329  cursor_set(x, y);
330  }
331  return (1);
332  default:
333  return (0);
334  }
335  }
336 
337  // Check that the character is not clipped
338  if (m_x == WIDTH) write('\n');
339  m_x += 1;
340 
341  // Access font for character width and bitmap
342  uint8_t width = FONT_WIDTH - 1;
343  const uint8_t* fp = m_font + ((c - ' ') * width);
344 
345  // Write character to the display memory and an extra byte
346  do write_data(m_mode ^ pgm_read_byte(fp++)); while (--width);
348  return (1);
349  }
350 
351 protected:
355  enum {
356  NOP = 0x00,
357  SET_FUNC = 0x20,
358  BASIC_INST = 0x00,
359  EXTENDED_INST = 0x01,
361  VERTICAL_ADDR = 0x02,
362  POWER_UP_MODE = 0x00,
364  DISPLAY_CNTL = 0x08,
365  DISPLAY_OFF = 0x00,
366  DISPLAY_ON = 0x01,
367  NORMAL_MODE = 0x04,
368  INVERSE_MODE = 0x05,
369  SET_Y_ADDR = 0x40,
370  Y_ADDR_MASK = 0x07,
371  SET_X_ADDR = 0x80,
372  X_ADDR_MASK = 0x7f,
373  SET_TEMP_COEFF = 0x04,
374  SET_BIAS_SYS = 0x10,
375  SET_VOP = 0x80,
376  VOP_MASK = 0x7f,
377  SCRIPT_END = 0xff
378  } __attribute__((packed));
379 
381  static const uint8_t BACKGROUND = 0x00;
382 
384  GPIO<SCE_PIN> m_sce;
385 
387  GPIO<DC_PIN> m_dc;
388 
390  SRPO<MSBFIRST, SDIN_PIN, SCLK_PIN> m_srpo;
391 
393  const uint8_t* m_font;
394 
395  void write_data(uint8_t value)
396  {
397  m_sce.low();
398  m_srpo.write(value);
399  m_sce.high();
400  }
401 
402  void write_data(uint8_t value, size_t count)
403  {
404  if (count == 0) return;
405  m_sce.low();
406  do m_srpo.write(value); while (--count);
407  m_sce.high();
408  }
409 
410  void write_command(uint8_t value)
411  {
412  m_dc.low();
413  m_sce.low();
414  m_srpo.write(value);
415  m_sce.high();
416  m_dc.high();
417  }
418 
419  void write_command_P(const uint8_t* buf, size_t count)
420  {
421  if (count == 0) return;
422  m_dc.low();
423  m_sce.low();
424  do m_srpo.write(pgm_read_byte(buf++)); while (--count);
425  m_sce.high();
426  m_dc.high();
427  }
428 };
429 
430 #endif
Set function.
Definition: PCD8544.h:357
static const uint8_t HEIGHT
Definition: PCD8544.h:68
uint8_t m_y
Cursor position y.
Definition: LCD.h:205
Display control.
Definition: PCD8544.h:364
const uint8_t * m_font
Definition: PCD8544.h:393
virtual size_t write(uint8_t c)
Definition: PCD8544.h:304
uint8_t m_x
Cursor position x.
Definition: LCD.h:204
Mask Y-address.
Definition: PCD8544.h:370
Normal display mode.
Definition: PCD8544.h:367
void write_command_P(const uint8_t *buf, size_t count)
Definition: PCD8544.h:419
void text_normal_mode()
Definition: LCD.h:188
Init script end.
Definition: PCD8544.h:377
virtual void display_clear()
Definition: PCD8544.h:273
Mask Vop.
Definition: PCD8544.h:376
Inverse display mode.
Definition: PCD8544.h:368
virtual bool begin()
Definition: PCD8544.h:187
static const uint8_t FONT_WIDTH
Definition: PCD8544.h:65
Extended instruction set control.
Definition: PCD8544.h:359
Definition: Debug.h:28
Turn display off.
Definition: PCD8544.h:365
static const uint8_t BACKGROUND
Definition: PCD8544.h:381
virtual void backlight_off()
Definition: LCD.h:70
Basic instruction set.
Definition: PCD8544.h:358
Sets X-address of RAM (0..83).
Definition: PCD8544.h:371
uint8_t m_tab
Tab step.
Definition: LCD.h:206
void write_command(uint8_t value)
Definition: PCD8544.h:410
void write_data(uint8_t value)
Definition: PCD8544.h:395
virtual void cursor_set(uint8_t x, uint8_t y)
Definition: PCD8544.h:286
GPIO< DC_PIN > m_dc
Definition: PCD8544.h:387
Write Vop to register (0..127).
Definition: PCD8544.h:375
Set Bias System (0..7).
Definition: PCD8544.h:374
virtual bool end()
Definition: PCD8544.h:213
GPIO< SCE_PIN > m_sce
Definition: PCD8544.h:384
PCD8544(const uint8_t *font=NULL)
Definition: PCD8544.h:76
virtual void backlight_on()
Definition: LCD.h:64
static const uint8_t WIDTH
Definition: PCD8544.h:67
Turn display on.
Definition: PCD8544.h:366
virtual void display_contrast(uint8_t level)
Definition: PCD8544.h:226
Vertical addressing.
Definition: PCD8544.h:361
Set temperature coefficient (0..3).
Definition: PCD8544.h:373
Sets Y-address of RAM (0..5).
Definition: PCD8544.h:369
static const uint8_t FONT_HEIGHT
Definition: PCD8544.h:66
void write_data(uint8_t value, size_t count)
Definition: PCD8544.h:402
uint8_t m_mode
Text mode.
Definition: LCD.h:207
virtual void display_off()
Definition: PCD8544.h:246
No operation.
Definition: PCD8544.h:356
SRPO< MSBFIRST, SDIN_PIN, SCLK_PIN > m_srpo
Definition: PCD8544.h:390
virtual void display_normal()
Definition: PCD8544.h:255
virtual void cursor_home()
Definition: LCD.h:145
Power up mode.
Definition: PCD8544.h:362
Device()
Definition: LCD.h:31
Mask X-addres.
Definition: PCD8544.h:372
virtual void display_on()
Definition: PCD8544.h:237
Horizontal addressing.
Definition: PCD8544.h:360
virtual void display_inverse()
Definition: PCD8544.h:264
Power down mode.
Definition: PCD8544.h:363