EPD_1in54_V2.c 12 KB

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