EPD_1in54_V3.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. #include "EPD_1in54_V3.h"
  2. #include "Debug.h"
  3. void lut_bw(void);
  4. /******************************************************************************
  5. function : Software reset
  6. parameter:
  7. ******************************************************************************/
  8. static void EPD_1IN54_V3_Reset(void)
  9. {
  10. DEV_Digital_Write(EPD_RST_PIN, 1);
  11. DEV_Delay_ms(200);
  12. DEV_Digital_Write(EPD_RST_PIN, 0);
  13. DEV_Delay_ms(10);
  14. DEV_Digital_Write(EPD_RST_PIN, 1);
  15. DEV_Delay_ms(200);
  16. }
  17. /******************************************************************************
  18. function : send command
  19. parameter:
  20. Reg : Command register
  21. ******************************************************************************/
  22. static void EPD_1IN54_V3_SendCommand(UBYTE Reg)
  23. {
  24. DEV_Digital_Write(EPD_DC_PIN, 0);
  25. DEV_Digital_Write(EPD_CS_PIN, 0);
  26. DEV_SPI_WriteByte(Reg);
  27. DEV_Digital_Write(EPD_CS_PIN, 1);
  28. }
  29. /******************************************************************************
  30. function : send data
  31. parameter:
  32. Data : Write data
  33. ******************************************************************************/
  34. static void EPD_1IN54_V3_SendData(UBYTE Data)
  35. {
  36. DEV_Digital_Write(EPD_DC_PIN, 1);
  37. DEV_Digital_Write(EPD_CS_PIN, 0);
  38. DEV_SPI_WriteByte(Data);
  39. DEV_Digital_Write(EPD_CS_PIN, 1);
  40. }
  41. /******************************************************************************
  42. function : Wait until the busy_pin goes LOW
  43. parameter:
  44. ******************************************************************************/
  45. static void EPD_1IN54_V3_ReadBusy(void)
  46. {
  47. EPD_Busy_WaitUntil(1,0);
  48. // unsigned char count = 100;
  49. // Debug("e-Paper busy\r\n");
  50. // while(DEV_Digital_Read(EPD_BUSY_PIN) == 0) { //0:BUSY,1:FREE
  51. // if(!(count--))
  52. // {
  53. // Debug("error: e-Paper busy timeout!!!\r\n");
  54. // break;
  55. // }
  56. // else
  57. // DEV_Delay_ms(100);
  58. // }
  59. // Debug("e-Paper busy release\r\n");
  60. }
  61. /******************************************************************************
  62. function : Turn On Display full
  63. parameter:
  64. ******************************************************************************/
  65. static void EPD_1IN54_V3_TurnOnDisplay(void)
  66. {
  67. EPD_1IN54_V3_SendCommand(0xE0); //cascade setting
  68. EPD_1IN54_V3_SendData(0x01);
  69. EPD_1IN54_V3_SendCommand(0x12); //DISPLAY REFRESH
  70. DEV_Delay_ms(100); //!!!The delay here is necessary, 200uS at least!!!
  71. EPD_1IN54_V3_ReadBusy();
  72. }
  73. /******************************************************************************
  74. function : Turn On Display part
  75. parameter:
  76. ******************************************************************************/
  77. static void EPD_1IN54_V3_TurnOnDisplayPart(void)
  78. {
  79. //不支持局刷好像
  80. EPD_1IN54_V3_TurnOnDisplay();
  81. }
  82. /******************************************************************************
  83. function : Initialize the e-Paper register
  84. parameter:
  85. ******************************************************************************/
  86. void EPD_1IN54_V3_Init(UBYTE mode)
  87. {
  88. EPD_1IN54_V3_Reset();
  89. EPD_1IN54_V3_SendCommand(0x01); //power setting
  90. EPD_1IN54_V3_SendData(0xC7);
  91. EPD_1IN54_V3_SendData(0x00);
  92. EPD_1IN54_V3_SendData(0x0D);
  93. EPD_1IN54_V3_SendData(0x00);
  94. EPD_1IN54_V3_SendCommand(0x04);
  95. EPD_1IN54_V3_ReadBusy();
  96. EPD_1IN54_V3_SendCommand(0x00); //panel setting
  97. EPD_1IN54_V3_SendData(0xDf); //df-bw cf-r
  98. EPD_1IN54_V3_SendCommand(0X50); //VCOM AND DATA INTERVAL SETTING
  99. EPD_1IN54_V3_SendData(0x77); //WBmode:VBDF 17|D7 VBDW 97 VBDB 57 WBRmode:VBDF F7 VBDW 77 VBDB 37 VBDR B7
  100. EPD_1IN54_V3_SendCommand(0x30); //PLL setting
  101. EPD_1IN54_V3_SendData(0x2A);
  102. EPD_1IN54_V3_SendCommand(0x61); //resolution setting
  103. EPD_1IN54_V3_SendData(0xC8);
  104. EPD_1IN54_V3_SendData(0x00);
  105. EPD_1IN54_V3_SendData(0xC8);
  106. EPD_1IN54_V3_SendCommand(0x82); //vcom setting
  107. EPD_1IN54_V3_SendData(0x0A);
  108. lut_bw();
  109. }
  110. //2s
  111. const unsigned char EPD_1IN54_V3_lut_vcom0[] ={0x02 ,0x03 ,0x03 ,0x08 ,0x08 ,0x03 ,0x05 ,0x05 ,0x03 ,0x00 ,0x00 ,0x00 ,0x00 ,0x00 ,0x00 };
  112. const unsigned char EPD_1IN54_V3_lut_w[] ={ 0x42 ,0x43 ,0x03 ,0x48 ,0x88 ,0x03 ,0x85 ,0x08 ,0x03 ,0x00 ,0x00 ,0x00 ,0x00 ,0x00 ,0x00 };
  113. const unsigned char EPD_1IN54_V3_lut_b[] ={ 0x82 ,0x83 ,0x03 ,0x48 ,0x88 ,0x03 ,0x05 ,0x45 ,0x03 ,0x00 ,0x00 ,0x00 ,0x00 ,0x00 ,0x00 };
  114. const unsigned char EPD_1IN54_V3_lut_g1[] ={ 0x82 ,0x83 ,0x03 ,0x48 ,0x88 ,0x03 ,0x05 ,0x45 ,0x03 ,0x00 ,0x00 ,0x00 ,0x00 ,0x00 ,0x00 };
  115. const unsigned char EPD_1IN54_V3_lut_g2[] ={ 0x82 ,0x83 ,0x03 ,0x48 ,0x88 ,0x03 ,0x05 ,0x45 ,0x03 ,0x00 ,0x00 ,0x00 ,0x00 ,0x00 ,0x00 };
  116. void lut_bw(void)
  117. {
  118. unsigned int count;
  119. EPD_1IN54_V3_SendCommand(0x20);
  120. for(count=0;count<15;count++)
  121. {EPD_1IN54_V3_SendData(EPD_1IN54_V3_lut_vcom0[count]);}
  122. EPD_1IN54_V3_SendCommand(0x21);
  123. for(count=0;count<15;count++)
  124. {EPD_1IN54_V3_SendData(EPD_1IN54_V3_lut_w[count]);}
  125. EPD_1IN54_V3_SendCommand(0x22);
  126. for(count=0;count<15;count++)
  127. {EPD_1IN54_V3_SendData(EPD_1IN54_V3_lut_b[count]);}
  128. EPD_1IN54_V3_SendCommand(0x23);
  129. for(count=0;count<15;count++)
  130. {EPD_1IN54_V3_SendData(EPD_1IN54_V3_lut_g1[count]);}
  131. EPD_1IN54_V3_SendCommand(0x24);
  132. for(count=0;count<15;count++)
  133. {EPD_1IN54_V3_SendData(EPD_1IN54_V3_lut_g2[count]);}
  134. }
  135. /******************************************************************************
  136. function : Clear screen
  137. parameter:
  138. ******************************************************************************/
  139. void EPD_1IN54_V3_Clear(void)
  140. {
  141. UWORD Width, Height;
  142. Width = (EPD_1IN54_V3_WIDTH % 8 == 0)? (EPD_1IN54_V3_WIDTH / 8 ): (EPD_1IN54_V3_WIDTH / 8 + 1);
  143. Height = EPD_1IN54_V3_HEIGHT*2;
  144. EPD_1IN54_V3_SendCommand(0x10);
  145. for (UWORD j = 0; j < Height; j++) {
  146. for (UWORD i = 0; i < Width; i++) {
  147. EPD_1IN54_V3_SendData(0XFF);
  148. }
  149. }
  150. EPD_1IN54_V3_TurnOnDisplay();
  151. }
  152. /******************************************************************************
  153. function : Sends the image buffer in RAM to e-Paper and displays
  154. parameter:
  155. ******************************************************************************/
  156. void EPD_1IN54_V3_Display(UBYTE *Image, UBYTE *Image2)
  157. {
  158. UBYTE Temp = 0x00;
  159. UWORD Width, Height;
  160. Width = (EPD_1IN54_V3_WIDTH % 8 == 0)? (EPD_1IN54_V3_WIDTH / 8 ): (EPD_1IN54_V3_WIDTH / 8 + 1);
  161. Height = EPD_1IN54_V3_HEIGHT;
  162. UDOUBLE Addr = 0;
  163. EPD_1IN54_V3_SendCommand(0x10);
  164. for (UWORD j = 0; j < Height; j++) {
  165. for (UWORD i = 0; i < Width; i++) {
  166. Addr = i + j * Width;
  167. Temp = 0x00;
  168. for (int bit = 0; bit < 4; bit++) {
  169. if ((Image[Addr] & (0x80 >> bit)) != 0) {
  170. Temp |= 0xC0 >> (bit * 2);
  171. }
  172. }
  173. EPD_1IN54_V3_SendData(Temp);
  174. Temp = 0x00;
  175. for (int bit = 4; bit < 8; bit++) {
  176. if ((Image[Addr] & (0x80 >> bit)) != 0) {
  177. Temp |= 0xC0 >> ((bit - 4) * 2);
  178. }
  179. }
  180. EPD_1IN54_V3_SendData(Temp);
  181. }
  182. }
  183. EPD_1IN54_V3_TurnOnDisplay();
  184. }
  185. /******************************************************************************
  186. function : The image of the previous frame must be uploaded, otherwise the
  187. first few seconds will display an exception.
  188. parameter:
  189. ******************************************************************************/
  190. void EPD_1IN54_V3_DisplayPartBaseImage(UBYTE *Image)
  191. {
  192. EPD_1IN54_V3_Display(Image, NULL);
  193. }
  194. /******************************************************************************
  195. function : Sends the image buffer in RAM to e-Paper and displays
  196. parameter:
  197. ******************************************************************************/
  198. void EPD_1IN54_V3_DisplayPart(UBYTE *Image)
  199. {
  200. EPD_1IN54_V3_Display(Image, NULL);
  201. }
  202. /******************************************************************************
  203. function : Enter sleep mode
  204. parameter:
  205. ******************************************************************************/
  206. void EPD_1IN54_V3_Sleep(void)
  207. {
  208. EPD_1IN54_V3_SendCommand(0x82);
  209. EPD_1IN54_V3_SendData(0x00);
  210. EPD_1IN54_V3_SendCommand(0x01); //power setting
  211. EPD_1IN54_V3_SendData(0x02); //gate switch to external
  212. EPD_1IN54_V3_SendData(0x00);
  213. EPD_1IN54_V3_SendData(0x00);
  214. EPD_1IN54_V3_SendData(0x00);
  215. DEV_Delay_ms(1500);
  216. EPD_1IN54_V3_SendCommand(0X02); //deep sleep
  217. DEV_Delay_ms(100);
  218. }