u8x8_d_pcd8544_84x48.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. /*
  2. u8x8_d_pcd8544_84x48.c (so called "Nokia 5110" displays)
  3. Universal 8bit Graphics Library (https://github.com/olikraus/u8g2/)
  4. Copyright (c) 2016, olikraus@gmail.com
  5. All rights reserved.
  6. Redistribution and use in source and binary forms, with or without modification,
  7. are permitted provided that the following conditions are met:
  8. * Redistributions of source code must retain the above copyright notice, this list
  9. of conditions and the following disclaimer.
  10. * Redistributions in binary form must reproduce the above copyright notice, this
  11. list of conditions and the following disclaimer in the documentation and/or other
  12. materials provided with the distribution.
  13. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  14. CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  15. INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  16. MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  17. DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
  18. CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  19. SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  20. NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  21. LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  23. STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  24. ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  25. ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. #include "u8x8.h"
  28. static const uint8_t u8x8_d_pcd8544_84x48_init_seq[] = {
  29. U8X8_START_TRANSFER(), /* enable chip, delay is part of the transfer start */
  30. U8X8_C(0x021), /* activate chip (PD=0), horizontal increment (V=0), enter extended command set (H=1) */
  31. U8X8_C(0x006), /* temp. control: b10 = 2 */
  32. U8X8_C(0x013), /* bias system 1:48 */
  33. U8X8_C(0x0c0), /* medium Vop */
  34. U8X8_C(0x020), /* activate chip (PD=0), horizontal increment (V=0), enter normal command set (H=0) */
  35. U8X8_C(0x008), /* blank */
  36. U8X8_C(0x024), /* power down (PD=1), horizontal increment (V=0), enter normal command set (H=0) */
  37. U8X8_END_TRANSFER(), /* disable chip */
  38. U8X8_END() /* end of sequence */
  39. };
  40. static const uint8_t u8x8_d_pcd8544_84x48_powersave0_seq[] = {
  41. U8X8_START_TRANSFER(), /* enable chip, delay is part of the transfer start */
  42. U8X8_C(0x020), /* activate chip (PD=0), horizontal increment (V=0), enter normal command set (H=0) */
  43. U8X8_C(0x00c), /* display on */
  44. U8X8_END_TRANSFER(), /* disable chip */
  45. U8X8_END() /* end of sequence */
  46. };
  47. static const uint8_t u8x8_d_pcd8544_84x48_powersave1_seq[] = {
  48. U8X8_START_TRANSFER(), /* enable chip, delay is part of the transfer start */
  49. U8X8_C(0x020), /* activate chip (PD=0), horizontal increment (V=0), enter normal command set (H=0) */
  50. U8X8_C(0x008), /* blank */
  51. U8X8_C(0x024), /* power down (PD=1), horizontal increment (V=0), enter normal command set (H=0) */
  52. U8X8_END_TRANSFER(), /* disable chip */
  53. U8X8_END() /* end of sequence */
  54. };
  55. static const u8x8_display_info_t u8x8_pcd8544_84x48_display_info =
  56. {
  57. /* chip_enable_level = */ 0,
  58. /* chip_disable_level = */ 1,
  59. /* post_chip_enable_wait_ns = */ 5,
  60. /* pre_chip_disable_wait_ns = */ 5,
  61. /* reset_pulse_width_ms = */ 2,
  62. /* post_reset_wait_ms = */ 2,
  63. /* sda_setup_time_ns = */ 12,
  64. /* sck_pulse_width_ns = */ 75, /* half of cycle time (100ns according to datasheet), AVR: below 70: 8 MHz, >= 70 --> 4MHz clock */
  65. /* sck_clock_hz = */ 4000000UL, /* since Arduino 1.6.0, the SPI bus speed in Hz. Should be 1000000000/sck_pulse_width_ns */
  66. /* spi_mode = */ 0, /* active high, rising edge */
  67. /* i2c_bus_clock_100kHz = */ 4,
  68. /* data_setup_time_ns = */ 30,
  69. /* write_pulse_width_ns = */ 40,
  70. /* tile_width = */ 11, /* width of 11*8=88 pixel */
  71. /* tile_height = */ 6,
  72. /* default_x_offset = */ 0,
  73. /* flipmode_x_offset = */ 0,
  74. /* pixel_width = */ 84,
  75. /* pixel_height = */ 48
  76. };
  77. uint8_t u8x8_d_pcd8544_84x48(u8x8_t *u8x8, uint8_t msg, uint8_t arg_int, void *arg_ptr)
  78. {
  79. uint8_t x, c;
  80. uint8_t *ptr;
  81. switch(msg)
  82. {
  83. case U8X8_MSG_DISPLAY_SETUP_MEMORY:
  84. u8x8_d_helper_display_setup_memory(u8x8, &u8x8_pcd8544_84x48_display_info);
  85. break;
  86. case U8X8_MSG_DISPLAY_INIT:
  87. u8x8_d_helper_display_init(u8x8);
  88. u8x8_cad_SendSequence(u8x8, u8x8_d_pcd8544_84x48_init_seq);
  89. break;
  90. case U8X8_MSG_DISPLAY_SET_POWER_SAVE:
  91. if ( arg_int == 0 )
  92. u8x8_cad_SendSequence(u8x8, u8x8_d_pcd8544_84x48_powersave0_seq);
  93. else
  94. u8x8_cad_SendSequence(u8x8, u8x8_d_pcd8544_84x48_powersave1_seq);
  95. break;
  96. // case U8X8_MSG_DISPLAY_SET_FLIP_MODE:
  97. // break; NOT SUPPORTED
  98. #ifdef U8X8_WITH_SET_CONTRAST
  99. case U8X8_MSG_DISPLAY_SET_CONTRAST:
  100. u8x8_cad_StartTransfer(u8x8);
  101. u8x8_cad_SendCmd(u8x8, 0x021 ); /* command mode, extended function set */
  102. u8x8_cad_SendCmd(u8x8, 0x080 | (arg_int >> 1) );
  103. u8x8_cad_EndTransfer(u8x8);
  104. break;
  105. #endif
  106. case U8X8_MSG_DISPLAY_DRAW_TILE:
  107. u8x8_cad_StartTransfer(u8x8);
  108. x = ((u8x8_tile_t *)arg_ptr)->x_pos;
  109. x *= 8;
  110. x += u8x8->x_offset;
  111. u8x8_cad_SendCmd(u8x8, 0x020 ); /* activate chip (PD=0), horizontal increment (V=0), enter normal command set (H=0) */
  112. u8x8_cad_SendCmd(u8x8, 0x080 | (x) ); /* set X address */
  113. u8x8_cad_SendCmd(u8x8, 0x040 | (((u8x8_tile_t *)arg_ptr)->y_pos) ); /* set Y address */
  114. ptr = ((u8x8_tile_t *)arg_ptr)->tile_ptr;
  115. c = ((u8x8_tile_t *)arg_ptr)->cnt;
  116. c *= 8;
  117. do
  118. {
  119. if ( c + x > 84u )
  120. {
  121. if ( x >= 84u )
  122. break;
  123. c = 84u;
  124. c -= x;
  125. }
  126. u8x8_cad_SendData(u8x8, c, ptr); /* note: SendData can not handle more than 255 bytes */
  127. x += c;
  128. arg_int--;
  129. } while( arg_int > 0 );
  130. u8x8_cad_EndTransfer(u8x8);
  131. break;
  132. default:
  133. return 0;
  134. }
  135. return 1;
  136. }