EPD_1in02d.c 12 KB

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