EPD_1in54.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  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(10);
  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. Debug("e-Paper busy\r\n");
  145. while(DEV_Digital_Read(EPD_BUSY_PIN) == 1) { //LOW: idle, HIGH: busy
  146. DEV_Delay_ms(100);
  147. }
  148. Debug("e-Paper busy release\r\n");
  149. }
  150. /******************************************************************************
  151. function : Setting the display window
  152. parameter:
  153. ******************************************************************************/
  154. static void EPD_1IN54_SetWindow(UWORD Xstart, UWORD Ystart, UWORD Xend, UWORD Yend)
  155. {
  156. EPD_1IN54_SendCommand(0x44); // SET_RAM_X_ADDRESS_START_END_POSITION
  157. EPD_1IN54_SendData((Xstart >> 3) & 0xFF);
  158. EPD_1IN54_SendData((Xend >> 3) & 0xFF);
  159. EPD_1IN54_SendCommand(0x45); // SET_RAM_Y_ADDRESS_START_END_POSITION
  160. EPD_1IN54_SendData(Ystart & 0xFF);
  161. EPD_1IN54_SendData((Ystart >> 8) & 0xFF);
  162. EPD_1IN54_SendData(Yend & 0xFF);
  163. EPD_1IN54_SendData((Yend >> 8) & 0xFF);
  164. }
  165. /******************************************************************************
  166. function : Set Cursor
  167. parameter:
  168. ******************************************************************************/
  169. static void EPD_1IN54_SetCursor(UWORD Xstart, UWORD Ystart)
  170. {
  171. EPD_1IN54_SendCommand(0x4E); // SET_RAM_X_ADDRESS_COUNTER
  172. EPD_1IN54_SendData((Xstart >> 3) & 0xFF);
  173. EPD_1IN54_SendCommand(0x4F); // SET_RAM_Y_ADDRESS_COUNTER
  174. EPD_1IN54_SendData(Ystart & 0xFF);
  175. EPD_1IN54_SendData((Ystart >> 8) & 0xFF);
  176. }
  177. /******************************************************************************
  178. function : Turn On Display
  179. parameter:
  180. ******************************************************************************/
  181. static void EPD_1IN54_TurnOnDisplay(void)
  182. {
  183. EPD_1IN54_SendCommand(0x22); // DISPLAY_UPDATE_CONTROL_2
  184. EPD_1IN54_SendData(0xC4);
  185. EPD_1IN54_SendCommand(0x20); // MASTER_ACTIVATION
  186. EPD_1IN54_SendCommand(0xFF); // TERMINATE_FRAME_READ_WRITE
  187. EPD_1IN54_ReadBusy();
  188. }
  189. /******************************************************************************
  190. function : Initialize the e-Paper register
  191. parameter:
  192. ******************************************************************************/
  193. void EPD_1IN54_Init(UBYTE Mode)
  194. {
  195. EPD_1IN54_Reset();
  196. EPD_1IN54_SendCommand(0x01); // DRIVER_OUTPUT_CONTROL
  197. EPD_1IN54_SendData((EPD_1IN54_HEIGHT - 1) & 0xFF);
  198. EPD_1IN54_SendData(((EPD_1IN54_HEIGHT - 1) >> 8) & 0xFF);
  199. EPD_1IN54_SendData(0x00); // GD = 0; SM = 0; TB = 0;
  200. EPD_1IN54_SendCommand(0x0C); // BOOSTER_SOFT_START_CONTROL
  201. EPD_1IN54_SendData(0xD7);
  202. EPD_1IN54_SendData(0xD6);
  203. EPD_1IN54_SendData(0x9D);
  204. EPD_1IN54_SendCommand(0x2C); // WRITE_VCOM_REGISTER
  205. EPD_1IN54_SendData(0xA8); // VCOM 7C
  206. EPD_1IN54_SendCommand(0x3A); // SET_DUMMY_LINE_PERIOD
  207. EPD_1IN54_SendData(0x1A); // 4 dummy lines per gate
  208. EPD_1IN54_SendCommand(0x3B); // SET_GATE_TIME
  209. EPD_1IN54_SendData(0x08); // 2us per line
  210. EPD_1IN54_SendCommand(0x11);
  211. EPD_1IN54_SendData(0x03);
  212. //set the look-up table register
  213. EPD_1IN54_SendCommand(0x32);
  214. if(Mode == EPD_1IN54_FULL){
  215. for (UWORD i = 0; i < 30; i++) {
  216. EPD_1IN54_SendData(EPD_1IN54_lut_full_update[i]);
  217. }
  218. }else if(Mode == EPD_1IN54_PART){
  219. for (UWORD i = 0; i < 30; i++) {
  220. EPD_1IN54_SendData(EPD_1IN54_lut_partial_update[i]);
  221. }
  222. }else{
  223. Debug("error, the Mode is EPD_1IN54_FULL or EPD_1IN54_PART");
  224. }
  225. }
  226. /******************************************************************************
  227. function : Clear screen
  228. parameter:
  229. ******************************************************************************/
  230. void EPD_1IN54_Clear(void)
  231. {
  232. UWORD Width, Height;
  233. Width = (EPD_1IN54_WIDTH % 8 == 0)? (EPD_1IN54_WIDTH / 8 ): (EPD_1IN54_WIDTH / 8 + 1);
  234. Height = EPD_1IN54_HEIGHT;
  235. EPD_1IN54_SetWindow(0, 0, EPD_1IN54_WIDTH, EPD_1IN54_HEIGHT);
  236. for (UWORD j = 0; j < Height; j++) {
  237. EPD_1IN54_SetCursor(0, j);
  238. EPD_1IN54_SendCommand(0x24);
  239. for (UWORD i = 0; i < Width; i++) {
  240. EPD_1IN54_SendData(0XFF);
  241. }
  242. }
  243. EPD_1IN54_TurnOnDisplay();
  244. }
  245. /******************************************************************************
  246. function : Sends the image buffer in RAM to e-Paper and displays
  247. parameter:
  248. ******************************************************************************/
  249. void EPD_1IN54_Display(UBYTE *Image)
  250. {
  251. UWORD Width, Height;
  252. Width = (EPD_1IN54_WIDTH % 8 == 0)? (EPD_1IN54_WIDTH / 8 ): (EPD_1IN54_WIDTH / 8 + 1);
  253. Height = EPD_1IN54_HEIGHT;
  254. UDOUBLE Addr = 0;
  255. // UDOUBLE Offset = ImageName;
  256. EPD_1IN54_SetWindow(0, 0, EPD_1IN54_WIDTH, EPD_1IN54_HEIGHT);
  257. for (UWORD j = 0; j < Height; j++) {
  258. EPD_1IN54_SetCursor(0, j);
  259. EPD_1IN54_SendCommand(0x24);
  260. for (UWORD i = 0; i < Width; i++) {
  261. Addr = i + j * Width;
  262. EPD_1IN54_SendData(Image[Addr]);
  263. }
  264. }
  265. EPD_1IN54_TurnOnDisplay();
  266. }
  267. /******************************************************************************
  268. function : Enter sleep mode
  269. parameter:
  270. ******************************************************************************/
  271. void EPD_1IN54_Sleep(void)
  272. {
  273. EPD_1IN54_SendCommand(0x10);
  274. EPD_1IN54_SendData(0x01);
  275. }