EPD_1in54_V2.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  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. // waveform full refresh
  33. unsigned char WF_Full_1IN54[159] =
  34. {
  35. 0x80, 0x48, 0x40, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
  36. 0x40, 0x48, 0x80, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
  37. 0x80, 0x48, 0x40, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
  38. 0x40, 0x48, 0x80, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
  39. 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
  40. 0xA, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
  41. 0x8, 0x1, 0x0, 0x8, 0x1, 0x0, 0x2,
  42. 0xA, 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, 0x0, 0x32, 0x20
  54. };
  55. // waveform partial refresh(fast)
  56. unsigned char WF_PARTIAL_1IN54_0[159] =
  57. {
  58. 0x0,0x40,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,
  59. 0x80,0x80,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,
  60. 0x40,0x40,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,
  61. 0x0,0x80,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,
  62. 0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,
  63. 0xF,0x0,0x0,0x0,0x0,0x0,0x0,
  64. 0x1,0x1,0x0,0x0,0x0,0x0,0x0,
  65. 0x0,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. 0x0,0x0,0x0,0x0,0x0,0x0,0x0,
  75. 0x22,0x22,0x22,0x22,0x22,0x22,0x0,0x0,0x0,
  76. 0x02,0x17,0x41,0xB0,0x32,0x28,
  77. };
  78. // waveform partial refresh(quality)
  79. unsigned char WF_PARTIAL_1IN54_1[159] =
  80. {
  81. 0x0,0x00,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,
  82. 0x80,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,
  83. 0x40,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,
  84. 0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,
  85. 0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,
  86. 0xA,0x0,0x0,0x0,0x0,0x0,0x1,
  87. 0x1,0x0,0x0,0x0,0x0,0x0,0x0,
  88. 0x0,0x0,0x0,0x0,0x0,0x0,0x0,
  89. 0x0,0x0,0x0,0x0,0x0,0x0,0x0,
  90. 0x0,0x0,0x0,0x0,0x0,0x0,0x0,
  91. 0x0,0x0,0x0,0x0,0x0,0x0,0x0,
  92. 0x0,0x0,0x0,0x0,0x0,0x0,0x0,
  93. 0x0,0x0,0x0,0x0,0x0,0x0,0x0,
  94. 0x0,0x0,0x0,0x0,0x0,0x0,0x0,
  95. 0x0,0x0,0x0,0x0,0x0,0x0,0x0,
  96. 0x0,0x0,0x0,0x0,0x0,0x0,0x0,
  97. 0x0,0x0,0x0,0x0,0x0,0x0,0x0,
  98. 0x22,0x22,0x22,0x22,0x22,0x22,0x0,0x0,0x0,
  99. 0x22,0x17,0x41,0x0,0x32,0x20,
  100. };
  101. /******************************************************************************
  102. function : Software reset
  103. parameter:
  104. ******************************************************************************/
  105. static void EPD_1IN54_V2_Reset(void)
  106. {
  107. DEV_Digital_Write(EPD_RST_PIN, 1);
  108. DEV_Delay_ms(200);
  109. DEV_Digital_Write(EPD_RST_PIN, 0);
  110. DEV_Delay_ms(2);
  111. DEV_Digital_Write(EPD_RST_PIN, 1);
  112. DEV_Delay_ms(200);
  113. }
  114. /******************************************************************************
  115. function : send command
  116. parameter:
  117. Reg : Command register
  118. ******************************************************************************/
  119. static void EPD_1IN54_V2_SendCommand(UBYTE Reg)
  120. {
  121. DEV_Digital_Write(EPD_DC_PIN, 0);
  122. DEV_Digital_Write(EPD_CS_PIN, 0);
  123. DEV_SPI_WriteByte(Reg);
  124. DEV_Digital_Write(EPD_CS_PIN, 1);
  125. }
  126. /******************************************************************************
  127. function : send data
  128. parameter:
  129. Data : Write data
  130. ******************************************************************************/
  131. static void EPD_1IN54_V2_SendData(UBYTE Data)
  132. {
  133. DEV_Digital_Write(EPD_DC_PIN, 1);
  134. DEV_Digital_Write(EPD_CS_PIN, 0);
  135. DEV_SPI_WriteByte(Data);
  136. DEV_Digital_Write(EPD_CS_PIN, 1);
  137. }
  138. /******************************************************************************
  139. function : Wait until the busy_pin goes LOW
  140. parameter:
  141. ******************************************************************************/
  142. static void EPD_1IN54_V2_ReadBusy(void)
  143. {
  144. Debug("e-Paper busy\r\n");
  145. while(DEV_Digital_Read(EPD_BUSY_PIN) == 1) { //LOW: idle, HIGH: busy
  146. DEV_Delay_ms(10);
  147. }
  148. Debug("e-Paper busy release\r\n");
  149. }
  150. /******************************************************************************
  151. function : Turn On Display full
  152. parameter:
  153. ******************************************************************************/
  154. static void EPD_1IN54_V2_TurnOnDisplay(void)
  155. {
  156. EPD_1IN54_V2_SendCommand(0x22);
  157. EPD_1IN54_V2_SendData(0xc7);
  158. EPD_1IN54_V2_SendCommand(0x20);
  159. EPD_1IN54_V2_ReadBusy();
  160. }
  161. /******************************************************************************
  162. function : Turn On Display part
  163. parameter:
  164. ******************************************************************************/
  165. static void EPD_1IN54_V2_TurnOnDisplayPart(void)
  166. {
  167. EPD_1IN54_V2_SendCommand(0x22);
  168. EPD_1IN54_V2_SendData(0xcF);
  169. EPD_1IN54_V2_SendCommand(0x20);
  170. EPD_1IN54_V2_ReadBusy();
  171. }
  172. static void EPD_1IN54_V2_Lut(UBYTE *lut)
  173. {
  174. EPD_1IN54_V2_SendCommand(0x32);
  175. for(UBYTE i=0; i<153; i++)
  176. EPD_1IN54_V2_SendData(lut[i]);
  177. EPD_1IN54_V2_ReadBusy();
  178. }
  179. static void EPD_1IN54_V2_SetLut(UBYTE *lut)
  180. {
  181. EPD_1IN54_V2_Lut(lut);
  182. EPD_1IN54_V2_SendCommand(0x3f);
  183. EPD_1IN54_V2_SendData(lut[153]);
  184. EPD_1IN54_V2_SendCommand(0x03);
  185. EPD_1IN54_V2_SendData(lut[154]);
  186. EPD_1IN54_V2_SendCommand(0x04);
  187. EPD_1IN54_V2_SendData(lut[155]);
  188. EPD_1IN54_V2_SendData(lut[156]);
  189. EPD_1IN54_V2_SendData(lut[157]);
  190. EPD_1IN54_V2_SendCommand(0x2c);
  191. EPD_1IN54_V2_SendData(lut[158]);
  192. }
  193. static void EPD_1IN54_V2_SetWindows(UWORD Xstart, UWORD Ystart, UWORD Xend, UWORD Yend)
  194. {
  195. EPD_1IN54_V2_SendCommand(0x44); // SET_RAM_X_ADDRESS_START_END_POSITION
  196. EPD_1IN54_V2_SendData((Xstart>>3) & 0xFF);
  197. EPD_1IN54_V2_SendData((Xend>>3) & 0xFF);
  198. EPD_1IN54_V2_SendCommand(0x45); // SET_RAM_Y_ADDRESS_START_END_POSITION
  199. EPD_1IN54_V2_SendData(Ystart & 0xFF);
  200. EPD_1IN54_V2_SendData((Ystart >> 8) & 0xFF);
  201. EPD_1IN54_V2_SendData(Yend & 0xFF);
  202. EPD_1IN54_V2_SendData((Yend >> 8) & 0xFF);
  203. }
  204. static void EPD_1IN54_V2_SetCursor(UWORD Xstart, UWORD Ystart)
  205. {
  206. EPD_1IN54_V2_SendCommand(0x4E); // SET_RAM_X_ADDRESS_COUNTER
  207. EPD_1IN54_V2_SendData(Xstart & 0xFF);
  208. EPD_1IN54_V2_SendCommand(0x4F); // SET_RAM_Y_ADDRESS_COUNTER
  209. EPD_1IN54_V2_SendData(Ystart & 0xFF);
  210. EPD_1IN54_V2_SendData((Ystart >> 8) & 0xFF);
  211. }
  212. /******************************************************************************
  213. function : Initialize the e-Paper register
  214. parameter:
  215. ******************************************************************************/
  216. void EPD_1IN54_V2_Init(void)
  217. {
  218. EPD_1IN54_V2_Reset();
  219. EPD_1IN54_V2_ReadBusy();
  220. EPD_1IN54_V2_SendCommand(0x12); //SWRESET
  221. EPD_1IN54_V2_ReadBusy();
  222. EPD_1IN54_V2_SendCommand(0x01); //Driver output control
  223. EPD_1IN54_V2_SendData(0xC7);
  224. EPD_1IN54_V2_SendData(0x00);
  225. EPD_1IN54_V2_SendData(0x01);
  226. EPD_1IN54_V2_SendCommand(0x11); //data entry mode
  227. EPD_1IN54_V2_SendData(0x01);
  228. EPD_1IN54_V2_SetWindows(0, EPD_1IN54_V2_HEIGHT-1, EPD_1IN54_V2_WIDTH-1, 0);
  229. EPD_1IN54_V2_SendCommand(0x3C); //BorderWavefrom
  230. EPD_1IN54_V2_SendData(0x01);
  231. EPD_1IN54_V2_SendCommand(0x18);
  232. EPD_1IN54_V2_SendData(0x80);
  233. EPD_1IN54_V2_SendCommand(0x22); // //Load Temperature and waveform setting.
  234. EPD_1IN54_V2_SendData(0XB1);
  235. EPD_1IN54_V2_SendCommand(0x20);
  236. EPD_1IN54_V2_SetCursor(0, EPD_1IN54_V2_HEIGHT-1);
  237. EPD_1IN54_V2_ReadBusy();
  238. EPD_1IN54_V2_SetLut(WF_Full_1IN54);
  239. }
  240. /******************************************************************************
  241. function : Initialize the e-Paper register (Partial display)
  242. parameter:
  243. ******************************************************************************/
  244. void EPD_1IN54_V2_Init_Partial(void)
  245. {
  246. EPD_1IN54_V2_Reset();
  247. EPD_1IN54_V2_ReadBusy();
  248. EPD_1IN54_V2_SetLut(WF_PARTIAL_1IN54_1);
  249. EPD_1IN54_V2_SendCommand(0x37);
  250. EPD_1IN54_V2_SendData(0x00);
  251. EPD_1IN54_V2_SendData(0x00);
  252. EPD_1IN54_V2_SendData(0x00);
  253. EPD_1IN54_V2_SendData(0x00);
  254. EPD_1IN54_V2_SendData(0x00);
  255. EPD_1IN54_V2_SendData(0x40);
  256. EPD_1IN54_V2_SendData(0x00);
  257. EPD_1IN54_V2_SendData(0x00);
  258. EPD_1IN54_V2_SendData(0x00);
  259. EPD_1IN54_V2_SendData(0x00);
  260. EPD_1IN54_V2_SendCommand(0x3C); //BorderWavefrom
  261. EPD_1IN54_V2_SendData(0x80);
  262. EPD_1IN54_V2_SendCommand(0x22);
  263. EPD_1IN54_V2_SendData(0xc0);
  264. EPD_1IN54_V2_SendCommand(0x20);
  265. EPD_1IN54_V2_ReadBusy();
  266. }
  267. /******************************************************************************
  268. function : Clear screen
  269. parameter:
  270. ******************************************************************************/
  271. void EPD_1IN54_V2_Clear(void)
  272. {
  273. UWORD Width, Height;
  274. Width = (EPD_1IN54_V2_WIDTH % 8 == 0)? (EPD_1IN54_V2_WIDTH / 8 ): (EPD_1IN54_V2_WIDTH / 8 + 1);
  275. Height = EPD_1IN54_V2_HEIGHT;
  276. EPD_1IN54_V2_SendCommand(0x24);
  277. for (UWORD j = 0; j < Height; j++) {
  278. for (UWORD i = 0; i < Width; i++) {
  279. EPD_1IN54_V2_SendData(0XFF);
  280. }
  281. }
  282. EPD_1IN54_V2_SendCommand(0x26);
  283. for (UWORD j = 0; j < Height; j++) {
  284. for (UWORD i = 0; i < Width; i++) {
  285. EPD_1IN54_V2_SendData(0XFF);
  286. }
  287. }
  288. EPD_1IN54_V2_TurnOnDisplay();
  289. }
  290. /******************************************************************************
  291. function : Sends the image buffer in RAM to e-Paper and displays
  292. parameter:
  293. ******************************************************************************/
  294. void EPD_1IN54_V2_Display(UBYTE *Image)
  295. {
  296. UWORD Width, Height;
  297. Width = (EPD_1IN54_V2_WIDTH % 8 == 0)? (EPD_1IN54_V2_WIDTH / 8 ): (EPD_1IN54_V2_WIDTH / 8 + 1);
  298. Height = EPD_1IN54_V2_HEIGHT;
  299. UDOUBLE Addr = 0;
  300. EPD_1IN54_V2_SendCommand(0x24);
  301. for (UWORD j = 0; j < Height; j++) {
  302. for (UWORD i = 0; i < Width; i++) {
  303. Addr = i + j * Width;
  304. EPD_1IN54_V2_SendData(Image[Addr]);
  305. }
  306. }
  307. EPD_1IN54_V2_TurnOnDisplay();
  308. }
  309. /******************************************************************************
  310. function : The image of the previous frame must be uploaded, otherwise the
  311. first few seconds will display an exception.
  312. parameter:
  313. ******************************************************************************/
  314. void EPD_1IN54_V2_DisplayPartBaseImage(UBYTE *Image)
  315. {
  316. UWORD Width, Height;
  317. Width = (EPD_1IN54_V2_WIDTH % 8 == 0)? (EPD_1IN54_V2_WIDTH / 8 ): (EPD_1IN54_V2_WIDTH / 8 + 1);
  318. Height = EPD_1IN54_V2_HEIGHT;
  319. UDOUBLE Addr = 0;
  320. EPD_1IN54_V2_SendCommand(0x24);
  321. for (UWORD j = 0; j < Height; j++) {
  322. for (UWORD i = 0; i < Width; i++) {
  323. Addr = i + j * Width;
  324. EPD_1IN54_V2_SendData(Image[Addr]);
  325. }
  326. }
  327. EPD_1IN54_V2_SendCommand(0x26);
  328. for (UWORD j = 0; j < Height; j++) {
  329. for (UWORD i = 0; i < Width; i++) {
  330. Addr = i + j * Width;
  331. EPD_1IN54_V2_SendData(Image[Addr]);
  332. }
  333. }
  334. EPD_1IN54_V2_TurnOnDisplayPart();
  335. }
  336. /******************************************************************************
  337. function : Sends the image buffer in RAM to e-Paper and displays
  338. parameter:
  339. ******************************************************************************/
  340. void EPD_1IN54_V2_DisplayPart(UBYTE *Image)
  341. {
  342. UWORD Width, Height;
  343. Width = (EPD_1IN54_V2_WIDTH % 8 == 0)? (EPD_1IN54_V2_WIDTH / 8 ): (EPD_1IN54_V2_WIDTH / 8 + 1);
  344. Height = EPD_1IN54_V2_HEIGHT;
  345. UDOUBLE Addr = 0;
  346. EPD_1IN54_V2_SendCommand(0x24);
  347. for (UWORD j = 0; j < Height; j++) {
  348. for (UWORD i = 0; i < Width; i++) {
  349. Addr = i + j * Width;
  350. EPD_1IN54_V2_SendData(Image[Addr]);
  351. }
  352. }
  353. EPD_1IN54_V2_TurnOnDisplayPart();
  354. }
  355. /******************************************************************************
  356. function : Enter sleep mode
  357. parameter:
  358. ******************************************************************************/
  359. void EPD_1IN54_V2_Sleep(void)
  360. {
  361. EPD_1IN54_V2_SendCommand(0x10); //enter deep sleep
  362. EPD_1IN54_V2_SendData(0x01);
  363. DEV_Delay_ms(100);
  364. }