EPD_1in54r.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. #include "EPD_1in54r.h"
  2. #include "Debug.h"
  3. const unsigned char EPD_1IN54R_lut_vcom0[] = {0x0E, 0x14, 0x01, 0x0A, 0x06, 0x04, 0x0A, 0x0A, 0x0F, 0x03, 0x03, 0x0C, 0x06, 0x0A, 0x00};
  4. const unsigned char EPD_1IN54R_lut_w[] = {0x0E, 0x14, 0x01, 0x0A, 0x46, 0x04, 0x8A, 0x4A, 0x0F, 0x83, 0x43, 0x0C, 0x86, 0x0A, 0x04};
  5. const unsigned char EPD_1IN54R_lut_b[] = {0x0E, 0x14, 0x01, 0x8A, 0x06, 0x04, 0x8A, 0x4A, 0x0F, 0x83, 0x43, 0x0C, 0x06, 0x4A, 0x04};
  6. const unsigned char EPD_1IN54R_lut_g1[] = {0x8E, 0x94, 0x01, 0x8A, 0x06, 0x04, 0x8A, 0x4A, 0x0F, 0x83, 0x43, 0x0C, 0x06, 0x0A, 0x04};
  7. const unsigned char EPD_1IN54R_lut_g2[] = {0x8E, 0x94, 0x01, 0x8A, 0x06, 0x04, 0x8A, 0x4A, 0x0F, 0x83, 0x43, 0x0C, 0x06, 0x0A, 0x04};
  8. const unsigned char EPD_1IN54R_lut_vcom1[] = {0x03, 0x1D, 0x01, 0x01, 0x08, 0x23, 0x37, 0x37, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
  9. const unsigned char EPD_1IN54R_lut_red0[] = {0x83, 0x5D, 0x01, 0x81, 0x48, 0x23, 0x77, 0x77, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
  10. const unsigned char EPD_1IN54R_lut_red1[] = {0x03, 0x1D, 0x01, 0x01, 0x08, 0x23, 0x37, 0x37, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
  11. /******************************************************************************
  12. function : Software reset
  13. parameter:
  14. ******************************************************************************/
  15. static void EPD_1IN54R_Reset(void)
  16. {
  17. DEV_Digital_Write(EPD_RST_PIN, 1);
  18. DEV_Delay_ms(100);
  19. DEV_Digital_Write(EPD_RST_PIN, 0);
  20. DEV_Delay_ms(2);
  21. DEV_Digital_Write(EPD_RST_PIN, 1);
  22. DEV_Delay_ms(100);
  23. }
  24. /******************************************************************************
  25. function : send command
  26. parameter:
  27. Reg : Command register
  28. ******************************************************************************/
  29. static void EPD_1IN54R_SendCommand(UBYTE Reg)
  30. {
  31. DEV_Digital_Write(EPD_DC_PIN, 0);
  32. DEV_Digital_Write(EPD_CS_PIN, 0);
  33. DEV_SPI_WriteByte(Reg);
  34. DEV_Digital_Write(EPD_CS_PIN, 1);
  35. }
  36. /******************************************************************************
  37. function : send data
  38. parameter:
  39. Data : Write data
  40. ******************************************************************************/
  41. static void EPD_1IN54R_SendData(UBYTE Data)
  42. {
  43. DEV_Digital_Write(EPD_DC_PIN, 1);
  44. DEV_Digital_Write(EPD_CS_PIN, 0);
  45. DEV_SPI_WriteByte(Data);
  46. DEV_Digital_Write(EPD_CS_PIN, 1);
  47. }
  48. /******************************************************************************
  49. function : Wait until the busy_pin goes LOW
  50. parameter:
  51. ******************************************************************************/
  52. static void EPD_1IN54R_ReadBusy(void)
  53. {
  54. EPD_Busy_WaitUntil(1,0);
  55. }
  56. /******************************************************************************
  57. function : Set the look-up black and white tables
  58. parameter:
  59. ******************************************************************************/
  60. static void EPD_1IN54R_SetLutBw(void)
  61. {
  62. UWORD count;
  63. EPD_1IN54R_SendCommand(0x20);// g vcom
  64. for(count = 0; count < 15; count++) {
  65. EPD_1IN54R_SendData(EPD_1IN54R_lut_vcom0[count]);
  66. }
  67. EPD_1IN54R_SendCommand(0x21);// g ww --
  68. for(count = 0; count < 15; count++) {
  69. EPD_1IN54R_SendData(EPD_1IN54R_lut_w[count]);
  70. }
  71. EPD_1IN54R_SendCommand(0x22);// g bw r
  72. for(count = 0; count < 15; count++) {
  73. EPD_1IN54R_SendData(EPD_1IN54R_lut_b[count]);
  74. }
  75. EPD_1IN54R_SendCommand(0x23);// g wb w
  76. for(count = 0; count < 15; count++) {
  77. EPD_1IN54R_SendData(EPD_1IN54R_lut_g1[count]);
  78. }
  79. EPD_1IN54R_SendCommand(0x24);// g bb b
  80. for(count = 0; count < 15; count++) {
  81. EPD_1IN54R_SendData(EPD_1IN54R_lut_g2[count]);
  82. }
  83. }
  84. /******************************************************************************
  85. function : Set the look-up red tables
  86. parameter:
  87. ******************************************************************************/
  88. static void EPD_1IN54R_SetLutRed(void)
  89. {
  90. UWORD count;
  91. EPD_1IN54R_SendCommand(0x25);
  92. for(count = 0; count < 15; count++) {
  93. EPD_1IN54R_SendData(EPD_1IN54R_lut_vcom1[count]);
  94. }
  95. EPD_1IN54R_SendCommand(0x26);
  96. for(count = 0; count < 15; count++) {
  97. EPD_1IN54R_SendData(EPD_1IN54R_lut_red0[count]);
  98. }
  99. EPD_1IN54R_SendCommand(0x27);
  100. for(count = 0; count < 15; count++) {
  101. EPD_1IN54R_SendData(EPD_1IN54R_lut_red1[count]);
  102. }
  103. }
  104. /******************************************************************************
  105. function : Initialize the e-Paper register
  106. parameter:
  107. ******************************************************************************/
  108. void EPD_1IN54R_Init(UBYTE mode)
  109. {
  110. EPD_1IN54R_Reset();
  111. EPD_1IN54R_SendCommand(0x01);// POWER_SETTING
  112. EPD_1IN54R_SendData(0x07);
  113. EPD_1IN54R_SendData(0x00);
  114. EPD_1IN54R_SendData(0x08);
  115. EPD_1IN54R_SendData(0x00);
  116. EPD_1IN54R_SendCommand(0x06);// BOOSTER_SOFT_START
  117. EPD_1IN54R_SendData(0x07);
  118. EPD_1IN54R_SendData(0x07);
  119. EPD_1IN54R_SendData(0x07);
  120. EPD_1IN54R_SendCommand(0x04);// POWER_ON
  121. EPD_1IN54R_ReadBusy();
  122. EPD_1IN54R_SendCommand(0X00);// PANEL_SETTING
  123. EPD_1IN54R_SendData(0xcf);
  124. EPD_1IN54R_SendCommand(0X50);// VCOM_AND_DATA_INTERVAL_SETTING
  125. EPD_1IN54R_SendData(0x37);// 0xF0
  126. EPD_1IN54R_SendCommand(0x30);// PLL_CONTROL
  127. EPD_1IN54R_SendData(0x39);
  128. EPD_1IN54R_SendCommand(0x61);// TCON_RESOLUTION set x and y
  129. EPD_1IN54R_SendData(0x98);// 152
  130. EPD_1IN54R_SendData(0x00);// y High eight: 0
  131. EPD_1IN54R_SendData(0x98);// y Low eight: 152
  132. EPD_1IN54R_SendCommand(0x82);// VCM_DC_SETTING_REGISTER
  133. EPD_1IN54R_SendData(0x0E);
  134. EPD_1IN54R_SetLutBw();
  135. EPD_1IN54R_SetLutRed();
  136. }
  137. /******************************************************************************
  138. function : Clear screen
  139. parameter:
  140. ******************************************************************************/
  141. void EPD_1IN54R_Clear(void)
  142. {
  143. UWORD Width, Height;
  144. Width = (EPD_1IN54R_WIDTH % 8 == 0)? (EPD_1IN54R_WIDTH / 8 ): (EPD_1IN54R_WIDTH / 8 + 1);
  145. Height = EPD_1IN54R_HEIGHT;
  146. //send black data
  147. EPD_1IN54R_SendCommand(0x10);// DATA_START_TRANSMISSION_1
  148. DEV_Delay_ms(2);
  149. for(UWORD i = 0; i < Height; i++) {
  150. for(UWORD i = 0; i < Width; i++) {
  151. EPD_1IN54R_SendData(0xFF);
  152. EPD_1IN54R_SendData(0xFF);
  153. }
  154. }
  155. DEV_Delay_ms(2);
  156. //send red data
  157. EPD_1IN54R_SendCommand(0x13);// DATA_START_TRANSMISSION_2
  158. DEV_Delay_ms(2);
  159. for(UWORD i = 0; i < Height; i++) {
  160. for(UWORD i = 0; i < Width; i++) {
  161. EPD_1IN54R_SendData(0xFF);
  162. }
  163. }
  164. DEV_Delay_ms(2);
  165. EPD_1IN54R_SendCommand(0x12);// DISPLAY_REFRESH
  166. EPD_1IN54R_ReadBusy();
  167. }
  168. /******************************************************************************
  169. function : Sends the image buffer in RAM to e-Paper and displays
  170. parameter:
  171. ******************************************************************************/
  172. void EPD_1IN54R_Display(const UBYTE *blackimage, const UBYTE *redimage)
  173. {
  174. // UBYTE Temp = 0x00;
  175. UWORD Width, Height;
  176. Width = (EPD_1IN54R_WIDTH % 8 == 0)? (EPD_1IN54R_WIDTH / 8 ): (EPD_1IN54R_WIDTH / 8 + 1);
  177. Height = EPD_1IN54R_HEIGHT;
  178. EPD_1IN54R_SendCommand(0x10);// DATA_START_TRANSMISSION_1
  179. for (UWORD j = 0; j < Height; j++) {
  180. for (UWORD i = 0; i < Width; i++) {
  181. EPD_1IN54R_SendData(blackimage[i + j * Width]);
  182. }
  183. }
  184. DEV_Delay_ms(2);
  185. EPD_1IN54R_SendCommand(0x13);// DATA_START_TRANSMISSION_2
  186. for (UWORD j = 0; j < Height; j++) {
  187. for (UWORD i = 0; i < Width; i++) {
  188. EPD_1IN54R_SendData(redimage[i + j * Width]);
  189. }
  190. }
  191. DEV_Delay_ms(2);
  192. //Display refresh
  193. EPD_1IN54R_SendCommand(0x12);// DISPLAY_REFRESH
  194. EPD_1IN54R_ReadBusy();
  195. }
  196. /******************************************************************************
  197. function : Enter sleep mode
  198. parameter:
  199. ******************************************************************************/
  200. void EPD_1IN54R_Sleep(void)
  201. {
  202. EPD_1IN54R_SendCommand(0x02);
  203. EPD_1IN54R_ReadBusy();
  204. EPD_1IN54R_SendCommand(0x07);
  205. EPD_1IN54R_SendData(0xa5);
  206. }