2
0

EPD_2in13_V2.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  1. /*****************************************************************************
  2. * | File : EPD_2in13_V2.c
  3. * | Author : Waveshare team
  4. * | Function : 2.13inch e-paper V2
  5. * | Info :
  6. *----------------
  7. * | This version: V3.0
  8. * | Date : 2019-06-13
  9. * | Info :
  10. * -----------------------------------------------------------------------------
  11. * V3.0(2019-06-13):
  12. * 1.Change name:
  13. * EPD_Reset() => EPD_2IN13_V2_Reset()
  14. * EPD_SendCommand() => EPD_2IN13_V2_SendCommand()
  15. * EPD_SendData() => EPD_2IN13_V2_SendData()
  16. * EPD_WaitUntilIdle() => EPD_2IN13_V2_ReadBusy()
  17. * EPD_Init() => EPD_2IN13_V2_Init()
  18. * EPD_Clear() => EPD_2IN13_V2_Clear()
  19. * EPD_Display() => EPD_2IN13_V2_Display()
  20. * EPD_Sleep() => EPD_2IN13_V2_Sleep()
  21. * 2.add:
  22. * EPD_2IN13_V2_DisplayPartBaseImage()
  23. * -----------------------------------------------------------------------------
  24. * V2.0(2018-11-14):
  25. * 1.Remove:ImageBuff[EPD_HEIGHT * EPD_WIDTH / 8]
  26. * 2.Change:EPD_2IN13_V2_Display(UBYTE *Image)
  27. * Need to pass parameters: pointer to cached data
  28. * 3.Change:
  29. * EPD_RST -> EPD_RST_PIN
  30. * EPD_DC -> EPD_DC_PIN
  31. * EPD_CS -> EPD_CS_PIN
  32. * EPD_BUSY -> EPD_BUSY_PIN
  33. #
  34. # Permission is hereby granted, free of charge, to any person obtaining a copy
  35. # of this software and associated documnetation files (the "Software"), to deal
  36. # in the Software without restriction, including without limitation the rights
  37. # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  38. # copies of the Software, and to permit persons to whom the Software is
  39. # furished to do so, subject to the following conditions:
  40. #
  41. # The above copyright notice and this permission notice shall be included in
  42. # all copies or substantial portions of the Software.
  43. #
  44. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  45. # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  46. # FITNESS OR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  47. # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  48. # LIABILITY WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  49. # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  50. # THE SOFTWARE.
  51. #
  52. ******************************************************************************/
  53. #include "EPD_2in13_V2.h"
  54. #include "Debug.h"
  55. const unsigned char EPD_2IN13_V2_lut_full_update[]= {
  56. 0x80,0x60,0x40,0x00,0x00,0x00,0x00, //LUT0: BB: VS 0 ~7
  57. 0x10,0x60,0x20,0x00,0x00,0x00,0x00, //LUT1: BW: VS 0 ~7
  58. 0x80,0x60,0x40,0x00,0x00,0x00,0x00, //LUT2: WB: VS 0 ~7
  59. 0x10,0x60,0x20,0x00,0x00,0x00,0x00, //LUT3: WW: VS 0 ~7
  60. 0x00,0x00,0x00,0x00,0x00,0x00,0x00, //LUT4: VCOM: VS 0 ~7
  61. 0x03,0x03,0x00,0x00,0x02, // TP0 A~D RP0
  62. 0x09,0x09,0x00,0x00,0x02, // TP1 A~D RP1
  63. 0x03,0x03,0x00,0x00,0x02, // TP2 A~D RP2
  64. 0x00,0x00,0x00,0x00,0x00, // TP3 A~D RP3
  65. 0x00,0x00,0x00,0x00,0x00, // TP4 A~D RP4
  66. 0x00,0x00,0x00,0x00,0x00, // TP5 A~D RP5
  67. 0x00,0x00,0x00,0x00,0x00, // TP6 A~D RP6
  68. 0x15,0x41,0xA8,0x32,0x30,0x0A,
  69. };
  70. const unsigned char EPD_2IN13_V2_lut_partial_update[]= { //20 bytes
  71. 0x00,0x00,0x00,0x00,0x00,0x00,0x00, //LUT0: BB: VS 0 ~7
  72. 0x80,0x00,0x00,0x00,0x00,0x00,0x00, //LUT1: BW: VS 0 ~7
  73. 0x40,0x00,0x00,0x00,0x00,0x00,0x00, //LUT2: WB: VS 0 ~7
  74. 0x00,0x00,0x00,0x00,0x00,0x00,0x00, //LUT3: WW: VS 0 ~7
  75. 0x00,0x00,0x00,0x00,0x00,0x00,0x00, //LUT4: VCOM: VS 0 ~7
  76. 0x0A,0x00,0x00,0x00,0x00, // TP0 A~D RP0
  77. 0x00,0x00,0x00,0x00,0x00, // TP1 A~D RP1
  78. 0x00,0x00,0x00,0x00,0x00, // TP2 A~D RP2
  79. 0x00,0x00,0x00,0x00,0x00, // TP3 A~D RP3
  80. 0x00,0x00,0x00,0x00,0x00, // TP4 A~D RP4
  81. 0x00,0x00,0x00,0x00,0x00, // TP5 A~D RP5
  82. 0x00,0x00,0x00,0x00,0x00, // TP6 A~D RP6
  83. 0x15,0x41,0xA8,0x32,0x30,0x0A,
  84. };
  85. /******************************************************************************
  86. function : Software reset
  87. parameter:
  88. ******************************************************************************/
  89. static void EPD_2IN13_V2_Reset(void)
  90. {
  91. DEV_Digital_Write(EPD_RST_PIN, 1);
  92. DEV_Delay_ms(200);
  93. DEV_Digital_Write(EPD_RST_PIN, 0);
  94. DEV_Delay_ms(2);
  95. DEV_Digital_Write(EPD_RST_PIN, 1);
  96. DEV_Delay_ms(200);
  97. }
  98. /******************************************************************************
  99. function : send command
  100. parameter:
  101. Reg : Command register
  102. ******************************************************************************/
  103. static void EPD_2IN13_V2_SendCommand(UBYTE Reg)
  104. {
  105. DEV_Digital_Write(EPD_DC_PIN, 0);
  106. DEV_Digital_Write(EPD_CS_PIN, 0);
  107. DEV_SPI_WriteByte(Reg);
  108. DEV_Digital_Write(EPD_CS_PIN, 1);
  109. }
  110. /******************************************************************************
  111. function : send data
  112. parameter:
  113. Data : Write data
  114. ******************************************************************************/
  115. static void EPD_2IN13_V2_SendData(UBYTE Data)
  116. {
  117. DEV_Digital_Write(EPD_DC_PIN, 1);
  118. DEV_Digital_Write(EPD_CS_PIN, 0);
  119. DEV_SPI_WriteByte(Data);
  120. DEV_Digital_Write(EPD_CS_PIN, 1);
  121. }
  122. /******************************************************************************
  123. function : Wait until the busy_pin goes LOW
  124. parameter:
  125. ******************************************************************************/
  126. void EPD_2IN13_V2_ReadBusy(void)
  127. {
  128. Debug("e-Paper busy\r\n");
  129. while(DEV_Digital_Read(EPD_BUSY_PIN) == 1) { //LOW: idle, HIGH: busy
  130. DEV_Delay_ms(100);
  131. }
  132. Debug("e-Paper busy release\r\n");
  133. }
  134. /******************************************************************************
  135. function : Turn On Display
  136. parameter:
  137. ******************************************************************************/
  138. static void EPD_2IN13_V2_TurnOnDisplay(void)
  139. {
  140. EPD_2IN13_V2_SendCommand(0x22);
  141. EPD_2IN13_V2_SendData(0xC7);
  142. EPD_2IN13_V2_SendCommand(0x20);
  143. EPD_2IN13_V2_ReadBusy();
  144. }
  145. /******************************************************************************
  146. function : Turn On Display
  147. parameter:
  148. ******************************************************************************/
  149. static void EPD_2IN13_V2_TurnOnDisplayPart(void)
  150. {
  151. EPD_2IN13_V2_SendCommand(0x22);
  152. EPD_2IN13_V2_SendData(0x0C);
  153. EPD_2IN13_V2_SendCommand(0x20);
  154. EPD_2IN13_V2_ReadBusy();
  155. }
  156. /******************************************************************************
  157. function : Initialize the e-Paper register
  158. parameter:
  159. ******************************************************************************/
  160. void EPD_2IN13_V2_Init(UBYTE Mode)
  161. {
  162. UBYTE count;
  163. EPD_2IN13_V2_Reset();
  164. if(Mode == EPD_2IN13_V2_FULL) {
  165. EPD_2IN13_V2_ReadBusy();
  166. EPD_2IN13_V2_SendCommand(0x12); // soft reset
  167. EPD_2IN13_V2_ReadBusy();
  168. EPD_2IN13_V2_SendCommand(0x74); //set analog block control
  169. EPD_2IN13_V2_SendData(0x54);
  170. EPD_2IN13_V2_SendCommand(0x7E); //set digital block control
  171. EPD_2IN13_V2_SendData(0x3B);
  172. EPD_2IN13_V2_SendCommand(0x01); //Driver output control
  173. EPD_2IN13_V2_SendData(0xF9);
  174. EPD_2IN13_V2_SendData(0x00);
  175. EPD_2IN13_V2_SendData(0x00);
  176. EPD_2IN13_V2_SendCommand(0x11); //data entry mode
  177. EPD_2IN13_V2_SendData(0x01);
  178. EPD_2IN13_V2_SendCommand(0x44); //set Ram-X address start/end position
  179. EPD_2IN13_V2_SendData(0x00);
  180. EPD_2IN13_V2_SendData(0x0F); //0x0C-->(15+1)*8=128
  181. EPD_2IN13_V2_SendCommand(0x45); //set Ram-Y address start/end position
  182. EPD_2IN13_V2_SendData(0xF9); //0xF9-->(249+1)=250
  183. EPD_2IN13_V2_SendData(0x00);
  184. EPD_2IN13_V2_SendData(0x00);
  185. EPD_2IN13_V2_SendData(0x00);
  186. EPD_2IN13_V2_SendCommand(0x3C); //BorderWavefrom
  187. EPD_2IN13_V2_SendData(0x03);
  188. EPD_2IN13_V2_SendCommand(0x2C); //VCOM Voltage
  189. EPD_2IN13_V2_SendData(0x55); //
  190. EPD_2IN13_V2_SendCommand(0x03);
  191. EPD_2IN13_V2_SendData(EPD_2IN13_V2_lut_full_update[70]);
  192. EPD_2IN13_V2_SendCommand(0x04); //
  193. EPD_2IN13_V2_SendData(EPD_2IN13_V2_lut_full_update[71]);
  194. EPD_2IN13_V2_SendData(EPD_2IN13_V2_lut_full_update[72]);
  195. EPD_2IN13_V2_SendData(EPD_2IN13_V2_lut_full_update[73]);
  196. EPD_2IN13_V2_SendCommand(0x3A); //Dummy Line
  197. EPD_2IN13_V2_SendData(EPD_2IN13_V2_lut_full_update[74]);
  198. EPD_2IN13_V2_SendCommand(0x3B); //Gate time
  199. EPD_2IN13_V2_SendData(EPD_2IN13_V2_lut_full_update[75]);
  200. EPD_2IN13_V2_SendCommand(0x32);
  201. for(count = 0; count < 70; count++) {
  202. EPD_2IN13_V2_SendData(EPD_2IN13_V2_lut_full_update[count]);
  203. }
  204. EPD_2IN13_V2_SendCommand(0x4E); // set RAM x address count to 0;
  205. EPD_2IN13_V2_SendData(0x00);
  206. EPD_2IN13_V2_SendCommand(0x4F); // set RAM y address count to 0X127;
  207. EPD_2IN13_V2_SendData(0xF9);
  208. EPD_2IN13_V2_SendData(0x00);
  209. EPD_2IN13_V2_ReadBusy();
  210. } else if(Mode == EPD_2IN13_V2_PART) {
  211. EPD_2IN13_V2_SendCommand(0x2C); //VCOM Voltage
  212. EPD_2IN13_V2_SendData(0x26);
  213. EPD_2IN13_V2_ReadBusy();
  214. EPD_2IN13_V2_SendCommand(0x32);
  215. for(count = 0; count < 70; count++) {
  216. EPD_2IN13_V2_SendData(EPD_2IN13_V2_lut_partial_update[count]);
  217. }
  218. EPD_2IN13_V2_SendCommand(0x37);
  219. EPD_2IN13_V2_SendData(0x00);
  220. EPD_2IN13_V2_SendData(0x00);
  221. EPD_2IN13_V2_SendData(0x00);
  222. EPD_2IN13_V2_SendData(0x00);
  223. EPD_2IN13_V2_SendData(0x40);
  224. EPD_2IN13_V2_SendData(0x00);
  225. EPD_2IN13_V2_SendData(0x00);
  226. EPD_2IN13_V2_SendCommand(0x22);
  227. EPD_2IN13_V2_SendData(0xC0);
  228. EPD_2IN13_V2_SendCommand(0x20);
  229. EPD_2IN13_V2_ReadBusy();
  230. EPD_2IN13_V2_SendCommand(0x3C); //BorderWavefrom
  231. EPD_2IN13_V2_SendData(0x01);
  232. } else {
  233. Debug("error, the Mode is EPD_2IN13_FULL or EPD_2IN13_PART");
  234. }
  235. }
  236. /******************************************************************************
  237. function : Clear screen
  238. parameter:
  239. ******************************************************************************/
  240. void EPD_2IN13_V2_Clear(void)
  241. {
  242. UWORD Width, Height;
  243. Width = (EPD_2IN13_V2_WIDTH % 8 == 0)? (EPD_2IN13_V2_WIDTH / 8 ): (EPD_2IN13_V2_WIDTH / 8 + 1);
  244. Height = EPD_2IN13_V2_HEIGHT;
  245. EPD_2IN13_V2_SendCommand(0x24);
  246. for (UWORD j = 0; j < Height; j++) {
  247. for (UWORD i = 0; i < Width; i++) {
  248. EPD_2IN13_V2_SendData(0XFF);
  249. }
  250. }
  251. EPD_2IN13_V2_TurnOnDisplay();
  252. }
  253. /******************************************************************************
  254. function : Sends the image buffer in RAM to e-Paper and displays
  255. parameter:
  256. ******************************************************************************/
  257. void EPD_2IN13_V2_Display(UBYTE *Image)
  258. {
  259. UWORD Width, Height;
  260. Width = (EPD_2IN13_V2_WIDTH % 8 == 0)? (EPD_2IN13_V2_WIDTH / 8 ): (EPD_2IN13_V2_WIDTH / 8 + 1);
  261. Height = EPD_2IN13_V2_HEIGHT;
  262. EPD_2IN13_V2_SendCommand(0x24);
  263. for (UWORD j = 0; j < Height; j++) {
  264. for (UWORD i = 0; i < Width; i++) {
  265. EPD_2IN13_V2_SendData(Image[i + j * Width]);
  266. }
  267. }
  268. EPD_2IN13_V2_TurnOnDisplay();
  269. }
  270. /******************************************************************************
  271. function : The image of the previous frame must be uploaded, otherwise the
  272. first few seconds will display an exception.
  273. parameter:
  274. ******************************************************************************/
  275. void EPD_2IN13_V2_DisplayPartBaseImage(UBYTE *Image)
  276. {
  277. UWORD Width, Height;
  278. Width = (EPD_2IN13_V2_WIDTH % 8 == 0)? (EPD_2IN13_V2_WIDTH / 8 ): (EPD_2IN13_V2_WIDTH / 8 + 1);
  279. Height = EPD_2IN13_V2_HEIGHT;
  280. UDOUBLE Addr = 0;
  281. EPD_2IN13_V2_SendCommand(0x24);
  282. for (UWORD j = 0; j < Height; j++) {
  283. for (UWORD i = 0; i < Width; i++) {
  284. Addr = i + j * Width;
  285. EPD_2IN13_V2_SendData(Image[Addr]);
  286. }
  287. }
  288. EPD_2IN13_V2_SendCommand(0x26);
  289. for (UWORD j = 0; j < Height; j++) {
  290. for (UWORD i = 0; i < Width; i++) {
  291. Addr = i + j * Width;
  292. EPD_2IN13_V2_SendData(Image[Addr]);
  293. }
  294. }
  295. EPD_2IN13_V2_TurnOnDisplay();
  296. }
  297. void EPD_2IN13_V2_DisplayPart(UBYTE *Image)
  298. {
  299. UWORD Width, Height;
  300. Width = (EPD_2IN13_V2_WIDTH % 8 == 0)? (EPD_2IN13_V2_WIDTH / 8 ): (EPD_2IN13_V2_WIDTH / 8 + 1);
  301. Height = EPD_2IN13_V2_HEIGHT;
  302. EPD_2IN13_V2_SendCommand(0x24);
  303. for (UWORD j = 0; j < Height; j++) {
  304. for (UWORD i = 0; i < Width; i++) {
  305. EPD_2IN13_V2_SendData(Image[i + j * Width]);
  306. }
  307. }
  308. EPD_2IN13_V2_TurnOnDisplayPart();
  309. }
  310. /******************************************************************************
  311. function : Enter sleep mode
  312. parameter:
  313. ******************************************************************************/
  314. void EPD_2IN13_V2_Sleep(void)
  315. {
  316. EPD_2IN13_V2_SendCommand(0x22); //POWER OFF
  317. EPD_2IN13_V2_SendData(0xC3);
  318. EPD_2IN13_V2_SendCommand(0x20);
  319. EPD_2IN13_V2_SendCommand(0x10); //enter deep sleep
  320. EPD_2IN13_V2_SendData(0x01);
  321. DEV_Delay_ms(100);
  322. }