EPD_7in5_V2.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  1. /*****************************************************************************
  2. * | File : EPD_7in5.c
  3. * | Author : Waveshare team
  4. * | Function : Electronic paper driver
  5. * | Info :
  6. *----------------
  7. * | This version: V2.0
  8. * | Date : 2018-11-09
  9. * | Info :
  10. *****************************************************************************
  11. #
  12. # Permission is hereby granted, free of charge, to any person obtaining a copy
  13. # of this software and associated documnetation files(the "Software"), to deal
  14. # in the Software without restriction, including without limitation the rights
  15. # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  16. # copies of the Software, and to permit persons to whom the Software is
  17. # furished to do so, subject to the following conditions:
  18. #
  19. # The above copyright notice and this permission notice shall be included in
  20. # all copies or substantial portions of the Software.
  21. #
  22. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  23. # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  24. # FITNESS OR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  25. # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  26. # LIABILITY WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  27. # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  28. # THE SOFTWARE.
  29. #
  30. ******************************************************************************/
  31. #include "EPD_7in5_V2.h"
  32. #include "Debug.h"
  33. UBYTE Voltage_Frame_7IN5_V2[]={
  34. 0x6, 0x3F, 0x3F, 0x11, 0x24, 0x7, 0x17,
  35. };
  36. UBYTE LUT_VCOM_7IN5_V2[]={
  37. 0x0, 0xF, 0xF, 0x0, 0x0, 0x1,
  38. 0x0, 0xF, 0x1, 0xF, 0x1, 0x2,
  39. 0x0, 0xF, 0xF, 0x0, 0x0, 0x1,
  40. 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
  41. 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
  42. 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
  43. 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
  44. };
  45. UBYTE LUT_WW_7IN5_V2[]={
  46. 0x10, 0xF, 0xF, 0x0, 0x0, 0x1,
  47. 0x84, 0xF, 0x1, 0xF, 0x1, 0x2,
  48. 0x20, 0xF, 0xF, 0x0, 0x0, 0x1,
  49. 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
  50. 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
  51. 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
  52. 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
  53. };
  54. UBYTE LUT_BW_7IN5_V2[]={
  55. 0x10, 0xF, 0xF, 0x0, 0x0, 0x1,
  56. 0x84, 0xF, 0x1, 0xF, 0x1, 0x2,
  57. 0x20, 0xF, 0xF, 0x0, 0x0, 0x1,
  58. 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
  59. 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
  60. 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
  61. 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
  62. };
  63. UBYTE LUT_WB_7IN5_V2[]={
  64. 0x80, 0xF, 0xF, 0x0, 0x0, 0x1,
  65. 0x84, 0xF, 0x1, 0xF, 0x1, 0x2,
  66. 0x40, 0xF, 0xF, 0x0, 0x0, 0x1,
  67. 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
  68. 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
  69. 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
  70. 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
  71. };
  72. UBYTE LUT_BB_7IN5_V2[]={
  73. 0x80, 0xF, 0xF, 0x0, 0x0, 0x1,
  74. 0x84, 0xF, 0x1, 0xF, 0x1, 0x2,
  75. 0x40, 0xF, 0xF, 0x0, 0x0, 0x1,
  76. 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
  77. 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
  78. 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
  79. 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
  80. };
  81. /******************************************************************************
  82. function : Software reset
  83. parameter:
  84. ******************************************************************************/
  85. static void EPD_Reset(void)
  86. {
  87. DEV_Digital_Write(EPD_RST_PIN, 1);
  88. DEV_Delay_ms(20);
  89. DEV_Digital_Write(EPD_RST_PIN, 0);
  90. DEV_Delay_ms(2);
  91. DEV_Digital_Write(EPD_RST_PIN, 1);
  92. DEV_Delay_ms(20);
  93. }
  94. /******************************************************************************
  95. function : send command
  96. parameter:
  97. Reg : Command register
  98. ******************************************************************************/
  99. static void EPD_SendCommand(UBYTE Reg)
  100. {
  101. DEV_Digital_Write(EPD_DC_PIN, 0);
  102. DEV_Digital_Write(EPD_CS_PIN, 0);
  103. DEV_SPI_WriteByte(Reg);
  104. DEV_Digital_Write(EPD_CS_PIN, 1);
  105. }
  106. /******************************************************************************
  107. function : send data
  108. parameter:
  109. Data : Write data
  110. ******************************************************************************/
  111. static void EPD_SendData(UBYTE Data)
  112. {
  113. DEV_Digital_Write(EPD_DC_PIN, 1);
  114. DEV_Digital_Write(EPD_CS_PIN, 0);
  115. DEV_SPI_WriteByte(Data);
  116. DEV_Digital_Write(EPD_CS_PIN, 1);
  117. }
  118. /******************************************************************************
  119. function : Wait until the busy_pin goes LOW
  120. parameter:
  121. ******************************************************************************/
  122. static void EPD_WaitUntilIdle(void)
  123. {
  124. Debug("e-Paper busy\r\n");
  125. do{
  126. DEV_Delay_ms(5);
  127. }while(!(DEV_Digital_Read(EPD_BUSY_PIN)));
  128. DEV_Delay_ms(5);
  129. Debug("e-Paper busy release\r\n");
  130. }
  131. static void EPD_7IN5_V2_LUT(UBYTE* lut_vcom, UBYTE* lut_ww, UBYTE* lut_bw, UBYTE* lut_wb, UBYTE* lut_bb)
  132. {
  133. UBYTE count;
  134. EPD_SendCommand(0x20); //VCOM
  135. for(count=0; count<60; count++)
  136. EPD_SendData(lut_vcom[count]);
  137. EPD_SendCommand(0x21); //LUTBW
  138. for(count=0; count<60; count++)
  139. EPD_SendData(lut_ww[count]);
  140. EPD_SendCommand(0x22); //LUTBW
  141. for(count=0; count<60; count++)
  142. EPD_SendData(lut_bw[count]);
  143. EPD_SendCommand(0x23); //LUTWB
  144. for(count=0; count<60; count++)
  145. EPD_SendData(lut_wb[count]);
  146. EPD_SendCommand(0x24); //LUTBB
  147. for(count=0; count<60; count++)
  148. EPD_SendData(lut_bb[count]);
  149. }
  150. /******************************************************************************
  151. function : Turn On Display
  152. parameter:
  153. ******************************************************************************/
  154. static void EPD_7IN5_V2_TurnOnDisplay(void)
  155. {
  156. EPD_SendCommand(0x12); //DISPLAY REFRESH
  157. DEV_Delay_ms(100); //!!!The delay here is necessary, 200uS at least!!!
  158. EPD_WaitUntilIdle();
  159. }
  160. /******************************************************************************
  161. function : Initialize the e-Paper register
  162. parameter:
  163. ******************************************************************************/
  164. UBYTE EPD_7IN5_V2_Init(void)
  165. {
  166. EPD_Reset();
  167. // EPD_SendCommand(0x01); //POWER SETTING
  168. // EPD_SendData(0x07);
  169. // EPD_SendData(0x07); //VGH=20V,VGL=-20V
  170. // EPD_SendData(0x3f); //VDH=15V
  171. // EPD_SendData(0x3f); //VDL=-15V
  172. EPD_SendCommand(0x01); // power setting
  173. EPD_SendData(0x17); // 1-0=11: internal power
  174. EPD_SendData(*(Voltage_Frame_7IN5_V2+6)); // VGH&VGL
  175. EPD_SendData(*(Voltage_Frame_7IN5_V2+1)); // VSH
  176. EPD_SendData(*(Voltage_Frame_7IN5_V2+2)); // VSL
  177. EPD_SendData(*(Voltage_Frame_7IN5_V2+3)); // VSHR
  178. EPD_SendCommand(0x82); // VCOM DC Setting
  179. EPD_SendData(*(Voltage_Frame_7IN5_V2+4)); // VCOM
  180. EPD_SendCommand(0x06); // Booster Setting
  181. EPD_SendData(0x27);
  182. EPD_SendData(0x27);
  183. EPD_SendData(0x2F);
  184. EPD_SendData(0x17);
  185. EPD_SendCommand(0x30); // OSC Setting
  186. EPD_SendData(*(Voltage_Frame_7IN5_V2+0)); // 2-0=100: N=4 ; 5-3=111: M=7 ; 3C=50Hz 3A=100HZ
  187. EPD_SendCommand(0x04); //POWER ON
  188. DEV_Delay_ms(100);
  189. EPD_WaitUntilIdle();
  190. EPD_SendCommand(0X00); //PANNEL SETTING
  191. EPD_SendData(0x3F); //KW-3f KWR-2F BWROTP 0f BWOTP 1f
  192. EPD_SendCommand(0x61); //tres
  193. EPD_SendData(0x03); //source 800
  194. EPD_SendData(0x20);
  195. EPD_SendData(0x01); //gate 480
  196. EPD_SendData(0xE0);
  197. EPD_SendCommand(0X15);
  198. EPD_SendData(0x00);
  199. EPD_SendCommand(0X50); //VCOM AND DATA INTERVAL SETTING
  200. EPD_SendData(0x10);
  201. EPD_SendData(0x00);
  202. EPD_SendCommand(0X60); //TCON SETTING
  203. EPD_SendData(0x22);
  204. EPD_SendCommand(0x65); // Resolution setting
  205. EPD_SendData(0x00);
  206. EPD_SendData(0x00);//800*480
  207. EPD_SendData(0x00);
  208. EPD_SendData(0x00);
  209. EPD_7IN5_V2_LUT(LUT_VCOM_7IN5_V2, LUT_WW_7IN5_V2, LUT_BW_7IN5_V2, LUT_WB_7IN5_V2, LUT_BB_7IN5_V2);
  210. return 0;
  211. }
  212. /******************************************************************************
  213. function : Clear screen
  214. parameter:
  215. ******************************************************************************/
  216. void EPD_7IN5_V2_Clear(void)
  217. {
  218. UWORD Width, Height;
  219. Width =(EPD_7IN5_V2_WIDTH % 8 == 0)?(EPD_7IN5_V2_WIDTH / 8 ):(EPD_7IN5_V2_WIDTH / 8 + 1);
  220. Height = EPD_7IN5_V2_HEIGHT;
  221. UWORD i;
  222. EPD_SendCommand(0x10);
  223. for(i=0; i<Height*Width; i++) {
  224. EPD_SendData(0xFF);
  225. }
  226. EPD_SendCommand(0x13);
  227. for(i=0; i<Height*Width; i++) {
  228. EPD_SendData(0x00);
  229. }
  230. EPD_7IN5_V2_TurnOnDisplay();
  231. }
  232. void EPD_7IN5_V2_ClearBlack(void)
  233. {
  234. UWORD Width, Height;
  235. Width =(EPD_7IN5_V2_WIDTH % 8 == 0)?(EPD_7IN5_V2_WIDTH / 8 ):(EPD_7IN5_V2_WIDTH / 8 + 1);
  236. Height = EPD_7IN5_V2_HEIGHT;
  237. UWORD i;
  238. EPD_SendCommand(0x10);
  239. for(i=0; i<Height*Width; i++) {
  240. EPD_SendData(0x00);
  241. }
  242. EPD_SendCommand(0x13);
  243. for(i=0; i<Height*Width; i++) {
  244. EPD_SendData(0xFF);
  245. }
  246. EPD_7IN5_V2_TurnOnDisplay();
  247. }
  248. /******************************************************************************
  249. function : Sends the image buffer in RAM to e-Paper and displays
  250. parameter:
  251. ******************************************************************************/
  252. void EPD_7IN5_V2_Display(const UBYTE *blackimage)
  253. {
  254. UDOUBLE Width, Height;
  255. Width =(EPD_7IN5_V2_WIDTH % 8 == 0)?(EPD_7IN5_V2_WIDTH / 8 ):(EPD_7IN5_V2_WIDTH / 8 + 1);
  256. Height = EPD_7IN5_V2_HEIGHT;
  257. // EPD_SendCommand(0x10);
  258. // for (UDOUBLE j = 0; j < Height; j++) {
  259. // for (UDOUBLE i = 0; i < Width; i++) {
  260. // EPD_SendData(blackimage[i + j * Width]);
  261. // }
  262. // }
  263. EPD_SendCommand(0x13);
  264. for (UDOUBLE j = 0; j < Height; j++) {
  265. for (UDOUBLE i = 0; i < Width; i++) {
  266. EPD_SendData(~blackimage[i + j * Width]);
  267. }
  268. }
  269. EPD_7IN5_V2_TurnOnDisplay();
  270. }
  271. /******************************************************************************
  272. function : Enter sleep mode
  273. parameter:
  274. ******************************************************************************/
  275. void EPD_7IN5_V2_Sleep(void)
  276. {
  277. EPD_SendCommand(0X02); //power off
  278. EPD_WaitUntilIdle();
  279. EPD_SendCommand(0X07); //deep sleep
  280. EPD_SendData(0xA5);
  281. }