EPD_7in5_V2.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  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(200);
  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(200);
  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. unsigned char busy;
  126. do{
  127. EPD_SendCommand(0x71);
  128. busy = DEV_Digital_Read(EPD_BUSY_PIN);
  129. busy =!(busy & 0x01);
  130. }while(busy);
  131. DEV_Delay_ms(200);
  132. Debug("e-Paper busy release\r\n");
  133. }
  134. static void EPD_7IN5_V2_LUT(UBYTE* lut_vcom, UBYTE* lut_ww, UBYTE* lut_bw, UBYTE* lut_wb, UBYTE* lut_bb)
  135. {
  136. UBYTE count;
  137. EPD_SendCommand(0x20); //VCOM
  138. for(count=0; count<60; count++)
  139. EPD_SendData(lut_vcom[count]);
  140. EPD_SendCommand(0x21); //LUTBW
  141. for(count=0; count<60; count++)
  142. EPD_SendData(lut_ww[count]);
  143. EPD_SendCommand(0x22); //LUTBW
  144. for(count=0; count<60; count++)
  145. EPD_SendData(lut_bw[count]);
  146. EPD_SendCommand(0x23); //LUTWB
  147. for(count=0; count<60; count++)
  148. EPD_SendData(lut_wb[count]);
  149. EPD_SendCommand(0x24); //LUTBB
  150. for(count=0; count<60; count++)
  151. EPD_SendData(lut_bb[count]);
  152. }
  153. /******************************************************************************
  154. function : Turn On Display
  155. parameter:
  156. ******************************************************************************/
  157. static void EPD_7IN5_V2_TurnOnDisplay(void)
  158. {
  159. EPD_SendCommand(0x12); //DISPLAY REFRESH
  160. DEV_Delay_ms(100); //!!!The delay here is necessary, 200uS at least!!!
  161. EPD_WaitUntilIdle();
  162. }
  163. /******************************************************************************
  164. function : Initialize the e-Paper register
  165. parameter:
  166. ******************************************************************************/
  167. UBYTE EPD_7IN5_V2_Init(void)
  168. {
  169. EPD_Reset();
  170. // EPD_SendCommand(0x01); //POWER SETTING
  171. // EPD_SendData(0x07);
  172. // EPD_SendData(0x07); //VGH=20V,VGL=-20V
  173. // EPD_SendData(0x3f); //VDH=15V
  174. // EPD_SendData(0x3f); //VDL=-15V
  175. // EPD_SendCommand(0x04); //POWER ON
  176. // DEV_Delay_ms(100);
  177. // EPD_WaitUntilIdle();
  178. // EPD_SendCommand(0X00); //PANNEL SETTING
  179. // EPD_SendData(0x1F); //KW-3f KWR-2F BWROTP 0f BWOTP 1f
  180. // EPD_SendCommand(0x61); //tres
  181. // EPD_SendData(0x03); //source 800
  182. // EPD_SendData(0x20);
  183. // EPD_SendData(0x01); //gate 480
  184. // EPD_SendData(0xE0);
  185. // EPD_SendCommand(0X15);
  186. // EPD_SendData(0x00);
  187. // EPD_SendCommand(0X50); //VCOM AND DATA INTERVAL SETTING
  188. // EPD_SendData(0x10);
  189. // EPD_SendData(0x07);
  190. // EPD_SendCommand(0X60); //TCON SETTING
  191. // EPD_SendData(0x22);
  192. EPD_SendCommand(0x01); // power setting
  193. EPD_SendData(0x17); // 1-0=11: internal power
  194. EPD_SendData(*(Voltage_Frame_7IN5_V2+6)); // VGH&VGL
  195. EPD_SendData(*(Voltage_Frame_7IN5_V2+1)); // VSH
  196. EPD_SendData(*(Voltage_Frame_7IN5_V2+2)); // VSL
  197. EPD_SendData(*(Voltage_Frame_7IN5_V2+3)); // VSHR
  198. EPD_SendCommand(0x82); // VCOM DC Setting
  199. EPD_SendData(*(Voltage_Frame_7IN5_V2+4)); // VCOM
  200. EPD_SendCommand(0x06); // Booster Setting
  201. EPD_SendData(0x27);
  202. EPD_SendData(0x27);
  203. EPD_SendData(0x2F);
  204. EPD_SendData(0x17);
  205. EPD_SendCommand(0x30); // OSC Setting
  206. EPD_SendData(*(Voltage_Frame_7IN5_V2+0)); // 2-0=100: N=4 ; 5-3=111: M=7 ; 3C=50Hz 3A=100HZ
  207. EPD_SendCommand(0x04); //POWER ON
  208. DEV_Delay_ms(100);
  209. EPD_WaitUntilIdle();
  210. EPD_SendCommand(0X00); //PANNEL SETTING
  211. EPD_SendData(0x3F); //KW-3f KWR-2F BWROTP 0f BWOTP 1f
  212. EPD_SendCommand(0x61); //tres
  213. EPD_SendData(0x03); //source 800
  214. EPD_SendData(0x20);
  215. EPD_SendData(0x01); //gate 480
  216. EPD_SendData(0xE0);
  217. EPD_SendCommand(0X15);
  218. EPD_SendData(0x00);
  219. EPD_SendCommand(0X50); //VCOM AND DATA INTERVAL SETTING
  220. EPD_SendData(0x10);
  221. EPD_SendData(0x00);
  222. EPD_SendCommand(0X60); //TCON SETTING
  223. EPD_SendData(0x22);
  224. EPD_SendCommand(0x65); // Resolution setting
  225. EPD_SendData(0x00);
  226. EPD_SendData(0x00);//800*480
  227. EPD_SendData(0x00);
  228. EPD_SendData(0x00);
  229. EPD_7IN5_V2_LUT(LUT_VCOM_7IN5_V2, LUT_WW_7IN5_V2, LUT_BW_7IN5_V2, LUT_WB_7IN5_V2, LUT_BB_7IN5_V2);
  230. return 0;
  231. }
  232. /******************************************************************************
  233. function : Clear screen
  234. parameter:
  235. ******************************************************************************/
  236. void EPD_7IN5_V2_Clear(void)
  237. {
  238. UWORD Width, Height;
  239. Width =(EPD_7IN5_V2_WIDTH % 8 == 0)?(EPD_7IN5_V2_WIDTH / 8 ):(EPD_7IN5_V2_WIDTH / 8 + 1);
  240. Height = EPD_7IN5_V2_HEIGHT;
  241. UWORD i;
  242. EPD_SendCommand(0x10);
  243. for(i=0; i<Height*Width; i++) {
  244. EPD_SendData(0x00);
  245. }
  246. EPD_SendCommand(0x13);
  247. for(i=0; i<Height*Width; i++) {
  248. EPD_SendData(0x00);
  249. }
  250. EPD_7IN5_V2_TurnOnDisplay();
  251. }
  252. void EPD_7IN5_V2_ClearBlack(void)
  253. {
  254. UWORD 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. UWORD i;
  258. EPD_SendCommand(0x13);
  259. for(i=0; i<Height*Width; i++) {
  260. EPD_SendData(0xFF);
  261. }
  262. EPD_7IN5_V2_TurnOnDisplay();
  263. }
  264. /******************************************************************************
  265. function : Sends the image buffer in RAM to e-Paper and displays
  266. parameter:
  267. ******************************************************************************/
  268. void EPD_7IN5_V2_Display(const UBYTE *blackimage)
  269. {
  270. UDOUBLE Width, Height;
  271. Width =(EPD_7IN5_V2_WIDTH % 8 == 0)?(EPD_7IN5_V2_WIDTH / 8 ):(EPD_7IN5_V2_WIDTH / 8 + 1);
  272. Height = EPD_7IN5_V2_HEIGHT;
  273. //send black data
  274. EPD_SendCommand(0x13);
  275. for (UDOUBLE j = 0; j < Height; j++) {
  276. for (UDOUBLE i = 0; i < Width; i++) {
  277. EPD_SendData(~blackimage[i + j * Width]);
  278. }
  279. }
  280. EPD_7IN5_V2_TurnOnDisplay();
  281. }
  282. void EPD_7IN5_V2_WritePicture(const UBYTE *blackimage, UBYTE Block)
  283. {
  284. UDOUBLE Width, Height;
  285. Width =(EPD_7IN5_V2_WIDTH % 8 == 0)?(EPD_7IN5_V2_WIDTH / 8 ):(EPD_7IN5_V2_WIDTH / 8 + 1);
  286. Height = EPD_7IN5_V2_HEIGHT;
  287. if(Block == 0){
  288. EPD_SendCommand(0x13);
  289. }
  290. for (UDOUBLE j = 0; j < Height/2; j++) {
  291. for (UDOUBLE i = 0; i < Width; i++) {
  292. EPD_SendData(~blackimage[i + j * Width]);
  293. }
  294. }
  295. if(Block == 1){
  296. EPD_7IN5_V2_TurnOnDisplay();
  297. }
  298. }
  299. /******************************************************************************
  300. function : Enter sleep mode
  301. parameter:
  302. ******************************************************************************/
  303. void EPD_7IN5_V2_Sleep(void)
  304. {
  305. EPD_SendCommand(0X02); //power off
  306. EPD_WaitUntilIdle();
  307. EPD_SendCommand(0X07); //deep sleep
  308. EPD_SendData(0xA5);
  309. }