EPD_2in66.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. /*****************************************************************************
  2. * | File : EPD_2in66.c
  3. * | Author : Waveshare team
  4. * | Function : 2.66inch e-paper
  5. * | Info :
  6. *----------------
  7. * | This version: V1.0
  8. * | Date : 2020-07-21
  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_2in66.h"
  31. #include "Debug.h"
  32. const unsigned char WF_PARTIAL[159] ={
  33. 0x00,0x40,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  34. 0x00,0x00,0x80,0x80,0x00,0x00,0x00,0x00,0x00,0x00,
  35. 0x00,0x00,0x00,0x00,0x40,0x40,0x00,0x00,0x00,0x00,
  36. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x80,0x00,0x00,
  37. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  38. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  39. 0x0A,0x00,0x00,0x00,0x00,0x00,0x02,0x01,0x00,0x00,
  40. 0x00,0x00,0x00,0x00,0x01,0x00,0x00,0x00,0x00,0x00,
  41. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  42. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  43. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  44. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  45. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  46. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  47. 0x00,0x00,0x00,0x00,0x22,0x22,0x22,0x22,0x22,0x22,
  48. 0x00,0x00,0x00,0x22,0x17,0x41,0xB0,0x32,0x36,
  49. };
  50. /******************************************************************************
  51. function : Software reset
  52. parameter:
  53. ******************************************************************************/
  54. static void EPD_2IN66_Reset(void)
  55. {
  56. DEV_Digital_Write(EPD_RST_PIN, 1);
  57. DEV_Delay_ms(200);
  58. DEV_Digital_Write(EPD_RST_PIN, 0);
  59. DEV_Delay_ms(2);
  60. DEV_Digital_Write(EPD_RST_PIN, 1);
  61. DEV_Delay_ms(200);
  62. }
  63. /******************************************************************************
  64. function : send command
  65. parameter:
  66. Reg : Command register
  67. ******************************************************************************/
  68. static void EPD_2IN66_SendCommand(UBYTE Reg)
  69. {
  70. DEV_Digital_Write(EPD_DC_PIN, 0);
  71. DEV_Digital_Write(EPD_CS_PIN, 0);
  72. DEV_SPI_WriteByte(Reg);
  73. DEV_Digital_Write(EPD_CS_PIN, 1);
  74. }
  75. /******************************************************************************
  76. function : send data
  77. parameter:
  78. Data : Write data
  79. ******************************************************************************/
  80. static void EPD_2IN66_SendData(UBYTE Data)
  81. {
  82. DEV_Digital_Write(EPD_DC_PIN, 1);
  83. DEV_Digital_Write(EPD_CS_PIN, 0);
  84. DEV_SPI_WriteByte(Data);
  85. DEV_Digital_Write(EPD_CS_PIN, 1);
  86. }
  87. /******************************************************************************
  88. function : Wait until the busy_pin goes LOW
  89. parameter:
  90. ******************************************************************************/
  91. static void EPD_2IN66_ReadBusy(void)
  92. {
  93. Debug("e-Paper busy\r\n");
  94. DEV_Delay_ms(20);
  95. while(DEV_Digital_Read(EPD_BUSY_PIN) == 1) { //LOW: idle, HIGH: busy
  96. DEV_Delay_ms(5);
  97. }
  98. DEV_Delay_ms(10);
  99. Debug("e-Paper busy release\r\n");
  100. }
  101. /******************************************************************************
  102. function : Send LUT
  103. parameter:
  104. ******************************************************************************/
  105. static void EPD_2IN66_SetLUA(void)
  106. {
  107. UWORD count;
  108. EPD_2IN66_SendCommand(0x32);
  109. for(count=0;count<153;count++){
  110. EPD_2IN66_SendData(WF_PARTIAL[count]);
  111. }
  112. EPD_2IN66_ReadBusy();
  113. }
  114. /******************************************************************************
  115. function : Turn On Display
  116. parameter:
  117. ******************************************************************************/
  118. static void EPD_2IN66_TurnOnDisplay(void)
  119. {
  120. EPD_2IN66_SendCommand(0x20);
  121. EPD_2IN66_ReadBusy();
  122. }
  123. /******************************************************************************
  124. function : Initialize the e-Paper register
  125. parameter:
  126. ******************************************************************************/
  127. void EPD_2IN66_Init(void)
  128. {
  129. EPD_2IN66_Reset();
  130. EPD_2IN66_ReadBusy();
  131. EPD_2IN66_SendCommand(0x12);//soft reset
  132. EPD_2IN66_ReadBusy();
  133. /* Y increment, X increment */
  134. EPD_2IN66_SendCommand(0x11);
  135. EPD_2IN66_SendData(0x03);
  136. /* Set RamX-address Start/End position */
  137. EPD_2IN66_SendCommand(0x44);
  138. EPD_2IN66_SendData(0x01);
  139. EPD_2IN66_SendData((EPD_2IN66_WIDTH % 8 == 0)? (EPD_2IN66_WIDTH / 8 ): (EPD_2IN66_WIDTH / 8 + 1) );
  140. /* Set RamY-address Start/End position */
  141. EPD_2IN66_SendCommand(0x45);
  142. EPD_2IN66_SendData(0);
  143. EPD_2IN66_SendData(0);
  144. EPD_2IN66_SendData((EPD_2IN66_HEIGHT&0xff));
  145. EPD_2IN66_SendData((EPD_2IN66_HEIGHT&0x100)>>8);
  146. EPD_2IN66_SendCommand(0x3C); // Border 设置 黑白一般设置为跟随白波形即 0x01 Border setting
  147. EPD_2IN66_SendData(0x01);
  148. }
  149. /******************************************************************************
  150. function : Initialize the e-Paper register (Partial display)
  151. parameter:
  152. ******************************************************************************/
  153. void EPD_2IN66_Init_partial(void)
  154. {
  155. EPD_2IN66_Reset();
  156. EPD_2IN66_ReadBusy();
  157. EPD_2IN66_SendCommand(0x12);//soft reset
  158. EPD_2IN66_ReadBusy();
  159. EPD_2IN66_SetLUA();
  160. EPD_2IN66_SendCommand(0x37);
  161. EPD_2IN66_SendData(0x00);
  162. EPD_2IN66_SendData(0x00);
  163. EPD_2IN66_SendData(0x00);
  164. EPD_2IN66_SendData(0x00);
  165. EPD_2IN66_SendData(0x00);
  166. EPD_2IN66_SendData(0x40);
  167. EPD_2IN66_SendData(0x00);
  168. EPD_2IN66_SendData(0x00);
  169. EPD_2IN66_SendData(0x00);
  170. EPD_2IN66_SendData(0x00);
  171. /* Y increment, X increment */
  172. EPD_2IN66_SendCommand(0x11);
  173. EPD_2IN66_SendData(0x03);
  174. /* Set RamX-address Start/End position */
  175. EPD_2IN66_SendCommand(0x44);
  176. EPD_2IN66_SendData(0x01);
  177. EPD_2IN66_SendData((EPD_2IN66_WIDTH % 8 == 0)? (EPD_2IN66_WIDTH / 8 ): (EPD_2IN66_WIDTH / 8 + 1) );
  178. /* Set RamY-address Start/End position */
  179. EPD_2IN66_SendCommand(0x45);
  180. EPD_2IN66_SendData(0);
  181. EPD_2IN66_SendData(0);
  182. EPD_2IN66_SendData((EPD_2IN66_HEIGHT&0xff));
  183. EPD_2IN66_SendData((EPD_2IN66_HEIGHT&0x100)>>8);
  184. EPD_2IN66_SendCommand(0x3C);
  185. EPD_2IN66_SendData(0x80);
  186. EPD_2IN66_SendCommand(0x22);
  187. EPD_2IN66_SendData(0xcf);
  188. EPD_2IN66_SendCommand(0x20);
  189. EPD_2IN66_ReadBusy();
  190. }
  191. /******************************************************************************
  192. function : Sends the image buffer in RAM to e-Paper and displays
  193. parameter:
  194. ******************************************************************************/
  195. void EPD_2IN66_Display(UBYTE *Image)
  196. {
  197. UWORD Width, Height;
  198. Width = (EPD_2IN66_WIDTH % 8 == 0)? (EPD_2IN66_WIDTH / 8 ): (EPD_2IN66_WIDTH / 8 + 1);
  199. Height = EPD_2IN66_HEIGHT;
  200. EPD_2IN66_SendCommand(0x4E);
  201. EPD_2IN66_SendData(0x01);
  202. EPD_2IN66_SendCommand(0x4F);
  203. EPD_2IN66_SendData(0x27);
  204. EPD_2IN66_SendData(0x01);
  205. EPD_2IN66_ReadBusy();
  206. EPD_2IN66_SendCommand(0x24);
  207. for (UWORD j = 0; j < Height; j++) {
  208. for (UWORD i = 0; i < Width; i++) {
  209. EPD_2IN66_SendData(Image[i + j * Width]);
  210. }
  211. }
  212. EPD_2IN66_TurnOnDisplay();
  213. }
  214. /******************************************************************************
  215. function : Clear screen
  216. parameter:
  217. ******************************************************************************/
  218. void EPD_2IN66_Clear(void)
  219. {
  220. UWORD Width, Height;
  221. Width = (EPD_2IN66_WIDTH % 8 == 0)? (EPD_2IN66_WIDTH / 8 ): (EPD_2IN66_WIDTH / 8 + 1);
  222. Height = EPD_2IN66_HEIGHT;
  223. EPD_2IN66_SendCommand(0x4E);
  224. EPD_2IN66_SendData(0x01);
  225. EPD_2IN66_SendCommand(0x4F);
  226. EPD_2IN66_SendData(0x27);
  227. EPD_2IN66_SendData(0x01);
  228. EPD_2IN66_ReadBusy();
  229. EPD_2IN66_SendCommand(0x24);
  230. for (UWORD j = 0; j < Height; j++) {
  231. for (UWORD i = 0; i < Width; i++) {
  232. EPD_2IN66_SendData(0xff);
  233. }
  234. }
  235. EPD_2IN66_SendCommand(0x26);
  236. for (UWORD j = 0; j < Height; j++) {
  237. for (UWORD i = 0; i < Width; i++) {
  238. EPD_2IN66_SendData(0xff);
  239. }
  240. }
  241. EPD_2IN66_TurnOnDisplay();
  242. }
  243. /******************************************************************************
  244. function : Enter sleep mode
  245. parameter:
  246. ******************************************************************************/
  247. void EPD_2IN66_Sleep(void)
  248. {
  249. EPD_2IN66_SendCommand(0x10);
  250. EPD_2IN66_SendData(0x01);
  251. }