EPD_5in83bc.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  1. /*****************************************************************************
  2. * | File : EPD_5IN83BC.c
  3. * | Author : Waveshare team
  4. * | Function : 5.83inch e-paper b&c
  5. * | Info :
  6. * | This version: V3.0
  7. * | Date : 2019-06-13
  8. * | Info :
  9. * -----------------------------------------------------------------------------
  10. * V3.0(2019-06-13):
  11. * 1.Change:
  12. * EPD_Reset() => EPD_5IN83BC_Reset()
  13. * EPD_SendCommand() => EPD_5IN83BC_SendCommand()
  14. * EPD_SendData() => EPD_5IN83BC_SendData()
  15. * EPD_WaitUntilIdle() => EPD_5IN83BC_ReadBusy()
  16. * EPD_Init() => EPD_5IN83BC_Init()
  17. * EPD_Clear() => EPD_5IN83BC_Clear()
  18. * EPD_Display() => EPD_5IN83BC_Display()
  19. * EPD_Sleep() => EPD_5IN83BC_Sleep()
  20. * 2.Remove:
  21. * #define PANEL_SETTING 0x00
  22. * #define POWER_SETTING 0x01
  23. * #define POWER_OFF 0x02
  24. * #define POWER_OFF_SEQUENCE_SETTING 0x03
  25. * #define POWER_ON 0x04
  26. * #define POWER_ON_MEASURE 0x05
  27. * #define BOOSTER_SOFT_START 0x06
  28. * #define DEEP_SLEEP 0x07
  29. * #define DATA_START_TRANSMISSION_1 0x10
  30. * #define DATA_STOP 0x11
  31. * #define DISPLAY_REFRESH 0x12
  32. * #define IMAGE_PROCESS 0x13
  33. * #define LUT_FOR_VCOM 0x20
  34. * #define LUT_BLUE 0x21
  35. * #define LUT_WHITE 0x22
  36. * #define LUT_GRAY_1 0x23
  37. * #define LUT_GRAY_2 0x24
  38. * #define LUT_RED_0 0x25
  39. * #define LUT_RED_1 0x26
  40. * #define LUT_RED_2 0x27
  41. * #define LUT_RED_3 0x28
  42. * #define LUT_XON 0x29
  43. * #define PLL_CONTROL 0x30
  44. * #define TEMPERATURE_SENSOR_COMMAND 0x40
  45. * #define TEMPERATURE_CALIBRATION 0x41
  46. * #define TEMPERATURE_SENSOR_WRITE 0x42
  47. * #define TEMPERATURE_SENSOR_READ 0x43
  48. * #define VCOM_AND_DATA_INTERVAL_SETTING 0x50
  49. * #define LOW_POWER_DETECTION 0x51
  50. * #define TCON_SETTING 0x60
  51. * #define TCON_RESOLUTION 0x61
  52. * #define SPI_FLASH_CONTROL 0x65
  53. * #define REVISION 0x70
  54. * #define GET_STATUS 0x71
  55. * #define AUTO_MEASUREMENT_VCOM 0x80
  56. * #define READ_VCOM_VALUE 0x81
  57. * #define VCM_DC_SETTING 0x82
  58. * 3.add:
  59. * EPD_5IN83BC_DisplayHalfScreen()
  60. * -----------------------------------------------------------------------------
  61. * V2.0(2018-11-09):
  62. * 1.Remove:ImageBuff[EPD_HEIGHT * EPD_WIDTH / 8]
  63. * 2.Change:EPD_Display(UBYTE *Image)
  64. * Need to pass parameters: pointer to cached data
  65. #
  66. # Permission is hereby granted, free of charge, to any person obtaining a copy
  67. # of this software and associated documnetation files (the "Software"), to deal
  68. # in the Software without restriction, including without limitation the rights
  69. # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  70. # copies of the Software, and to permit persons to whom the Software is
  71. # furished to do so, subject to the following conditions:
  72. #
  73. # The above copyright notice and this permission notice shall be included in
  74. # all copies or substantial portions of the Software.
  75. #
  76. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  77. # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  78. # FITNESS OR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  79. # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  80. # LIABILITY WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  81. # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  82. # THE SOFTWARE.
  83. #
  84. ******************************************************************************/
  85. #include "EPD_5in83bc.h"
  86. #include "Debug.h"
  87. /******************************************************************************
  88. function : Software reset
  89. parameter:
  90. ******************************************************************************/
  91. static void EPD_5IN83BC_Reset(void)
  92. {
  93. DEV_Digital_Write(EPD_RST_PIN, 1);
  94. DEV_Delay_ms(200);
  95. DEV_Digital_Write(EPD_RST_PIN, 0);
  96. DEV_Delay_ms(2);
  97. DEV_Digital_Write(EPD_RST_PIN, 1);
  98. DEV_Delay_ms(200);
  99. }
  100. /******************************************************************************
  101. function : send command
  102. parameter:
  103. Reg : Command register
  104. ******************************************************************************/
  105. static void EPD_5IN83BC_SendCommand(UBYTE Reg)
  106. {
  107. DEV_Digital_Write(EPD_DC_PIN, 0);
  108. DEV_Digital_Write(EPD_CS_PIN, 0);
  109. DEV_SPI_WriteByte(Reg);
  110. DEV_Digital_Write(EPD_CS_PIN, 1);
  111. }
  112. /******************************************************************************
  113. function : send data
  114. parameter:
  115. Data : Write data
  116. ******************************************************************************/
  117. static void EPD_5IN83BC_SendData(UBYTE Data)
  118. {
  119. DEV_Digital_Write(EPD_DC_PIN, 1);
  120. DEV_Digital_Write(EPD_CS_PIN, 0);
  121. DEV_SPI_WriteByte(Data);
  122. DEV_Digital_Write(EPD_CS_PIN, 1);
  123. }
  124. /******************************************************************************
  125. function : Wait until the busy_pin goes LOW
  126. parameter:
  127. ******************************************************************************/
  128. void EPD_5IN83BC_ReadBusy(void)
  129. {
  130. UBYTE busy;
  131. Debug("e-Paper busy\r\n");
  132. do {
  133. EPD_5IN83BC_SendCommand(0x71);
  134. busy = DEV_Digital_Read(EPD_BUSY_PIN);
  135. busy =!(busy & 0x01);
  136. } while(busy);
  137. Debug("e-Paper busy release\r\n");
  138. }
  139. /******************************************************************************
  140. function : Turn On Display
  141. parameter:
  142. ******************************************************************************/
  143. static void EPD_5IN83BC_TurnOnDisplay(void)
  144. {
  145. EPD_5IN83BC_SendCommand(0x04); // POWER ON
  146. EPD_5IN83BC_ReadBusy();
  147. EPD_5IN83BC_SendCommand(0x12); // display refresh
  148. DEV_Delay_ms(100);
  149. EPD_5IN83BC_ReadBusy();
  150. }
  151. /******************************************************************************
  152. function : Initialize the e-Paper register
  153. parameter:
  154. ******************************************************************************/
  155. void EPD_5IN83BC_Init(void)
  156. {
  157. EPD_5IN83BC_Reset();
  158. EPD_5IN83BC_SendCommand(0x01); // POWER_SETTING
  159. EPD_5IN83BC_SendData(0x37);
  160. EPD_5IN83BC_SendData(0x00);
  161. EPD_5IN83BC_SendCommand(0x00); // PANEL_SETTING
  162. EPD_5IN83BC_SendData(0xCF);
  163. EPD_5IN83BC_SendData(0x08);
  164. EPD_5IN83BC_SendCommand(0x30); // PLL_CONTROL
  165. EPD_5IN83BC_SendData(0x3A); //PLL: 0-15:0x3C, 15+:0x3A
  166. EPD_5IN83BC_SendCommand(0x82); // VCM_DC_SETTING
  167. EPD_5IN83BC_SendData(0x28); //all temperature range
  168. EPD_5IN83BC_SendCommand(0x06); // BOOSTER_SOFT_START
  169. EPD_5IN83BC_SendData (0xc7);
  170. EPD_5IN83BC_SendData (0xcc);
  171. EPD_5IN83BC_SendData (0x15);
  172. EPD_5IN83BC_SendCommand(0x50); // VCOM AND DATA INTERVAL SETTING
  173. EPD_5IN83BC_SendData(0x77);
  174. EPD_5IN83BC_SendCommand(0x60); // TCON_SETTING
  175. EPD_5IN83BC_SendData(0x22);
  176. EPD_5IN83BC_SendCommand(0x65); // FLASH CONTROL
  177. EPD_5IN83BC_SendData(0x00);
  178. EPD_5IN83BC_SendCommand(0x61); // TCON_RESOLUTION
  179. EPD_5IN83BC_SendData (0x02); // source 600
  180. EPD_5IN83BC_SendData (0x58);
  181. EPD_5IN83BC_SendData (0x01); // gate 448
  182. EPD_5IN83BC_SendData (0xc0);
  183. EPD_5IN83BC_SendCommand(0xe5); // FLASH MODE
  184. EPD_5IN83BC_SendData(0x03);
  185. }
  186. /******************************************************************************
  187. function : Clear screen
  188. parameter:
  189. ******************************************************************************/
  190. void EPD_5IN83BC_Clear(void)
  191. {
  192. UWORD Width, Height;
  193. Width = (EPD_5IN83BC_WIDTH % 8 == 0)? (EPD_5IN83BC_WIDTH / 8): (EPD_5IN83BC_WIDTH / 8 + 1);
  194. Height = EPD_5IN83BC_HEIGHT;
  195. EPD_5IN83BC_SendCommand(0x10);
  196. for (UWORD j = 0; j < Height; j++) {
  197. for (UWORD i = 0; i < Width; i++) {
  198. for(UBYTE k = 0; k < 4; k++) {
  199. EPD_5IN83BC_SendData(0x33);
  200. }
  201. }
  202. }
  203. EPD_5IN83BC_TurnOnDisplay();
  204. }
  205. /******************************************************************************
  206. function : Sends the image buffer in RAM to e-Paper and displays
  207. parameter:
  208. ******************************************************************************/
  209. void EPD_5IN83BC_Display(const UBYTE *blackimage, const UBYTE *ryimage)
  210. {
  211. UBYTE Data_Black, Data_RY, Data;
  212. UDOUBLE i, j, Width, Height;
  213. Width = (EPD_5IN83BC_WIDTH % 8 == 0)? (EPD_5IN83BC_WIDTH / 8 ): (EPD_5IN83BC_WIDTH / 8 + 1);
  214. Height = EPD_5IN83BC_HEIGHT;
  215. EPD_5IN83BC_SendCommand(0x10);
  216. for (j = 0; j < Height; j++) {
  217. for (i = 0; i < Width; i++) {
  218. Data_Black = blackimage[i + j * Width];
  219. Data_RY = ryimage[i + j * Width]; // red or yellow
  220. for(UBYTE k = 0; k < 8; k++) {
  221. if ((Data_RY & 0x80) == 0x00) {
  222. Data = 0x04; //red
  223. } else if ((Data_Black & 0x80) == 0x00) {
  224. Data = 0x00; //black
  225. } else {
  226. Data = 0x03; //white
  227. }
  228. Data = (Data << 4) & 0xFF;
  229. Data_Black = (Data_Black << 1) & 0xFF;
  230. Data_RY = (Data_RY << 1) & 0xFF;
  231. k += 1;
  232. if((Data_RY & 0x80) == 0x00) {
  233. Data |= 0x04; //red
  234. } else if ((Data_Black & 0x80) == 0x00) {
  235. Data |= 0x00; //black
  236. } else {
  237. Data |= 0x03; //white
  238. }
  239. Data_Black = (Data_Black << 1) & 0xFF;
  240. Data_RY = (Data_RY << 1) & 0xFF;
  241. EPD_5IN83BC_SendData(Data);
  242. }
  243. }
  244. }
  245. EPD_5IN83BC_TurnOnDisplay();
  246. }
  247. /******************************************************************************
  248. function : Sends the image buffer in RAM to e-Paper and displays
  249. parameter:
  250. ******************************************************************************/
  251. void EPD_5IN83BC_DisplayHalfScreen(const UBYTE *blackimage, const UBYTE *ryimage)
  252. {
  253. UBYTE Data_Black, Data_RY, Data;
  254. UDOUBLE i, j, Width, Height;
  255. Width = (EPD_5IN83BC_WIDTH % 8 == 0)? (EPD_5IN83BC_WIDTH / 8 ): (EPD_5IN83BC_WIDTH / 8 + 1);
  256. Height = EPD_5IN83BC_HEIGHT;
  257. EPD_5IN83BC_SendCommand(0x10);
  258. for (j = 0; j < Height / 2; j++) {
  259. for (i = 0; i < Width; i++) {
  260. Data_Black = blackimage[i + j * Width];
  261. Data_RY = ryimage[i + j * Width]; // red or yellow
  262. for(UBYTE k = 0; k < 8; k++) {
  263. if ((Data_RY & 0x80) == 0x00) {
  264. Data = 0x04; //red
  265. } else if ((Data_Black & 0x80) == 0x00) {
  266. Data = 0x00; //black
  267. } else {
  268. Data = 0x03; //white
  269. }
  270. Data = (Data << 4) & 0xFF;
  271. Data_Black = (Data_Black << 1) & 0xFF;
  272. Data_RY = (Data_RY << 1) & 0xFF;
  273. k += 1;
  274. if((Data_RY & 0x80) == 0x00) {
  275. Data |= 0x04; //red
  276. } else if ((Data_Black & 0x80) == 0x00) {
  277. Data |= 0x00; //black
  278. } else {
  279. Data |= 0x03; //white
  280. }
  281. Data_Black = (Data_Black << 1) & 0xFF;
  282. Data_RY = (Data_RY << 1) & 0xFF;
  283. EPD_5IN83BC_SendData(Data);
  284. }
  285. }
  286. }
  287. for (j = 0; j < Height / 2; j++) {
  288. for (i = 0; i < Width; i++) {
  289. for(UBYTE k = 0; k < 4; k++) {
  290. EPD_5IN83BC_SendData(0x33);
  291. }
  292. }
  293. }
  294. EPD_5IN83BC_TurnOnDisplay();
  295. }
  296. /******************************************************************************
  297. function : Enter sleep mode
  298. parameter:
  299. ******************************************************************************/
  300. void EPD_5IN83BC_Sleep(void)
  301. {
  302. EPD_5IN83BC_SendCommand(0x02); // POWER_OFF
  303. EPD_5IN83BC_ReadBusy();
  304. EPD_5IN83BC_SendCommand(0x07); // DEEP_SLEEP
  305. EPD_5IN83BC_SendData(0XA5);
  306. }