EPD_2in66.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  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-29
  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. void EPD_2IN66_ReadBusy(void)
  92. {
  93. EPD_Busy_WaitUntil(0,0);
  94. // unsigned char count = 100;
  95. // Debug("e-Paper busy\r\n");
  96. // DEV_Delay_ms(100);
  97. // while(DEV_Digital_Read(EPD_BUSY_PIN) == 1) { //LOW: idle, HIGH: busy
  98. // if(!(count--))
  99. // {
  100. // Debug("error: e-Paper busy timeout!!!\r\n");
  101. // break;
  102. // }
  103. // else
  104. // DEV_Delay_ms(100);
  105. // }
  106. // DEV_Delay_ms(100);
  107. // Debug("e-Paper busy release\r\n");
  108. }
  109. /******************************************************************************
  110. function : Turn On Display
  111. parameter:
  112. ******************************************************************************/
  113. static void EPD_2IN66_TurnOnDisplay(void)
  114. {
  115. EPD_2IN66_SendCommand(0x20);
  116. EPD_2IN66_ReadBusy();
  117. }
  118. /******************************************************************************
  119. function : Send LUT
  120. parameter:
  121. ******************************************************************************/
  122. static void EPD_2IN66_SetLUA(void)
  123. {
  124. UWORD count;
  125. EPD_2IN66_SendCommand(0x32);
  126. for(count=0;count<153;count++){
  127. EPD_2IN66_SendData(WF_PARTIAL[count]);
  128. }
  129. EPD_2IN66_ReadBusy();
  130. }
  131. /******************************************************************************
  132. function : Initialize the e-Paper register
  133. parameter:
  134. ******************************************************************************/
  135. void EPD_2IN66_Init(UBYTE mode)
  136. {
  137. EPD_2IN66_Reset();
  138. EPD_2IN66_ReadBusy();
  139. EPD_2IN66_SendCommand(0x12);//soft reset
  140. EPD_2IN66_ReadBusy();
  141. /* Y increment, X increment */
  142. EPD_2IN66_SendCommand(0x11);
  143. EPD_2IN66_SendData(0x03);
  144. /* Set RamX-address Start/End position */
  145. EPD_2IN66_SendCommand(0x44);
  146. EPD_2IN66_SendData(0x01);
  147. EPD_2IN66_SendData((EPD_2IN66_WIDTH % 8 == 0)? (EPD_2IN66_WIDTH / 8 ): (EPD_2IN66_WIDTH / 8 + 1) );
  148. /* Set RamY-address Start/End position */
  149. EPD_2IN66_SendCommand(0x45);
  150. EPD_2IN66_SendData(0);
  151. EPD_2IN66_SendData(0);
  152. EPD_2IN66_SendData((EPD_2IN66_HEIGHT&0xff));
  153. EPD_2IN66_SendData((EPD_2IN66_HEIGHT&0x100)>>8);
  154. EPD_2IN66_ReadBusy();
  155. }
  156. /******************************************************************************
  157. function : Initialize the e-Paper register(Partial display)
  158. parameter:
  159. ******************************************************************************/
  160. void EPD_2IN66_Init_Partial(void)
  161. {
  162. EPD_2IN66_Reset();
  163. EPD_2IN66_ReadBusy();
  164. EPD_2IN66_SendCommand(0x12);//soft reset
  165. EPD_2IN66_ReadBusy();
  166. EPD_2IN66_SetLUA();
  167. EPD_2IN66_SendCommand(0x37);
  168. EPD_2IN66_SendData(0x00);
  169. EPD_2IN66_SendData(0x00);
  170. EPD_2IN66_SendData(0x00);
  171. EPD_2IN66_SendData(0x00);
  172. EPD_2IN66_SendData(0x00);
  173. EPD_2IN66_SendData(0x40);
  174. EPD_2IN66_SendData(0x00);
  175. EPD_2IN66_SendData(0x00);
  176. EPD_2IN66_SendData(0x00);
  177. EPD_2IN66_SendData(0x00);
  178. /* Y increment, X increment */
  179. EPD_2IN66_SendCommand(0x11);
  180. EPD_2IN66_SendData(0x03);
  181. /* Set RamX-address Start/End position */
  182. EPD_2IN66_SendCommand(0x44);
  183. EPD_2IN66_SendData(0x01);
  184. EPD_2IN66_SendData((EPD_2IN66_WIDTH % 8 == 0)? (EPD_2IN66_WIDTH / 8 ): (EPD_2IN66_WIDTH / 8 + 1) );
  185. /* Set RamY-address Start/End position */
  186. EPD_2IN66_SendCommand(0x45);
  187. EPD_2IN66_SendData(0);
  188. EPD_2IN66_SendData(0);
  189. EPD_2IN66_SendData((EPD_2IN66_HEIGHT&0xff));
  190. EPD_2IN66_SendData((EPD_2IN66_HEIGHT&0x100)>>8);
  191. EPD_2IN66_SendCommand(0x3C);
  192. EPD_2IN66_SendData(0x80);
  193. EPD_2IN66_SendCommand(0x22);
  194. EPD_2IN66_SendData(0xcf);
  195. EPD_2IN66_SendCommand(0x20);
  196. EPD_2IN66_ReadBusy();
  197. }
  198. /******************************************************************************
  199. function : Clear screen
  200. parameter:
  201. ******************************************************************************/
  202. void EPD_2IN66_Clear(void)
  203. {
  204. UWORD Width, Height;
  205. Width = (EPD_2IN66_WIDTH % 8 == 0)? (EPD_2IN66_WIDTH / 8 ): (EPD_2IN66_WIDTH / 8 + 1);
  206. Height = EPD_2IN66_HEIGHT;
  207. EPD_2IN66_SendCommand(0x24);
  208. for (UWORD j = 0; j <=Height; j++) {
  209. for (UWORD i = 0; i < Width; i++) {
  210. EPD_2IN66_SendData(0xff);
  211. }
  212. }
  213. EPD_2IN66_TurnOnDisplay();
  214. }
  215. /******************************************************************************
  216. function : Sends the image buffer in RAM to e-Paper and displays
  217. parameter:
  218. ******************************************************************************/
  219. void EPD_2IN66_Display(UBYTE *Image, UBYTE *Image2)
  220. {
  221. UWORD Width, Height;
  222. Width = (EPD_2IN66_WIDTH % 8 == 0)? (EPD_2IN66_WIDTH / 8 ): (EPD_2IN66_WIDTH / 8 + 1);
  223. Height = EPD_2IN66_HEIGHT;
  224. UDOUBLE Addr = 0;
  225. // UDOUBLE Offset = ImageName;
  226. EPD_2IN66_SendCommand(0x24);
  227. for (UWORD j = 0; j <Height; j++) {
  228. for (UWORD i = 0; i <Width; i++) {
  229. Addr = i + j * Width;
  230. EPD_2IN66_SendData(Image[Addr]);
  231. }
  232. }
  233. EPD_2IN66_TurnOnDisplay();
  234. }
  235. /******************************************************************************
  236. function : Enter sleep mode
  237. parameter:
  238. ******************************************************************************/
  239. void EPD_2IN66_Sleep(void)
  240. {
  241. EPD_2IN66_SendCommand(0x10);
  242. EPD_2IN66_SendData(0x01);
  243. //EPD_2IN66_ReadBusy();
  244. }