EPD_2in13_V3.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  1. /*****************************************************************************
  2. * | File : EPD_2in13_V3.c
  3. * | Author : Waveshare team
  4. * | Function : 2.13inch e-paper V3
  5. * | Info :
  6. *----------------
  7. * | This version: V1.1
  8. * | Date : 2021-10-30
  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_2in13_V3.h"
  32. #include "Debug.h"
  33. UBYTE WF_PARTIAL_2IN13_V3[159] =
  34. {
  35. 0x0,0x40,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,
  36. 0x80,0x80,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,
  37. 0x40,0x40,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,
  38. 0x0,0x80,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,
  39. 0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,
  40. 0x14,0x0,0x0,0x0,0x0,0x0,0x0,
  41. 0x1,0x0,0x0,0x0,0x0,0x0,0x0,
  42. 0x1,0x0,0x0,0x0,0x0,0x0,0x0,
  43. 0x0,0x0,0x0,0x0,0x0,0x0,0x0,
  44. 0x0,0x0,0x0,0x0,0x0,0x0,0x0,
  45. 0x0,0x0,0x0,0x0,0x0,0x0,0x0,
  46. 0x0,0x0,0x0,0x0,0x0,0x0,0x0,
  47. 0x0,0x0,0x0,0x0,0x0,0x0,0x0,
  48. 0x0,0x0,0x0,0x0,0x0,0x0,0x0,
  49. 0x0,0x0,0x0,0x0,0x0,0x0,0x0,
  50. 0x0,0x0,0x0,0x0,0x0,0x0,0x0,
  51. 0x0,0x0,0x0,0x0,0x0,0x0,0x0,
  52. 0x22,0x22,0x22,0x22,0x22,0x22,0x0,0x0,0x0,
  53. 0x22,0x17,0x41,0x00,0x32,0x36,
  54. };
  55. UBYTE WS_20_30_2IN13_V3[159] =
  56. {
  57. 0x80, 0x4A, 0x40, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
  58. 0x40, 0x4A, 0x80, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
  59. 0x80, 0x4A, 0x40, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
  60. 0x40, 0x4A, 0x80, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
  61. 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
  62. 0xF, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
  63. 0xF, 0x0, 0x0, 0xF, 0x0, 0x0, 0x2,
  64. 0xF, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
  65. 0x1, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
  66. 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
  67. 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
  68. 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
  69. 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
  70. 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
  71. 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
  72. 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
  73. 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
  74. 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x0, 0x0, 0x0,
  75. 0x22, 0x17, 0x41, 0x0, 0x32, 0x36
  76. };
  77. /******************************************************************************
  78. function : Software reset
  79. parameter:
  80. ******************************************************************************/
  81. static void EPD_2in13_V3_Reset(void)
  82. {
  83. DEV_Digital_Write(EPD_RST_PIN, 1);
  84. DEV_Delay_ms(20);
  85. DEV_Digital_Write(EPD_RST_PIN, 0);
  86. DEV_Delay_ms(2);
  87. DEV_Digital_Write(EPD_RST_PIN, 1);
  88. DEV_Delay_ms(20);
  89. }
  90. /******************************************************************************
  91. function : send command
  92. parameter:
  93. Reg : Command register
  94. ******************************************************************************/
  95. static void EPD_2in13_V3_SendCommand(UBYTE Reg)
  96. {
  97. DEV_Digital_Write(EPD_DC_PIN, 0);
  98. DEV_Digital_Write(EPD_CS_PIN, 0);
  99. DEV_SPI_WriteByte(Reg);
  100. DEV_Digital_Write(EPD_CS_PIN, 1);
  101. }
  102. /******************************************************************************
  103. function : send data
  104. parameter:
  105. Data : Write data
  106. ******************************************************************************/
  107. static void EPD_2in13_V3_SendData(UBYTE Data)
  108. {
  109. DEV_Digital_Write(EPD_DC_PIN, 1);
  110. DEV_Digital_Write(EPD_CS_PIN, 0);
  111. DEV_SPI_WriteByte(Data);
  112. DEV_Digital_Write(EPD_CS_PIN, 1);
  113. }
  114. /******************************************************************************
  115. function : Wait until the busy_pin goes LOW
  116. parameter:
  117. ******************************************************************************/
  118. void EPD_2in13_V3_ReadBusy(void)
  119. {
  120. Debug("e-Paper busy\r\n");
  121. while(1)
  122. { //=1 BUSY
  123. if(DEV_Digital_Read(EPD_BUSY_PIN)==0)
  124. break;
  125. DEV_Delay_ms(10);
  126. }
  127. DEV_Delay_ms(10);
  128. Debug("e-Paper busy release\r\n");
  129. }
  130. /******************************************************************************
  131. function : Turn On Display
  132. parameter:
  133. ******************************************************************************/
  134. static void EPD_2in13_V3_TurnOnDisplay(void)
  135. {
  136. EPD_2in13_V3_SendCommand(0x22); // Display Update Control
  137. EPD_2in13_V3_SendData(0xc7);
  138. EPD_2in13_V3_SendCommand(0x20); // Activate Display Update Sequence
  139. EPD_2in13_V3_ReadBusy();
  140. }
  141. /******************************************************************************
  142. function : Turn On Display
  143. parameter:
  144. ******************************************************************************/
  145. static void EPD_2in13_V3_TurnOnDisplay_Partial(void)
  146. {
  147. EPD_2in13_V3_SendCommand(0x22); // Display Update Control
  148. EPD_2in13_V3_SendData(0x0f); // fast:0x0c, quality:0x0f, 0xcf
  149. EPD_2in13_V3_SendCommand(0x20); // Activate Display Update Sequence
  150. EPD_2in13_V3_ReadBusy();
  151. }
  152. /******************************************************************************
  153. function : Set lut
  154. parameter:
  155. lut : lut data
  156. ******************************************************************************/
  157. static void EPD_2IN13_V3_LUT(UBYTE *lut)
  158. {
  159. UBYTE count;
  160. EPD_2in13_V3_SendCommand(0x32);
  161. for(count=0; count<153; count++)
  162. EPD_2in13_V3_SendData(lut[count]);
  163. EPD_2in13_V3_ReadBusy();
  164. }
  165. /******************************************************************************
  166. function : Send lut data and configuration
  167. parameter:
  168. lut : lut data
  169. ******************************************************************************/
  170. static void EPD_2IN13_V2_LUT_by_host(UBYTE *lut)
  171. {
  172. EPD_2IN13_V3_LUT((UBYTE *)lut); //lut
  173. EPD_2in13_V3_SendCommand(0x3f);
  174. EPD_2in13_V3_SendData(*(lut+153));
  175. EPD_2in13_V3_SendCommand(0x03); // gate voltage
  176. EPD_2in13_V3_SendData(*(lut+154));
  177. EPD_2in13_V3_SendCommand(0x04); // source voltage
  178. EPD_2in13_V3_SendData(*(lut+155)); // VSH
  179. EPD_2in13_V3_SendData(*(lut+156)); // VSH2
  180. EPD_2in13_V3_SendData(*(lut+157)); // VSL
  181. EPD_2in13_V3_SendCommand(0x2c); // VCOM
  182. EPD_2in13_V3_SendData(*(lut+158));
  183. }
  184. /******************************************************************************
  185. function : Setting the display window
  186. parameter:
  187. Xstart : X-axis starting position
  188. Ystart : Y-axis starting position
  189. Xend : End position of X-axis
  190. Yend : End position of Y-axis
  191. ******************************************************************************/
  192. static void EPD_2in13_V3_SetWindows(UWORD Xstart, UWORD Ystart, UWORD Xend, UWORD Yend)
  193. {
  194. EPD_2in13_V3_SendCommand(0x44); // SET_RAM_X_ADDRESS_START_END_POSITION
  195. EPD_2in13_V3_SendData((Xstart>>3) & 0xFF);
  196. EPD_2in13_V3_SendData((Xend>>3) & 0xFF);
  197. EPD_2in13_V3_SendCommand(0x45); // SET_RAM_Y_ADDRESS_START_END_POSITION
  198. EPD_2in13_V3_SendData(Ystart & 0xFF);
  199. EPD_2in13_V3_SendData((Ystart >> 8) & 0xFF);
  200. EPD_2in13_V3_SendData(Yend & 0xFF);
  201. EPD_2in13_V3_SendData((Yend >> 8) & 0xFF);
  202. }
  203. /******************************************************************************
  204. function : Set Cursor
  205. parameter:
  206. Xstart : X-axis starting position
  207. Ystart : Y-axis starting position
  208. ******************************************************************************/
  209. static void EPD_2in13_V3_SetCursor(UWORD Xstart, UWORD Ystart)
  210. {
  211. EPD_2in13_V3_SendCommand(0x4E); // SET_RAM_X_ADDRESS_COUNTER
  212. EPD_2in13_V3_SendData(Xstart & 0xFF);
  213. EPD_2in13_V3_SendCommand(0x4F); // SET_RAM_Y_ADDRESS_COUNTER
  214. EPD_2in13_V3_SendData(Ystart & 0xFF);
  215. EPD_2in13_V3_SendData((Ystart >> 8) & 0xFF);
  216. }
  217. /******************************************************************************
  218. function : Initialize the e-Paper register
  219. parameter:
  220. ******************************************************************************/
  221. void EPD_2in13_V3_Init(void)
  222. {
  223. EPD_2in13_V3_Reset();
  224. DEV_Delay_ms(100);
  225. EPD_2in13_V3_ReadBusy();
  226. EPD_2in13_V3_SendCommand(0x12); //SWRESET
  227. EPD_2in13_V3_ReadBusy();
  228. EPD_2in13_V3_SendCommand(0x01); //Driver output control
  229. EPD_2in13_V3_SendData(0xf9);
  230. EPD_2in13_V3_SendData(0x00);
  231. EPD_2in13_V3_SendData(0x00);
  232. EPD_2in13_V3_SendCommand(0x11); //data entry mode
  233. EPD_2in13_V3_SendData(0x03);
  234. EPD_2in13_V3_SetWindows(0, 0, EPD_2in13_V3_WIDTH-1, EPD_2in13_V3_HEIGHT-1);
  235. EPD_2in13_V3_SetCursor(0, 0);
  236. EPD_2in13_V3_SendCommand(0x3C); //BorderWavefrom
  237. EPD_2in13_V3_SendData(0x05);
  238. EPD_2in13_V3_SendCommand(0x21); // Display update control
  239. EPD_2in13_V3_SendData(0x00);
  240. EPD_2in13_V3_SendData(0x80);
  241. EPD_2in13_V3_SendCommand(0x18); //Read built-in temperature sensor
  242. EPD_2in13_V3_SendData(0x80);
  243. EPD_2in13_V3_ReadBusy();
  244. EPD_2IN13_V2_LUT_by_host(WS_20_30_2IN13_V3);
  245. }
  246. /******************************************************************************
  247. function : Clear screen
  248. parameter:
  249. ******************************************************************************/
  250. void EPD_2in13_V3_Clear(void)
  251. {
  252. UWORD Width, Height;
  253. Width = (EPD_2in13_V3_WIDTH % 8 == 0)? (EPD_2in13_V3_WIDTH / 8 ): (EPD_2in13_V3_WIDTH / 8 + 1);
  254. Height = EPD_2in13_V3_HEIGHT;
  255. EPD_2in13_V3_SendCommand(0x24);
  256. for (UWORD j = 0; j < Height; j++) {
  257. for (UWORD i = 0; i < Width; i++) {
  258. EPD_2in13_V3_SendData(0XFF);
  259. }
  260. }
  261. EPD_2in13_V3_TurnOnDisplay();
  262. }
  263. /******************************************************************************
  264. function : Sends the image buffer in RAM to e-Paper and displays
  265. parameter:
  266. Image : Image data
  267. ******************************************************************************/
  268. void EPD_2in13_V3_Display(UBYTE *Image)
  269. {
  270. UWORD Width, Height;
  271. Width = (EPD_2in13_V3_WIDTH % 8 == 0)? (EPD_2in13_V3_WIDTH / 8 ): (EPD_2in13_V3_WIDTH / 8 + 1);
  272. Height = EPD_2in13_V3_HEIGHT;
  273. EPD_2in13_V3_SendCommand(0x24);
  274. for (UWORD j = 0; j < Height; j++) {
  275. for (UWORD i = 0; i < Width; i++) {
  276. EPD_2in13_V3_SendData(Image[i + j * Width]);
  277. }
  278. }
  279. EPD_2in13_V3_TurnOnDisplay();
  280. }
  281. /******************************************************************************
  282. function : Refresh a base image
  283. parameter:
  284. Image : Image data
  285. ******************************************************************************/
  286. void EPD_2in13_V3_Display_Base(UBYTE *Image)
  287. {
  288. UWORD Width, Height;
  289. Width = (EPD_2in13_V3_WIDTH % 8 == 0)? (EPD_2in13_V3_WIDTH / 8 ): (EPD_2in13_V3_WIDTH / 8 + 1);
  290. Height = EPD_2in13_V3_HEIGHT;
  291. EPD_2in13_V3_SendCommand(0x24); //Write Black and White image to RAM
  292. for (UWORD j = 0; j < Height; j++) {
  293. for (UWORD i = 0; i < Width; i++) {
  294. EPD_2in13_V3_SendData(Image[i + j * Width]);
  295. }
  296. }
  297. EPD_2in13_V3_SendCommand(0x26); //Write Black and White image to RAM
  298. for (UWORD j = 0; j < Height; j++) {
  299. for (UWORD i = 0; i < Width; i++) {
  300. EPD_2in13_V3_SendData(Image[i + j * Width]);
  301. }
  302. }
  303. EPD_2in13_V3_TurnOnDisplay();
  304. }
  305. /******************************************************************************
  306. function : Sends the image buffer in RAM to e-Paper and partial refresh
  307. parameter:
  308. Image : Image data
  309. ******************************************************************************/
  310. void EPD_2in13_V3_Display_Partial(UBYTE *Image)
  311. {
  312. UWORD Width, Height;
  313. Width = (EPD_2in13_V3_WIDTH % 8 == 0)? (EPD_2in13_V3_WIDTH / 8 ): (EPD_2in13_V3_WIDTH / 8 + 1);
  314. Height = EPD_2in13_V3_HEIGHT;
  315. //Reset
  316. DEV_Digital_Write(EPD_RST_PIN, 0);
  317. DEV_Delay_ms(1);
  318. DEV_Digital_Write(EPD_RST_PIN, 1);
  319. EPD_2IN13_V2_LUT_by_host(WF_PARTIAL_2IN13_V3);
  320. EPD_2in13_V3_SendCommand(0x37);
  321. EPD_2in13_V3_SendData(0x00);
  322. EPD_2in13_V3_SendData(0x00);
  323. EPD_2in13_V3_SendData(0x00);
  324. EPD_2in13_V3_SendData(0x00);
  325. EPD_2in13_V3_SendData(0x00);
  326. EPD_2in13_V3_SendData(0x40); ///RAM Ping-Pong enable
  327. EPD_2in13_V3_SendData(0x00);
  328. EPD_2in13_V3_SendData(0x00);
  329. EPD_2in13_V3_SendData(0x00);
  330. EPD_2in13_V3_SendData(0x00);
  331. EPD_2in13_V3_SendCommand(0x3C); //BorderWavefrom
  332. EPD_2in13_V3_SendData(0x80);
  333. EPD_2in13_V3_SendCommand(0x22); //Display Update Sequence Option
  334. EPD_2in13_V3_SendData(0xC0); // Enable clock and Enable analog
  335. EPD_2in13_V3_SendCommand(0x20); //Activate Display Update Sequence
  336. EPD_2in13_V3_ReadBusy();
  337. EPD_2in13_V3_SetWindows(0, 0, EPD_2in13_V3_WIDTH-1, EPD_2in13_V3_HEIGHT-1);
  338. EPD_2in13_V3_SetCursor(0, 0);
  339. EPD_2in13_V3_SendCommand(0x24); //Write Black and White image to RAM
  340. for (UWORD j = 0; j < Height; j++) {
  341. for (UWORD i = 0; i < Width; i++) {
  342. EPD_2in13_V3_SendData(Image[i + j * Width]);
  343. }
  344. }
  345. EPD_2in13_V3_TurnOnDisplay_Partial();
  346. }
  347. /******************************************************************************
  348. function : Enter sleep mode
  349. parameter:
  350. ******************************************************************************/
  351. void EPD_2in13_V3_Sleep(void)
  352. {
  353. EPD_2in13_V3_SendCommand(0x10); //enter deep sleep
  354. EPD_2in13_V3_SendData(0x01);
  355. DEV_Delay_ms(100);
  356. }