EPD_1in02d.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  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. unsigned char busy;
  148. do
  149. {
  150. EPD_1IN02_SendCommand(0x71);
  151. busy = DEV_Digital_Read(EPD_BUSY_PIN);
  152. busy =!(busy & 0x01);
  153. }
  154. while(busy);
  155. DEV_Delay_ms(800);
  156. }
  157. /******************************************************************************
  158. function : Turn On Display
  159. ******************************************************************************/
  160. void EPD_1IN02_TurnOnDisplay(void)
  161. {
  162. // EPD_1IN02_SendCommand(0x04); //power on
  163. // EPD_1IN02_WaitUntilIdle();
  164. EPD_1IN02_SendCommand(0x12); //Start refreshing the screen
  165. DEV_Delay_ms(10);
  166. EPD_1IN02_WaitUntilIdle();
  167. // EPD_1IN02_SendCommand(0x02);
  168. // EPD_1IN02_WaitUntilIdle(); //power off
  169. }
  170. /******************************************************************************
  171. function :Initialize the e-Paper register
  172. ******************************************************************************/
  173. UBYTE EPD_1IN02_Init(void)
  174. {
  175. EPD_1IN02_Reset();
  176. EPD_1IN02_SendCommand(0xD2);
  177. EPD_1IN02_SendData(0x3F);
  178. EPD_1IN02_SendCommand(0x00);
  179. EPD_1IN02_SendData (0x6F); //from outside
  180. EPD_1IN02_SendCommand(0x01); //power setting
  181. EPD_1IN02_SendData (0x03);
  182. EPD_1IN02_SendData (0x00);
  183. EPD_1IN02_SendData (0x2b);
  184. EPD_1IN02_SendData (0x2b);
  185. EPD_1IN02_SendCommand(0x06); //Configuring the charge pump
  186. EPD_1IN02_SendData(0x3f);
  187. EPD_1IN02_SendCommand(0x2A); //Setting XON and the options of LUT
  188. EPD_1IN02_SendData(0x00);
  189. EPD_1IN02_SendData(0x00);
  190. EPD_1IN02_SendCommand(0x30); //Set the clock frequency
  191. EPD_1IN02_SendData(0x17); //50Hz
  192. EPD_1IN02_SendCommand(0x50); //Set VCOM and data output interval
  193. EPD_1IN02_SendData(0x57);
  194. EPD_1IN02_SendCommand(0x60); //Set The non-overlapping period of Gate and Source.
  195. EPD_1IN02_SendData(0x22);
  196. EPD_1IN02_SendCommand(0x61); //resolution setting
  197. EPD_1IN02_SendData (0x50); //source 128
  198. EPD_1IN02_SendData (0x80);
  199. EPD_1IN02_SendCommand(0x82); //sets VCOM_DC value
  200. EPD_1IN02_SendData(0x12); //-1v
  201. EPD_1IN02_SendCommand(0xe3);//Set POWER SAVING
  202. EPD_1IN02_SendData(0x33);
  203. EPD_1IN02_SetFulltReg();
  204. EPD_1IN02_SendCommand(0x04); //power on
  205. EPD_1IN02_WaitUntilIdle();
  206. return 0;
  207. }
  208. /******************************************************************************
  209. function :Partial refresh initialization e-paper
  210. ******************************************************************************/
  211. void EPD_1IN02_Part_Init(void)
  212. {
  213. EPD_1IN02_Reset();
  214. EPD_1IN02_SendCommand(0xD2);
  215. EPD_1IN02_SendData(0x3F);
  216. EPD_1IN02_SendCommand(0x00);
  217. EPD_1IN02_SendData (0x6F); //from outside
  218. EPD_1IN02_SendCommand(0x01); //power setting
  219. EPD_1IN02_SendData (0x03);
  220. EPD_1IN02_SendData (0x00);
  221. EPD_1IN02_SendData (0x2b);
  222. EPD_1IN02_SendData (0x2b);
  223. EPD_1IN02_SendCommand(0x06); //Configuring the charge pump
  224. EPD_1IN02_SendData(0x3f);
  225. EPD_1IN02_SendCommand(0x2A); //Setting XON and the options of LUT
  226. EPD_1IN02_SendData(0x00);
  227. EPD_1IN02_SendData(0x00);
  228. EPD_1IN02_SendCommand(0x30); //Set the clock frequency
  229. EPD_1IN02_SendData(0x17);
  230. EPD_1IN02_SendCommand(0x50); //Set VCOM and data output interval
  231. EPD_1IN02_SendData(0xf2);
  232. EPD_1IN02_SendCommand(0x60); //Set The non-overlapping period of Gate and Source.
  233. EPD_1IN02_SendData(0x22);
  234. EPD_1IN02_SendCommand(0x82); //Set VCOM_DC value
  235. EPD_1IN02_SendData(0x12);//-1v
  236. EPD_1IN02_SendCommand(0xe3);//Set POWER SAVING
  237. EPD_1IN02_SendData(0x33);
  238. EPD_1IN02_SetPartReg();
  239. EPD_1IN02_SendCommand(0x04);//Set POWER SAVING
  240. EPD_1IN02_WaitUntilIdle();
  241. }
  242. /******************************************************************************
  243. function : Clear screen
  244. ******************************************************************************/
  245. void EPD_1IN02_Clear(void)
  246. {
  247. unsigned int i;
  248. EPD_1IN02_SendCommand(0x10);
  249. for(i=0;i<1280;i++){
  250. EPD_1IN02_SendData(0X00);
  251. }
  252. EPD_1IN02_SendCommand(0x13); //Transfer new data
  253. for(i=0;i<1280;i++){
  254. EPD_1IN02_SendData(0xff);
  255. }
  256. EPD_1IN02_TurnOnDisplay();
  257. }
  258. /******************************************************************************
  259. function : Sends the image buffer in RAM to e-Paper and displays
  260. parameter:
  261. Image :Displayed data
  262. ******************************************************************************/
  263. void EPD_1IN02_Display(UBYTE *Image)
  264. {
  265. UWORD Width;
  266. Width = (EPD_1IN02_WIDTH % 8 == 0)? (EPD_1IN02_WIDTH / 8 ): (EPD_1IN02_WIDTH / 8 + 1);
  267. //EPD_1IN02_Init();
  268. EPD_1IN02_SendCommand(0x10);
  269. for (UWORD j = 0; j < EPD_1IN02_HEIGHT; j++) {
  270. for (UWORD i = 0; i < Width; i++) {
  271. EPD_1IN02_SendData(0xff);
  272. }
  273. }
  274. EPD_1IN02_SendCommand(0x13);
  275. for (UWORD j = 0; j < EPD_1IN02_HEIGHT; j++) {
  276. for (UWORD i = 0; i < Width; i++) {
  277. EPD_1IN02_SendData(Image[i + j * Width]);
  278. }
  279. }
  280. EPD_1IN02_TurnOnDisplay();
  281. }
  282. /******************************************************************************
  283. function : Sends the image buffer in RAM to e-Paper and displays
  284. parameter:
  285. old_Image: Last displayed data
  286. Image2 : New data
  287. ******************************************************************************/
  288. void EPD_1IN02_DisplayPartial(UBYTE *old_Image, UBYTE *Image)
  289. {
  290. /* Set partial Windows */
  291. EPD_1IN02_SendCommand(0x91); //This command makes the display enter partial mode
  292. EPD_1IN02_SendCommand(0x90); //resolution setting
  293. EPD_1IN02_SendData(0); //x-start
  294. EPD_1IN02_SendData(79); //x-end
  295. EPD_1IN02_SendData(0);
  296. EPD_1IN02_SendData(127); //y-end
  297. EPD_1IN02_SendData(0x00);
  298. UWORD Width;
  299. Width = (EPD_1IN02_WIDTH % 8 == 0)? (EPD_1IN02_WIDTH / 8 ): (EPD_1IN02_WIDTH / 8 + 1);
  300. /* send data */
  301. EPD_1IN02_SendCommand(0x10);
  302. for (UWORD j = 0; j < EPD_1IN02_HEIGHT; j++) {
  303. for (UWORD i = 0; i < Width; i++) {
  304. EPD_1IN02_SendData(old_Image[i + j * Width]);
  305. }
  306. }
  307. EPD_1IN02_SendCommand(0x13);
  308. for (UWORD j = 0; j < EPD_1IN02_HEIGHT; j++) {
  309. for (UWORD i = 0; i < Width; i++) {
  310. EPD_1IN02_SendData(Image[i + j * Width]);
  311. }
  312. }
  313. /* Set partial refresh */
  314. EPD_1IN02_TurnOnDisplay();
  315. }
  316. /******************************************************************************
  317. function : Enter sleep mode
  318. ******************************************************************************/
  319. void EPD_1IN02_Sleep(void)
  320. {
  321. EPD_1IN02_SendCommand(0X50);
  322. EPD_1IN02_SendData(0xf7);
  323. EPD_1IN02_SendCommand(0X02); //power off
  324. EPD_1IN02_WaitUntilIdle();
  325. EPD_1IN02_SendCommand(0X07); //deep sleep
  326. EPD_1IN02_SendData(0xA5);
  327. DEV_Delay_ms(200);
  328. printf("Turn off the power!!! \r\n");
  329. DEV_Digital_Write(EPD_RST_PIN, 0);// Module reset
  330. }