u8x8_d_st7528.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462
  1. /*
  2. u8x8_d_st7528.c
  3. Universal 8bit Graphics Library (https://github.com/olikraus/u8g2/)
  4. Copyright (c) 2019, 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. ST7528: 16 Graylevel Controller
  27. https://github.com/olikraus/u8g2/issues/986
  28. I2C Address: 0x03f (0x7e)
  29. ERC16004
  30. https://www.buydisplay.com/default/2-inch-lcd-160x64-graphic-module-serial-spi-display-st7528-black-on-white
  31. */
  32. #include "u8x8.h"
  33. static const uint8_t u8x8_d_st7528_powersave0_seq[] = {
  34. U8X8_START_TRANSFER(), /* enable chip, delay is part of the transfer start */
  35. U8X8_CA(0x038, 0x074), /* ext mode 0*/
  36. U8X8_C(0x0af), /* display on */
  37. U8X8_END_TRANSFER(), /* disable chip */
  38. U8X8_END() /* end of sequence */
  39. };
  40. static const uint8_t u8x8_d_st7528_powersave1_seq[] = {
  41. U8X8_START_TRANSFER(), /* enable chip, delay is part of the transfer start */
  42. U8X8_CA(0x038, 0x074), /* ext mode 0*/
  43. U8X8_C(0x0ae), /* display off */
  44. U8X8_END_TRANSFER(), /* disable chip */
  45. U8X8_END() /* end of sequence */
  46. };
  47. static const uint8_t u8x8_d_st7528_flip0_seq[] = {
  48. U8X8_START_TRANSFER(), /* enable chip, delay is part of the transfer start */
  49. U8X8_C(0x0a0), /* ADC */
  50. U8X8_C(0x0c8), /* SHL */
  51. U8X8_END_TRANSFER(), /* disable chip */
  52. U8X8_END() /* end of sequence */
  53. };
  54. static const uint8_t u8x8_d_st7528_flip1_seq[] = {
  55. U8X8_START_TRANSFER(), /* enable chip, delay is part of the transfer start */
  56. U8X8_C(0x0a1), /* ADC */
  57. U8X8_C(0x0c0), /* SHL */
  58. U8X8_END_TRANSFER(), /* disable chip */
  59. U8X8_END() /* end of sequence */
  60. };
  61. /*
  62. input:
  63. one tile (8 Bytes)
  64. output:
  65. Tile for st7528 (32 Bytes)
  66. */
  67. static uint8_t u8x8_st7528_8to32_dest_buf[32];
  68. static uint8_t *u8x8_st7528_8to32(U8X8_UNUSED u8x8_t *u8x8, uint8_t *ptr)
  69. {
  70. uint8_t j;
  71. uint8_t *dest;
  72. dest = u8x8_st7528_8to32_dest_buf;
  73. for( j = 0; j < 8; j++ )
  74. {
  75. *dest++ =*ptr;
  76. *dest++ =*ptr;
  77. *dest++ =*ptr;
  78. *dest++ =*ptr;
  79. ptr++;
  80. }
  81. return u8x8_st7528_8to32_dest_buf;
  82. }
  83. static uint8_t u8x8_d_st7528_generic(u8x8_t *u8x8, uint8_t msg, uint8_t arg_int, void *arg_ptr)
  84. {
  85. uint8_t x;
  86. uint8_t y, c;
  87. uint8_t *ptr;
  88. switch(msg)
  89. {
  90. /* handled by the calling function
  91. case U8X8_MSG_DISPLAY_SETUP_MEMORY:
  92. u8x8_d_helper_display_setup_memory(u8x8, &u8x8_st7528_display_info);
  93. break;
  94. case U8X8_MSG_DISPLAY_INIT:
  95. u8x8_d_helper_display_init(u8x8);
  96. u8x8_cad_SendSequence(u8x8, u8x8_d_st7528_nhd_c160100_init_seq);
  97. */
  98. break;
  99. case U8X8_MSG_DISPLAY_SET_POWER_SAVE:
  100. if ( arg_int == 0 )
  101. u8x8_cad_SendSequence(u8x8, u8x8_d_st7528_powersave0_seq);
  102. else
  103. u8x8_cad_SendSequence(u8x8, u8x8_d_st7528_powersave1_seq);
  104. break;
  105. case U8X8_MSG_DISPLAY_SET_FLIP_MODE:
  106. if ( arg_int == 0 )
  107. {
  108. u8x8_cad_SendSequence(u8x8, u8x8_d_st7528_flip0_seq);
  109. u8x8->x_offset = u8x8->display_info->default_x_offset;
  110. }
  111. else
  112. {
  113. u8x8_cad_SendSequence(u8x8, u8x8_d_st7528_flip1_seq);
  114. u8x8->x_offset = u8x8->display_info->flipmode_x_offset;
  115. }
  116. break;
  117. #ifdef U8X8_WITH_SET_CONTRAST
  118. case U8X8_MSG_DISPLAY_SET_CONTRAST:
  119. u8x8_cad_StartTransfer(u8x8);
  120. u8x8_cad_SendCmd(u8x8, 0x081 );
  121. u8x8_cad_SendArg(u8x8, arg_int ); /* ssd1326 has range from 0 to 255 */
  122. u8x8_cad_EndTransfer(u8x8);
  123. break;
  124. #endif
  125. case U8X8_MSG_DISPLAY_DRAW_TILE:
  126. u8x8_cad_StartTransfer(u8x8);
  127. x = ((u8x8_tile_t *)arg_ptr)->x_pos;
  128. x *= 8; // not clear
  129. y = (((u8x8_tile_t *)arg_ptr)->y_pos);
  130. do
  131. {
  132. c = ((u8x8_tile_t *)arg_ptr)->cnt;
  133. ptr = ((u8x8_tile_t *)arg_ptr)->tile_ptr;
  134. do
  135. {
  136. u8x8_cad_SendCmd(u8x8, 0xb0 | y ); /* set page address */
  137. u8x8_cad_SendCmd(u8x8, 0x10| (x>>4) ); /* set col msb*/
  138. u8x8_cad_SendCmd(u8x8, 0x00| (x&15) ); /* set col lsb*/
  139. u8x8_cad_SendData(u8x8, 32, u8x8_st7528_8to32(u8x8, ptr));
  140. ptr += 8;
  141. x += 8;
  142. c--;
  143. } while( c > 0 );
  144. arg_int--;
  145. } while( arg_int > 0 );
  146. u8x8_cad_EndTransfer(u8x8);
  147. break;
  148. default:
  149. return 0;
  150. }
  151. return 1;
  152. }
  153. static void u8x8_d_st7528_graylevel_init(u8x8_t *u8x8, uint8_t mode0)
  154. {
  155. uint8_t i;
  156. u8x8_cad_StartTransfer(u8x8);
  157. u8x8_cad_SendCmd(u8x8, 0x38 );
  158. u8x8_cad_SendArg(u8x8, mode0+1 );
  159. for( i = 0; i < 64; i++ )
  160. {
  161. u8x8_cad_SendCmd(u8x8, i+0x080 );
  162. u8x8_cad_SendArg(u8x8, i & 0xfc);
  163. }
  164. u8x8_cad_SendCmd(u8x8, 0x38 );
  165. u8x8_cad_SendArg(u8x8, mode0 );
  166. u8x8_cad_EndTransfer(u8x8);
  167. }
  168. /*===============================================================*/
  169. /* NHD C160100 */
  170. static const uint8_t u8x8_d_st7528_nhd_c160100_init_seq[] = {
  171. U8X8_START_TRANSFER(), /* enable chip, delay is part of the transfer start */
  172. /*
  173. I2C_out(0x48);//partial display duty ratio
  174. I2C_out(0x64);// 1/100 duty
  175. I2C_out(0xA0);//ADC select
  176. I2C_out(0xC8);//SHL select
  177. I2C_out(0x44);//initial Com0 register
  178. I2C_out(0x00);//scan from Com0
  179. I2C_out(0xAB);//OSC on
  180. I2C_out(0x26);//
  181. I2C_out(0x81); //set electronic volume
  182. I2C_out(0x15);//vopcode=0x1C
  183. I2C_out(0x56);//set 1/11 bias
  184. I2C_out(0x64);//3x
  185. delay(2);
  186. I2C_out(0x2C);//
  187. I2C_out(0x66);//5x
  188. delay(2);
  189. I2C_out(0x2E);//
  190. delay(2);
  191. I2C_out(0x2F);//power control
  192. I2C_out(0xF3);//bias save circuit
  193. I2C_out(0x00);//
  194. I2C_out(0x96);//frc and pwm
  195. I2C_out(0x38);//external mode
  196. I2C_out(0x75);//
  197. I2C_out(0x97);//3frc, 45 pwm THIS IS A MODE0 CMD, IT IS USELESS HERE
  198. I2C_out(0x80);//start 16-level grayscale settings
  199. */
  200. U8X8_CA(0x038, 0x064), /* ext mode 0*/
  201. U8X8_CA(0x048, 0x064), /* partial display duty ratio, 1/100 duty*/
  202. U8X8_C(0x0a0), /* ADC */
  203. U8X8_C(0x0c8), /* SHL */
  204. U8X8_CA(0x044, 0x000), /* initial Com0 */
  205. U8X8_C(0x0ab), /* start oscillator */
  206. U8X8_C(0x026), /* Select the internal resistance ratio of the regulator resistor */
  207. U8X8_CA(0x081, 0x015), /* volumn */
  208. U8X8_C(0x056), /* LCD Bias */
  209. U8X8_C(0x064), /* DC DC step up */
  210. U8X8_DLY(2),
  211. U8X8_C(0x02c), /* Power Control */
  212. U8X8_C(0x066), /* DC DC step up */
  213. U8X8_DLY(2),
  214. U8X8_C(0x02e), /* Power Control */
  215. U8X8_DLY(2),
  216. U8X8_C(0x02f), /* Power Control */
  217. U8X8_CA(0x0f3, 0x000), /* bias power save */
  218. U8X8_C(0x096), /* frc and pwm */
  219. U8X8_END_TRANSFER(), /* disable chip */
  220. U8X8_END() /* end of sequence */
  221. };
  222. static const u8x8_display_info_t u8x8_st7528_160x100_display_info =
  223. {
  224. /* chip_enable_level = */ 0,
  225. /* chip_disable_level = */ 1,
  226. /* post_chip_enable_wait_ns = */ 20,
  227. /* pre_chip_disable_wait_ns = */ 20,
  228. /* reset_pulse_width_ms = */ 5,
  229. /* post_reset_wait_ms = */ 5, /**/
  230. /* sda_setup_time_ns = */ 20, /* st7528 */
  231. /* sck_pulse_width_ns = */ 25, /* st7528 */
  232. /* sck_clock_hz = */ 8000000UL, /* since Arduino 1.6.0, the SPI bus speed in Hz. Should be 1000000000/sck_pulse_width_ns */
  233. /* st7528 actually allows 20MHz according to the datasheet */
  234. /* spi_mode = */ 0, /* active high, rising edge */
  235. /* i2c_bus_clock_100kHz = */ 4,
  236. /* data_setup_time_ns = */ 40,
  237. /* write_pulse_width_ns = */ 80, /* st7528 */
  238. /* tile_width = */ 20,
  239. /* tile_hight = */ 13,
  240. /* default_x_offset = */ 0, /* x_offset is used as y offset for the ssd1326 */
  241. /* flipmode_x_offset = */ 0, /* x_offset is used as y offset for the ssd1326 */
  242. /* pixel_width = */ 160,
  243. /* pixel_height = */ 100
  244. };
  245. uint8_t u8x8_d_st7528_nhd_c160100(u8x8_t *u8x8, uint8_t msg, uint8_t arg_int, void *arg_ptr)
  246. {
  247. if ( msg == U8X8_MSG_DISPLAY_SETUP_MEMORY )
  248. {
  249. u8x8_d_helper_display_setup_memory(u8x8, &u8x8_st7528_160x100_display_info);
  250. return 1;
  251. }
  252. if ( msg == U8X8_MSG_DISPLAY_INIT )
  253. {
  254. u8x8_d_helper_display_init(u8x8);
  255. u8x8_cad_SendSequence(u8x8, u8x8_d_st7528_nhd_c160100_init_seq);
  256. u8x8_d_st7528_graylevel_init(u8x8, 0x074);
  257. return 1;
  258. }
  259. return u8x8_d_st7528_generic(u8x8, msg, arg_int, arg_ptr);
  260. }
  261. /*===============================================================*/
  262. /* ERC16064, https://www.buydisplay.com/2-inch-lcd-160x64-graphic-module-serial-spi-display-st7528-black-on-white */
  263. /*
  264. #define ModeSet 0x38
  265. #define ModeSetP1 0x64 //EXT=0
  266. #define ModeSetP2 0x65 //EXT=1
  267. #define Display_on 0xaf
  268. #define Display_off 0xae
  269. #define Regulator 0x26
  270. #define Contrast_level 0x81
  271. #define PowerControll_on1 0x2c
  272. #define PowerControll_on2 0x2e
  273. #define PowerControll_on3 0x2f
  274. #define LcdBias_9 0x54
  275. #define InterOsc_on 0xab
  276. #define EntireDisp_on 0xa5
  277. #define EntireDisp_off 0xa4
  278. #define BoostLevel_5 0x66
  279. #define Duty_set 0x48
  280. #define Duty_setP 0x40
  281. #define Start_columnlsb 0x00
  282. #define Start_columnmsb 0x10
  283. #define Start_page 0xb0
  284. #define StartLine_set 0x40
  285. //#define StartLine_setP 0x00
  286. #define Set_Initial_COM0 0x44
  287. #define Set_Initial_COM0P 0x12
  288. #define Entire_Displa_ON 0xA5
  289. #define Entire_Displa_OFF 0xA4
  290. #define FrcPwm_set 0x92
  291. #define NLineInversion_on 0x4c
  292. #define NLineInversion_onP 0x1f
  293. #define NLineInversion_off 0xe4
  294. #define ReverseDisp_on 0xa7
  295. #define ReverseDisp_off 0xa6
  296. #define AdcSelect 0xa0
  297. #define ComScanDirection 0xc8
  298. Write_command(ModeSet); 0x38, 0x64
  299. Write_command(ModeSetP1);
  300. Write_command(InterOsc_on); 0xab
  301. Write_command(Set_Initial_COM0); 0x44, 0x12
  302. Write_command(Set_Initial_COM0P);
  303. Write_command(AdcSelect); 0xa0
  304. Write_command(ComScanDirection); 0xc8
  305. Write_command(BoostLevel_5); 0x66
  306. Delay(1000);
  307. Write_command(LcdBias_9); 0x54
  308. Write_command(Duty_set); 0x48, 0x40
  309. Write_command(Duty_setP);
  310. Write_command(Regulator); 0x26
  311. Write_command(Contrast_level); 0x81, 0x0b
  312. Write_command(Contrast_levelP);
  313. Write_command(PowerControll_on1); 0x2c
  314. Delay(10000);
  315. Write_command(PowerControll_on2); 0x2e
  316. Delay(1000);
  317. Write_command(PowerControll_on3); 0x2f
  318. Delay(1000);
  319. Write_command(FrcPwm_set); 0x92
  320. Write_command(ModeSet);
  321. Write_command(ModeSetP2);
  322. for(i=0;i<64;i++)
  323. {
  324. Write_command(0x80+i);
  325. Write_command(Gray_Parameters[i]);
  326. }
  327. Write_command(ModeSet);
  328. Write_command(ModeSetP1);
  329. */
  330. static const uint8_t u8x8_d_st7528_erc16064_init_seq[] = {
  331. U8X8_START_TRANSFER(), /* enable chip, delay is part of the transfer start */
  332. U8X8_CA(0x038, 0x064), /* ext mode 0*/
  333. U8X8_C(0x0ab), /* start oscillator */
  334. U8X8_CA(0x044, 0x012), /* initial Com0 */
  335. U8X8_C(0x0a0), /* ADC */
  336. U8X8_C(0x0c8), /* SHL */
  337. U8X8_C(0x066), /* boost level, ERC16064: 0x066 */
  338. U8X8_DLY(1),
  339. U8X8_C(0x054), /* LCD Bias, ERC16064: 0x054 */
  340. U8X8_CA(0x048, 0x040), /* partial display duty ratio */
  341. U8X8_C(0x026), /* Select the internal resistance ratio of the regulator resistor */
  342. U8X8_CA(0x081, 0x00b), /* contrast, ERC16064: 0x00b */
  343. U8X8_C(0x02c), /* Power Control */
  344. U8X8_DLY(2),
  345. U8X8_C(0x02e), /* Power Control */
  346. U8X8_DLY(2),
  347. U8X8_C(0x02f), /* Power Control */
  348. U8X8_DLY(2),
  349. U8X8_C(0x092), /* frc and pwm, ERC160624: 0x092 */
  350. U8X8_END_TRANSFER(), /* disable chip */
  351. U8X8_END() /* end of sequence */
  352. };
  353. static const u8x8_display_info_t u8x8_st7528_erc16064_display_info =
  354. {
  355. /* chip_enable_level = */ 0,
  356. /* chip_disable_level = */ 1,
  357. /* post_chip_enable_wait_ns = */ 20,
  358. /* pre_chip_disable_wait_ns = */ 20,
  359. /* reset_pulse_width_ms = */ 5,
  360. /* post_reset_wait_ms = */ 5, /**/
  361. /* sda_setup_time_ns = */ 20, /* st7528 */
  362. /* sck_pulse_width_ns = */ 25, /* st7528 */
  363. /* sck_clock_hz = */ 8000000UL, /* since Arduino 1.6.0, the SPI bus speed in Hz. Should be 1000000000/sck_pulse_width_ns */
  364. /* st7528 actually allows 20MHz according to the datasheet */
  365. /* spi_mode = */ 0, /* active high, rising edge */
  366. /* i2c_bus_clock_100kHz = */ 4,
  367. /* data_setup_time_ns = */ 40,
  368. /* write_pulse_width_ns = */ 80, /* st7528 */
  369. /* tile_width = */ 20,
  370. /* tile_hight = */ 8,
  371. /* default_x_offset = */ 0, /* x_offset is used as y offset for the ssd1326 */
  372. /* flipmode_x_offset = */ 0, /* x_offset is used as y offset for the ssd1326 */
  373. /* pixel_width = */ 160,
  374. /* pixel_height = */ 64
  375. };
  376. uint8_t u8x8_d_st7528_erc16064(u8x8_t *u8x8, uint8_t msg, uint8_t arg_int, void *arg_ptr)
  377. {
  378. if ( msg == U8X8_MSG_DISPLAY_SETUP_MEMORY )
  379. {
  380. u8x8_d_helper_display_setup_memory(u8x8, &u8x8_st7528_erc16064_display_info);
  381. return 1;
  382. }
  383. if ( msg == U8X8_MSG_DISPLAY_INIT )
  384. {
  385. u8x8_d_helper_display_init(u8x8);
  386. u8x8_cad_SendSequence(u8x8, u8x8_d_st7528_erc16064_init_seq);
  387. u8x8_d_st7528_graylevel_init(u8x8, 0x064);
  388. return 1;
  389. }
  390. return u8x8_d_st7528_generic(u8x8, msg, arg_int, arg_ptr);
  391. }