EPD_1in54.c 11 KB

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