EPD_2in7b.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  1. /*****************************************************************************
  2. * | File : EPD_2in7b.c
  3. * | Author : Waveshare team
  4. * | Function : 2.7inch e-paper b
  5. * | Info :
  6. *----------------
  7. * | This version: V3.0
  8. * | Date : 2019-06-12
  9. * | Info :
  10. * -----------------------------------------------------------------------------
  11. * V3.0(2019-06-12):
  12. * lut_vcom_dc[] => EPD_2in7B_lut_vcom_dc[]
  13. * lut_ww[] => EPD_2in7B_lut_ww[]
  14. * lut_bw[] => EPD_2in7B_lut_bw[]
  15. * lut_bb[] => EPD_2in7B_lut_bb[]
  16. * lut_wb[] => EPD_2in7B_lut_wb[]
  17. * EPD_Reset() => EPD_2in7B_Reset()
  18. * EPD_SendCommand() => EPD_2in7B_SendCommand()
  19. * EPD_SendData() => EPD_2in7B_SendData()
  20. * EPD_WaitUntilIdle() => EPD_2in7B_ReadBusy()
  21. * EPD_SetLut() => EPD_2in7B_SetLut()
  22. * EPD_Init() => EPD_2in7B_Init()
  23. * EPD_Clear() => EPD_2in7B_Clear()
  24. * EPD_Display() => EPD_2in7B_Display()
  25. * EPD_Sleep() => EPD_2in7B_Sleep()
  26. * 2.remove commands define:
  27. * #define PANEL_SETTING 0x00
  28. * #define POWER_SETTING 0x01
  29. * #define POWER_OFF 0x02
  30. * #define POWER_OFF_SEQUENCE_SETTING 0x03
  31. * #define POWER_ON 0x04
  32. * #define POWER_ON_MEASURE 0x05
  33. * #define BOOSTER_SOFT_START 0x06
  34. * #define DEEP_SLEEP 0x07
  35. * #define DATA_START_TRANSMISSION_1 0x10
  36. * #define DATA_STOP 0x11
  37. * #define DISPLAY_REFRESH 0x12
  38. * #define DATA_START_TRANSMISSION_2 0x13
  39. * #define PARTIAL_DATA_START_TRANSMISSION_1 0x14
  40. * #define PARTIAL_DATA_START_TRANSMISSION_2 0x15
  41. * #define PARTIAL_DISPLAY_REFRESH 0x16
  42. * #define LUT_FOR_VCOM 0x20
  43. * #define LUT_WHITE_TO_WHITE 0x21
  44. * #define LUT_BLACK_TO_WHITE 0x22
  45. * #define LUT_WHITE_TO_BLACK 0x23
  46. * #define LUT_BLACK_TO_BLACK 0x24
  47. * #define PLL_CONTROL 0x30
  48. * #define TEMPERATURE_SENSOR_COMMAND 0x40
  49. * #define TEMPERATURE_SENSOR_CALIBRATION 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 TCON_RESOLUTION 0x61
  56. * #define SOURCE_AND_GATE_START_SETTING 0x62
  57. * #define GET_STATUS 0x71
  58. * #define AUTO_MEASURE_VCOM 0x80
  59. * #define VCOM_VALUE 0x81
  60. * #define VCM_DC_SETTING_REGISTER 0x82
  61. * #define PROGRAM_MODE 0xA0
  62. * #define ACTIVE_PROGRAM 0xA1
  63. * #define READ_OTP_DATA 0xA2
  64. * -----------------------------------------------------------------------------
  65. * V2.0(2018-10-30):
  66. * 1.Remove:ImageBuff[EPD_2IN7B_HEIGHT * EPD_2IN7B_WIDTH / 8]
  67. * 2.Change:EPD_Display(UBYTE *Image)
  68. * Need to pass parameters: pointer to cached data
  69. #
  70. # Permission is hereby granted, free of charge, to any person obtaining a copy
  71. # of this software and associated documnetation files (the "Software"), to deal
  72. # in the Software without restriction, including without limitation the rights
  73. # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  74. # copies of the Software, and to permit persons to whom the Software is
  75. # furished to do so, subject to the following conditions:
  76. #
  77. # The above copyright notice and this permission notice shall be included in
  78. # all copies or substantial portions of the Software.
  79. #
  80. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  81. # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  82. # FITNESS OR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  83. # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  84. # LIABILITY WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  85. # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  86. # THE SOFTWARE.
  87. #
  88. ******************************************************************************/
  89. #include "EPD_2in7b.h"
  90. #include "Debug.h"
  91. static const unsigned char EPD_2IN7B_lut_vcom_dc[] = {
  92. 0x00, 0x00,
  93. 0x00, 0x1A, 0x1A, 0x00, 0x00, 0x01,
  94. 0x00, 0x0A, 0x0A, 0x00, 0x00, 0x08,
  95. 0x00, 0x0E, 0x01, 0x0E, 0x01, 0x10,
  96. 0x00, 0x0A, 0x0A, 0x00, 0x00, 0x08,
  97. 0x00, 0x04, 0x10, 0x00, 0x00, 0x05,
  98. 0x00, 0x03, 0x0E, 0x00, 0x00, 0x0A,
  99. 0x00, 0x23, 0x00, 0x00, 0x00, 0x01
  100. };
  101. //R21H
  102. static const unsigned char EPD_2IN7B_lut_ww[] = {
  103. 0x90, 0x1A, 0x1A, 0x00, 0x00, 0x01,
  104. 0x40, 0x0A, 0x0A, 0x00, 0x00, 0x08,
  105. 0x84, 0x0E, 0x01, 0x0E, 0x01, 0x10,
  106. 0x80, 0x0A, 0x0A, 0x00, 0x00, 0x08,
  107. 0x00, 0x04, 0x10, 0x00, 0x00, 0x05,
  108. 0x00, 0x03, 0x0E, 0x00, 0x00, 0x0A,
  109. 0x00, 0x23, 0x00, 0x00, 0x00, 0x01
  110. };
  111. //R22H r
  112. static const unsigned char EPD_2IN7B_lut_bw[] = {
  113. 0xA0, 0x1A, 0x1A, 0x00, 0x00, 0x01,
  114. 0x00, 0x0A, 0x0A, 0x00, 0x00, 0x08,
  115. 0x84, 0x0E, 0x01, 0x0E, 0x01, 0x10,
  116. 0x90, 0x0A, 0x0A, 0x00, 0x00, 0x08,
  117. 0xB0, 0x04, 0x10, 0x00, 0x00, 0x05,
  118. 0xB0, 0x03, 0x0E, 0x00, 0x00, 0x0A,
  119. 0xC0, 0x23, 0x00, 0x00, 0x00, 0x01
  120. };
  121. //R23H w
  122. static const unsigned char EPD_2IN7B_lut_bb[] = {
  123. 0x90, 0x1A, 0x1A, 0x00, 0x00, 0x01,
  124. 0x40, 0x0A, 0x0A, 0x00, 0x00, 0x08,
  125. 0x84, 0x0E, 0x01, 0x0E, 0x01, 0x10,
  126. 0x80, 0x0A, 0x0A, 0x00, 0x00, 0x08,
  127. 0x00, 0x04, 0x10, 0x00, 0x00, 0x05,
  128. 0x00, 0x03, 0x0E, 0x00, 0x00, 0x0A,
  129. 0x00, 0x23, 0x00, 0x00, 0x00, 0x01
  130. };
  131. //R24H b
  132. static const unsigned char EPD_2IN7B_lut_wb[] = {
  133. 0x90, 0x1A, 0x1A, 0x00, 0x00, 0x01,
  134. 0x20, 0x0A, 0x0A, 0x00, 0x00, 0x08,
  135. 0x84, 0x0E, 0x01, 0x0E, 0x01, 0x10,
  136. 0x10, 0x0A, 0x0A, 0x00, 0x00, 0x08,
  137. 0x00, 0x04, 0x10, 0x00, 0x00, 0x05,
  138. 0x00, 0x03, 0x0E, 0x00, 0x00, 0x0A,
  139. 0x00, 0x23, 0x00, 0x00, 0x00, 0x01
  140. };
  141. /******************************************************************************
  142. function : Software reset
  143. parameter:
  144. ******************************************************************************/
  145. static void EPD_2IN7B_Reset(void)
  146. {
  147. DEV_Digital_Write(EPD_RST_PIN, 1);
  148. DEV_Delay_ms(200);
  149. DEV_Digital_Write(EPD_RST_PIN, 0);
  150. DEV_Delay_ms(2);
  151. DEV_Digital_Write(EPD_RST_PIN, 1);
  152. DEV_Delay_ms(200);
  153. }
  154. /******************************************************************************
  155. function : send command
  156. parameter:
  157. Reg : Command register
  158. ******************************************************************************/
  159. static void EPD_2IN7B_SendCommand(UBYTE Reg)
  160. {
  161. DEV_Digital_Write(EPD_DC_PIN, 0);
  162. DEV_Digital_Write(EPD_CS_PIN, 0);
  163. DEV_SPI_WriteByte(Reg);
  164. DEV_Digital_Write(EPD_CS_PIN, 1);
  165. }
  166. /******************************************************************************
  167. function : send data
  168. parameter:
  169. Data : Write data
  170. ******************************************************************************/
  171. static void EPD_2IN7B_SendData(UBYTE Data)
  172. {
  173. DEV_Digital_Write(EPD_DC_PIN, 1);
  174. DEV_Digital_Write(EPD_CS_PIN, 0);
  175. DEV_SPI_WriteByte(Data);
  176. DEV_Digital_Write(EPD_CS_PIN, 1);
  177. }
  178. /******************************************************************************
  179. function : Wait until the busy_pin goes LOW
  180. parameter:
  181. ******************************************************************************/
  182. static void EPD_2IN7B_ReadBusy(void)
  183. {
  184. unsigned char count = 100;
  185. Debug("e-Paper busy\r\n");
  186. while(DEV_Digital_Read(EPD_BUSY_PIN) == 0) { //0: busy, 1: idle
  187. if(!(count--))
  188. {
  189. Debug("error: e-Paper busy timeout!!!\r\n");
  190. break;
  191. }
  192. else
  193. DEV_Delay_ms(100);
  194. }
  195. Debug("e-Paper busy release\r\n");
  196. }
  197. /******************************************************************************
  198. function : set the look-up tables
  199. parameter:
  200. ******************************************************************************/
  201. static void EPD_2IN7B_SetLut(void)
  202. {
  203. unsigned int count;
  204. EPD_2IN7B_SendCommand(0x20); //vcom
  205. for(count = 0; count < 44; count++) {
  206. EPD_2IN7B_SendData(EPD_2IN7B_lut_vcom_dc[count]);
  207. }
  208. EPD_2IN7B_SendCommand(0x21); //ww --
  209. for(count = 0; count < 42; count++) {
  210. EPD_2IN7B_SendData(EPD_2IN7B_lut_ww[count]);
  211. }
  212. EPD_2IN7B_SendCommand(0x22); //bw r
  213. for(count = 0; count < 42; count++) {
  214. EPD_2IN7B_SendData(EPD_2IN7B_lut_bw[count]);
  215. }
  216. EPD_2IN7B_SendCommand(0x23); //wb w
  217. for(count = 0; count < 42; count++) {
  218. EPD_2IN7B_SendData(EPD_2IN7B_lut_bb[count]);
  219. }
  220. EPD_2IN7B_SendCommand(0x24); //bb b
  221. for(count = 0; count < 42; count++) {
  222. EPD_2IN7B_SendData(EPD_2IN7B_lut_wb[count]);
  223. }
  224. }
  225. /******************************************************************************
  226. function : Initialize the e-Paper register
  227. parameter:
  228. ******************************************************************************/
  229. void EPD_2IN7B_Init(UBYTE mode)
  230. {
  231. EPD_2IN7B_Reset();
  232. EPD_2IN7B_SendCommand(0x04); // POWER_ON
  233. EPD_2IN7B_ReadBusy();
  234. EPD_2IN7B_SendCommand(0x00); // PANEL_SETTING
  235. EPD_2IN7B_SendData(0xaf); // KW-BF KWR-AF BWROTP 0f
  236. EPD_2IN7B_SendCommand(0x30); // PLL_CONTROL
  237. EPD_2IN7B_SendData(0x3a); //3A 100HZ 29 150Hz 39 200HZ 31 171HZ
  238. EPD_2IN7B_SendCommand(0x01); // PANEL_SETTING
  239. EPD_2IN7B_SendData(0x03); // VDS_EN, VDG_EN
  240. EPD_2IN7B_SendData(0x00); // VCOM_HV, VGHL_LV[1], VGHL_LV[0]
  241. EPD_2IN7B_SendData(0x2b); // VDH
  242. EPD_2IN7B_SendData(0x2b); // VDL
  243. EPD_2IN7B_SendData(0x09); // VDHR
  244. EPD_2IN7B_SendCommand(0x06); // BOOSTER_SOFT_START
  245. EPD_2IN7B_SendData(0x07);
  246. EPD_2IN7B_SendData(0x07);
  247. EPD_2IN7B_SendData(0x17);
  248. // Power optimization
  249. EPD_2IN7B_SendCommand(0xF8);
  250. EPD_2IN7B_SendData(0x60);
  251. EPD_2IN7B_SendData(0xA5);
  252. // Power optimization
  253. EPD_2IN7B_SendCommand(0xF8);
  254. EPD_2IN7B_SendData(0x89);
  255. EPD_2IN7B_SendData(0xA5);
  256. // Power optimization
  257. EPD_2IN7B_SendCommand(0xF8);
  258. EPD_2IN7B_SendData(0x90);
  259. EPD_2IN7B_SendData(0x00);
  260. // Power optimization
  261. EPD_2IN7B_SendCommand(0xF8);
  262. EPD_2IN7B_SendData(0x93);
  263. EPD_2IN7B_SendData(0x2A);
  264. // Power optimization
  265. EPD_2IN7B_SendCommand(0xF8);
  266. EPD_2IN7B_SendData(0x73);
  267. EPD_2IN7B_SendData(0x41);
  268. EPD_2IN7B_SendCommand(0x82); // VCM_DC_SETTING_REGISTER
  269. EPD_2IN7B_SendData(0x12);
  270. EPD_2IN7B_SendCommand(0x50); // VCOM_AND_DATA_INTERVAL_SETTING
  271. EPD_2IN7B_SendData(0x87); // define by OTP
  272. EPD_2IN7B_SetLut();
  273. EPD_2IN7B_SendCommand(0x16); // PARTIAL_DISPLAY_REFRESH
  274. EPD_2IN7B_SendData(0x00);
  275. }
  276. /******************************************************************************
  277. function : Clear screen
  278. parameter:
  279. ******************************************************************************/
  280. void EPD_2IN7B_Clear(void)
  281. {
  282. UWORD Width, Height;
  283. Width = (EPD_2IN7B_WIDTH % 8 == 0)? (EPD_2IN7B_WIDTH / 8 ): (EPD_2IN7B_WIDTH / 8 + 1);
  284. Height = EPD_2IN7B_HEIGHT;
  285. EPD_2IN7B_SendCommand(0x10);
  286. for (UWORD j = 0; j < Height; j++) {
  287. for (UWORD i = 0; i < Width; i++) {
  288. EPD_2IN7B_SendData(0X00);
  289. }
  290. }
  291. EPD_2IN7B_SendCommand(0x11); // DATA_STOP
  292. EPD_2IN7B_SendCommand(0x13);
  293. for (UWORD j = 0; j < Height; j++) {
  294. for (UWORD i = 0; i < Width; i++) {
  295. EPD_2IN7B_SendData(0X00);
  296. }
  297. }
  298. EPD_2IN7B_SendCommand(0x11); // DATA_STOP
  299. EPD_2IN7B_SendCommand(0x12);
  300. EPD_2IN7B_ReadBusy();
  301. }
  302. /******************************************************************************
  303. function : Sends the image buffer in RAM to e-Paper and displays
  304. parameter:
  305. ******************************************************************************/
  306. void EPD_2IN7B_Display(UBYTE *Imageblack, UBYTE *Imagered)
  307. {
  308. UWORD Width, Height;
  309. Width = (EPD_2IN7B_WIDTH % 8 == 0)? (EPD_2IN7B_WIDTH / 8 ): (EPD_2IN7B_WIDTH / 8 + 1);
  310. Height = EPD_2IN7B_HEIGHT;
  311. EPD_2IN7B_SendCommand(0x10);
  312. for (UWORD j = 0; j < Height; j++) {
  313. for (UWORD i = 0; i < Width; i++) {
  314. EPD_2IN7B_SendData(~Imageblack[i + j * Width]);
  315. }
  316. }
  317. EPD_2IN7B_SendCommand(0x11); // DATA_STOP
  318. EPD_2IN7B_SendCommand(0x13);
  319. for (UWORD j = 0; j < Height; j++) {
  320. for (UWORD i = 0; i < Width; i++) {
  321. EPD_2IN7B_SendData(~Imagered[i + j * Width]);
  322. }
  323. }
  324. EPD_2IN7B_SendCommand(0x11); // DATA_STOP
  325. EPD_2IN7B_SendCommand(0x12);
  326. EPD_2IN7B_ReadBusy();
  327. }
  328. /******************************************************************************
  329. function : Enter sleep mode
  330. parameter:
  331. ******************************************************************************/
  332. void EPD_2IN7B_Sleep(void)
  333. {
  334. EPD_2IN7B_SendCommand(0X50);
  335. EPD_2IN7B_SendData(0xf7);
  336. EPD_2IN7B_SendCommand(0X02); //power off
  337. EPD_2IN7B_SendCommand(0X07); //deep sleep
  338. EPD_2IN7B_SendData(0xA5);
  339. }