EPD_1in54.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. /*****************************************************************************
  2. * | File : EPD_1in54.C
  3. * | Author : Waveshare team
  4. * | Function : 1.54inch e-paper
  5. * | Info :
  6. *----------------
  7. * | This version: V3.0
  8. * | Date : 2019-06-12
  9. * | Info :
  10. * -----------------------------------------------------------------------------
  11. * V3.0(2019-06-12):
  12. * 1.Change:
  13. * lut_full_update[] => EPD_1IN54_lut_full_update[]
  14. * lut_partial_update[] => EPD_1IN54_lut_partial_update[]
  15. * EPD_Reset() => EPD_1IN54_Reset()
  16. * EPD_SendCommand() => EPD_1IN54_SendCommand()
  17. * EPD_SendData() => EPD_1IN54_SendData()
  18. * EPD_WaitUntilIdle() => EPD_1IN54_ReadBusy()
  19. * EPD_SetLut() => EPD_1IN54_SetLut()
  20. * EPD_SetWindow() => EPD_1IN54_SetWindow()
  21. * EPD_SetCursor() => EPD_1IN54_SetCursor()
  22. * EPD_TurnOnDisplay() => EPD_1IN54_TurnOnDisplay()
  23. * EPD_Init() => EPD_1IN54_Init()
  24. * EPD_Clear() => EPD_1IN54_Clear()
  25. * EPD_Display() => EPD_1IN54_Display()
  26. * EPD_Sleep() => EPD_1IN54_Sleep()
  27. * 2.remove commands define:
  28. * #define PANEL_SETTING 0x00
  29. * #define POWER_SETTING 0x01
  30. * #define POWER_OFF 0x02
  31. * #define POWER_OFF_SEQUENCE_SETTING 0x03
  32. * #define POWER_ON 0x04
  33. * #define POWER_ON_MEASURE 0x05
  34. * #define BOOSTER_SOFT_START 0x06
  35. * #define DEEP_SLEEP 0x07
  36. * #define DATA_START_TRANSMISSION_1 0x10
  37. * #define DATA_STOP 0x11
  38. * #define DISPLAY_REFRESH 0x12
  39. * #define DATA_START_TRANSMISSION_2 0x13
  40. * #define PLL_CONTROL 0x30
  41. * #define TEMPERATURE_SENSOR_COMMAND 0x40
  42. * #define TEMPERATURE_SENSOR_CALIBRATION 0x41
  43. * #define TEMPERATURE_SENSOR_WRITE 0x42
  44. * #define TEMPERATURE_SENSOR_READ 0x43
  45. * #define VCOM_AND_DATA_INTERVAL_SETTING 0x50
  46. * #define LOW_POWER_DETECTION 0x51
  47. * #define TCON_SETTING 0x60
  48. * #define TCON_RESOLUTION 0x61
  49. * #define SOURCE_AND_GATE_START_SETTING 0x62
  50. * #define GET_STATUS 0x71
  51. * #define AUTO_MEASURE_VCOM 0x80
  52. * #define VCOM_VALUE 0x81
  53. * #define VCM_DC_SETTING_REGISTER 0x82
  54. * #define PROGRAM_MODE 0xA0
  55. * #define ACTIVE_PROGRAM 0xA1
  56. * #define READ_OTP_DATA 0xA2
  57. * -----------------------------------------------------------------------------
  58. * V2.0(2018-10-30):
  59. * 1.Remove:ImageBuff[EPD_1IN54_HEIGHT * EPD_1IN54_WIDTH / 8]
  60. * 2.Change:EPD_Display(UBYTE *Image)
  61. * Need to pass parameters: pointer to cached data
  62. * 3.Change:
  63. * EPD_RST -> EPD_RST_PIN
  64. * EPD_DC -> EPD_DC_PIN
  65. * EPD_CS -> EPD_CS_PIN
  66. * EPD_BUSY -> EPD_BUSY_PIN
  67. #
  68. # Permission is hereby granted, free of charge, to any person obtaining a copy
  69. # of this software and associated documnetation files (the "Software"), to deal
  70. # in the Software without restriction, including without limitation the rights
  71. # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  72. # copies of the Software, and to permit persons to whom the Software is
  73. # furished to do so, subject to the following conditions:
  74. #
  75. # The above copyright notice and this permission notice shall be included in
  76. # all copies or substantial portions of the Software.
  77. #
  78. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  79. # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  80. # FITNESS OR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  81. # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  82. # LIABILITY WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  83. # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  84. # THE SOFTWARE.
  85. #
  86. ******************************************************************************/
  87. #include "EPD_1in54.h"
  88. #include "Debug.h"
  89. static const unsigned char EPD_1IN54_lut_full_update[] = {
  90. 0x02, 0x02, 0x01, 0x11, 0x12, 0x12, 0x22, 0x22,
  91. 0x66, 0x69, 0x69, 0x59, 0x58, 0x99, 0x99, 0x88,
  92. 0x00, 0x00, 0x00, 0x00, 0xF8, 0xB4, 0x13, 0x51,
  93. 0x35, 0x51, 0x51, 0x19, 0x01, 0x00
  94. };
  95. static const unsigned char EPD_1IN54_lut_partial_update[] = {
  96. 0x10, 0x18, 0x18, 0x08, 0x18, 0x18, 0x08, 0x00,
  97. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  98. 0x00, 0x00, 0x00, 0x00, 0x13, 0x14, 0x44, 0x12,
  99. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  100. };
  101. /******************************************************************************
  102. function : Software reset
  103. parameter:
  104. ******************************************************************************/
  105. static void EPD_1IN54_Reset(void)
  106. {
  107. DEV_Digital_Write(EPD_RST_PIN, 1);
  108. DEV_Delay_ms(200);
  109. DEV_Digital_Write(EPD_RST_PIN, 0);
  110. DEV_Delay_ms(2);
  111. DEV_Digital_Write(EPD_RST_PIN, 1);
  112. DEV_Delay_ms(200);
  113. }
  114. /******************************************************************************
  115. function : send command
  116. parameter:
  117. Reg : Command register
  118. ******************************************************************************/
  119. static void EPD_1IN54_SendCommand(UBYTE Reg)
  120. {
  121. DEV_Digital_Write(EPD_DC_PIN, 0);
  122. DEV_Digital_Write(EPD_CS_PIN, 0);
  123. DEV_SPI_WriteByte(Reg);
  124. DEV_Digital_Write(EPD_CS_PIN, 1);
  125. }
  126. /******************************************************************************
  127. function : send data
  128. parameter:
  129. Data : Write data
  130. ******************************************************************************/
  131. static void EPD_1IN54_SendData(UBYTE Data)
  132. {
  133. DEV_Digital_Write(EPD_DC_PIN, 1);
  134. DEV_Digital_Write(EPD_CS_PIN, 0);
  135. DEV_SPI_WriteByte(Data);
  136. DEV_Digital_Write(EPD_CS_PIN, 1);
  137. }
  138. /******************************************************************************
  139. function : Wait until the busy_pin goes LOW
  140. parameter:
  141. ******************************************************************************/
  142. void EPD_1IN54_ReadBusy(void)
  143. {
  144. EPD_Busy_WaitUntil(0,0);
  145. // unsigned char count = 100;
  146. // Debug("e-Paper busy\r\n");
  147. // while(DEV_Digital_Read(EPD_BUSY_PIN) == 1) { //LOW: idle, HIGH: busy
  148. // if(!(count--))
  149. // {
  150. // Debug("error: e-Paper busy timeout!!!\r\n");
  151. // break;
  152. // }
  153. // else
  154. // DEV_Delay_ms(100);
  155. // }
  156. // Debug("e-Paper busy release\r\n");
  157. }
  158. /******************************************************************************
  159. function : Setting the display window
  160. parameter:
  161. ******************************************************************************/
  162. static void EPD_1IN54_SetWindow(UWORD Xstart, UWORD Ystart, UWORD Xend, UWORD Yend)
  163. {
  164. EPD_1IN54_SendCommand(0x44); // SET_RAM_X_ADDRESS_START_END_POSITION
  165. EPD_1IN54_SendData((Xstart >> 3) & 0xFF);
  166. EPD_1IN54_SendData((Xend >> 3) & 0xFF);
  167. EPD_1IN54_SendCommand(0x45); // SET_RAM_Y_ADDRESS_START_END_POSITION
  168. EPD_1IN54_SendData(Ystart & 0xFF);
  169. EPD_1IN54_SendData((Ystart >> 8) & 0xFF);
  170. EPD_1IN54_SendData(Yend & 0xFF);
  171. EPD_1IN54_SendData((Yend >> 8) & 0xFF);
  172. }
  173. /******************************************************************************
  174. function : Set Cursor
  175. parameter:
  176. ******************************************************************************/
  177. static void EPD_1IN54_SetCursor(UWORD Xstart, UWORD Ystart)
  178. {
  179. EPD_1IN54_SendCommand(0x4E); // SET_RAM_X_ADDRESS_COUNTER
  180. EPD_1IN54_SendData((Xstart >> 3) & 0xFF);
  181. EPD_1IN54_SendCommand(0x4F); // SET_RAM_Y_ADDRESS_COUNTER
  182. EPD_1IN54_SendData(Ystart & 0xFF);
  183. EPD_1IN54_SendData((Ystart >> 8) & 0xFF);
  184. }
  185. /******************************************************************************
  186. function : Turn On Display
  187. parameter:
  188. ******************************************************************************/
  189. static void EPD_1IN54_TurnOnDisplay(void)
  190. {
  191. EPD_1IN54_SendCommand(0x22); // DISPLAY_UPDATE_CONTROL_2
  192. EPD_1IN54_SendData(0xC4);
  193. EPD_1IN54_SendCommand(0x20); // MASTER_ACTIVATION
  194. EPD_1IN54_SendCommand(0xFF); // TERMINATE_FRAME_READ_WRITE
  195. EPD_1IN54_ReadBusy();
  196. }
  197. /******************************************************************************
  198. function : Initialize the e-Paper register
  199. parameter:
  200. ******************************************************************************/
  201. void EPD_1IN54_Init(UBYTE Mode)
  202. {
  203. EPD_1IN54_Reset();
  204. EPD_1IN54_SendCommand(0x01); // DRIVER_OUTPUT_CONTROL
  205. EPD_1IN54_SendData((EPD_1IN54_HEIGHT - 1) & 0xFF);
  206. EPD_1IN54_SendData(((EPD_1IN54_HEIGHT - 1) >> 8) & 0xFF);
  207. EPD_1IN54_SendData(0x00); // GD = 0; SM = 0; TB = 0;
  208. EPD_1IN54_SendCommand(0x0C); // BOOSTER_SOFT_START_CONTROL
  209. EPD_1IN54_SendData(0xD7);
  210. EPD_1IN54_SendData(0xD6);
  211. EPD_1IN54_SendData(0x9D);
  212. EPD_1IN54_SendCommand(0x2C); // WRITE_VCOM_REGISTER
  213. EPD_1IN54_SendData(0xA8); // VCOM 7C
  214. EPD_1IN54_SendCommand(0x3A); // SET_DUMMY_LINE_PERIOD
  215. EPD_1IN54_SendData(0x1A); // 4 dummy lines per gate
  216. EPD_1IN54_SendCommand(0x3B); // SET_GATE_TIME
  217. EPD_1IN54_SendData(0x08); // 2us per line
  218. EPD_1IN54_SendCommand(0x11);
  219. EPD_1IN54_SendData(0x03);
  220. //set the look-up table register
  221. EPD_1IN54_SendCommand(0x32);
  222. if(Mode == EPD_1IN54_FULL){
  223. for (UWORD i = 0; i < 30; i++) {
  224. EPD_1IN54_SendData(EPD_1IN54_lut_full_update[i]);
  225. }
  226. }else if(Mode == EPD_1IN54_PART){
  227. for (UWORD i = 0; i < 30; i++) {
  228. EPD_1IN54_SendData(EPD_1IN54_lut_partial_update[i]);
  229. }
  230. }else{
  231. Debug("error, the Mode is EPD_1IN54_FULL or EPD_1IN54_PART");
  232. }
  233. }
  234. /******************************************************************************
  235. function : Clear screen
  236. parameter:
  237. ******************************************************************************/
  238. void EPD_1IN54_Clear(void)
  239. {
  240. UWORD Width, Height;
  241. Width = (EPD_1IN54_WIDTH % 8 == 0)? (EPD_1IN54_WIDTH / 8 ): (EPD_1IN54_WIDTH / 8 + 1);
  242. Height = EPD_1IN54_HEIGHT;
  243. EPD_1IN54_SetWindow(0, 0, EPD_1IN54_WIDTH, EPD_1IN54_HEIGHT);
  244. for (UWORD j = 0; j < Height; j++) {
  245. EPD_1IN54_SetCursor(0, j);
  246. EPD_1IN54_SendCommand(0x24);
  247. for (UWORD i = 0; i < Width; i++) {
  248. EPD_1IN54_SendData(0XFF);
  249. }
  250. }
  251. EPD_1IN54_TurnOnDisplay();
  252. }
  253. /******************************************************************************
  254. function : Sends the image buffer in RAM to e-Paper and displays
  255. parameter:
  256. ******************************************************************************/
  257. void EPD_1IN54_Display(UBYTE *Image, UBYTE *Image2)
  258. {
  259. UWORD Width, Height;
  260. Width = (EPD_1IN54_WIDTH % 8 == 0)? (EPD_1IN54_WIDTH / 8 ): (EPD_1IN54_WIDTH / 8 + 1);
  261. Height = EPD_1IN54_HEIGHT;
  262. UDOUBLE Addr = 0;
  263. // UDOUBLE Offset = ImageName;
  264. EPD_1IN54_SetWindow(0, 0, EPD_1IN54_WIDTH, EPD_1IN54_HEIGHT);
  265. for (UWORD j = 0; j < Height; j++) {
  266. EPD_1IN54_SetCursor(0, j);
  267. EPD_1IN54_SendCommand(0x24);
  268. for (UWORD i = 0; i < Width; i++) {
  269. Addr = i + j * Width;
  270. EPD_1IN54_SendData(Image[Addr]);
  271. }
  272. }
  273. EPD_1IN54_TurnOnDisplay();
  274. }
  275. /******************************************************************************
  276. function : Enter sleep mode
  277. parameter:
  278. ******************************************************************************/
  279. void EPD_1IN54_Sleep(void)
  280. {
  281. EPD_1IN54_SendCommand(0x10);
  282. EPD_1IN54_SendData(0x01);
  283. }