EPD_2in7b.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  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(10);
  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. Debug("e-Paper busy\r\n");
  185. while(DEV_Digital_Read(EPD_BUSY_PIN) == 0) { //0: busy, 1: idle
  186. DEV_Delay_ms(100);
  187. }
  188. Debug("e-Paper busy release\r\n");
  189. }
  190. /******************************************************************************
  191. function : set the look-up tables
  192. parameter:
  193. ******************************************************************************/
  194. static void EPD_2IN7B_SetLut(void)
  195. {
  196. unsigned int count;
  197. EPD_2IN7B_SendCommand(0x20); //vcom
  198. for(count = 0; count < 44; count++) {
  199. EPD_2IN7B_SendData(EPD_2IN7B_lut_vcom_dc[count]);
  200. }
  201. EPD_2IN7B_SendCommand(0x21); //ww --
  202. for(count = 0; count < 42; count++) {
  203. EPD_2IN7B_SendData(EPD_2IN7B_lut_ww[count]);
  204. }
  205. EPD_2IN7B_SendCommand(0x22); //bw r
  206. for(count = 0; count < 42; count++) {
  207. EPD_2IN7B_SendData(EPD_2IN7B_lut_bw[count]);
  208. }
  209. EPD_2IN7B_SendCommand(0x23); //wb w
  210. for(count = 0; count < 42; count++) {
  211. EPD_2IN7B_SendData(EPD_2IN7B_lut_bb[count]);
  212. }
  213. EPD_2IN7B_SendCommand(0x24); //bb b
  214. for(count = 0; count < 42; count++) {
  215. EPD_2IN7B_SendData(EPD_2IN7B_lut_wb[count]);
  216. }
  217. }
  218. /******************************************************************************
  219. function : Initialize the e-Paper register
  220. parameter:
  221. ******************************************************************************/
  222. void EPD_2IN7B_Init(void)
  223. {
  224. EPD_2IN7B_Reset();
  225. EPD_2IN7B_SendCommand(0x04); // POWER_ON
  226. EPD_2IN7B_ReadBusy();
  227. EPD_2IN7B_SendCommand(0x00); // PANEL_SETTING
  228. EPD_2IN7B_SendData(0xaf); // KW-BF KWR-AF BWROTP 0f
  229. EPD_2IN7B_SendCommand(0x30); // PLL_CONTROL
  230. EPD_2IN7B_SendData(0x3a); //3A 100HZ 29 150Hz 39 200HZ 31 171HZ
  231. EPD_2IN7B_SendCommand(0x01); // PANEL_SETTING
  232. EPD_2IN7B_SendData(0x03); // VDS_EN, VDG_EN
  233. EPD_2IN7B_SendData(0x00); // VCOM_HV, VGHL_LV[1], VGHL_LV[0]
  234. EPD_2IN7B_SendData(0x2b); // VDH
  235. EPD_2IN7B_SendData(0x2b); // VDL
  236. EPD_2IN7B_SendData(0x09); // VDHR
  237. EPD_2IN7B_SendCommand(0x06); // BOOSTER_SOFT_START
  238. EPD_2IN7B_SendData(0x07);
  239. EPD_2IN7B_SendData(0x07);
  240. EPD_2IN7B_SendData(0x17);
  241. // Power optimization
  242. EPD_2IN7B_SendCommand(0xF8);
  243. EPD_2IN7B_SendData(0x60);
  244. EPD_2IN7B_SendData(0xA5);
  245. // Power optimization
  246. EPD_2IN7B_SendCommand(0xF8);
  247. EPD_2IN7B_SendData(0x89);
  248. EPD_2IN7B_SendData(0xA5);
  249. // Power optimization
  250. EPD_2IN7B_SendCommand(0xF8);
  251. EPD_2IN7B_SendData(0x90);
  252. EPD_2IN7B_SendData(0x00);
  253. // Power optimization
  254. EPD_2IN7B_SendCommand(0xF8);
  255. EPD_2IN7B_SendData(0x93);
  256. EPD_2IN7B_SendData(0x2A);
  257. // Power optimization
  258. EPD_2IN7B_SendCommand(0xF8);
  259. EPD_2IN7B_SendData(0x73);
  260. EPD_2IN7B_SendData(0x41);
  261. EPD_2IN7B_SendCommand(0x82); // VCM_DC_SETTING_REGISTER
  262. EPD_2IN7B_SendData(0x12);
  263. EPD_2IN7B_SendCommand(0x50); // VCOM_AND_DATA_INTERVAL_SETTING
  264. EPD_2IN7B_SendData(0x87); // define by OTP
  265. EPD_2IN7B_SetLut();
  266. EPD_2IN7B_SendCommand(0x16); // PARTIAL_DISPLAY_REFRESH
  267. EPD_2IN7B_SendData(0x00);
  268. }
  269. /******************************************************************************
  270. function : Clear screen
  271. parameter:
  272. ******************************************************************************/
  273. void EPD_2IN7B_Clear(void)
  274. {
  275. UWORD Width, Height;
  276. Width = (EPD_2IN7B_WIDTH % 8 == 0)? (EPD_2IN7B_WIDTH / 8 ): (EPD_2IN7B_WIDTH / 8 + 1);
  277. Height = EPD_2IN7B_HEIGHT;
  278. EPD_2IN7B_SendCommand(0x10);
  279. for (UWORD j = 0; j < Height; j++) {
  280. for (UWORD i = 0; i < Width; i++) {
  281. EPD_2IN7B_SendData(0X00);
  282. }
  283. }
  284. EPD_2IN7B_SendCommand(0x11); // DATA_STOP
  285. EPD_2IN7B_SendCommand(0x13);
  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(0x12);
  293. EPD_2IN7B_ReadBusy();
  294. }
  295. /******************************************************************************
  296. function : Sends the image buffer in RAM to e-Paper and displays
  297. parameter:
  298. ******************************************************************************/
  299. void EPD_2IN7B_Display(UBYTE *Imageblack, UBYTE *Imagered)
  300. {
  301. UWORD Width, Height;
  302. Width = (EPD_2IN7B_WIDTH % 8 == 0)? (EPD_2IN7B_WIDTH / 8 ): (EPD_2IN7B_WIDTH / 8 + 1);
  303. Height = EPD_2IN7B_HEIGHT;
  304. EPD_2IN7B_SendCommand(0x10);
  305. for (UWORD j = 0; j < Height; j++) {
  306. for (UWORD i = 0; i < Width; i++) {
  307. EPD_2IN7B_SendData(~Imageblack[i + j * Width]);
  308. }
  309. }
  310. EPD_2IN7B_SendCommand(0x11); // DATA_STOP
  311. EPD_2IN7B_SendCommand(0x13);
  312. for (UWORD j = 0; j < Height; j++) {
  313. for (UWORD i = 0; i < Width; i++) {
  314. EPD_2IN7B_SendData(~Imagered[i + j * Width]);
  315. }
  316. }
  317. EPD_2IN7B_SendCommand(0x11); // DATA_STOP
  318. EPD_2IN7B_SendCommand(0x12);
  319. EPD_2IN7B_ReadBusy();
  320. }
  321. /******************************************************************************
  322. function : Enter sleep mode
  323. parameter:
  324. ******************************************************************************/
  325. void EPD_2IN7B_Sleep(void)
  326. {
  327. EPD_2IN7B_SendCommand(0X50);
  328. EPD_2IN7B_SendData(0xf7);
  329. EPD_2IN7B_SendCommand(0X02); //power off
  330. EPD_2IN7B_SendCommand(0X07); //deep sleep
  331. EPD_2IN7B_SendData(0xA5);
  332. }