EPD_2in13.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. /*****************************************************************************
  2. * | File : EPD_2IN13.c
  3. * | Author : Waveshare team
  4. * | Function : 2.13inch e-paper
  5. * | Info :
  6. *----------------
  7. * | This version: V3.0
  8. * | Date : 2019-06-12
  9. * | Info :
  10. * -----------------------------------------------------------------------------
  11. * V3.0(2019-06-12):
  12. * 1.Change:
  13. * EPD_Reset() => EPD_2IN13_Reset()
  14. * EPD_SendCommand() => EPD_2IN13_SendCommand()
  15. * EPD_SendData() => EPD_2IN13_SendData()
  16. * EPD_WaitUntilIdle() => EPD_2IN13_ReadBusy()
  17. * EPD_Init() => EPD_2IN13_Init()
  18. * EPD_Clear() => EPD_2IN13_Clear()
  19. * EPD_Display() => EPD_2IN13_Display()
  20. * EPD_Sleep() => EPD_2IN13_Sleep()
  21. * -----------------------------------------------------------------------------
  22. * V2.0(2019-01-03):
  23. * 1.Remove:ImageBuff[EPD_2IN13_HEIGHT * EPD_2IN13_WIDTH / 8]
  24. * 2.Change:EPD_Display(UBYTE *Image)
  25. * Need to pass parameters: pointer to cached data
  26. * 3.Change:
  27. * EPD_RST -> EPD_RST_PIN
  28. * EPD_DC -> EPD_DC_PIN
  29. * EPD_CS -> EPD_CS_PIN
  30. * EPD_BUSY -> EPD_BUSY_PIN
  31. #
  32. # Permission is hereby granted, free of charge, to any person obtaining a copy
  33. # of this software and associated documnetation files (the "Software"), to deal
  34. # in the Software without restriction, including without limitation the rights
  35. # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  36. # copies of the Software, and to permit persons to whom the Software is
  37. # furished to do so, subject to the following conditions:
  38. #
  39. # The above copyright notice and this permission notice shall be included in
  40. # all copies or substantial portions of the Software.
  41. #
  42. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  43. # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  44. # FITNESS OR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  45. # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  46. # LIABILITY WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  47. # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  48. # THE SOFTWARE.
  49. #
  50. ******************************************************************************/
  51. #include "EPD_2in13.h"
  52. #include "Debug.h"
  53. const unsigned char EPD_2IN13_lut_full_update[] = {
  54. 0x22, 0x55, 0xAA, 0x55, 0xAA, 0x55, 0xAA, 0x11,
  55. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  56. 0x1E, 0x1E, 0x1E, 0x1E, 0x1E, 0x1E, 0x1E, 0x1E,
  57. 0x01, 0x00, 0x00, 0x00, 0x00, 0x00
  58. };
  59. const unsigned char EPD_2IN13_lut_partial_update[] = {
  60. 0x18, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  61. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  62. 0x0F, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  63. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  64. };
  65. /******************************************************************************
  66. function : Software reset
  67. parameter:
  68. ******************************************************************************/
  69. static void EPD_2IN13_Reset(void)
  70. {
  71. DEV_Digital_Write(EPD_RST_PIN, 1);
  72. DEV_Delay_ms(200);
  73. DEV_Digital_Write(EPD_RST_PIN, 0);
  74. DEV_Delay_ms(2);
  75. DEV_Digital_Write(EPD_RST_PIN, 1);
  76. DEV_Delay_ms(200);
  77. }
  78. /******************************************************************************
  79. function : send command
  80. parameter:
  81. Reg : Command register
  82. ******************************************************************************/
  83. static void EPD_2IN13_SendCommand(UBYTE Reg)
  84. {
  85. DEV_Digital_Write(EPD_DC_PIN, 0);
  86. DEV_Digital_Write(EPD_CS_PIN, 0);
  87. DEV_SPI_WriteByte(Reg);
  88. DEV_Digital_Write(EPD_CS_PIN, 1);
  89. }
  90. /******************************************************************************
  91. function : send data
  92. parameter:
  93. Data : Write data
  94. ******************************************************************************/
  95. static void EPD_2IN13_SendData(UBYTE Data)
  96. {
  97. DEV_Digital_Write(EPD_DC_PIN, 1);
  98. DEV_Digital_Write(EPD_CS_PIN, 0);
  99. DEV_SPI_WriteByte(Data);
  100. DEV_Digital_Write(EPD_CS_PIN, 1);
  101. }
  102. /******************************************************************************
  103. function : Wait until the busy_pin goes LOW
  104. parameter:
  105. ******************************************************************************/
  106. void EPD_2IN13_ReadBusy(void)
  107. {
  108. EPD_Busy_WaitUntil(0,0);
  109. // unsigned char count = 100;
  110. // Debug("e-Paper busy\r\n");
  111. // while(DEV_Digital_Read(EPD_BUSY_PIN) == 1) { //LOW: idle, HIGH: busy
  112. // if(!(count--))
  113. // {
  114. // Debug("error: e-Paper busy timeout!!!\r\n");
  115. // break;
  116. // }
  117. // else
  118. // DEV_Delay_ms(100);
  119. // }
  120. // Debug("e-Paper busy release\r\n");
  121. }
  122. /******************************************************************************
  123. function : Turn On Display
  124. parameter:
  125. ******************************************************************************/
  126. static void EPD_2IN13_TurnOnDisplay(void)
  127. {
  128. EPD_2IN13_SendCommand(0x22); // DISPLAY_UPDATE_CONTROL_2
  129. EPD_2IN13_SendData(0xC4);
  130. EPD_2IN13_SendCommand(0X20); // MASTER_ACTIVATION
  131. EPD_2IN13_SendCommand(0xFF); // TERMINATE_FRAME_READ_WRITE
  132. EPD_2IN13_ReadBusy();
  133. }
  134. static void EPD_2IN13_SetWindows(int x_start, int y_start, int x_end, int y_end)
  135. {
  136. EPD_2IN13_SendCommand(0x44);
  137. /* x point must be the multiple of 8 or the last 3 bits will be ignored */
  138. EPD_2IN13_SendData((x_start >> 3) & 0xFF);
  139. EPD_2IN13_SendData((x_end >> 3) & 0xFF);
  140. EPD_2IN13_SendCommand(0x45);
  141. EPD_2IN13_SendData(y_start & 0xFF);
  142. EPD_2IN13_SendData((y_start >> 8) & 0xFF);
  143. EPD_2IN13_SendData(y_end & 0xFF);
  144. EPD_2IN13_SendData((y_end >> 8) & 0xFF);
  145. }
  146. static void EPD_2IN13_SetCursor(int x, int y)
  147. {
  148. EPD_2IN13_SendCommand(0x4E);
  149. /* x point must be the multiple of 8 or the last 3 bits will be ignored */
  150. EPD_2IN13_SendData((x >> 3) & 0xFF);
  151. EPD_2IN13_SendCommand(0x4F);
  152. EPD_2IN13_SendData(y & 0xFF);
  153. EPD_2IN13_SendData((y >> 8) & 0xFF);
  154. // EPD_2IN13_ReadBusy();
  155. }
  156. /******************************************************************************
  157. function : Initialize the e-Paper register
  158. parameter:
  159. ******************************************************************************/
  160. void EPD_2IN13_Init(UBYTE Mode)
  161. {
  162. EPD_2IN13_Reset();
  163. EPD_2IN13_SendCommand(0x01); // DRIVER_OUTPUT_CONTROL
  164. EPD_2IN13_SendData((EPD_2IN13_HEIGHT - 1) & 0xFF);
  165. EPD_2IN13_SendData(((EPD_2IN13_HEIGHT - 1) >> 8) & 0xFF);
  166. EPD_2IN13_SendData(0x00); // GD = 0; SM = 0; TB = 0;
  167. EPD_2IN13_SendCommand(0x0C); // BOOSTER_SOFT_START_CONTROL
  168. EPD_2IN13_SendData(0xD7);
  169. EPD_2IN13_SendData(0xD6);
  170. EPD_2IN13_SendData(0x9D);
  171. EPD_2IN13_SendCommand(0x2C); // WRITE_VCOM_REGISTER
  172. EPD_2IN13_SendData(0xA8); // VCOM 7C
  173. EPD_2IN13_SendCommand(0x3A); // SET_DUMMY_LINE_PERIOD
  174. EPD_2IN13_SendData(0x1A); // 4 dummy lines per gate
  175. EPD_2IN13_SendCommand(0x3B); // SET_GATE_TIME
  176. EPD_2IN13_SendData(0x08); // 2us per line
  177. EPD_2IN13_SendCommand(0X3C); // BORDER_WAVEFORM_CONTROL
  178. EPD_2IN13_SendData(0x03);
  179. EPD_2IN13_SendCommand(0X11); // DATA_ENTRY_MODE_SETTING
  180. EPD_2IN13_SendData(0x03); // X increment; Y increment
  181. //set the look-up table register
  182. EPD_2IN13_SendCommand(0x32);
  183. if(Mode == EPD_2IN13_FULL) {
  184. for (UWORD i = 0; i < 30; i++) {
  185. EPD_2IN13_SendData(EPD_2IN13_lut_full_update[i]);
  186. }
  187. } else if(Mode == EPD_2IN13_PART) {
  188. for (UWORD i = 0; i < 30; i++) {
  189. EPD_2IN13_SendData(EPD_2IN13_lut_partial_update[i]);
  190. }
  191. } else {
  192. Debug("error, the Mode is EPD_2IN13_FULL or EPD_2IN13_PART");
  193. }
  194. }
  195. /******************************************************************************
  196. function : Clear screen
  197. parameter:
  198. ******************************************************************************/
  199. void EPD_2IN13_Clear(void)
  200. {
  201. UWORD Width, Height;
  202. Width = (EPD_2IN13_WIDTH % 8 == 0)? (EPD_2IN13_WIDTH / 8 ): (EPD_2IN13_WIDTH / 8 + 1);
  203. Height = EPD_2IN13_HEIGHT;
  204. EPD_2IN13_SetWindows(0, 0, EPD_2IN13_WIDTH, EPD_2IN13_HEIGHT);
  205. for (UWORD j = 0; j < Height; j++) {
  206. EPD_2IN13_SetCursor(0, j);
  207. EPD_2IN13_SendCommand(0x24);
  208. for (UWORD i = 0; i < Width; i++) {
  209. EPD_2IN13_SendData(0Xff);
  210. }
  211. }
  212. EPD_2IN13_TurnOnDisplay();
  213. }
  214. /******************************************************************************
  215. function : Sends the image buffer in RAM to e-Paper and displays
  216. parameter:
  217. ******************************************************************************/
  218. void EPD_2IN13_Display(UBYTE *Image, UBYTE *Image2)
  219. {
  220. UWORD Width, Height;
  221. Width = (EPD_2IN13_WIDTH % 8 == 0)? (EPD_2IN13_WIDTH / 8 ): (EPD_2IN13_WIDTH / 8 + 1);
  222. Height = EPD_2IN13_HEIGHT;
  223. EPD_2IN13_SetWindows(0, 0, EPD_2IN13_WIDTH, EPD_2IN13_HEIGHT);
  224. for (UWORD j = 0; j < Height; j++) {
  225. EPD_2IN13_SetCursor(0, j);
  226. EPD_2IN13_SendCommand(0x24);
  227. for (UWORD i = 0; i < Width; i++) {
  228. EPD_2IN13_SendData(Image[i + j * Width]);
  229. }
  230. }
  231. EPD_2IN13_TurnOnDisplay();
  232. }
  233. /******************************************************************************
  234. function : Enter sleep mode
  235. parameter:
  236. ******************************************************************************/
  237. void EPD_2IN13_Sleep(void)
  238. {
  239. EPD_2IN13_SendCommand(0x10); //DEEP_SLEEP_MODE
  240. EPD_2IN13_SendData(0x01);
  241. }