My Marlin configs for Fabrikator Mini and CTC i3 Pro B
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ultralcd_st7565_u8glib_VIKI.h 9.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. /**
  2. * Marlin 3D Printer Firmware
  3. * Copyright (C) 2016, 2017 MarlinFirmware [https://github.com/MarlinFirmware/Marlin]
  4. *
  5. * Based on Sprinter and grbl.
  6. * Copyright (C) 2011 Camiel Gubbels / Erik van der Zalm
  7. *
  8. * This program is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation, either version 3 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  20. *
  21. */
  22. #ifndef ULCDST7565_H
  23. #define ULCDST7565_H
  24. #include "../../inc/MarlinConfig.h"
  25. #if !( defined(DOGLCD_SCK) && DOGLCD_SCK >= 0 \
  26. && defined(DOGLCD_MOSI) && DOGLCD_MOSI >= 0 \
  27. && defined(DOGLCD_CS) && DOGLCD_CS >= 0 \
  28. && defined(DOGLCD_A0) && DOGLCD_A0 >= 0 )
  29. #error "DOGLCD_SCK, DOGLCD_MOSI, DOGLCD_CS, and DOGLCD_A0 are required for VIKI."
  30. #endif
  31. #define ST7565_CLK_PIN DOGLCD_SCK
  32. #define ST7565_DAT_PIN DOGLCD_MOSI
  33. #define ST7565_CS_PIN DOGLCD_CS
  34. #define ST7565_A0_PIN DOGLCD_A0
  35. #include <U8glib.h>
  36. #define WIDTH 128
  37. #define HEIGHT 64
  38. #define PAGE_HEIGHT 8
  39. //set optimization so ARDUINO optimizes this file
  40. #pragma GCC optimize (3)
  41. // If you want you can define your own set of delays in Configuration.h
  42. //#define ST7565_DELAY_1 DELAY_0_NOP
  43. //#define ST7565_DELAY_2 DELAY_0_NOP
  44. //#define ST7565_DELAY_3 DELAY_0_NOP
  45. /*
  46. #define ST7565_DELAY_1 u8g_10MicroDelay()
  47. #define ST7565_DELAY_2 u8g_10MicroDelay()
  48. #define ST7565_DELAY_3 u8g_10MicroDelay()
  49. */
  50. #if F_CPU >= 20000000
  51. #define CPU_ST7565_DELAY_1 DELAY_0_NOP
  52. #define CPU_ST7565_DELAY_2 DELAY_0_NOP
  53. #define CPU_ST7565_DELAY_3 DELAY_1_NOP
  54. #elif (MOTHERBOARD == BOARD_3DRAG) || (MOTHERBOARD == BOARD_K8200) || (MOTHERBOARD == BOARD_K8400)
  55. #define CPU_ST7565_DELAY_1 DELAY_0_NOP
  56. #define CPU_ST7565_DELAY_2 DELAY_3_NOP
  57. #define CPU_ST7565_DELAY_3 DELAY_0_NOP
  58. #elif (MOTHERBOARD == BOARD_MINIRAMBO)
  59. #define CPU_ST7565_DELAY_1 DELAY_0_NOP
  60. #define CPU_ST7565_DELAY_2 DELAY_4_NOP
  61. #define CPU_ST7565_DELAY_3 DELAY_0_NOP
  62. #elif (MOTHERBOARD == BOARD_RAMBO)
  63. #define CPU_ST7565_DELAY_1 DELAY_0_NOP
  64. #define CPU_ST7565_DELAY_2 DELAY_0_NOP
  65. #define CPU_ST7565_DELAY_3 DELAY_0_NOP
  66. #elif F_CPU == 16000000
  67. #define CPU_ST7565_DELAY_1 DELAY_0_NOP
  68. #define CPU_ST7565_DELAY_2 DELAY_0_NOP
  69. #define CPU_ST7565_DELAY_3 DELAY_1_NOP
  70. #else
  71. #error "No valid condition for delays in 'ultralcd_st7565_u8glib_VIKI.h'"
  72. #endif
  73. #ifndef ST7565_DELAY_1
  74. #define ST7565_DELAY_1 CPU_ST7565_DELAY_1
  75. #endif
  76. #ifndef ST7565_DELAY_2
  77. #define ST7565_DELAY_2 CPU_ST7565_DELAY_2
  78. #endif
  79. #ifndef ST7565_DELAY_3
  80. #define ST7565_DELAY_3 CPU_ST7565_DELAY_3
  81. #endif
  82. #define ST7565_SND_BIT \
  83. WRITE(ST7565_CLK_PIN, LOW); ST7565_DELAY_1; \
  84. WRITE(ST7565_DAT_PIN, val & 0x80); ST7565_DELAY_2; \
  85. WRITE(ST7565_CLK_PIN, HIGH); ST7565_DELAY_3; \
  86. WRITE(ST7565_CLK_PIN, LOW);\
  87. val <<= 1
  88. static void ST7565_SWSPI_SND_8BIT(uint8_t val) {
  89. ST7565_SND_BIT; // 1
  90. ST7565_SND_BIT; // 2
  91. ST7565_SND_BIT; // 3
  92. ST7565_SND_BIT; // 4
  93. ST7565_SND_BIT; // 5
  94. ST7565_SND_BIT; // 6
  95. ST7565_SND_BIT; // 7
  96. ST7565_SND_BIT; // 8
  97. }
  98. #if defined(DOGM_SPI_DELAY_US) && DOGM_SPI_DELAY_US > 0
  99. #define U8G_DELAY delayMicroseconds(DOGM_SPI_DELAY_US)
  100. #else
  101. #define U8G_DELAY u8g_10MicroDelay()
  102. #endif
  103. #define ST7565_CS() do{ WRITE(ST7565_CS_PIN, HIGH); U8G_DELAY; }while(0)
  104. #define ST7565_NCS() WRITE(ST7565_CS_PIN, LOW)
  105. #define ST7565_A0() do{ WRITE(ST7565_A0_PIN, HIGH); U8G_DELAY; }while(0)
  106. #define ST7565_NA0() WRITE(ST7565_A0_PIN, LOW)
  107. #define ST7565_WRITE_BYTE(a) do{ ST7565_SWSPI_SND_8BIT((uint8_t)a); U8G_DELAY; }while(0)
  108. #define ST7560_WriteSequence(count, pointer) do{ uint8_t *ptr = pointer; for (uint8_t i = 0; i < count; ++i) { ST7565_SWSPI_SND_8BIT(*ptr++); } DELAY_10US; }while(0)
  109. uint8_t u8g_dev_st7565_64128n_2x_VIKI_fn(u8g_t *u8g, u8g_dev_t *dev, uint8_t msg, void *arg) {
  110. switch (msg) {
  111. case U8G_DEV_MSG_INIT: {
  112. OUT_WRITE(ST7565_CS_PIN, LOW);
  113. OUT_WRITE(ST7565_DAT_PIN, LOW);
  114. OUT_WRITE(ST7565_CLK_PIN, LOW);
  115. OUT_WRITE(ST7565_A0_PIN, LOW);
  116. ST7565_CS(); // disable chip
  117. ST7565_NA0(); // instruction mode
  118. ST7565_NCS(); // enable chip
  119. ST7565_WRITE_BYTE(0xA2); // 0xA2: LCD bias 1/9 (according to Displaytech 64128N datasheet)
  120. ST7565_WRITE_BYTE(0xA0); // Normal ADC Select (according to Displaytech 64128N datasheet)
  121. ST7565_WRITE_BYTE(0xC8); // common output mode: set scan direction normal operation/SHL Select; 0xC0 --> SHL = 0; normal; 0xC8 --> SHL = 1
  122. ST7565_WRITE_BYTE(0x40); // Display start line for Displaytech 64128N
  123. ST7565_WRITE_BYTE(0x28 | 0x04); // power control: turn on voltage converter
  124. //U8G_ESC_DLY(50); // delay 50 ms - hangs after a reset if used
  125. ST7565_WRITE_BYTE(0x28 | 0x06); // power control: turn on voltage regulator
  126. //U8G_ESC_DLY(50); // delay 50 ms - hangs after a reset if used
  127. ST7565_WRITE_BYTE(0x28 | 0x07); // power control: turn on voltage follower
  128. //U8G_ESC_DLY(50); // delay 50 ms - hangs after a reset if used
  129. ST7565_WRITE_BYTE(0x10); // Set V0 voltage resistor ratio. Setting for controlling brightness of Displaytech 64128N
  130. ST7565_WRITE_BYTE(0xA6); // display normal, bit val 0: LCD pixel off.
  131. ST7565_WRITE_BYTE(0x81); // set contrast
  132. ST7565_WRITE_BYTE(0x1E); // Contrast value. Setting for controlling brightness of Displaytech 64128N
  133. ST7565_WRITE_BYTE(0xAF); // display on
  134. U8G_ESC_DLY(100); // delay 100 ms
  135. ST7565_WRITE_BYTE(0xA5); // display all points; ST7565
  136. U8G_ESC_DLY(100); // delay 100 ms
  137. U8G_ESC_DLY(100); // delay 100 ms
  138. ST7565_WRITE_BYTE(0xA4); // normal display
  139. ST7565_CS(); // disable chip
  140. } // end of sequence
  141. break;
  142. case U8G_DEV_MSG_STOP:
  143. break;
  144. case U8G_DEV_MSG_PAGE_NEXT: {
  145. u8g_pb_t *pb = (u8g_pb_t *)(dev->dev_mem);
  146. ST7565_CS(); // disable chip
  147. ST7565_NA0(); // instruction mode
  148. ST7565_NCS(); // enable chip
  149. ST7565_WRITE_BYTE(0x10); // set upper 4 bit of the col adr to 0x10
  150. ST7565_WRITE_BYTE(0x00); // set lower 4 bit of the col adr to 0x00. Changed for DisplayTech 64128N
  151. // end of sequence
  152. ST7565_WRITE_BYTE(0xB0 | (2 * pb->p.page)); // select current page (ST7565R)
  153. ST7565_A0(); // data mode
  154. ST7560_WriteSequence((uint8_t)pb->width, (uint8_t*)pb->buf);
  155. ST7565_CS(); // disable chip
  156. ST7565_NA0(); // instruction mode
  157. ST7565_NCS(); // enable chip
  158. ST7565_WRITE_BYTE(0x10); // set upper 4 bit of the col adr to 0x10
  159. ST7565_WRITE_BYTE(0x00); // set lower 4 bit of the col adr to 0x00. Changed for DisplayTech 64128N
  160. // end of sequence
  161. ST7565_WRITE_BYTE(0xB0 | (2 * pb->p.page + 1)); // select current page (ST7565R)
  162. ST7565_A0(); // data mode
  163. ST7560_WriteSequence((uint8_t)pb->width, (uint8_t*)(pb->buf)+pb->width);
  164. ST7565_CS(); // disable chip
  165. }
  166. break;
  167. case U8G_DEV_MSG_CONTRAST:
  168. ST7565_NCS();
  169. ST7565_NA0(); // instruction mode
  170. ST7565_WRITE_BYTE(0x81);
  171. ST7565_WRITE_BYTE((*(uint8_t *)arg) >> 2);
  172. ST7565_CS(); // disable chip
  173. return 1;
  174. case U8G_DEV_MSG_SLEEP_ON:
  175. ST7565_NA0(); // instruction mode
  176. ST7565_NCS(); // enable chip
  177. ST7565_WRITE_BYTE(0xAC); // static indicator off
  178. ST7565_WRITE_BYTE(0x00); // indicator register set (not sure if this is required)
  179. ST7565_WRITE_BYTE(0xAE); // display off
  180. ST7565_WRITE_BYTE(0xA5); // all points on
  181. ST7565_CS(); // disable chip , bugfix 12 nov 2014
  182. // end of sequence
  183. return 1;
  184. case U8G_DEV_MSG_SLEEP_OFF:
  185. ST7565_NA0(); // instruction mode
  186. ST7565_NCS(); // enable chip
  187. ST7565_WRITE_BYTE(0xA4); // all points off
  188. ST7565_WRITE_BYTE(0xAF); // display on
  189. U8G_ESC_DLY(50); // delay 50 ms
  190. ST7565_CS(); // disable chip , bugfix 12 nov 2014
  191. // end of sequence
  192. return 1;
  193. }
  194. return u8g_dev_pb16v1_base_fn(u8g, dev, msg, arg);
  195. }
  196. uint8_t u8g_dev_st7565_64128n_2x_VIKI_buf[WIDTH*2] U8G_NOCOMMON;
  197. u8g_pb_t u8g_dev_st7565_64128n_2x_VIKI_pb = { { 16, HEIGHT, 0, 0, 0 }, WIDTH, u8g_dev_st7565_64128n_2x_VIKI_buf };
  198. u8g_dev_t u8g_dev_st7565_64128n_2x_VIKI_sw_spi = { u8g_dev_st7565_64128n_2x_VIKI_fn, &u8g_dev_st7565_64128n_2x_VIKI_pb, &u8g_com_null_fn };
  199. class U8GLIB_ST7565_64128n_2x_VIKI : public U8GLIB {
  200. public:
  201. U8GLIB_ST7565_64128n_2x_VIKI(uint8_t dummy)
  202. : U8GLIB(&u8g_dev_st7565_64128n_2x_VIKI_sw_spi)
  203. { }
  204. U8GLIB_ST7565_64128n_2x_VIKI(uint8_t sck, uint8_t mosi, uint8_t cs, uint8_t a0, uint8_t reset = U8G_PIN_NONE)
  205. : U8GLIB(&u8g_dev_st7565_64128n_2x_VIKI_sw_spi)
  206. { }
  207. };
  208. #pragma GCC reset_options
  209. #endif // ULCDST7565_H