2
0

EPD_2in7b.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  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_2IN7B_SendCommand() => EPD_2in7B_SendCommand()
  19. * EPD_2IN7B_SendData() => EPD_2in7B_SendData()
  20. * EPD_2IN7B_ReadBusy() => 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. 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(0x06); //boost soft start
  226. EPD_2IN7B_SendData (0x07); //A
  227. EPD_2IN7B_SendData (0x07); //B
  228. EPD_2IN7B_SendData (0x17); //C
  229. EPD_2IN7B_SendCommand(0x04);
  230. EPD_2IN7B_ReadBusy();//waiting for the electronic paper IC to release the idle signal
  231. EPD_2IN7B_SendCommand(0x00); //panel setting
  232. EPD_2IN7B_SendData(0x0f); //LUT from OTP£?128x296
  233. EPD_2IN7B_SendCommand(0x16);
  234. EPD_2IN7B_SendData(0x00); //KW-BF KWR-AF BWROTP 0f
  235. EPD_2IN7B_SendCommand(0xF8); //boostéè?¨
  236. EPD_2IN7B_SendData (0x60);
  237. EPD_2IN7B_SendData(0xa5);
  238. EPD_2IN7B_SendCommand(0xF8); //boostéè?¨
  239. EPD_2IN7B_SendData (0x90);
  240. EPD_2IN7B_SendData (0x00);
  241. EPD_2IN7B_SendCommand(0xF8); //boostéè?¨
  242. EPD_2IN7B_SendData (0x93);
  243. EPD_2IN7B_SendData(0x2A);
  244. EPD_2IN7B_SendCommand(0x01); // PANEL_SETTING
  245. EPD_2IN7B_SendData(0x03); // VDS_EN, VDG_EN
  246. EPD_2IN7B_SendData(0x00); // VCOM_HV, VGHL_LV[1], VGHL_LV[0]
  247. EPD_2IN7B_SendData(0x2b); // VDH
  248. EPD_2IN7B_SendData(0x2b); // VDL
  249. EPD_2IN7B_SendData(0x2b); // VDHR
  250. }
  251. /******************************************************************************
  252. function : Clear screen
  253. parameter:
  254. ******************************************************************************/
  255. void EPD_2IN7B_Clear(void)
  256. {
  257. UWORD Width, Height;
  258. Width = (EPD_2IN7B_WIDTH % 8 == 0)? (EPD_2IN7B_WIDTH / 8 ): (EPD_2IN7B_WIDTH / 8 + 1);
  259. Height = EPD_2IN7B_HEIGHT;
  260. EPD_2IN7B_SendCommand(0x10);
  261. for (UWORD j = 0; j < Height; j++) {
  262. for (UWORD i = 0; i < Width; i++) {
  263. EPD_2IN7B_SendData(0X00);
  264. }
  265. }
  266. EPD_2IN7B_SendCommand(0x11); // DATA_STOP
  267. EPD_2IN7B_SendCommand(0x13);
  268. for (UWORD j = 0; j < Height; j++) {
  269. for (UWORD i = 0; i < Width; i++) {
  270. EPD_2IN7B_SendData(0X00);
  271. }
  272. }
  273. EPD_2IN7B_SendCommand(0x11); // DATA_STOP
  274. EPD_2IN7B_SendCommand(0x12);
  275. EPD_2IN7B_ReadBusy();
  276. }
  277. /******************************************************************************
  278. function : Sends the image buffer in RAM to e-Paper and displays
  279. parameter:
  280. ******************************************************************************/
  281. void EPD_2IN7B_Display(UBYTE *Imageblack, UBYTE *Imagered)
  282. {
  283. UWORD Width, Height;
  284. Width = (EPD_2IN7B_WIDTH % 8 == 0)? (EPD_2IN7B_WIDTH / 8 ): (EPD_2IN7B_WIDTH / 8 + 1);
  285. Height = EPD_2IN7B_HEIGHT;
  286. EPD_2IN7B_SendCommand(0x10);
  287. for (UWORD j = 0; j < Height; j++) {
  288. for (UWORD i = 0; i < Width; i++) {
  289. EPD_2IN7B_SendData(~Imageblack[i + j * Width]);
  290. }
  291. }
  292. EPD_2IN7B_SendCommand(0x11); // DATA_STOP
  293. EPD_2IN7B_SendCommand(0x13);
  294. for (UWORD j = 0; j < Height; j++) {
  295. for (UWORD i = 0; i < Width; i++) {
  296. EPD_2IN7B_SendData(~Imagered[i + j * Width]);
  297. }
  298. }
  299. EPD_2IN7B_SendCommand(0x11); // DATA_STOP
  300. EPD_2IN7B_SendCommand(0x12);
  301. EPD_2IN7B_ReadBusy();
  302. }
  303. /******************************************************************************
  304. function : Enter sleep mode
  305. parameter:
  306. ******************************************************************************/
  307. void EPD_2IN7B_Sleep(void)
  308. {
  309. EPD_2IN7B_SendCommand(0X50);
  310. EPD_2IN7B_SendData(0xf7);
  311. EPD_2IN7B_SendCommand(0X02); //power off
  312. EPD_2IN7B_SendCommand(0X07); //deep sleep
  313. EPD_2IN7B_SendData(0xA5);
  314. }