EPD_1in02d.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  1. /*****************************************************************************
  2. * | File : EPD_1IN02_1in02.c
  3. * | Author : Waveshare team
  4. * | Function : Electronic paper driver
  5. * | Info :
  6. *----------------
  7. * | This version: V1.0
  8. * | Date : 2019-09-27
  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_1in02d.h"
  31. /**
  32. * full screen update LUT
  33. **/
  34. const unsigned char lut_w1[] =
  35. {
  36. 0x60 ,0x5A ,0x5A ,0x00 ,0x00 ,0x01 ,
  37. 0x00 ,0x00 ,0x00 ,0x00 ,0x00 ,0x00 ,
  38. 0x00 ,0x00 ,0x00 ,0x00 ,0x00 ,0x00 ,
  39. 0x00 ,0x00 ,0x00 ,0x00 ,0x00 ,0x00 ,
  40. 0x00 ,0x00 ,0x00 ,0x00 ,0x00 ,0x00 ,
  41. 0x00 ,0x00 ,0x00 ,0x00 ,0x00 ,0x00 ,
  42. 0x00 ,0x00 ,0x00 ,0x00 ,0x00 ,0x00 ,
  43. };
  44. const unsigned char lut_b1[] =
  45. {
  46. 0x90 ,0x5A ,0x5A ,0x00 ,0x00 ,0x01 ,
  47. 0x00 ,0x00 ,0x00 ,0x00 ,0x00 ,0x00 ,
  48. 0x00 ,0x00 ,0x00 ,0x00 ,0x00 ,0x00 ,
  49. 0x00 ,0x00 ,0x00 ,0x00 ,0x00 ,0x00 ,
  50. 0x00 ,0x00 ,0x00 ,0x00 ,0x00 ,0x00 ,
  51. 0x00 ,0x00 ,0x00 ,0x00 ,0x00 ,0x00 ,
  52. 0x00 ,0x00 ,0x00 ,0x00 ,0x00 ,0x00 ,
  53. };
  54. /**
  55. * partial screen update LUT
  56. **/
  57. const unsigned char lut_w[] =
  58. {
  59. 0x60 ,0x01 ,0x01 ,0x00 ,0x00 ,0x01 ,
  60. 0x80 ,0x1f ,0x00 ,0x00 ,0x00 ,0x01 ,
  61. 0x00 ,0x00 ,0x00 ,0x00 ,0x00 ,0x00 ,
  62. 0x00 ,0x00 ,0x00 ,0x00 ,0x00 ,0x00 ,
  63. 0x00 ,0x00 ,0x00 ,0x00 ,0x00 ,0x00 ,
  64. 0x00 ,0x00 ,0x00 ,0x00 ,0x00 ,0x00 ,
  65. 0x00 ,0x00 ,0x00 ,0x00 ,0x00 ,0x00 ,
  66. };
  67. const unsigned char lut_b[] =
  68. {
  69. 0x90 ,0x01 ,0x01 ,0x00 ,0x00 ,0x01 ,
  70. 0x40 ,0x1f ,0x00 ,0x00 ,0x00 ,0x01 ,
  71. 0x00 ,0x00 ,0x00 ,0x00 ,0x00 ,0x00 ,
  72. 0x00 ,0x00 ,0x00 ,0x00 ,0x00 ,0x00 ,
  73. 0x00 ,0x00 ,0x00 ,0x00 ,0x00 ,0x00 ,
  74. 0x00 ,0x00 ,0x00 ,0x00 ,0x00 ,0x00 ,
  75. 0x00 ,0x00 ,0x00 ,0x00 ,0x00 ,0x00 ,
  76. };
  77. /******************************************************************************
  78. function : Software reset
  79. parameter:
  80. ******************************************************************************/
  81. static void EPD_1IN02_Reset(void)
  82. {
  83. DEV_Digital_Write(EPD_RST_PIN, 1);
  84. DEV_Delay_ms(20);
  85. DEV_Digital_Write(EPD_RST_PIN, 0);// Module reset
  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. command : Command register
  94. ******************************************************************************/
  95. static void EPD_1IN02_SendCommand(UBYTE command)
  96. {
  97. DEV_Digital_Write(EPD_CS_PIN, 0);
  98. DEV_Digital_Write(EPD_DC_PIN, 0);// command write
  99. DEV_SPI_WriteByte(command);
  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_1IN02_SendData(UBYTE Data)
  108. {
  109. DEV_Digital_Write(EPD_CS_PIN, 0);
  110. DEV_Digital_Write(EPD_DC_PIN, 1); // command write
  111. DEV_SPI_WriteByte(Data);
  112. DEV_Digital_Write(EPD_CS_PIN, 1);
  113. }
  114. /******************************************************************************
  115. function : LUT download
  116. ******************************************************************************/
  117. void EPD_1IN02_SetFulltReg(void)
  118. {
  119. unsigned int count;
  120. EPD_1IN02_SendCommand(0x23);
  121. for(count=0;count<42;count++)
  122. {EPD_1IN02_SendData(lut_w1[count]);}
  123. EPD_1IN02_SendCommand(0x24);
  124. for(count=0;count<42;count++)
  125. {EPD_1IN02_SendData(lut_b1[count]);}
  126. }
  127. /******************************************************************************
  128. function : LUT download
  129. ******************************************************************************/
  130. void EPD_1IN02_SetPartReg(void)
  131. {
  132. unsigned int count;
  133. EPD_1IN02_SendCommand(0x23);
  134. for(count=0;count<42;count++){
  135. EPD_1IN02_SendData(lut_w[count]);
  136. }
  137. EPD_1IN02_SendCommand(0x24);
  138. for(count=0;count<42;count++){
  139. EPD_1IN02_SendData(lut_b[count]);
  140. }
  141. }
  142. /******************************************************************************
  143. function : Wait until the busy_pin goes LOW
  144. ******************************************************************************/
  145. void EPD_1IN02_WaitUntilIdle(void)
  146. {
  147. EPD_Busy_WaitUntil(1,1);
  148. // unsigned char busy;
  149. // unsigned char count = 100;
  150. // do
  151. // {
  152. // EPD_1IN02_SendCommand(0x71);
  153. // busy = DEV_Digital_Read(EPD_BUSY_PIN);
  154. // busy =!(busy & 0x01);
  155. // if(!(count--))
  156. // break;
  157. // else
  158. // DEV_Delay_ms(100);
  159. // }
  160. // while(busy);
  161. // DEV_Delay_ms(800);
  162. }
  163. /******************************************************************************
  164. function : Turn On Display
  165. ******************************************************************************/
  166. void EPD_1IN02_TurnOnDisplay(void)
  167. {
  168. // EPD_1IN02_SendCommand(0x04); //power on
  169. // EPD_1IN02_WaitUntilIdle();
  170. EPD_1IN02_SendCommand(0x12); //Start refreshing the screen
  171. DEV_Delay_ms(10);
  172. EPD_1IN02_WaitUntilIdle();
  173. // EPD_1IN02_SendCommand(0x02);
  174. // EPD_1IN02_WaitUntilIdle(); //power off
  175. }
  176. /******************************************************************************
  177. function :Initialize the e-Paper register
  178. ******************************************************************************/
  179. void EPD_1IN02_Init(UBYTE Mode)
  180. {
  181. EPD_1IN02_Reset();
  182. EPD_1IN02_SendCommand(0xD2);
  183. EPD_1IN02_SendData(0x3F);
  184. EPD_1IN02_SendCommand(0x00);
  185. EPD_1IN02_SendData (0x6F); //from outside
  186. EPD_1IN02_SendCommand(0x01); //power setting
  187. EPD_1IN02_SendData (0x03);
  188. EPD_1IN02_SendData (0x00);
  189. EPD_1IN02_SendData (0x2b);
  190. EPD_1IN02_SendData (0x2b);
  191. EPD_1IN02_SendCommand(0x06); //Configuring the charge pump
  192. EPD_1IN02_SendData(0x3f);
  193. EPD_1IN02_SendCommand(0x2A); //Setting XON and the options of LUT
  194. EPD_1IN02_SendData(0x00);
  195. EPD_1IN02_SendData(0x00);
  196. EPD_1IN02_SendCommand(0x30); //Set the clock frequency
  197. EPD_1IN02_SendData(0x17); //50Hz
  198. EPD_1IN02_SendCommand(0x50); //Set VCOM and data output interval
  199. EPD_1IN02_SendData(0x57);
  200. EPD_1IN02_SendCommand(0x60); //Set The non-overlapping period of Gate and Source.
  201. EPD_1IN02_SendData(0x22);
  202. EPD_1IN02_SendCommand(0x61); //resolution setting
  203. EPD_1IN02_SendData (0x50); //source 128
  204. EPD_1IN02_SendData (0x80);
  205. EPD_1IN02_SendCommand(0x82); //sets VCOM_DC value
  206. EPD_1IN02_SendData(0x12); //-1v
  207. EPD_1IN02_SendCommand(0xe3);//Set POWER SAVING
  208. EPD_1IN02_SendData(0x33);
  209. EPD_1IN02_SetFulltReg();
  210. EPD_1IN02_SendCommand(0x04); //power on
  211. EPD_1IN02_WaitUntilIdle();
  212. // return 0;
  213. }
  214. /******************************************************************************
  215. function :Partial refresh initialization e-paper
  216. ******************************************************************************/
  217. void EPD_1IN02_Part_Init(void)
  218. {
  219. EPD_1IN02_Reset();
  220. EPD_1IN02_SendCommand(0xD2);
  221. EPD_1IN02_SendData(0x3F);
  222. EPD_1IN02_SendCommand(0x00);
  223. EPD_1IN02_SendData (0x6F); //from outside
  224. EPD_1IN02_SendCommand(0x01); //power setting
  225. EPD_1IN02_SendData (0x03);
  226. EPD_1IN02_SendData (0x00);
  227. EPD_1IN02_SendData (0x2b);
  228. EPD_1IN02_SendData (0x2b);
  229. EPD_1IN02_SendCommand(0x06); //Configuring the charge pump
  230. EPD_1IN02_SendData(0x3f);
  231. EPD_1IN02_SendCommand(0x2A); //Setting XON and the options of LUT
  232. EPD_1IN02_SendData(0x00);
  233. EPD_1IN02_SendData(0x00);
  234. EPD_1IN02_SendCommand(0x30); //Set the clock frequency
  235. EPD_1IN02_SendData(0x17);
  236. EPD_1IN02_SendCommand(0x50); //Set VCOM and data output interval
  237. EPD_1IN02_SendData(0xf2);
  238. EPD_1IN02_SendCommand(0x60); //Set The non-overlapping period of Gate and Source.
  239. EPD_1IN02_SendData(0x22);
  240. EPD_1IN02_SendCommand(0x82); //Set VCOM_DC value
  241. EPD_1IN02_SendData(0x12);//-1v
  242. EPD_1IN02_SendCommand(0xe3);//Set POWER SAVING
  243. EPD_1IN02_SendData(0x33);
  244. EPD_1IN02_SetPartReg();
  245. EPD_1IN02_SendCommand(0x04);//Set POWER SAVING
  246. EPD_1IN02_WaitUntilIdle();
  247. }
  248. /******************************************************************************
  249. function : Clear screen
  250. ******************************************************************************/
  251. void EPD_1IN02_Clear(void)
  252. {
  253. unsigned int i;
  254. EPD_1IN02_SendCommand(0x10);
  255. for(i=0;i<1280;i++){
  256. EPD_1IN02_SendData(0X00);
  257. }
  258. EPD_1IN02_SendCommand(0x13); //Transfer new data
  259. for(i=0;i<1280;i++){
  260. EPD_1IN02_SendData(0xff);
  261. }
  262. EPD_1IN02_TurnOnDisplay();
  263. }
  264. /******************************************************************************
  265. function : Sends the image buffer in RAM to e-Paper and displays
  266. parameter:
  267. Image :Displayed data
  268. ******************************************************************************/
  269. void EPD_1IN02_Display(UBYTE *Image, UBYTE *Image2)
  270. {
  271. UWORD Width;
  272. Width = (EPD_1IN02_WIDTH % 8 == 0)? (EPD_1IN02_WIDTH / 8 ): (EPD_1IN02_WIDTH / 8 + 1);
  273. //EPD_1IN02_Init();
  274. EPD_1IN02_SendCommand(0x10);
  275. for (UWORD j = 0; j < EPD_1IN02_HEIGHT; j++) {
  276. for (UWORD i = 0; i < Width; i++) {
  277. EPD_1IN02_SendData(0xff);
  278. }
  279. }
  280. EPD_1IN02_SendCommand(0x13);
  281. for (UWORD j = 0; j < EPD_1IN02_HEIGHT; j++) {
  282. for (UWORD i = 0; i < Width; i++) {
  283. EPD_1IN02_SendData(Image[i + j * Width]);
  284. }
  285. }
  286. EPD_1IN02_TurnOnDisplay();
  287. }
  288. /******************************************************************************
  289. function : Sends the image buffer in RAM to e-Paper and displays
  290. parameter:
  291. old_Image: Last displayed data
  292. Image2 : New data
  293. ******************************************************************************/
  294. void EPD_1IN02_DisplayPartial(UBYTE *old_Image, UBYTE *Image)
  295. {
  296. /* Set partial Windows */
  297. EPD_1IN02_SendCommand(0x91); //This command makes the display enter partial mode
  298. EPD_1IN02_SendCommand(0x90); //resolution setting
  299. EPD_1IN02_SendData(0); //x-start
  300. EPD_1IN02_SendData(79); //x-end
  301. EPD_1IN02_SendData(0);
  302. EPD_1IN02_SendData(127); //y-end
  303. EPD_1IN02_SendData(0x00);
  304. UWORD Width;
  305. Width = (EPD_1IN02_WIDTH % 8 == 0)? (EPD_1IN02_WIDTH / 8 ): (EPD_1IN02_WIDTH / 8 + 1);
  306. /* send data */
  307. EPD_1IN02_SendCommand(0x10);
  308. for (UWORD j = 0; j < EPD_1IN02_HEIGHT; j++) {
  309. for (UWORD i = 0; i < Width; i++) {
  310. EPD_1IN02_SendData(old_Image[i + j * Width]);
  311. }
  312. }
  313. EPD_1IN02_SendCommand(0x13);
  314. for (UWORD j = 0; j < EPD_1IN02_HEIGHT; j++) {
  315. for (UWORD i = 0; i < Width; i++) {
  316. EPD_1IN02_SendData(Image[i + j * Width]);
  317. }
  318. }
  319. /* Set partial refresh */
  320. EPD_1IN02_TurnOnDisplay();
  321. }
  322. /******************************************************************************
  323. function : Enter sleep mode
  324. ******************************************************************************/
  325. void EPD_1IN02_Sleep(void)
  326. {
  327. EPD_1IN02_SendCommand(0X50);
  328. EPD_1IN02_SendData(0xf7);
  329. EPD_1IN02_SendCommand(0X02); //power off
  330. EPD_1IN02_WaitUntilIdle();
  331. EPD_1IN02_SendCommand(0X07); //deep sleep
  332. EPD_1IN02_SendData(0xA5);
  333. DEV_Delay_ms(200);
  334. //printf("Turn off the power!!! \r\n");
  335. DEV_Digital_Write(EPD_RST_PIN, 0);// Module reset
  336. }