EPD_2in13.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  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(10);
  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. Debug("e-Paper busy\r\n");
  109. while(DEV_Digital_Read(EPD_BUSY_PIN) == 1) { //LOW: idle, HIGH: busy
  110. DEV_Delay_ms(100);
  111. }
  112. Debug("e-Paper busy release\r\n");
  113. }
  114. /******************************************************************************
  115. function : Turn On Display
  116. parameter:
  117. ******************************************************************************/
  118. static void EPD_2IN13_TurnOnDisplay(void)
  119. {
  120. EPD_2IN13_SendCommand(0x22); // DISPLAY_UPDATE_CONTROL_2
  121. EPD_2IN13_SendData(0xC4);
  122. EPD_2IN13_SendCommand(0X20); // MASTER_ACTIVATION
  123. EPD_2IN13_SendCommand(0xFF); // TERMINATE_FRAME_READ_WRITE
  124. EPD_2IN13_ReadBusy();
  125. }
  126. static void EPD_2IN13_SetWindows(int x_start, int y_start, int x_end, int y_end)
  127. {
  128. EPD_2IN13_SendCommand(0x44);
  129. /* x point must be the multiple of 8 or the last 3 bits will be ignored */
  130. EPD_2IN13_SendData((x_start >> 3) & 0xFF);
  131. EPD_2IN13_SendData((x_end >> 3) & 0xFF);
  132. EPD_2IN13_SendCommand(0x45);
  133. EPD_2IN13_SendData(y_start & 0xFF);
  134. EPD_2IN13_SendData((y_start >> 8) & 0xFF);
  135. EPD_2IN13_SendData(y_end & 0xFF);
  136. EPD_2IN13_SendData((y_end >> 8) & 0xFF);
  137. }
  138. static void EPD_2IN13_SetCursor(int x, int y)
  139. {
  140. EPD_2IN13_SendCommand(0x4E);
  141. /* x point must be the multiple of 8 or the last 3 bits will be ignored */
  142. EPD_2IN13_SendData((x >> 3) & 0xFF);
  143. EPD_2IN13_SendCommand(0x4F);
  144. EPD_2IN13_SendData(y & 0xFF);
  145. EPD_2IN13_SendData((y >> 8) & 0xFF);
  146. // EPD_2IN13_ReadBusy();
  147. }
  148. /******************************************************************************
  149. function : Initialize the e-Paper register
  150. parameter:
  151. ******************************************************************************/
  152. void EPD_2IN13_Init(UBYTE Mode)
  153. {
  154. EPD_2IN13_Reset();
  155. EPD_2IN13_SendCommand(0x01); // DRIVER_OUTPUT_CONTROL
  156. EPD_2IN13_SendData((EPD_2IN13_HEIGHT - 1) & 0xFF);
  157. EPD_2IN13_SendData(((EPD_2IN13_HEIGHT - 1) >> 8) & 0xFF);
  158. EPD_2IN13_SendData(0x00); // GD = 0; SM = 0; TB = 0;
  159. EPD_2IN13_SendCommand(0x0C); // BOOSTER_SOFT_START_CONTROL
  160. EPD_2IN13_SendData(0xD7);
  161. EPD_2IN13_SendData(0xD6);
  162. EPD_2IN13_SendData(0x9D);
  163. EPD_2IN13_SendCommand(0x2C); // WRITE_VCOM_REGISTER
  164. EPD_2IN13_SendData(0xA8); // VCOM 7C
  165. EPD_2IN13_SendCommand(0x3A); // SET_DUMMY_LINE_PERIOD
  166. EPD_2IN13_SendData(0x1A); // 4 dummy lines per gate
  167. EPD_2IN13_SendCommand(0x3B); // SET_GATE_TIME
  168. EPD_2IN13_SendData(0x08); // 2us per line
  169. EPD_2IN13_SendCommand(0X3C); // BORDER_WAVEFORM_CONTROL
  170. EPD_2IN13_SendData(0x63);
  171. EPD_2IN13_SendCommand(0X11); // DATA_ENTRY_MODE_SETTING
  172. EPD_2IN13_SendData(0x03); // X increment; Y increment
  173. //set the look-up table register
  174. EPD_2IN13_SendCommand(0x32);
  175. if(Mode == EPD_2IN13_FULL) {
  176. for (UWORD i = 0; i < 30; i++) {
  177. EPD_2IN13_SendData(EPD_2IN13_lut_full_update[i]);
  178. }
  179. } else if(Mode == EPD_2IN13_PART) {
  180. for (UWORD i = 0; i < 30; i++) {
  181. EPD_2IN13_SendData(EPD_2IN13_lut_partial_update[i]);
  182. }
  183. } else {
  184. Debug("error, the Mode is EPD_2IN13_FULL or EPD_2IN13_PART");
  185. }
  186. }
  187. /******************************************************************************
  188. function : Clear screen
  189. parameter:
  190. ******************************************************************************/
  191. void EPD_2IN13_Clear(void)
  192. {
  193. UWORD Width, Height;
  194. Width = (EPD_2IN13_WIDTH % 8 == 0)? (EPD_2IN13_WIDTH / 8 ): (EPD_2IN13_WIDTH / 8 + 1);
  195. Height = EPD_2IN13_HEIGHT;
  196. EPD_2IN13_SetWindows(0, 0, EPD_2IN13_WIDTH, EPD_2IN13_HEIGHT);
  197. for (UWORD j = 0; j < Height; j++) {
  198. EPD_2IN13_SetCursor(0, j);
  199. EPD_2IN13_SendCommand(0x24);
  200. for (UWORD i = 0; i < Width; i++) {
  201. EPD_2IN13_SendData(0Xff);
  202. }
  203. }
  204. EPD_2IN13_TurnOnDisplay();
  205. }
  206. /******************************************************************************
  207. function : Sends the image buffer in RAM to e-Paper and displays
  208. parameter:
  209. ******************************************************************************/
  210. void EPD_2IN13_Display(UBYTE *Image)
  211. {
  212. UWORD Width, Height;
  213. Width = (EPD_2IN13_WIDTH % 8 == 0)? (EPD_2IN13_WIDTH / 8 ): (EPD_2IN13_WIDTH / 8 + 1);
  214. Height = EPD_2IN13_HEIGHT;
  215. EPD_2IN13_SetWindows(0, 0, EPD_2IN13_WIDTH, EPD_2IN13_HEIGHT);
  216. for (UWORD j = 0; j < Height; j++) {
  217. EPD_2IN13_SetCursor(0, j);
  218. EPD_2IN13_SendCommand(0x24);
  219. for (UWORD i = 0; i < Width; i++) {
  220. EPD_2IN13_SendData(Image[i + j * Width]);
  221. }
  222. }
  223. EPD_2IN13_TurnOnDisplay();
  224. }
  225. /******************************************************************************
  226. function : Enter sleep mode
  227. parameter:
  228. ******************************************************************************/
  229. void EPD_2IN13_Sleep(void)
  230. {
  231. EPD_2IN13_SendCommand(0x10); //DEEP_SLEEP_MODE
  232. EPD_2IN13_SendData(0x01);
  233. }