EPD_2in13b_V4.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. /*****************************************************************************
  2. * | File : EPD_2IN13b_V4.c
  3. * | Author : Waveshare team
  4. * | Function : 2.13inch e-paper B V4
  5. * | Info :
  6. *----------------
  7. * | This version: V1.0
  8. * | Date : 2022-04-25
  9. * | Info :
  10. * -----------------------------------------------------------------------------
  11. #
  12. # Permission is hereby granted, free of charge, to any person obtaining a copy
  13. # of this software and associated documnetation files (the "Software"), to deal
  14. # in the Software without restriction, including without limitation the rights
  15. # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  16. # copies of the Software, and to permit persons to whom the Software is
  17. # furished to do so, subject to the following conditions:
  18. #
  19. # The above copyright notice and this permission notice shall be included in
  20. # all copies or substantial portions of the Software.
  21. #
  22. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  23. # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  24. # FITNESS OR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  25. # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  26. # LIABILITY WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  27. # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  28. # THE SOFTWARE.
  29. #
  30. ******************************************************************************/
  31. #include "EPD_2in13b_V4.h"
  32. #include "Debug.h"
  33. /******************************************************************************
  34. function : Software reset
  35. parameter:
  36. ******************************************************************************/
  37. static void EPD_2IN13B_V4_Reset(void)
  38. {
  39. DEV_Digital_Write(EPD_RST_PIN, 1);
  40. DEV_Delay_ms(20);
  41. DEV_Digital_Write(EPD_RST_PIN, 0);
  42. DEV_Delay_ms(2);
  43. DEV_Digital_Write(EPD_RST_PIN, 1);
  44. DEV_Delay_ms(20);
  45. }
  46. /******************************************************************************
  47. function : send command
  48. parameter:
  49. Reg : Command register
  50. ******************************************************************************/
  51. static void EPD_2IN13B_V4_SendCommand(UBYTE Reg)
  52. {
  53. DEV_Digital_Write(EPD_DC_PIN, 0);
  54. DEV_Digital_Write(EPD_CS_PIN, 0);
  55. DEV_SPI_WriteByte(Reg);
  56. DEV_Digital_Write(EPD_CS_PIN, 1);
  57. }
  58. /******************************************************************************
  59. function : send data
  60. parameter:
  61. Data : Write data
  62. ******************************************************************************/
  63. static void EPD_2IN13B_V4_SendData(UBYTE Data)
  64. {
  65. DEV_Digital_Write(EPD_DC_PIN, 1);
  66. DEV_Digital_Write(EPD_CS_PIN, 0);
  67. DEV_SPI_WriteByte(Data);
  68. DEV_Digital_Write(EPD_CS_PIN, 1);
  69. }
  70. /******************************************************************************
  71. function : Wait until the busy_pin goes LOW
  72. parameter:
  73. ******************************************************************************/
  74. void EPD_2IN13B_V4_ReadBusy(void)
  75. {
  76. Debug("e-Paper busy\r\n");
  77. while(1)
  78. { //=1 BUSY
  79. if(DEV_Digital_Read(EPD_BUSY_PIN)==0)
  80. break;
  81. DEV_Delay_ms(20);
  82. }
  83. DEV_Delay_ms(20);
  84. Debug("e-Paper busy release\r\n");
  85. }
  86. /******************************************************************************
  87. function : Turn On Display
  88. parameter:
  89. ******************************************************************************/
  90. static void EPD_2IN13B_V4_TurnOnDisplay(void)
  91. {
  92. EPD_2IN13B_V4_SendCommand(0x20); // Activate Display Update Sequence
  93. EPD_2IN13B_V4_ReadBusy();
  94. }
  95. /******************************************************************************
  96. function : Setting the display window
  97. parameter:
  98. ******************************************************************************/
  99. static void EPD_2IN13B_V4_SetWindows(UWORD Xstart, UWORD Ystart, UWORD Xend, UWORD Yend)
  100. {
  101. EPD_2IN13B_V4_SendCommand(0x44); // SET_RAM_X_ADDRESS_START_END_POSITION
  102. EPD_2IN13B_V4_SendData((Xstart>>3) & 0xFF);
  103. EPD_2IN13B_V4_SendData((Xend>>3) & 0xFF);
  104. EPD_2IN13B_V4_SendCommand(0x45); // SET_RAM_Y_ADDRESS_START_END_POSITION
  105. EPD_2IN13B_V4_SendData(Ystart & 0xFF);
  106. EPD_2IN13B_V4_SendData((Ystart >> 8) & 0xFF);
  107. EPD_2IN13B_V4_SendData(Yend & 0xFF);
  108. EPD_2IN13B_V4_SendData((Yend >> 8) & 0xFF);
  109. }
  110. /******************************************************************************
  111. function : Set Cursor
  112. parameter:
  113. ******************************************************************************/
  114. static void EPD_2IN13B_V4_SetCursor(UWORD Xstart, UWORD Ystart)
  115. {
  116. EPD_2IN13B_V4_SendCommand(0x4E); // SET_RAM_X_ADDRESS_COUNTER
  117. EPD_2IN13B_V4_SendData(Xstart & 0xFF);
  118. EPD_2IN13B_V4_SendCommand(0x4F); // SET_RAM_Y_ADDRESS_COUNTER
  119. EPD_2IN13B_V4_SendData(Ystart & 0xFF);
  120. EPD_2IN13B_V4_SendData((Ystart >> 8) & 0xFF);
  121. }
  122. /******************************************************************************
  123. function : Initialize the e-Paper register
  124. parameter:
  125. ******************************************************************************/
  126. void EPD_2IN13B_V4_Init(void)
  127. {
  128. EPD_2IN13B_V4_Reset();
  129. EPD_2IN13B_V4_ReadBusy();
  130. EPD_2IN13B_V4_SendCommand(0x12); //SWRESET
  131. EPD_2IN13B_V4_ReadBusy();
  132. EPD_2IN13B_V4_SendCommand(0x01); //Driver output control
  133. EPD_2IN13B_V4_SendData(0xf9);
  134. EPD_2IN13B_V4_SendData(0x00);
  135. EPD_2IN13B_V4_SendData(0x00);
  136. EPD_2IN13B_V4_SendCommand(0x11); //data entry mode
  137. EPD_2IN13B_V4_SendData(0x03);
  138. EPD_2IN13B_V4_SetWindows(0, 0, EPD_2IN13B_V4_WIDTH-1, EPD_2IN13B_V4_HEIGHT-1);
  139. EPD_2IN13B_V4_SetCursor(0, 0);
  140. EPD_2IN13B_V4_SendCommand(0x3C); //BorderWavefrom
  141. EPD_2IN13B_V4_SendData(0x05);
  142. EPD_2IN13B_V4_SendCommand(0x18); //Read built-in temperature sensor
  143. EPD_2IN13B_V4_SendData(0x80);
  144. EPD_2IN13B_V4_SendCommand(0x21); // Display update control
  145. EPD_2IN13B_V4_SendData(0x80);
  146. EPD_2IN13B_V4_SendData(0x80);
  147. EPD_2IN13B_V4_ReadBusy();
  148. }
  149. /******************************************************************************
  150. function : Clear screen
  151. parameter:
  152. ******************************************************************************/
  153. void EPD_2IN13B_V4_Clear(void)
  154. {
  155. UWORD Width, Height;
  156. Width = (EPD_2IN13B_V4_WIDTH % 8 == 0)? (EPD_2IN13B_V4_WIDTH / 8 ): (EPD_2IN13B_V4_WIDTH / 8 + 1);
  157. Height = EPD_2IN13B_V4_HEIGHT;
  158. EPD_2IN13B_V4_SendCommand(0x24);
  159. for (UWORD j = 0; j < Height; j++) {
  160. for (UWORD i = 0; i < Width; i++) {
  161. EPD_2IN13B_V4_SendData(0XFF);
  162. }
  163. }
  164. EPD_2IN13B_V4_SendCommand(0x26);
  165. for (UWORD j = 0; j < Height; j++) {
  166. for (UWORD i = 0; i < Width; i++) {
  167. EPD_2IN13B_V4_SendData(0XFF);
  168. }
  169. }
  170. EPD_2IN13B_V4_TurnOnDisplay();
  171. }
  172. /******************************************************************************
  173. function : Sends the image buffer in RAM to e-Paper and displays
  174. parameter:
  175. ******************************************************************************/
  176. void EPD_2IN13B_V4_Display(const UBYTE *blackImage, const UBYTE *redImage)
  177. {
  178. UWORD Width, Height;
  179. Width = (EPD_2IN13B_V4_WIDTH % 8 == 0)? (EPD_2IN13B_V4_WIDTH / 8 ): (EPD_2IN13B_V4_WIDTH / 8 + 1);
  180. Height = EPD_2IN13B_V4_HEIGHT;
  181. EPD_2IN13B_V4_SendCommand(0x24);
  182. for (UWORD j = 0; j < Height; j++) {
  183. for (UWORD i = 0; i < Width; i++) {
  184. EPD_2IN13B_V4_SendData(blackImage[i + j * Width]);
  185. }
  186. }
  187. EPD_2IN13B_V4_SendCommand(0x26);
  188. for (UWORD j = 0; j < Height; j++) {
  189. for (UWORD i = 0; i < Width; i++) {
  190. EPD_2IN13B_V4_SendData(redImage[i + j * Width]);
  191. }
  192. }
  193. EPD_2IN13B_V4_TurnOnDisplay();
  194. }
  195. /******************************************************************************
  196. function : Enter sleep mode
  197. parameter:
  198. ******************************************************************************/
  199. void EPD_2IN13B_V4_Sleep(void)
  200. {
  201. EPD_2IN13B_V4_SendCommand(0x10); //enter deep sleep
  202. EPD_2IN13B_V4_SendData(0x01);
  203. DEV_Delay_ms(100);
  204. }