EPD_7in5b_V2.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  1. /*****************************************************************************
  2. * | File : EPD_7IN5b_V2.c
  3. * | Author : Waveshare team
  4. * | Function : Electronic paper driver
  5. * | Info :
  6. *----------------
  7. * | This version: V2.1
  8. * | Date : 2020-11-30
  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_7in5b_V2.h"
  31. #include "Debug.h"
  32. /******************************************************************************
  33. function : Software reset
  34. parameter:
  35. ******************************************************************************/
  36. static void EPD_7IN5B_V2_Reset(void)
  37. {
  38. DEV_Digital_Write(EPD_RST_PIN, 1);
  39. DEV_Delay_ms(200);
  40. DEV_Digital_Write(EPD_RST_PIN, 0);
  41. DEV_Delay_ms(2);
  42. DEV_Digital_Write(EPD_RST_PIN, 1);
  43. DEV_Delay_ms(200);
  44. }
  45. /******************************************************************************
  46. function : send command
  47. parameter:
  48. Reg : Command register
  49. ******************************************************************************/
  50. static void EPD_7IN5B_V2_SendCommand(UBYTE Reg)
  51. {
  52. DEV_Digital_Write(EPD_DC_PIN, 0);
  53. DEV_Digital_Write(EPD_CS_PIN, 0);
  54. DEV_SPI_WriteByte(Reg);
  55. DEV_Digital_Write(EPD_CS_PIN, 1);
  56. }
  57. /******************************************************************************
  58. function : send data
  59. parameter:
  60. Data : Write data
  61. ******************************************************************************/
  62. static void EPD_7IN5B_V2_SendData(UBYTE Data)
  63. {
  64. DEV_Digital_Write(EPD_DC_PIN, 1);
  65. DEV_Digital_Write(EPD_CS_PIN, 0);
  66. DEV_SPI_WriteByte(Data);
  67. DEV_Digital_Write(EPD_CS_PIN, 1);
  68. }
  69. /******************************************************************************
  70. function : Wait until the busy_pin goes LOW
  71. parameter:
  72. ******************************************************************************/
  73. void EPD_7IN5B_V2_WaitUntilIdle(void)
  74. {
  75. Debug("e-Paper busy\r\n");
  76. unsigned char busy;
  77. do {
  78. EPD_7IN5B_V2_SendCommand(0x71);
  79. busy = DEV_Digital_Read(EPD_BUSY_PIN);
  80. busy =!(busy & 0x01);
  81. }while(busy);
  82. DEV_Delay_ms(200);
  83. Debug("e-Paper busy release\r\n");
  84. }
  85. /******************************************************************************
  86. function : Turn On Display
  87. parameter:
  88. ******************************************************************************/
  89. static void EPD_7IN5B_V2_TurnOnDisplay(void)
  90. {
  91. EPD_7IN5B_V2_SendCommand(0x12); //DISPLAY REFRESH
  92. DEV_Delay_ms(100); //!!!The delay here is necessary, 200uS at least!!!
  93. EPD_7IN5B_V2_WaitUntilIdle();
  94. }
  95. /******************************************************************************
  96. function : Initialize the e-Paper register
  97. parameter:
  98. ******************************************************************************/
  99. UBYTE EPD_7IN5B_V2_Init(void)
  100. {
  101. EPD_7IN5B_V2_Reset();
  102. EPD_7IN5B_V2_SendCommand(0x01); //POWER SETTING
  103. EPD_7IN5B_V2_SendData(0x07);
  104. EPD_7IN5B_V2_SendData(0x07); //VGH=20V,VGL=-20V
  105. EPD_7IN5B_V2_SendData(0x3f); //VDH=15V
  106. EPD_7IN5B_V2_SendData(0x3f); //VDL=-15V
  107. EPD_7IN5B_V2_SendCommand(0x04); //POWER ON
  108. DEV_Delay_ms(100);
  109. EPD_7IN5B_V2_WaitUntilIdle();
  110. EPD_7IN5B_V2_SendCommand(0X00); //PANNEL SETTING
  111. EPD_7IN5B_V2_SendData(0x0F); //KW-3f KWR-2F BWROTP 0f BWOTP 1f
  112. EPD_7IN5B_V2_SendCommand(0x61); //tres
  113. EPD_7IN5B_V2_SendData(0x03); //source 800
  114. EPD_7IN5B_V2_SendData(0x20);
  115. EPD_7IN5B_V2_SendData(0x01); //gate 480
  116. EPD_7IN5B_V2_SendData(0xE0);
  117. EPD_7IN5B_V2_SendCommand(0X15);
  118. EPD_7IN5B_V2_SendData(0x00);
  119. EPD_7IN5B_V2_SendCommand(0X50); //VCOM AND DATA INTERVAL SETTING
  120. EPD_7IN5B_V2_SendData(0x11);
  121. EPD_7IN5B_V2_SendData(0x07);
  122. EPD_7IN5B_V2_SendCommand(0X60); //TCON SETTING
  123. EPD_7IN5B_V2_SendData(0x22);
  124. EPD_7IN5B_V2_SendCommand(0X82);
  125. EPD_7IN5B_V2_SendData(0x08);
  126. EPD_7IN5B_V2_SendCommand(0X30);
  127. EPD_7IN5B_V2_SendData(0x06);
  128. EPD_7IN5B_V2_SendCommand(0x65); // Resolution setting
  129. EPD_7IN5B_V2_SendData(0x00);
  130. EPD_7IN5B_V2_SendData(0x00);//800*480
  131. EPD_7IN5B_V2_SendData(0x00);
  132. EPD_7IN5B_V2_SendData(0x00);
  133. return 0;
  134. }
  135. UBYTE EPD_7IN5B_V2_Init_Fast(void)
  136. {
  137. EPD_7IN5B_V2_Reset();
  138. EPD_7IN5B_V2_SendCommand(0X00); //PANNEL SETTING
  139. EPD_7IN5B_V2_SendData(0x0F); //KW-3f KWR-2F BWROTP 0f BWOTP 1f
  140. EPD_7IN5B_V2_SendCommand(0x04); //POWER ON
  141. DEV_Delay_ms(100);
  142. EPD_7IN5B_V2_WaitUntilIdle();
  143. //Enhanced display drive(Add 0x06 command)
  144. EPD_7IN5B_V2_SendCommand(0x06); //Booster Soft Start
  145. EPD_7IN5B_V2_SendData(0x27);
  146. EPD_7IN5B_V2_SendData(0x27);
  147. EPD_7IN5B_V2_SendData(0x18);
  148. EPD_7IN5B_V2_SendData(0x17);
  149. EPD_7IN5B_V2_SendCommand(0xE0);
  150. EPD_7IN5B_V2_SendData(0x02);
  151. EPD_7IN5B_V2_SendCommand(0xE5);
  152. EPD_7IN5B_V2_SendData(0x5A);
  153. EPD_7IN5B_V2_SendCommand(0X50); //VCOM AND DATA INTERVAL SETTING
  154. EPD_7IN5B_V2_SendData(0x11);
  155. EPD_7IN5B_V2_SendData(0x07);
  156. return 0;
  157. }
  158. UBYTE EPD_7IN5B_V2_Init_Part(void)
  159. {
  160. EPD_7IN5B_V2_Reset();
  161. EPD_7IN5B_V2_SendCommand(0X00); //PANNEL SETTING
  162. EPD_7IN5B_V2_SendData(0x1F); //KW-3f KWR-2F BWROTP 0f BWOTP 1f
  163. EPD_7IN5B_V2_SendCommand(0x04); //POWER ON
  164. DEV_Delay_ms(100);
  165. EPD_7IN5B_V2_WaitUntilIdle();
  166. EPD_7IN5B_V2_SendCommand(0xE0);
  167. EPD_7IN5B_V2_SendData(0x02);
  168. EPD_7IN5B_V2_SendCommand(0xE5);
  169. EPD_7IN5B_V2_SendData(0x6E);
  170. EPD_7IN5B_V2_SendCommand(0X50); //VCOM AND DATA INTERVAL SETTING
  171. EPD_7IN5B_V2_SendData(0xA9);
  172. EPD_7IN5B_V2_SendData(0x07);
  173. return 0;
  174. }
  175. /******************************************************************************
  176. function : Clear screen
  177. parameter:
  178. ******************************************************************************/
  179. void EPD_7IN5B_V2_Clear(void)
  180. {
  181. UWORD Width, Height;
  182. Width =(EPD_7IN5B_V2_WIDTH % 8 == 0)?(EPD_7IN5B_V2_WIDTH / 8 ):(EPD_7IN5B_V2_WIDTH / 8 + 1);
  183. Height = EPD_7IN5B_V2_HEIGHT;
  184. UWORD i;
  185. EPD_7IN5B_V2_SendCommand(0x10);
  186. for(i=0; i<Width*Height; i++) {
  187. EPD_7IN5B_V2_SendData(0xff);
  188. }
  189. EPD_7IN5B_V2_SendCommand(0x13);
  190. for(i=0; i<Width*Height; i++) {
  191. EPD_7IN5B_V2_SendData(0x00);
  192. }
  193. EPD_7IN5B_V2_TurnOnDisplay();
  194. }
  195. void EPD_7IN5B_V2_ClearRed(void)
  196. {
  197. UWORD Width, Height;
  198. Width =(EPD_7IN5B_V2_WIDTH % 8 == 0)?(EPD_7IN5B_V2_WIDTH / 8 ):(EPD_7IN5B_V2_WIDTH / 8 + 1);
  199. Height = EPD_7IN5B_V2_HEIGHT;
  200. UWORD i;
  201. EPD_7IN5B_V2_SendCommand(0x10);
  202. for(i=0; i<Width*Height; i++) {
  203. EPD_7IN5B_V2_SendData(0xff);
  204. }
  205. EPD_7IN5B_V2_SendCommand(0x13);
  206. for(i=0; i<Width*Height; i++) {
  207. EPD_7IN5B_V2_SendData(0xff);
  208. }
  209. EPD_7IN5B_V2_TurnOnDisplay();
  210. }
  211. void EPD_7IN5B_V2_ClearBlack(void)
  212. {
  213. UWORD Width, Height;
  214. Width =(EPD_7IN5B_V2_WIDTH % 8 == 0)?(EPD_7IN5B_V2_WIDTH / 8 ):(EPD_7IN5B_V2_WIDTH / 8 + 1);
  215. Height = EPD_7IN5B_V2_HEIGHT;
  216. UWORD i;
  217. EPD_7IN5B_V2_SendCommand(0x10);
  218. for(i=0; i<Width*Height; i++) {
  219. EPD_7IN5B_V2_SendData(0x00);
  220. }
  221. EPD_7IN5B_V2_SendCommand(0x13);
  222. for(i=0; i<Width*Height; i++) {
  223. EPD_7IN5B_V2_SendData(0x00);
  224. }
  225. EPD_7IN5B_V2_TurnOnDisplay();
  226. }
  227. /******************************************************************************
  228. function : Sends the image buffer in RAM to e-Paper and displays
  229. parameter:
  230. ******************************************************************************/
  231. void EPD_7IN5B_V2_Display(const UBYTE *blackimage, const UBYTE *ryimage)
  232. {
  233. UDOUBLE Width, Height;
  234. Width =(EPD_7IN5B_V2_WIDTH % 8 == 0)?(EPD_7IN5B_V2_WIDTH / 8 ):(EPD_7IN5B_V2_WIDTH / 8 + 1);
  235. Height = EPD_7IN5B_V2_HEIGHT;
  236. //send black data
  237. EPD_7IN5B_V2_SendCommand(0x10);
  238. for (UDOUBLE j = 0; j < Height; j++) {
  239. for (UDOUBLE i = 0; i < Width; i++) {
  240. EPD_7IN5B_V2_SendData(blackimage[i + j * Width]);
  241. }
  242. }
  243. //send red data
  244. EPD_7IN5B_V2_SendCommand(0x13);
  245. for (UDOUBLE j = 0; j < Height; j++) {
  246. for (UDOUBLE i = 0; i < Width; i++) {
  247. EPD_7IN5B_V2_SendData(~ryimage[i + j * Width]);
  248. }
  249. }
  250. EPD_7IN5B_V2_TurnOnDisplay();
  251. }
  252. //0 1 is a black area and 2 3 is a red area
  253. void EPD_7IN5B_V2_WritePicture(const UBYTE *blackimage, UBYTE Block)
  254. {
  255. UDOUBLE Width, Height;
  256. Width =(EPD_7IN5B_V2_WIDTH % 8 == 0)?(EPD_7IN5B_V2_WIDTH / 8 ):(EPD_7IN5B_V2_WIDTH / 8 + 1);
  257. Height = EPD_7IN5B_V2_HEIGHT;
  258. if(Block == 0){
  259. EPD_7IN5B_V2_SendCommand(0x10);
  260. }else if(Block == 2){
  261. EPD_7IN5B_V2_SendCommand(0x13);
  262. }
  263. for (UDOUBLE j = 0; j < Height/2; j++) {
  264. for (UDOUBLE i = 0; i < Width; i++) {
  265. if(Block>=2){
  266. EPD_7IN5B_V2_SendData(~blackimage[i + j * Width]);
  267. }else{
  268. EPD_7IN5B_V2_SendData(blackimage[i + j * Width]);
  269. }
  270. }
  271. }
  272. if(Block == 3){
  273. EPD_7IN5B_V2_TurnOnDisplay();
  274. }
  275. }
  276. void EPD_7IN5B_V2_Display_Base_color(UBYTE color)
  277. {
  278. UWORD Width, Height;
  279. Width =(EPD_7IN5B_V2_WIDTH % 8 == 0)?(EPD_7IN5B_V2_WIDTH / 8 ):(EPD_7IN5B_V2_WIDTH / 8 + 1);
  280. Height = EPD_7IN5B_V2_HEIGHT;
  281. EPD_7IN5B_V2_SendCommand(0x10); //Write Black and White image to RAM
  282. for (UWORD j = 0; j < Height; j++) {
  283. for (UWORD i = 0; i < Width; i++) {
  284. EPD_7IN5B_V2_SendData(~color);
  285. }
  286. }
  287. EPD_7IN5B_V2_SendCommand(0x13); //Write Black and White image to RAM
  288. for (UWORD j = 0; j < Height; j++) {
  289. for (UWORD i = 0; i < Width; i++) {
  290. EPD_7IN5B_V2_SendData(color);
  291. }
  292. }
  293. // EPD_7IN5B_V2_TurnOnDisplay();
  294. }
  295. void EPD_7IN5B_V2_Display_Partial(const UBYTE *Image, UWORD Xstart, UWORD Ystart, UWORD Xend, UWORD Yend)
  296. {
  297. UDOUBLE Width, Height;
  298. Width =((Xend - Xstart) % 8 == 0)?((Xend - Xstart) / 8 ):((Xend - Xstart) / 8 + 1);
  299. Height = Yend - Ystart;
  300. //Reset
  301. // EPD_7IN5B_V2_Reset();
  302. // EPD_7IN5B_V2_SendCommand(0X50); //VCOM AND DATA INTERVAL SETTING
  303. // EPD_7IN5B_V2_SendData(0xA9);
  304. // EPD_7IN5B_V2_SendData(0x07);
  305. EPD_7IN5B_V2_SendCommand(0x91); //BorderWavefrom
  306. EPD_7IN5B_V2_SendCommand(0x90);
  307. EPD_7IN5B_V2_SendData(Xstart/256);
  308. EPD_7IN5B_V2_SendData(Xstart%256); //x-start
  309. EPD_7IN5B_V2_SendData(Xend/256);
  310. EPD_7IN5B_V2_SendData(Xend%256-1); //x-end
  311. EPD_7IN5B_V2_SendData(Ystart/256); //
  312. EPD_7IN5B_V2_SendData(Ystart%256); //y-start
  313. EPD_7IN5B_V2_SendData(Yend/256);
  314. EPD_7IN5B_V2_SendData(Yend%256-1); //y-end
  315. EPD_7IN5B_V2_SendData(0x01);
  316. EPD_7IN5B_V2_SendCommand(0x10); //Write Black and White image to RAM
  317. for (UDOUBLE j = 0; j < Height; j++) {
  318. for (UDOUBLE i = 0; i < Width; i++) {
  319. EPD_7IN5B_V2_SendData(0xff);
  320. }
  321. }
  322. EPD_7IN5B_V2_SendCommand(0x13); //Write Black and White image to RAM
  323. for (UDOUBLE j = 0; j < Height; j++) {
  324. for (UDOUBLE i = 0; i < Width; i++) {
  325. EPD_7IN5B_V2_SendData(Image[i + j * Width]);
  326. }
  327. }
  328. EPD_7IN5B_V2_TurnOnDisplay();
  329. EPD_7IN5B_V2_SendCommand(0x92);
  330. }
  331. /******************************************************************************
  332. function : Enter sleep mode
  333. parameter:
  334. ******************************************************************************/
  335. void EPD_7IN5B_V2_Sleep(void)
  336. {
  337. EPD_7IN5B_V2_SendCommand(0X02); //power off
  338. EPD_7IN5B_V2_WaitUntilIdle();
  339. EPD_7IN5B_V2_SendCommand(0X07); //deep sleep
  340. EPD_7IN5B_V2_SendData(0xA5);
  341. }