EPD_1in54b.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  1. /*****************************************************************************
  2. * | File : EPD_1in54b.c
  3. * | Author : Waveshare team
  4. * | Function : 1.54inch e-paper b
  5. * | Info :
  6. *----------------
  7. * | This version: V3.1
  8. * | Date : 2019-06-12
  9. * | Info :
  10. * -----------------------------------------------------------------------------
  11. * V3.1(2019-06-12):
  12. * 1.Change:
  13. * lut_vcom0[] => EPD_1IN54_lut_vcom0[]
  14. * lut_w[] => EPD_1IN54_lut_w[]
  15. * lut_b[] => EPD_1IN54B_lut_b[]
  16. * lut_g1[] => EPD_1IN54B_lut_g1[]
  17. * lut_g2[] => EPD_1IN54B_lut_g2[]
  18. * lut_vcom1[] => EPD_1IN54B_lut_vcom1[]
  19. * lut_red0[] => EPD_1IN54B_lut_red0[]
  20. * lut_red1[] => EPD_1IN54B_lut_red1[]
  21. * EPD_Reset() => EPD_1IN54B_Reset()
  22. * EPD_SendCommand() => EPD_1IN54B_SendCommand()
  23. * EPD_SendData() => EPD_1IN54B_SendData()
  24. * EPD_WaitUntilIdle() => EPD_1IN54B_ReadBusy()
  25. * EPD_SetLutBw() => EPD_1IN54B_SetLutBw()
  26. * EPD_SetLutBw => EPD_1IN54B_SetLutBw()
  27. * EPD_Init() => EPD_1IN54B_Init()
  28. * EPD_Clear() => EPD_1IN54B_Clear()
  29. * EPD_Display() => EPD_1IN54B_Display()
  30. * EPD_Sleep() => EPD_1IN54B_Sleep()
  31. * -----------------------------------------------------------------------------
  32. * V3.0(2019-04-24):
  33. * 1.remove commands define:
  34. * #define PANEL_SETTING 0x00
  35. * #define POWER_SETTING 0x01
  36. * #define POWER_OFF 0x02
  37. * #define POWER_OFF_SEQUENCE_SETTING 0x03
  38. * #define POWER_ON 0x04
  39. * #define POWER_ON_MEASURE 0x05
  40. * #define BOOSTER_SOFT_START 0x06
  41. * #define DEEP_SLEEP 0x07
  42. * #define DATA_START_TRANSMISSION_1 0x10
  43. * #define DATA_STOP 0x11
  44. * #define DISPLAY_REFRESH 0x12
  45. * #define DATA_START_TRANSMISSION_2 0x13
  46. * #define PLL_CONTROL 0x30
  47. * #define TEMPERATURE_SENSOR_COMMAND 0x40
  48. * #define TEMPERATURE_SENSOR_CALIBRATION 0x41
  49. * #define TEMPERATURE_SENSOR_WRITE 0x42
  50. * #define TEMPERATURE_SENSOR_READ 0x43
  51. * #define VCOM_AND_DATA_INTERVAL_SETTING 0x50
  52. * #define LOW_POWER_DETECTION 0x51
  53. * #define TCON_SETTING 0x60
  54. * #define TCON_RESOLUTION 0x61
  55. * #define SOURCE_AND_GATE_START_SETTING 0x62
  56. * #define GET_STATUS 0x71
  57. * #define AUTO_MEASURE_VCOM 0x80
  58. * #define VCOM_VALUE 0x81
  59. * #define VCM_DC_SETTING_REGISTER 0x82
  60. * #define PROGRAM_MODE 0xA0
  61. * #define ACTIVE_PROGRAM 0xA1
  62. * #define READ_OTP_DATA 0xA2
  63. *
  64. * -----------------------------------------------------------------------------
  65. * V2.0(2018-10-30):
  66. * 1.Remove:ImageBuff[EPD_1IN54B_HEIGHT * EPD_1IN54B_WIDTH / 8]
  67. * 2.Change:EPD_Display(UBYTE *Image)
  68. * Need to pass parameters: pointer to cached data
  69. * 3.Change:
  70. * EPD_RST -> EPD_RST_PIN
  71. * EPD_DC -> EPD_DC_PIN
  72. * EPD_CS -> EPD_CS_PIN
  73. * EPD_BUSY -> EPD_BUSY_PIN
  74. #
  75. # Permission is hereby granted, free of charge, to any person obtaining a copy
  76. # of this software and associated documnetation files (the "Software"), to deal
  77. # in the Software without restriction, including without limitation the rights
  78. # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  79. # copies of the Software, and to permit persons to whom the Software is
  80. # furished to do so, subject to the following conditions:
  81. #
  82. # The above copyright notice and this permission notice shall be included in
  83. # all copies or substantial portions of the Software.
  84. #
  85. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  86. # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  87. # FITNESS OR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  88. # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  89. # LIABILITY WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  90. # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  91. # THE SOFTWARE.
  92. #
  93. ******************************************************************************/
  94. #include "EPD_1in54b.h"
  95. #include "Debug.h"
  96. const unsigned char EPD_1IN54B_lut_vcom0[] = {0x0E, 0x14, 0x01, 0x0A, 0x06, 0x04, 0x0A, 0x0A, 0x0F, 0x03, 0x03, 0x0C, 0x06, 0x0A, 0x00};
  97. const unsigned char EPD_1IN54B_lut_w[] = {0x0E, 0x14, 0x01, 0x0A, 0x46, 0x04, 0x8A, 0x4A, 0x0F, 0x83, 0x43, 0x0C, 0x86, 0x0A, 0x04};
  98. const unsigned char EPD_1IN54B_lut_b[] = {0x0E, 0x14, 0x01, 0x8A, 0x06, 0x04, 0x8A, 0x4A, 0x0F, 0x83, 0x43, 0x0C, 0x06, 0x4A, 0x04};
  99. const unsigned char EPD_1IN54B_lut_g1[] = {0x8E, 0x94, 0x01, 0x8A, 0x06, 0x04, 0x8A, 0x4A, 0x0F, 0x83, 0x43, 0x0C, 0x06, 0x0A, 0x04};
  100. const unsigned char EPD_1IN54B_lut_g2[] = {0x8E, 0x94, 0x01, 0x8A, 0x06, 0x04, 0x8A, 0x4A, 0x0F, 0x83, 0x43, 0x0C, 0x06, 0x0A, 0x04};
  101. const unsigned char EPD_1IN54B_lut_vcom1[] = {0x03, 0x1D, 0x01, 0x01, 0x08, 0x23, 0x37, 0x37, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
  102. const unsigned char EPD_1IN54B_lut_red0[] = {0x83, 0x5D, 0x01, 0x81, 0x48, 0x23, 0x77, 0x77, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
  103. const unsigned char EPD_1IN54B_lut_red1[] = {0x03, 0x1D, 0x01, 0x01, 0x08, 0x23, 0x37, 0x37, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
  104. /******************************************************************************
  105. function : Software reset
  106. parameter:
  107. ******************************************************************************/
  108. static void EPD_1IN54B_Reset(void)
  109. {
  110. DEV_Digital_Write(EPD_RST_PIN, 1);
  111. DEV_Delay_ms(100);
  112. DEV_Digital_Write(EPD_RST_PIN, 0);
  113. DEV_Delay_ms(2);
  114. DEV_Digital_Write(EPD_RST_PIN, 1);
  115. DEV_Delay_ms(100);
  116. }
  117. /******************************************************************************
  118. function : send command
  119. parameter:
  120. Reg : Command register
  121. ******************************************************************************/
  122. static void EPD_1IN54B_SendCommand(UBYTE Reg)
  123. {
  124. DEV_Digital_Write(EPD_DC_PIN, 0);
  125. DEV_Digital_Write(EPD_CS_PIN, 0);
  126. DEV_SPI_WriteByte(Reg);
  127. DEV_Digital_Write(EPD_CS_PIN, 1);
  128. }
  129. /******************************************************************************
  130. function : send data
  131. parameter:
  132. Data : Write data
  133. ******************************************************************************/
  134. static void EPD_1IN54B_SendData(UBYTE Data)
  135. {
  136. DEV_Digital_Write(EPD_DC_PIN, 1);
  137. DEV_Digital_Write(EPD_CS_PIN, 0);
  138. DEV_SPI_WriteByte(Data);
  139. DEV_Digital_Write(EPD_CS_PIN, 1);
  140. }
  141. /******************************************************************************
  142. function : Wait until the busy_pin goes LOW
  143. parameter:
  144. ******************************************************************************/
  145. static void EPD_1IN54B_ReadBusy(void)
  146. {
  147. EPD_Busy_WaitUntil(1,0);
  148. // unsigned char count = 100;
  149. // Debug("e-Paper busy\r\n");
  150. // while(1) {
  151. // if(DEV_Digital_Read(EPD_BUSY_PIN) == 1)
  152. // break;
  153. // if(!(count--))
  154. // {
  155. // Debug("error: e-Paper busy timeout!!!\r\n");
  156. // break;
  157. // }
  158. // else
  159. // DEV_Delay_ms(100);
  160. // }
  161. // DEV_Delay_ms(200);
  162. // Debug("e-Paper busy release\r\n");
  163. }
  164. /******************************************************************************
  165. function : Set the look-up black and white tables
  166. parameter:
  167. ******************************************************************************/
  168. static void EPD_1IN54B_SetLutBw(void)
  169. {
  170. UWORD count;
  171. EPD_1IN54B_SendCommand(0x20);// g vcom
  172. for(count = 0; count < 15; count++) {
  173. EPD_1IN54B_SendData(EPD_1IN54B_lut_vcom0[count]);
  174. }
  175. EPD_1IN54B_SendCommand(0x21);// g ww --
  176. for(count = 0; count < 15; count++) {
  177. EPD_1IN54B_SendData(EPD_1IN54B_lut_w[count]);
  178. }
  179. EPD_1IN54B_SendCommand(0x22);// g bw r
  180. for(count = 0; count < 15; count++) {
  181. EPD_1IN54B_SendData(EPD_1IN54B_lut_b[count]);
  182. }
  183. EPD_1IN54B_SendCommand(0x23);// g wb w
  184. for(count = 0; count < 15; count++) {
  185. EPD_1IN54B_SendData(EPD_1IN54B_lut_g1[count]);
  186. }
  187. EPD_1IN54B_SendCommand(0x24);// g bb b
  188. for(count = 0; count < 15; count++) {
  189. EPD_1IN54B_SendData(EPD_1IN54B_lut_g2[count]);
  190. }
  191. }
  192. /******************************************************************************
  193. function : Set the look-up red tables
  194. parameter:
  195. ******************************************************************************/
  196. static void EPD_1IN54B_SetLutRed(void)
  197. {
  198. UWORD count;
  199. EPD_1IN54B_SendCommand(0x25);
  200. for(count = 0; count < 15; count++) {
  201. EPD_1IN54B_SendData(EPD_1IN54B_lut_vcom1[count]);
  202. }
  203. EPD_1IN54B_SendCommand(0x26);
  204. for(count = 0; count < 15; count++) {
  205. EPD_1IN54B_SendData(EPD_1IN54B_lut_red0[count]);
  206. }
  207. EPD_1IN54B_SendCommand(0x27);
  208. for(count = 0; count < 15; count++) {
  209. EPD_1IN54B_SendData(EPD_1IN54B_lut_red1[count]);
  210. }
  211. }
  212. /******************************************************************************
  213. function : Initialize the e-Paper register
  214. parameter:
  215. ******************************************************************************/
  216. void EPD_1IN54B_Init(UBYTE mode)
  217. {
  218. EPD_1IN54B_Reset();
  219. EPD_1IN54B_SendCommand(0x01);// POWER_SETTING
  220. EPD_1IN54B_SendData(0x07);
  221. EPD_1IN54B_SendData(0x00);
  222. EPD_1IN54B_SendData(0x08);
  223. EPD_1IN54B_SendData(0x00);
  224. EPD_1IN54B_SendCommand(0x06);// BOOSTER_SOFT_START
  225. EPD_1IN54B_SendData(0x07);
  226. EPD_1IN54B_SendData(0x07);
  227. EPD_1IN54B_SendData(0x07);
  228. EPD_1IN54B_SendCommand(0x04);// POWER_ON
  229. EPD_1IN54B_ReadBusy();
  230. EPD_1IN54B_SendCommand(0X00);// PANEL_SETTING
  231. EPD_1IN54B_SendData(0xcf);
  232. EPD_1IN54B_SendCommand(0X50);// VCOM_AND_DATA_INTERVAL_SETTING
  233. EPD_1IN54B_SendData(0x37);// 0xF0
  234. EPD_1IN54B_SendCommand(0x30);// PLL_CONTROL
  235. EPD_1IN54B_SendData(0x39);
  236. EPD_1IN54B_SendCommand(0x61);// TCON_RESOLUTION set x and y
  237. EPD_1IN54B_SendData(0xC8);// 200
  238. EPD_1IN54B_SendData(0x00);// y High eight: 0
  239. EPD_1IN54B_SendData(0xC8);// y Low eight: 200
  240. EPD_1IN54B_SendCommand(0x82);// VCM_DC_SETTING_REGISTER
  241. EPD_1IN54B_SendData(0x0E);
  242. EPD_1IN54B_SetLutBw();
  243. EPD_1IN54B_SetLutRed();
  244. }
  245. /******************************************************************************
  246. function : Clear screen
  247. parameter:
  248. ******************************************************************************/
  249. void EPD_1IN54B_Clear(void)
  250. {
  251. UWORD Width, Height;
  252. Width = (EPD_1IN54B_WIDTH % 8 == 0)? (EPD_1IN54B_WIDTH / 8 ): (EPD_1IN54B_WIDTH / 8 + 1);
  253. Height = EPD_1IN54B_HEIGHT;
  254. //send black data
  255. EPD_1IN54B_SendCommand(0x10);// DATA_START_TRANSMISSION_1
  256. DEV_Delay_ms(2);
  257. for(UWORD i = 0; i < Height; i++) {
  258. for(UWORD i = 0; i < Width; i++) {
  259. EPD_1IN54B_SendData(0xFF);
  260. EPD_1IN54B_SendData(0xFF);
  261. }
  262. }
  263. DEV_Delay_ms(2);
  264. //send red data
  265. EPD_1IN54B_SendCommand(0x13);// DATA_START_TRANSMISSION_2
  266. DEV_Delay_ms(2);
  267. for(UWORD i = 0; i < Height; i++) {
  268. for(UWORD i = 0; i < Width; i++) {
  269. EPD_1IN54B_SendData(0xFF);
  270. }
  271. }
  272. DEV_Delay_ms(2);
  273. EPD_1IN54B_SendCommand(0x12);// DISPLAY_REFRESH
  274. EPD_1IN54B_ReadBusy();
  275. }
  276. /******************************************************************************
  277. function : Sends the image buffer in RAM to e-Paper and displays
  278. parameter:
  279. ******************************************************************************/
  280. void EPD_1IN54B_Display(const UBYTE *blackimage, const UBYTE *redimage)
  281. {
  282. UBYTE Temp = 0x00;
  283. UWORD Width, Height;
  284. Width = (EPD_1IN54B_WIDTH % 8 == 0)? (EPD_1IN54B_WIDTH / 8 ): (EPD_1IN54B_WIDTH / 8 + 1);
  285. Height = EPD_1IN54B_HEIGHT;
  286. EPD_1IN54B_SendCommand(0x10);// DATA_START_TRANSMISSION_1
  287. for (UWORD j = 0; j < Height; j++) {
  288. for (UWORD i = 0; i < Width; i++) {
  289. Temp = 0x00;
  290. for (int bit = 0; bit < 4; bit++) {
  291. if ((blackimage[i + j * Width] & (0x80 >> bit)) != 0) {
  292. Temp |= 0xC0 >> (bit * 2);
  293. }
  294. }
  295. EPD_1IN54B_SendData(Temp);
  296. Temp = 0x00;
  297. for (int bit = 4; bit < 8; bit++) {
  298. if ((blackimage[i + j * Width] & (0x80 >> bit)) != 0) {
  299. Temp |= 0xC0 >> ((bit - 4) * 2);
  300. }
  301. }
  302. EPD_1IN54B_SendData(Temp);
  303. }
  304. }
  305. DEV_Delay_ms(2);
  306. EPD_1IN54B_SendCommand(0x13);// DATA_START_TRANSMISSION_2
  307. for (UWORD j = 0; j < Height; j++) {
  308. for (UWORD i = 0; i < Width; i++) {
  309. EPD_1IN54B_SendData(redimage[i + j * Width]);
  310. }
  311. }
  312. DEV_Delay_ms(2);
  313. //Display refresh
  314. EPD_1IN54B_SendCommand(0x12);// DISPLAY_REFRESH
  315. EPD_1IN54B_ReadBusy();
  316. }
  317. /******************************************************************************
  318. function : Enter sleep mode
  319. parameter:
  320. ******************************************************************************/
  321. void EPD_1IN54B_Sleep(void)
  322. {
  323. EPD_1IN54B_SendCommand(0x50);// VCOM_AND_DATA_INTERVAL_SETTING
  324. EPD_1IN54B_SendData(0x17);
  325. EPD_1IN54B_SendCommand(0x82);// VCM_DC_SETTING_REGISTER
  326. EPD_1IN54B_SendData(0x00);
  327. EPD_1IN54B_SendCommand(0x01);// POWER_SETTING
  328. EPD_1IN54B_SendData(0x02);
  329. EPD_1IN54B_SendData(0x00);
  330. EPD_1IN54B_SendData(0x00);
  331. EPD_1IN54B_SendData(0x00);
  332. EPD_1IN54B_ReadBusy();
  333. DEV_Delay_ms(1000);
  334. EPD_1IN54B_SendCommand(0x02);// POWER_OFF
  335. }