EPD_2in7.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  1. /*****************************************************************************
  2. * | File : EPD_2in7.c
  3. * | Author : Waveshare team
  4. * | Function : 2.7inch 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_vcom_dc[] => EPD_2in7_lut_vcom_dc[]
  14. * lut_ww[] => EPD_2in7_lut_ww[]
  15. * lut_bw[] => EPD_2in7_lut_bw[]
  16. * lut_bb[] => EPD_2in7_lut_bb[]
  17. * lut_wb[] => EPD_2in7_lut_wb[]
  18. * EPD_Reset() => EPD_2in7_Reset()
  19. * EPD_SendCommand() => EPD_2in7_SendCommand()
  20. * EPD_SendData() => EPD_2in7_SendData()
  21. * EPD_WaitUntilIdle() => EPD_2in7_ReadBusy()
  22. * EPD_SetLut() => EPD_2in7_SetLut()
  23. * EPD_Init() => EPD_2in7_Init()
  24. * EPD_Clear() => EPD_2in7_Clear()
  25. * EPD_Display() => EPD_2in7_Display()
  26. * EPD_Sleep() => EPD_2in7_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-11-06):
  59. * 1.Remove:ImageBuff[EPD_HEIGHT * EPD_WIDTH / 8]
  60. * 2.Change:EPD_Display(UBYTE *Image)
  61. * Need to pass parameters: pointer to cached data
  62. #
  63. # Permission is hereby granted, free of charge, to any person obtaining a copy
  64. # of this software and associated documnetation files (the "Software"), to deal
  65. # in the Software without restriction, including without limitation the rights
  66. # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  67. # copies of the Software, and to permit persons to whom the Software is
  68. # furished to do so, subject to the following conditions:
  69. #
  70. # The above copyright notice and this permission notice shall be included in
  71. # all copies or substantial portions of the Software.
  72. #
  73. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  74. # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  75. # FITNESS OR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  76. # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  77. # LIABILITY WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  78. # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  79. # THE SOFTWARE.
  80. #
  81. ******************************************************************************/
  82. #include "EPD_2in7.h"
  83. #include "Debug.h"
  84. static const unsigned char EPD_2in7_lut_vcom_dc[] = {
  85. 0x00 ,0x00,
  86. 0x00 ,0x08 ,0x00 ,0x00 ,0x00 ,0x02,
  87. 0x60 ,0x28 ,0x28 ,0x00 ,0x00 ,0x01,
  88. 0x00 ,0x14 ,0x00 ,0x00 ,0x00 ,0x01,
  89. 0x00 ,0x12 ,0x12 ,0x00 ,0x00 ,0x01,
  90. 0x00 ,0x00 ,0x00 ,0x00 ,0x00 ,0x00,
  91. 0x00 ,0x00 ,0x00 ,0x00 ,0x00 ,0x00,
  92. 0x00 ,0x00 ,0x00 ,0x00 ,0x00 ,0x00
  93. };
  94. static const unsigned char EPD_2in7_lut_ww[] = {
  95. 0x40 ,0x08 ,0x00 ,0x00 ,0x00 ,0x02,
  96. 0x90 ,0x28 ,0x28 ,0x00 ,0x00 ,0x01,
  97. 0x40 ,0x14 ,0x00 ,0x00 ,0x00 ,0x01,
  98. 0xA0 ,0x12 ,0x12 ,0x00 ,0x00 ,0x01,
  99. 0x00 ,0x00 ,0x00 ,0x00 ,0x00 ,0x00,
  100. 0x00 ,0x00 ,0x00 ,0x00 ,0x00 ,0x00,
  101. 0x00 ,0x00 ,0x00 ,0x00 ,0x00 ,0x00,
  102. };
  103. static const unsigned char EPD_2in7_lut_bw[] = {
  104. 0x40 ,0x08 ,0x00 ,0x00 ,0x00 ,0x02,
  105. 0x90 ,0x28 ,0x28 ,0x00 ,0x00 ,0x01,
  106. 0x40 ,0x14 ,0x00 ,0x00 ,0x00 ,0x01,
  107. 0xA0 ,0x12 ,0x12 ,0x00 ,0x00 ,0x01,
  108. 0x00 ,0x00 ,0x00 ,0x00 ,0x00 ,0x00,
  109. 0x00 ,0x00 ,0x00 ,0x00 ,0x00 ,0x00,
  110. 0x00 ,0x00 ,0x00 ,0x00 ,0x00 ,0x00,
  111. };
  112. static const unsigned char EPD_2in7_lut_bb[] = {
  113. 0x80 ,0x08 ,0x00 ,0x00 ,0x00 ,0x02,
  114. 0x90 ,0x28 ,0x28 ,0x00 ,0x00 ,0x01,
  115. 0x80 ,0x14 ,0x00 ,0x00 ,0x00 ,0x01,
  116. 0x50 ,0x12 ,0x12 ,0x00 ,0x00 ,0x01,
  117. 0x00 ,0x00 ,0x00 ,0x00 ,0x00 ,0x00,
  118. 0x00 ,0x00 ,0x00 ,0x00 ,0x00 ,0x00,
  119. 0x00 ,0x00 ,0x00 ,0x00 ,0x00 ,0x00,
  120. };
  121. static const unsigned char EPD_2in7_lut_wb[] = {
  122. 0x80 ,0x08 ,0x00 ,0x00 ,0x00 ,0x02,
  123. 0x90 ,0x28 ,0x28 ,0x00 ,0x00 ,0x01,
  124. 0x80 ,0x14 ,0x00 ,0x00 ,0x00 ,0x01,
  125. 0x50 ,0x12 ,0x12 ,0x00 ,0x00 ,0x01,
  126. 0x00 ,0x00 ,0x00 ,0x00 ,0x00 ,0x00,
  127. 0x00 ,0x00 ,0x00 ,0x00 ,0x00 ,0x00,
  128. 0x00 ,0x00 ,0x00 ,0x00 ,0x00 ,0x00,
  129. };
  130. /******************************************************************************
  131. function : Software reset
  132. parameter:
  133. ******************************************************************************/
  134. static void EPD_2in7_Reset(void)
  135. {
  136. DEV_Digital_Write(EPD_RST_PIN, 1);
  137. DEV_Delay_ms(200);
  138. DEV_Digital_Write(EPD_RST_PIN, 0);
  139. DEV_Delay_ms(10);
  140. DEV_Digital_Write(EPD_RST_PIN, 1);
  141. DEV_Delay_ms(200);
  142. }
  143. /******************************************************************************
  144. function : send command
  145. parameter:
  146. Reg : Command register
  147. ******************************************************************************/
  148. static void EPD_2in7_SendCommand(UBYTE Reg)
  149. {
  150. DEV_Digital_Write(EPD_DC_PIN, 0);
  151. DEV_Digital_Write(EPD_CS_PIN, 0);
  152. DEV_SPI_WriteByte(Reg);
  153. DEV_Digital_Write(EPD_CS_PIN, 1);
  154. }
  155. /******************************************************************************
  156. function : send data
  157. parameter:
  158. Data : Write data
  159. ******************************************************************************/
  160. static void EPD_2in7_SendData(UBYTE Data)
  161. {
  162. DEV_Digital_Write(EPD_DC_PIN, 1);
  163. DEV_Digital_Write(EPD_CS_PIN, 0);
  164. DEV_SPI_WriteByte(Data);
  165. DEV_Digital_Write(EPD_CS_PIN, 1);
  166. }
  167. /******************************************************************************
  168. function : Wait until the busy_pin goes LOW
  169. parameter:
  170. ******************************************************************************/
  171. static void EPD_2in7_ReadBusy(void)
  172. {
  173. Debug("e-Paper busy\r\n");
  174. UBYTE busy;
  175. do {
  176. EPD_2in7_SendCommand(0x71);
  177. busy = DEV_Digital_Read(EPD_BUSY_PIN);
  178. busy =!(busy & 0x01);
  179. } while(busy);
  180. DEV_Delay_ms(200);
  181. Debug("e-Paper busy release\r\n");
  182. }
  183. /******************************************************************************
  184. function : set the look-up tables
  185. parameter:
  186. ******************************************************************************/
  187. static void EPD_2in7_SetLut(void)
  188. {
  189. unsigned int count;
  190. EPD_2in7_SendCommand(0x20); //vcom
  191. for(count = 0; count < 44; count++) {
  192. EPD_2in7_SendData(EPD_2in7_lut_vcom_dc[count]);
  193. }
  194. EPD_2in7_SendCommand(0x21); //ww --
  195. for(count = 0; count < 42; count++) {
  196. EPD_2in7_SendData(EPD_2in7_lut_ww[count]);
  197. }
  198. EPD_2in7_SendCommand(0x22); //bw r
  199. for(count = 0; count < 42; count++) {
  200. EPD_2in7_SendData(EPD_2in7_lut_bw[count]);
  201. }
  202. EPD_2in7_SendCommand(0x23); //wb w
  203. for(count = 0; count < 42; count++) {
  204. EPD_2in7_SendData(EPD_2in7_lut_bb[count]);
  205. }
  206. EPD_2in7_SendCommand(0x24); //bb b
  207. for(count = 0; count < 42; count++) {
  208. EPD_2in7_SendData(EPD_2in7_lut_wb[count]);
  209. }
  210. }
  211. /******************************************************************************
  212. function : Initialize the e-Paper register
  213. parameter:
  214. ******************************************************************************/
  215. void EPD_2IN7_Init(void)
  216. {
  217. EPD_2in7_Reset();
  218. EPD_2in7_SendCommand(0x01); // POWER_SETTING
  219. EPD_2in7_SendData(0x03); // VDS_EN, VDG_EN
  220. EPD_2in7_SendData(0x00); // VCOM_HV, VGHL_LV[1], VGHL_LV[0]
  221. EPD_2in7_SendData(0x2b); // VDH
  222. EPD_2in7_SendData(0x2b); // VDL
  223. EPD_2in7_SendData(0x09); // VDHR
  224. EPD_2in7_SendCommand(0x06); // BOOSTER_SOFT_START
  225. EPD_2in7_SendData(0x07);
  226. EPD_2in7_SendData(0x07);
  227. EPD_2in7_SendData(0x17);
  228. // Power optimization
  229. EPD_2in7_SendCommand(0xF8);
  230. EPD_2in7_SendData(0x60);
  231. EPD_2in7_SendData(0xA5);
  232. // Power optimization
  233. EPD_2in7_SendCommand(0xF8);
  234. EPD_2in7_SendData(0x89);
  235. EPD_2in7_SendData(0xA5);
  236. // Power optimization
  237. EPD_2in7_SendCommand(0xF8);
  238. EPD_2in7_SendData(0x90);
  239. EPD_2in7_SendData(0x00);
  240. // Power optimization
  241. EPD_2in7_SendCommand(0xF8);
  242. EPD_2in7_SendData(0x93);
  243. EPD_2in7_SendData(0x2A);
  244. // Power optimization
  245. EPD_2in7_SendCommand(0xF8);
  246. EPD_2in7_SendData(0xA0);
  247. EPD_2in7_SendData(0xA5);
  248. // Power optimization
  249. EPD_2in7_SendCommand(0xF8);
  250. EPD_2in7_SendData(0xA1);
  251. EPD_2in7_SendData(0x00);
  252. // Power optimization
  253. EPD_2in7_SendCommand(0xF8);
  254. EPD_2in7_SendData(0x73);
  255. EPD_2in7_SendData(0x41);
  256. EPD_2in7_SendCommand(0x16); // PARTIAL_DISPLAY_REFRESH
  257. EPD_2in7_SendData(0x00);
  258. EPD_2in7_SendCommand(0x04); // POWER_ON
  259. EPD_2in7_ReadBusy();
  260. EPD_2in7_SendCommand(0x00); // PANEL_SETTING
  261. EPD_2in7_SendData(0xAF); // KW-BF KWR-AF BWROTP 0f
  262. EPD_2in7_SendCommand(0x30); // PLL_CONTROL
  263. EPD_2in7_SendData(0x3A); // 3A 100HZ 29 150Hz 39 200HZ 31 171HZ
  264. EPD_2in7_SendCommand(0x82); // VCM_DC_SETTING_REGISTER
  265. EPD_2in7_SendData(0x12);
  266. EPD_2in7_SetLut();
  267. }
  268. /******************************************************************************
  269. function : Clear screen
  270. parameter:
  271. ******************************************************************************/
  272. void EPD_2IN7_Clear(void)
  273. {
  274. UWORD Width, Height;
  275. Width = (EPD_2IN7_WIDTH % 8 == 0)? (EPD_2IN7_WIDTH / 8 ): (EPD_2IN7_WIDTH / 8 + 1);
  276. Height = EPD_2IN7_HEIGHT;
  277. EPD_2in7_SendCommand(0x10);
  278. for (UWORD j = 0; j < Height; j++) {
  279. for (UWORD i = 0; i < Width; i++) {
  280. EPD_2in7_SendData(0XFF);
  281. }
  282. }
  283. EPD_2in7_SendCommand(0x13);
  284. for (UWORD j = 0; j < Height; j++) {
  285. for (UWORD i = 0; i < Width; i++) {
  286. EPD_2in7_SendData(0XFF);
  287. }
  288. }
  289. EPD_2in7_SendCommand(0x12);
  290. EPD_2in7_ReadBusy();
  291. }
  292. /******************************************************************************
  293. function : Sends the image buffer in RAM to e-Paper and displays
  294. parameter:
  295. ******************************************************************************/
  296. void EPD_2IN7_Display(UBYTE *Image)
  297. {
  298. UWORD Width, Height;
  299. Width = (EPD_2IN7_WIDTH % 8 == 0)? (EPD_2IN7_WIDTH / 8 ): (EPD_2IN7_WIDTH / 8 + 1);
  300. Height = EPD_2IN7_HEIGHT;
  301. EPD_2in7_SendCommand(0x10);
  302. for (UWORD j = 0; j < Height; j++) {
  303. for (UWORD i = 0; i < Width; i++) {
  304. EPD_2in7_SendData(0XFF);
  305. }
  306. }
  307. EPD_2in7_SendCommand(0x13);
  308. for (UWORD j = 0; j < Height; j++) {
  309. for (UWORD i = 0; i < Width; i++) {
  310. EPD_2in7_SendData(Image[i + j * Width]);
  311. }
  312. }
  313. EPD_2in7_SendCommand(0x12);
  314. EPD_2in7_ReadBusy();
  315. }
  316. /******************************************************************************
  317. function : Enter sleep mode
  318. parameter:
  319. ******************************************************************************/
  320. void EPD_2IN7_Sleep(void)
  321. {
  322. EPD_2in7_SendCommand(0X50);
  323. EPD_2in7_SendData(0xf7);
  324. EPD_2in7_SendCommand(0X02); //power off
  325. EPD_2in7_SendCommand(0X07); //deep sleep
  326. EPD_2in7_SendData(0xA5);
  327. }