EPD_1in54_V3.c 8.3 KB

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