EPD_2in13_V2.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  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. EPD_Busy_WaitUntil(0,0);
  129. // unsigned char count = 100;
  130. // Debug("e-Paper busy\r\n");
  131. // while(DEV_Digital_Read(EPD_BUSY_PIN) == 1) { //LOW: idle, HIGH: busy
  132. // if(!(count--))
  133. // {
  134. // Debug("error: e-Paper busy timeout!!!\r\n");
  135. // break;
  136. // }
  137. // else
  138. // DEV_Delay_ms(100);
  139. // }
  140. // Debug("e-Paper busy release\r\n");
  141. }
  142. /******************************************************************************
  143. function : Turn On Display
  144. parameter:
  145. ******************************************************************************/
  146. static void EPD_2IN13_V2_TurnOnDisplay(void)
  147. {
  148. EPD_2IN13_V2_SendCommand(0x22);
  149. EPD_2IN13_V2_SendData(0xC7);
  150. EPD_2IN13_V2_SendCommand(0x20);
  151. EPD_2IN13_V2_ReadBusy();
  152. }
  153. /******************************************************************************
  154. function : Turn On Display
  155. parameter:
  156. ******************************************************************************/
  157. static void EPD_2IN13_V2_TurnOnDisplayPart(void)
  158. {
  159. EPD_2IN13_V2_SendCommand(0x22);
  160. EPD_2IN13_V2_SendData(0x0C);
  161. EPD_2IN13_V2_SendCommand(0x20);
  162. EPD_2IN13_V2_ReadBusy();
  163. }
  164. /******************************************************************************
  165. function : Initialize the e-Paper register
  166. parameter:
  167. ******************************************************************************/
  168. void EPD_2IN13_V2_Init(UBYTE Mode)
  169. {
  170. UBYTE count;
  171. EPD_2IN13_V2_Reset();
  172. if(Mode == EPD_2IN13_V2_FULL) {
  173. EPD_2IN13_V2_ReadBusy();
  174. EPD_2IN13_V2_SendCommand(0x12); // soft reset
  175. EPD_2IN13_V2_ReadBusy();
  176. EPD_2IN13_V2_SendCommand(0x74); //set analog block control
  177. EPD_2IN13_V2_SendData(0x54);
  178. EPD_2IN13_V2_SendCommand(0x7E); //set digital block control
  179. EPD_2IN13_V2_SendData(0x3B);
  180. EPD_2IN13_V2_SendCommand(0x01); //Driver output control
  181. EPD_2IN13_V2_SendData(0xF9);
  182. EPD_2IN13_V2_SendData(0x00);
  183. EPD_2IN13_V2_SendData(0x00);
  184. EPD_2IN13_V2_SendCommand(0x11); //data entry mode
  185. EPD_2IN13_V2_SendData(0x01);
  186. EPD_2IN13_V2_SendCommand(0x44); //set Ram-X address start/end position
  187. EPD_2IN13_V2_SendData(0x00);
  188. EPD_2IN13_V2_SendData(0x0F); //0x0C-->(15+1)*8=128
  189. EPD_2IN13_V2_SendCommand(0x45); //set Ram-Y address start/end position
  190. EPD_2IN13_V2_SendData(0xF9); //0xF9-->(249+1)=250
  191. EPD_2IN13_V2_SendData(0x00);
  192. EPD_2IN13_V2_SendData(0x00);
  193. EPD_2IN13_V2_SendData(0x00);
  194. EPD_2IN13_V2_SendCommand(0x3C); //BorderWavefrom
  195. EPD_2IN13_V2_SendData(0x03);
  196. EPD_2IN13_V2_SendCommand(0x2C); //VCOM Voltage
  197. EPD_2IN13_V2_SendData(0x55); //
  198. EPD_2IN13_V2_SendCommand(0x03);
  199. EPD_2IN13_V2_SendData(EPD_2IN13_V2_lut_full_update[70]);
  200. EPD_2IN13_V2_SendCommand(0x04); //
  201. EPD_2IN13_V2_SendData(EPD_2IN13_V2_lut_full_update[71]);
  202. EPD_2IN13_V2_SendData(EPD_2IN13_V2_lut_full_update[72]);
  203. EPD_2IN13_V2_SendData(EPD_2IN13_V2_lut_full_update[73]);
  204. EPD_2IN13_V2_SendCommand(0x3A); //Dummy Line
  205. EPD_2IN13_V2_SendData(EPD_2IN13_V2_lut_full_update[74]);
  206. EPD_2IN13_V2_SendCommand(0x3B); //Gate time
  207. EPD_2IN13_V2_SendData(EPD_2IN13_V2_lut_full_update[75]);
  208. EPD_2IN13_V2_SendCommand(0x32);
  209. for(count = 0; count < 70; count++) {
  210. EPD_2IN13_V2_SendData(EPD_2IN13_V2_lut_full_update[count]);
  211. }
  212. EPD_2IN13_V2_SendCommand(0x4E); // set RAM x address count to 0;
  213. EPD_2IN13_V2_SendData(0x00);
  214. EPD_2IN13_V2_SendCommand(0x4F); // set RAM y address count to 0X127;
  215. EPD_2IN13_V2_SendData(0xF9);
  216. EPD_2IN13_V2_SendData(0x00);
  217. EPD_2IN13_V2_ReadBusy();
  218. } else if(Mode == EPD_2IN13_V2_PART) {
  219. EPD_2IN13_V2_SendCommand(0x2C); //VCOM Voltage
  220. EPD_2IN13_V2_SendData(0x26);
  221. EPD_2IN13_V2_ReadBusy();
  222. EPD_2IN13_V2_SendCommand(0x32);
  223. for(count = 0; count < 70; count++) {
  224. EPD_2IN13_V2_SendData(EPD_2IN13_V2_lut_partial_update[count]);
  225. }
  226. EPD_2IN13_V2_SendCommand(0x37);
  227. EPD_2IN13_V2_SendData(0x00);
  228. EPD_2IN13_V2_SendData(0x00);
  229. EPD_2IN13_V2_SendData(0x00);
  230. EPD_2IN13_V2_SendData(0x00);
  231. EPD_2IN13_V2_SendData(0x40);
  232. EPD_2IN13_V2_SendData(0x00);
  233. EPD_2IN13_V2_SendData(0x00);
  234. EPD_2IN13_V2_SendCommand(0x22);
  235. EPD_2IN13_V2_SendData(0xC0);
  236. EPD_2IN13_V2_SendCommand(0x20);
  237. EPD_2IN13_V2_ReadBusy();
  238. EPD_2IN13_V2_SendCommand(0x3C); //BorderWavefrom
  239. EPD_2IN13_V2_SendData(0x01);
  240. } else {
  241. Debug("error, the Mode is EPD_2IN13_FULL or EPD_2IN13_PART");
  242. }
  243. }
  244. /******************************************************************************
  245. function : Clear screen
  246. parameter:
  247. ******************************************************************************/
  248. void EPD_2IN13_V2_Clear(void)
  249. {
  250. UWORD Width, Height;
  251. Width = (EPD_2IN13_V2_WIDTH % 8 == 0)? (EPD_2IN13_V2_WIDTH / 8 ): (EPD_2IN13_V2_WIDTH / 8 + 1);
  252. Height = EPD_2IN13_V2_HEIGHT;
  253. EPD_2IN13_V2_SendCommand(0x24);
  254. for (UWORD j = 0; j < Height; j++) {
  255. for (UWORD i = 0; i < Width; i++) {
  256. EPD_2IN13_V2_SendData(0XFF);
  257. }
  258. }
  259. EPD_2IN13_V2_TurnOnDisplay();
  260. }
  261. /******************************************************************************
  262. function : Sends the image buffer in RAM to e-Paper and displays
  263. parameter:
  264. ******************************************************************************/
  265. void EPD_2IN13_V2_Display(UBYTE *Image, UBYTE *Image2)
  266. {
  267. UWORD Width, Height;
  268. Width = (EPD_2IN13_V2_WIDTH % 8 == 0)? (EPD_2IN13_V2_WIDTH / 8 ): (EPD_2IN13_V2_WIDTH / 8 + 1);
  269. Height = EPD_2IN13_V2_HEIGHT;
  270. EPD_2IN13_V2_SendCommand(0x24);
  271. for (UWORD j = 0; j < Height; j++) {
  272. for (UWORD i = 0; i < Width; i++) {
  273. EPD_2IN13_V2_SendData(Image[i + j * Width]);
  274. }
  275. }
  276. EPD_2IN13_V2_TurnOnDisplay();
  277. }
  278. /******************************************************************************
  279. function : The image of the previous frame must be uploaded, otherwise the
  280. first few seconds will display an exception.
  281. parameter:
  282. ******************************************************************************/
  283. void EPD_2IN13_V2_DisplayPartBaseImage(UBYTE *Image)
  284. {
  285. UWORD Width, Height;
  286. Width = (EPD_2IN13_V2_WIDTH % 8 == 0)? (EPD_2IN13_V2_WIDTH / 8 ): (EPD_2IN13_V2_WIDTH / 8 + 1);
  287. Height = EPD_2IN13_V2_HEIGHT;
  288. UDOUBLE Addr = 0;
  289. EPD_2IN13_V2_SendCommand(0x24);
  290. for (UWORD j = 0; j < Height; j++) {
  291. for (UWORD i = 0; i < Width; i++) {
  292. Addr = i + j * Width;
  293. EPD_2IN13_V2_SendData(Image[Addr]);
  294. }
  295. }
  296. EPD_2IN13_V2_SendCommand(0x26);
  297. for (UWORD j = 0; j < Height; j++) {
  298. for (UWORD i = 0; i < Width; i++) {
  299. Addr = i + j * Width;
  300. EPD_2IN13_V2_SendData(Image[Addr]);
  301. }
  302. }
  303. EPD_2IN13_V2_TurnOnDisplay();
  304. }
  305. void EPD_2IN13_V2_DisplayPart(UBYTE *Image)
  306. {
  307. UWORD Width, Height;
  308. Width = (EPD_2IN13_V2_WIDTH % 8 == 0)? (EPD_2IN13_V2_WIDTH / 8 ): (EPD_2IN13_V2_WIDTH / 8 + 1);
  309. Height = EPD_2IN13_V2_HEIGHT;
  310. EPD_2IN13_V2_SendCommand(0x24);
  311. for (UWORD j = 0; j < Height; j++) {
  312. for (UWORD i = 0; i < Width; i++) {
  313. EPD_2IN13_V2_SendData(Image[i + j * Width]);
  314. }
  315. }
  316. EPD_2IN13_V2_TurnOnDisplayPart();
  317. }
  318. /******************************************************************************
  319. function : Enter sleep mode
  320. parameter:
  321. ******************************************************************************/
  322. void EPD_2IN13_V2_Sleep(void)
  323. {
  324. EPD_2IN13_V2_SendCommand(0x22); //POWER OFF
  325. EPD_2IN13_V2_SendData(0xC3);
  326. EPD_2IN13_V2_SendCommand(0x20);
  327. EPD_2IN13_V2_SendCommand(0x10); //enter deep sleep
  328. EPD_2IN13_V2_SendData(0x01);
  329. DEV_Delay_ms(100);
  330. }