EPD_1in54_V2.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. /*****************************************************************************
  2. * | File : EPD_1in54_V2.c
  3. * | Author : Waveshare team
  4. * | Function : 1.54inch e-paper V2
  5. * | Info :
  6. *----------------
  7. * | This version: V1.0
  8. * | Date : 2019-06-11
  9. * | Info :
  10. #
  11. # Permission is hereby granted, free of charge, to any person obtaining a copy
  12. # of this software and associated documnetation files (the "Software"), to deal
  13. # in the Software without restriction, including without limitation the rights
  14. # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  15. # copies of the Software, and to permit persons to whom the Software is
  16. # furished to do so, subject to the following conditions:
  17. #
  18. # The above copyright notice and this permission notice shall be included in
  19. # all copies or substantial portions of the Software.
  20. #
  21. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  22. # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  23. # FITNESS OR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  24. # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  25. # LIABILITY WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  26. # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  27. # THE SOFTWARE.
  28. #
  29. ******************************************************************************/
  30. #include "EPD_1in54_V2.h"
  31. #include "Debug.h"
  32. /******************************************************************************
  33. function : Software reset
  34. parameter:
  35. ******************************************************************************/
  36. static void EPD_1IN54_V2_Reset(void)
  37. {
  38. DEV_Digital_Write(EPD_RST_PIN, 1);
  39. DEV_Delay_ms(200);
  40. DEV_Digital_Write(EPD_RST_PIN, 0);
  41. DEV_Delay_ms(2);
  42. DEV_Digital_Write(EPD_RST_PIN, 1);
  43. DEV_Delay_ms(200);
  44. }
  45. /******************************************************************************
  46. function : send command
  47. parameter:
  48. Reg : Command register
  49. ******************************************************************************/
  50. static void EPD_1IN54_V2_SendCommand(UBYTE Reg)
  51. {
  52. DEV_Digital_Write(EPD_DC_PIN, 0);
  53. DEV_Digital_Write(EPD_CS_PIN, 0);
  54. DEV_SPI_WriteByte(Reg);
  55. DEV_Digital_Write(EPD_CS_PIN, 1);
  56. }
  57. /******************************************************************************
  58. function : send data
  59. parameter:
  60. Data : Write data
  61. ******************************************************************************/
  62. static void EPD_1IN54_V2_SendData(UBYTE Data)
  63. {
  64. DEV_Digital_Write(EPD_DC_PIN, 1);
  65. DEV_Digital_Write(EPD_CS_PIN, 0);
  66. DEV_SPI_WriteByte(Data);
  67. DEV_Digital_Write(EPD_CS_PIN, 1);
  68. }
  69. /******************************************************************************
  70. function : Wait until the busy_pin goes LOW
  71. parameter:
  72. ******************************************************************************/
  73. static void EPD_1IN54_V2_ReadBusy(void)
  74. {
  75. EPD_Busy_WaitUntil(0,0);
  76. // unsigned char count = 100;
  77. // Debug("e-Paper busy\r\n");
  78. // // UBYTE busy;
  79. // // do {
  80. // // EPD_1IN54_V2_SendCommand(0x71);
  81. // // busy = DEV_Digital_Read(EPD_BUSY_PIN);
  82. // // busy = !(busy & 0x01);
  83. // // } while(busy);
  84. // // DEV_Delay_ms(200);
  85. // while(DEV_Digital_Read(EPD_BUSY_PIN) == 1) { //LOW: idle, HIGH: busy
  86. // if(!(count--))
  87. // {
  88. // Debug("error: e-Paper busy timeout!!!\r\n");
  89. // break;
  90. // }
  91. // else
  92. // DEV_Delay_ms(100);
  93. // }
  94. // Debug("e-Paper busy release\r\n");
  95. }
  96. /******************************************************************************
  97. function : Turn On Display full
  98. parameter:
  99. ******************************************************************************/
  100. static void EPD_1IN54_V2_TurnOnDisplay(void)
  101. {
  102. EPD_1IN54_V2_SendCommand(0x22);
  103. EPD_1IN54_V2_SendData(0xF7);
  104. EPD_1IN54_V2_SendCommand(0x20);
  105. EPD_1IN54_V2_ReadBusy();
  106. }
  107. /******************************************************************************
  108. function : Turn On Display part
  109. parameter:
  110. ******************************************************************************/
  111. static void EPD_1IN54_V2_TurnOnDisplayPart(void)
  112. {
  113. EPD_1IN54_V2_SendCommand(0x22);
  114. EPD_1IN54_V2_SendData(0xFF);
  115. EPD_1IN54_V2_SendCommand(0x20);
  116. EPD_1IN54_V2_ReadBusy();
  117. }
  118. /******************************************************************************
  119. function : Initialize the e-Paper register
  120. parameter:
  121. ******************************************************************************/
  122. void EPD_1IN54_V2_Init(UBYTE mode)
  123. {
  124. EPD_1IN54_V2_Reset();
  125. EPD_1IN54_V2_ReadBusy();
  126. EPD_1IN54_V2_SendCommand(0x12); //SWRESET
  127. EPD_1IN54_V2_ReadBusy();
  128. EPD_1IN54_V2_SendCommand(0x01); //Driver output control
  129. EPD_1IN54_V2_SendData(0xC7);
  130. EPD_1IN54_V2_SendData(0x00);
  131. EPD_1IN54_V2_SendData(0x01);
  132. EPD_1IN54_V2_SendCommand(0x11); //data entry mode
  133. EPD_1IN54_V2_SendData(0x01);
  134. EPD_1IN54_V2_SendCommand(0x44); //set Ram-X address start/end position
  135. EPD_1IN54_V2_SendData(0x00);
  136. EPD_1IN54_V2_SendData(0x18); //0x0C-->(18+1)*8=200
  137. EPD_1IN54_V2_SendCommand(0x45); //set Ram-Y address start/end position
  138. EPD_1IN54_V2_SendData(0xC7); //0xC7-->(199+1)=200
  139. EPD_1IN54_V2_SendData(0x00);
  140. EPD_1IN54_V2_SendData(0x00);
  141. EPD_1IN54_V2_SendData(0x00);
  142. EPD_1IN54_V2_SendCommand(0x3C); //BorderWavefrom
  143. EPD_1IN54_V2_SendData(0x01);
  144. EPD_1IN54_V2_SendCommand(0x18);
  145. EPD_1IN54_V2_SendData(0x80);
  146. EPD_1IN54_V2_SendCommand(0x22); // //Load Temperature and waveform setting.
  147. EPD_1IN54_V2_SendData(0XB1);
  148. EPD_1IN54_V2_SendCommand(0x20);
  149. EPD_1IN54_V2_SendCommand(0x4E); // set RAM x address count to 0;
  150. EPD_1IN54_V2_SendData(0x00);
  151. EPD_1IN54_V2_SendCommand(0x4F); // set RAM y address count to 0X199;
  152. EPD_1IN54_V2_SendData(0xC7);
  153. EPD_1IN54_V2_SendData(0x00);
  154. EPD_1IN54_V2_ReadBusy();
  155. }
  156. /******************************************************************************
  157. function : Clear screen
  158. parameter:
  159. ******************************************************************************/
  160. void EPD_1IN54_V2_Clear(void)
  161. {
  162. UWORD Width, Height;
  163. Width = (EPD_1IN54_V2_WIDTH % 8 == 0)? (EPD_1IN54_V2_WIDTH / 8 ): (EPD_1IN54_V2_WIDTH / 8 + 1);
  164. Height = EPD_1IN54_V2_HEIGHT;
  165. EPD_1IN54_V2_SendCommand(0x24);
  166. for (UWORD j = 0; j < Height; j++) {
  167. for (UWORD i = 0; i < Width; i++) {
  168. EPD_1IN54_V2_SendData(0XFF);
  169. }
  170. }
  171. EPD_1IN54_V2_TurnOnDisplay();
  172. }
  173. /******************************************************************************
  174. function : Sends the image buffer in RAM to e-Paper and displays
  175. parameter:
  176. ******************************************************************************/
  177. void EPD_1IN54_V2_Display(UBYTE *Image, UBYTE *Image2)
  178. {
  179. UWORD Width, Height;
  180. Width = (EPD_1IN54_V2_WIDTH % 8 == 0)? (EPD_1IN54_V2_WIDTH / 8 ): (EPD_1IN54_V2_WIDTH / 8 + 1);
  181. Height = EPD_1IN54_V2_HEIGHT;
  182. UDOUBLE Addr = 0;
  183. EPD_1IN54_V2_SendCommand(0x24);
  184. for (UWORD j = 0; j < Height; j++) {
  185. for (UWORD i = 0; i < Width; i++) {
  186. Addr = i + j * Width;
  187. EPD_1IN54_V2_SendData(Image[Addr]);
  188. }
  189. }
  190. EPD_1IN54_V2_TurnOnDisplay();
  191. }
  192. /******************************************************************************
  193. function : The image of the previous frame must be uploaded, otherwise the
  194. first few seconds will display an exception.
  195. parameter:
  196. ******************************************************************************/
  197. void EPD_1IN54_V2_DisplayPartBaseImage(UBYTE *Image)
  198. {
  199. UWORD Width, Height;
  200. Width = (EPD_1IN54_V2_WIDTH % 8 == 0)? (EPD_1IN54_V2_WIDTH / 8 ): (EPD_1IN54_V2_WIDTH / 8 + 1);
  201. Height = EPD_1IN54_V2_HEIGHT;
  202. UDOUBLE Addr = 0;
  203. EPD_1IN54_V2_SendCommand(0x24);
  204. for (UWORD j = 0; j < Height; j++) {
  205. for (UWORD i = 0; i < Width; i++) {
  206. Addr = i + j * Width;
  207. EPD_1IN54_V2_SendData(Image[Addr]);
  208. }
  209. }
  210. EPD_1IN54_V2_SendCommand(0x26);
  211. for (UWORD j = 0; j < Height; j++) {
  212. for (UWORD i = 0; i < Width; i++) {
  213. Addr = i + j * Width;
  214. EPD_1IN54_V2_SendData(Image[Addr]);
  215. }
  216. }
  217. EPD_1IN54_V2_TurnOnDisplayPart();
  218. }
  219. /******************************************************************************
  220. function : Sends the image buffer in RAM to e-Paper and displays
  221. parameter:
  222. ******************************************************************************/
  223. void EPD_1IN54_V2_DisplayPart(UBYTE *Image)
  224. {
  225. UWORD Width, Height;
  226. Width = (EPD_1IN54_V2_WIDTH % 8 == 0)? (EPD_1IN54_V2_WIDTH / 8 ): (EPD_1IN54_V2_WIDTH / 8 + 1);
  227. Height = EPD_1IN54_V2_HEIGHT;
  228. DEV_Digital_Write(EPD_RST_PIN, 0);
  229. DEV_Delay_ms(10);
  230. DEV_Digital_Write(EPD_RST_PIN, 1);
  231. DEV_Delay_ms(10);
  232. EPD_1IN54_V2_SendCommand(0x3C); //BorderWavefrom
  233. EPD_1IN54_V2_SendData(0x80);
  234. UDOUBLE Addr = 0;
  235. EPD_1IN54_V2_SendCommand(0x24);
  236. for (UWORD j = 0; j < Height; j++) {
  237. for (UWORD i = 0; i < Width; i++) {
  238. Addr = i + j * Width;
  239. EPD_1IN54_V2_SendData(Image[Addr]);
  240. }
  241. }
  242. EPD_1IN54_V2_TurnOnDisplayPart();
  243. }
  244. /******************************************************************************
  245. function : Enter sleep mode
  246. parameter:
  247. ******************************************************************************/
  248. void EPD_1IN54_V2_Sleep(void)
  249. {
  250. EPD_1IN54_V2_SendCommand(0x10); //enter deep sleep
  251. EPD_1IN54_V2_SendData(0x01);
  252. DEV_Delay_ms(100);
  253. }