EPD_4in2.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  1. /*****************************************************************************
  2. * | File : EPD_4in2.c
  3. * | Author : Waveshare team
  4. * | Function : 4.2inch e-paper
  5. * | Info :
  6. *----------------
  7. * | This version: V3.0
  8. * | Date : 2019-06-13
  9. * | Info :
  10. * -----------------------------------------------------------------------------
  11. * V3.0(2019-06-13):
  12. * 1.Change:
  13. * lut_vcomDC[] => EPD_4IN2_lut_vcomDC[]
  14. * lut_ww[] => EPD_4IN2_lut_ww[]
  15. * lut_bw[] => EPD_4IN2_lut_bw[]
  16. * lut_wb[] => EPD_4IN2_lut_wb[]
  17. * lut_bb[] => EPD_4IN2_lut_bb[]
  18. * EPD_Reset() => EPD_4IN2_Reset()
  19. * EPD_SendCommand() => EPD_4IN2_SendCommand()
  20. * EPD_SendData() => EPD_4IN2_SendData()
  21. * EPD_WaitUntilIdle() => EPD_4IN2_ReadBusy()
  22. * EPD_SetFullReg() => EPD_4IN2_SetFullReg()
  23. * EPD_SetPartReg() => EPD_4IN2_SetPartReg()
  24. * EPD_TurnOnDisplay() => EPD_4IN2_TurnOnDisplay()
  25. * EPD_Init() => EPD_4IN2_Init()
  26. * EPD_Clear() => EPD_4IN2_Clear()
  27. * EPD_Display() => EPD_4IN2_Display()
  28. * EPD_Sleep() => EPD_4IN2_Sleep()
  29. * 2.remove commands define:
  30. * #define PANEL_SETTING 0x00
  31. * #define POWER_SETTING 0x01
  32. * #define POWER_OFF 0x02
  33. * #define POWER_OFF_SEQUENCE_SETTING 0x03
  34. * #define POWER_ON 0x04
  35. * #define POWER_ON_MEASURE 0x05
  36. * #define BOOSTER_SOFT_START 0x06
  37. * #define DEEP_SLEEP 0x07
  38. * #define DATA_START_TRANSMISSION_1 0x10
  39. * #define DATA_STOP 0x11
  40. * #define DISPLAY_REFRESH 0x12
  41. * #define DATA_START_TRANSMISSION_2 0x13
  42. * #define VCOM_LUT 0x20
  43. * #define W2W_LUT 0x21
  44. * #define B2W_LUT 0x22
  45. * #define W2B_LUT 0x23
  46. * #define B2B_LUT 0x24
  47. * #define PLL_CONTROL 0x30
  48. * #define TEMPERATURE_SENSOR_CALIBRATION 0x40
  49. * #define TEMPERATURE_SENSOR_SELECTION 0x41
  50. * #define TEMPERATURE_SENSOR_WRITE 0x42
  51. * #define TEMPERATURE_SENSOR_READ 0x43
  52. * #define VCOM_AND_DATA_INTERVAL_SETTING 0x50
  53. * #define LOW_POWER_DETECTION 0x51
  54. * #define TCON_SETTING 0x60
  55. * #define RESOLUTION_SETTING 0x61
  56. * #define GET_STATUS 0x71
  57. * #define AUTO_MEASURE_VCOM 0x80
  58. * #define READ_VCOM_VALUE 0x81
  59. * #define VCM_DC_SETTING 0x82
  60. * #define PARTIAL_WINDOW 0x90
  61. * #define PARTIAL_IN 0x91
  62. * #define PARTIAL_OUT 0x92
  63. * #define PROGRAM_MODE 0xA0
  64. * #define ACTIVE_PROGRAM 0xA1
  65. * #define READ_OTP_DATA 0xA2
  66. * #define POWER_SAVING 0xE3
  67. * V2.0(2018-10-30):
  68. * 1.Remove:ImageBuff[EPD_HEIGHT * EPD_WIDTH / 8]
  69. * 2.Change:EPD_Display(UBYTE *Image)
  70. * Need to pass parameters: pointer to cached data
  71. #
  72. # Permission is hereby granted, free of charge, to any person obtaining a copy
  73. # of this software and associated documnetation files (the "Software"), to deal
  74. # in the Software without restriction, including without limitation the rights
  75. # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  76. # copies of the Software, and to permit persons to whom the Software is
  77. # furished to do so, subject to the following conditions:
  78. #
  79. # The above copyright notice and this permission notice shall be included in
  80. # all copies or substantial portions of the Software.
  81. #
  82. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  83. # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  84. # FITNESS OR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  85. # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  86. # LIABILITY WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  87. # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  88. # THE SOFTWARE.
  89. #
  90. ******************************************************************************/
  91. #include "EPD_4in2.h"
  92. #include "Debug.h"
  93. static const unsigned char EPD_4IN2_lut_vcom0[] = {
  94. 0x00, 0x17, 0x00, 0x00, 0x00, 0x02,
  95. 0x00, 0x17, 0x17, 0x00, 0x00, 0x02,
  96. 0x00, 0x0A, 0x01, 0x00, 0x00, 0x01,
  97. 0x00, 0x0E, 0x0E, 0x00, 0x00, 0x02,
  98. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  99. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  100. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  101. };
  102. static const unsigned char EPD_4IN2_lut_ww[] = {
  103. 0x40, 0x17, 0x00, 0x00, 0x00, 0x02,
  104. 0x90, 0x17, 0x17, 0x00, 0x00, 0x02,
  105. 0x40, 0x0A, 0x01, 0x00, 0x00, 0x01,
  106. 0xA0, 0x0E, 0x0E, 0x00, 0x00, 0x02,
  107. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  108. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  109. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  110. };
  111. static const unsigned char EPD_4IN2_lut_bw[] = {
  112. 0x40, 0x17, 0x00, 0x00, 0x00, 0x02,
  113. 0x90, 0x17, 0x17, 0x00, 0x00, 0x02,
  114. 0x40, 0x0A, 0x01, 0x00, 0x00, 0x01,
  115. 0xA0, 0x0E, 0x0E, 0x00, 0x00, 0x02,
  116. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  117. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  118. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  119. };
  120. static const unsigned char EPD_4IN2_lut_wb[] = {
  121. 0x80, 0x17, 0x00, 0x00, 0x00, 0x02,
  122. 0x90, 0x17, 0x17, 0x00, 0x00, 0x02,
  123. 0x80, 0x0A, 0x01, 0x00, 0x00, 0x01,
  124. 0x50, 0x0E, 0x0E, 0x00, 0x00, 0x02,
  125. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  126. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  127. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  128. };
  129. static const unsigned char EPD_4IN2_lut_bb[] = {
  130. 0x80, 0x17, 0x00, 0x00, 0x00, 0x02,
  131. 0x90, 0x17, 0x17, 0x00, 0x00, 0x02,
  132. 0x80, 0x0A, 0x01, 0x00, 0x00, 0x01,
  133. 0x50, 0x0E, 0x0E, 0x00, 0x00, 0x02,
  134. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  135. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  136. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  137. };
  138. /******************************************************************************
  139. function : Software reset
  140. parameter:
  141. ******************************************************************************/
  142. static void EPD_4IN2_Reset(void)
  143. {
  144. DEV_Digital_Write(EPD_RST_PIN, 1);
  145. DEV_Delay_ms(200);
  146. DEV_Digital_Write(EPD_RST_PIN, 0);
  147. DEV_Delay_ms(200);
  148. DEV_Digital_Write(EPD_RST_PIN, 1);
  149. DEV_Delay_ms(200);
  150. }
  151. /******************************************************************************
  152. function : send command
  153. parameter:
  154. Reg : Command register
  155. ******************************************************************************/
  156. static void EPD_4IN2_SendCommand(UBYTE Reg)
  157. {
  158. DEV_Digital_Write(EPD_DC_PIN, 0);
  159. DEV_Digital_Write(EPD_CS_PIN, 0);
  160. DEV_SPI_WriteByte(Reg);
  161. DEV_Digital_Write(EPD_CS_PIN, 1);
  162. }
  163. /******************************************************************************
  164. function : send data
  165. parameter:
  166. Data : Write data
  167. ******************************************************************************/
  168. static void EPD_4IN2_SendData(UBYTE Data)
  169. {
  170. DEV_Digital_Write(EPD_DC_PIN, 1);
  171. DEV_Digital_Write(EPD_CS_PIN, 0);
  172. DEV_SPI_WriteByte(Data);
  173. DEV_Digital_Write(EPD_CS_PIN, 1);
  174. }
  175. /******************************************************************************
  176. function : Wait until the busy_pin goes LOW
  177. parameter:
  178. ******************************************************************************/
  179. void EPD_4IN2_ReadBusy(void)
  180. {
  181. Debug("e-Paper busy\r\n");
  182. while(DEV_Digital_Read(EPD_BUSY_PIN) == 0) { //LOW: idle, HIGH: busy
  183. DEV_Delay_ms(100);
  184. }
  185. Debug("e-Paper busy release\r\n");
  186. }
  187. /******************************************************************************
  188. function : Turn On Display
  189. parameter:
  190. ******************************************************************************/
  191. static void EPD_4IN2_TurnOnDisplay(void)
  192. {
  193. EPD_4IN2_SendCommand(0x12);
  194. DEV_Delay_ms(100);
  195. EPD_4IN2_ReadBusy();
  196. }
  197. /******************************************************************************
  198. function : set the look-up tables
  199. parameter:
  200. ******************************************************************************/
  201. static void EPD_4IN2_SetLut(void)
  202. {
  203. UWORD count;
  204. EPD_4IN2_SendCommand(0x20); //g vcom
  205. for(count=0; count<44; count++) {
  206. EPD_4IN2_SendData(EPD_4IN2_lut_vcom0[count]);
  207. }
  208. EPD_4IN2_SendCommand(0x21);
  209. for(count=0; count<42; count++) {
  210. EPD_4IN2_SendData(EPD_4IN2_lut_ww[count]);
  211. }
  212. EPD_4IN2_SendCommand(0x22);
  213. for(count=0; count<42; count++) {
  214. EPD_4IN2_SendData(EPD_4IN2_lut_bw[count]);
  215. }
  216. EPD_4IN2_SendCommand(0x23);
  217. for(count=0; count<42; count++) {
  218. EPD_4IN2_SendData(EPD_4IN2_lut_wb[count]);
  219. }
  220. EPD_4IN2_SendCommand(0x24);
  221. for(count=0; count<42; count++) {
  222. EPD_4IN2_SendData(EPD_4IN2_lut_bb[count]);
  223. }
  224. }
  225. /******************************************************************************
  226. function : Initialize the e-Paper register
  227. parameter:
  228. ******************************************************************************/
  229. void EPD_4IN2_Init(void)
  230. {
  231. EPD_4IN2_Reset();
  232. EPD_4IN2_SendCommand(0x01); // POWER SETTING
  233. EPD_4IN2_SendData(0x03);
  234. EPD_4IN2_SendData(0x00);
  235. EPD_4IN2_SendData(0x2b);
  236. EPD_4IN2_SendData(0x2b);
  237. EPD_4IN2_SendCommand(0x06); // boost soft start
  238. EPD_4IN2_SendData(0x17); //A
  239. EPD_4IN2_SendData(0x17); //B
  240. EPD_4IN2_SendData(0x17); //C
  241. EPD_4IN2_SendCommand(0x04); // POWER_ON
  242. EPD_4IN2_ReadBusy();
  243. EPD_4IN2_SendCommand(0x00); // panel setting
  244. EPD_4IN2_SendData(0xbf); // KW-BF KWR-AF BWROTP 0f BWOTP 1f
  245. EPD_4IN2_SendData(0x0d);
  246. EPD_4IN2_SendCommand(0x30); // PLL setting
  247. EPD_4IN2_SendData(0x3C); // 3A 100HZ 29 150Hz 39 200HZ 31 171HZ
  248. EPD_4IN2_SendCommand(0x61); // resolution setting
  249. EPD_4IN2_SendData(0x01);
  250. EPD_4IN2_SendData(0x90); //128
  251. EPD_4IN2_SendData(0x01); //
  252. EPD_4IN2_SendData(0x2c);
  253. EPD_4IN2_SendCommand(0x82); // vcom_DC setting
  254. EPD_4IN2_SendData(0x28);
  255. EPD_4IN2_SendCommand(0X50); // VCOM AND DATA INTERVAL SETTING
  256. EPD_4IN2_SendData(0x97); // 97white border 77black border VBDF 17|D7 VBDW 97 VBDB 57 VBDF F7 VBDW 77 VBDB 37 VBDR B7
  257. EPD_4IN2_SetLut();
  258. }
  259. /******************************************************************************
  260. function : Clear screen
  261. parameter:
  262. ******************************************************************************/
  263. void EPD_4IN2_Clear(void)
  264. {
  265. UWORD Width, Height;
  266. Width = (EPD_4IN2_WIDTH % 8 == 0)? (EPD_4IN2_WIDTH / 8 ): (EPD_4IN2_WIDTH / 8 + 1);
  267. Height = EPD_4IN2_HEIGHT;
  268. EPD_4IN2_SendCommand(0x10);
  269. for (UWORD j = 0; j < Height; j++) {
  270. for (UWORD i = 0; i < Width; i++) {
  271. EPD_4IN2_SendData(0xFF);
  272. }
  273. }
  274. EPD_4IN2_SendCommand(0x13);
  275. for (UWORD j = 0; j < Height; j++) {
  276. for (UWORD i = 0; i < Width; i++) {
  277. EPD_4IN2_SendData(0xFF);
  278. }
  279. }
  280. EPD_4IN2_TurnOnDisplay();
  281. }
  282. /******************************************************************************
  283. function : Sends the image buffer in RAM to e-Paper and displays
  284. parameter:
  285. ******************************************************************************/
  286. void EPD_4IN2_Display(UBYTE *Image)
  287. {
  288. UWORD Width, Height;
  289. Width = (EPD_4IN2_WIDTH % 8 == 0)? (EPD_4IN2_WIDTH / 8 ): (EPD_4IN2_WIDTH / 8 + 1);
  290. Height = EPD_4IN2_HEIGHT;
  291. EPD_4IN2_SendCommand(0x13);
  292. for (UWORD j = 0; j < Height; j++) {
  293. for (UWORD i = 0; i < Width; i++) {
  294. EPD_4IN2_SendData(Image[i + j * Width]);
  295. }
  296. }
  297. EPD_4IN2_TurnOnDisplay();
  298. }
  299. /******************************************************************************
  300. function : Enter sleep mode
  301. parameter:
  302. ******************************************************************************/
  303. void EPD_4IN2_Sleep(void)
  304. {
  305. EPD_4IN2_SendCommand(0x02); // POWER_OFF
  306. EPD_4IN2_ReadBusy();
  307. EPD_4IN2_SendCommand(0x07); // DEEP_SLEEP
  308. EPD_4IN2_SendData(0XA5);
  309. }