EPD_5in83.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. /*****************************************************************************
  2. * | File : EPD_5in83.c
  3. * | Author : Waveshare team
  4. * | Function : 5.83inch e-paper
  5. * | Info :
  6. *----------------
  7. * | This version: V3.0
  8. * | Date : 2019-06-13
  9. * | Info :
  10. * -----------------------------------------------------------------------------
  11. * V3.0(2019-06-13):
  12. * 1.Change:
  13. * EPD_Reset() => EPD_5IN83_Reset()
  14. * EPD_SendCommand() => EPD_5IN83_SendCommand()
  15. * EPD_SendData() => EPD_5IN83_SendData()
  16. * EPD_WaitUntilIdle() => EPD_5IN83_ReadBusy()
  17. * EPD_SetFullReg() => EPD_5IN83_SetFullReg()
  18. * EPD_SetPartReg() => EPD_5IN83_SetPartReg()
  19. * EPD_TurnOnDisplay() => EPD_5IN83_TurnOnDisplay()
  20. * EPD_Init() => EPD_5IN83_Init()
  21. * EPD_Clear() => EPD_5IN83_Clear()
  22. * EPD_Display() => EPD_5IN83_Display()
  23. * EPD_Sleep() => EPD_5IN83_Sleep()
  24. * 2.remove commands define:
  25. * #define PANEL_SETTING 0x00
  26. * #define POWER_SETTING 0x01
  27. * #define POWER_OFF 0x02
  28. * #define POWER_OFF_SEQUENCE_SETTING 0x03
  29. * #define POWER_ON 0x04
  30. * #define POWER_ON_MEASURE 0x05
  31. * #define BOOSTER_SOFT_START 0x06
  32. * #define DEEP_SLEEP 0x07
  33. * #define DATA_START_TRANSMISSION_1 0x10
  34. * #define DATA_STOP 0x11
  35. * #define DISPLAY_REFRESH 0x12
  36. * #define DATA_START_TRANSMISSION_2 0x13
  37. * #define VCOM_LUT 0x20
  38. * #define W2W_LUT 0x21
  39. * #define B2W_LUT 0x22
  40. * #define W2B_LUT 0x23
  41. * #define B2B_LUT 0x24
  42. * #define PLL_CONTROL 0x30
  43. * #define TEMPERATURE_SENSOR_CALIBRATION 0x40
  44. * #define TEMPERATURE_SENSOR_SELECTION 0x41
  45. * #define TEMPERATURE_SENSOR_WRITE 0x42
  46. * #define TEMPERATURE_SENSOR_READ 0x43
  47. * #define VCOM_AND_DATA_INTERVAL_SETTING 0x50
  48. * #define LOW_POWER_DETECTION 0x51
  49. * #define TCON_SETTING 0x60
  50. * #define RESOLUTION_SETTING 0x61
  51. * #define GET_STATUS 0x71
  52. * #define AUTO_MEASURE_VCOM 0x80
  53. * #define READ_VCOM_VALUE 0x81
  54. * #define VCM_DC_SETTING 0x82
  55. * #define PARTIAL_WINDOW 0x90
  56. * #define PARTIAL_IN 0x91
  57. * #define PARTIAL_OUT 0x92
  58. * #define PROGRAM_MODE 0xA0
  59. * #define ACTIVE_PROGRAM 0xA1
  60. * #define READ_OTP_DATA 0xA2
  61. * #define POWER_SAVING 0xE3
  62. * -----------------------------------------------------------------------------
  63. * V2.0(2018-11-09):
  64. * 1.Remove:ImageBuff[EPD_HEIGHT * EPD_WIDTH / 8]
  65. * 2.Change:EPD_Display(UBYTE *Image)
  66. * Need to pass parameters: pointer to cached data
  67. #
  68. # Permission is hereby granted, free of charge, to any person obtaining a copy
  69. # of this software and associated documnetation files (the "Software"), to deal
  70. # in the Software without restriction, including without limitation the rights
  71. # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  72. # copies of the Software, and to permit persons to whom the Software is
  73. # furished to do so, subject to the following conditions:
  74. #
  75. # The above copyright notice and this permission notice shall be included in
  76. # all copies or substantial portions of the Software.
  77. #
  78. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  79. # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  80. # FITNESS OR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  81. # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  82. # LIABILITY WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  83. # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  84. # THE SOFTWARE.
  85. #
  86. ******************************************************************************/
  87. #include "EPD_5in83.h"
  88. #include "Debug.h"
  89. /******************************************************************************
  90. function : Software reset
  91. parameter:
  92. ******************************************************************************/
  93. static void EPD_5IN83_Reset(void)
  94. {
  95. DEV_Digital_Write(EPD_RST_PIN, 1);
  96. DEV_Delay_ms(200);
  97. DEV_Digital_Write(EPD_RST_PIN, 0);
  98. DEV_Delay_ms(2);
  99. DEV_Digital_Write(EPD_RST_PIN, 1);
  100. DEV_Delay_ms(200);
  101. }
  102. /******************************************************************************
  103. function : send command
  104. parameter:
  105. Reg : Command register
  106. ******************************************************************************/
  107. static void EPD_5IN83_SendCommand(UBYTE Reg)
  108. {
  109. DEV_Digital_Write(EPD_DC_PIN, 0);
  110. DEV_Digital_Write(EPD_CS_PIN, 0);
  111. DEV_SPI_WriteByte(Reg);
  112. DEV_Digital_Write(EPD_CS_PIN, 1);
  113. }
  114. /******************************************************************************
  115. function : send data
  116. parameter:
  117. Data : Write data
  118. ******************************************************************************/
  119. static void EPD_5IN83_SendData(UBYTE Data)
  120. {
  121. DEV_Digital_Write(EPD_DC_PIN, 1);
  122. DEV_Digital_Write(EPD_CS_PIN, 0);
  123. DEV_SPI_WriteByte(Data);
  124. DEV_Digital_Write(EPD_CS_PIN, 1);
  125. }
  126. /******************************************************************************
  127. function : Wait until the busy_pin goes LOW
  128. parameter:
  129. ******************************************************************************/
  130. static void EPD_5IN83_ReadBusy(void)
  131. {
  132. Debug("e-Paper busy\r\n");
  133. while(DEV_Digital_Read(EPD_BUSY_PIN) == 0) { //LOW: idle, HIGH: busy
  134. DEV_Delay_ms(100);
  135. }
  136. Debug("e-Paper busy release\r\n");
  137. }
  138. /******************************************************************************
  139. function : Turn On Display
  140. parameter:
  141. ******************************************************************************/
  142. static void EPD_5IN83_TurnOnDisplay(void)
  143. {
  144. EPD_5IN83_SendCommand(0x12); // DISPLAY_REFRESH
  145. DEV_Delay_ms(100);
  146. EPD_5IN83_ReadBusy();
  147. }
  148. /******************************************************************************
  149. function : Initialize the e-Paper register
  150. parameter:
  151. ******************************************************************************/
  152. void EPD_5IN83_Init(void)
  153. {
  154. EPD_5IN83_Reset();
  155. EPD_5IN83_SendCommand(0x01); // POWER_SETTING
  156. EPD_5IN83_SendData(0x37);
  157. EPD_5IN83_SendData(0x00);
  158. EPD_5IN83_SendCommand(0x00); // PANEL_SETTING
  159. EPD_5IN83_SendData(0xCF);
  160. EPD_5IN83_SendData(0x08);
  161. EPD_5IN83_SendCommand(0x06); // BOOSTER_SOFT_START
  162. EPD_5IN83_SendData(0xc7);
  163. EPD_5IN83_SendData(0xcc);
  164. EPD_5IN83_SendData(0x28);
  165. EPD_5IN83_SendCommand(0x04); // POWER_ON
  166. EPD_5IN83_ReadBusy();
  167. EPD_5IN83_SendCommand(0x30); // PLL_CONTROL
  168. EPD_5IN83_SendData(0x3c);
  169. EPD_5IN83_SendCommand(0x41); // TEMPERATURE_CALIBRATION
  170. EPD_5IN83_SendData(0x00);
  171. EPD_5IN83_SendCommand(0x50); // VCOM_AND_DATA_INTERVAL_SETTING
  172. EPD_5IN83_SendData(0x77);
  173. EPD_5IN83_SendCommand(0x60); // TCON_SETTING
  174. EPD_5IN83_SendData(0x22);
  175. EPD_5IN83_SendCommand(0x61); // TCON_RESOLUTION
  176. EPD_5IN83_SendData(EPD_5IN83_WIDTH >> 8); // source 600
  177. EPD_5IN83_SendData(EPD_5IN83_WIDTH & 0xff);
  178. EPD_5IN83_SendData(EPD_5IN83_HEIGHT >> 8); // gate 448
  179. EPD_5IN83_SendData(EPD_5IN83_HEIGHT & 0xff);
  180. EPD_5IN83_SendCommand(0x82); // VCM_DC_SETTING
  181. EPD_5IN83_SendData(0x1E); // decide by LUT file
  182. EPD_5IN83_SendCommand(0xe5); // FLASH MODE
  183. EPD_5IN83_SendData(0x03);
  184. }
  185. /******************************************************************************
  186. function : Clear screen
  187. parameter:
  188. ******************************************************************************/
  189. void EPD_5IN83_Clear(void)
  190. {
  191. UWORD Width, Height;
  192. Width = (EPD_5IN83_WIDTH % 8 == 0)? (EPD_5IN83_WIDTH / 8 ): (EPD_5IN83_WIDTH / 8 + 1);
  193. Height = EPD_5IN83_HEIGHT;
  194. EPD_5IN83_SendCommand(0x10);
  195. for (UWORD j = 0; j < Height; j++) {
  196. for (UWORD i = 0; i < Width; i++) {
  197. for(UBYTE k = 0; k < 4; k++) {
  198. EPD_5IN83_SendData(0x33);
  199. }
  200. }
  201. }
  202. EPD_5IN83_TurnOnDisplay();
  203. }
  204. /******************************************************************************
  205. function : Sends the image buffer in RAM to e-Paper and displays
  206. parameter:
  207. ******************************************************************************/
  208. void EPD_5IN83_Display(UBYTE *Image)
  209. {
  210. UBYTE Data_Black, Data;
  211. UWORD Width, Height;
  212. Width = (EPD_5IN83_WIDTH % 8 == 0)? (EPD_5IN83_WIDTH / 8 ): (EPD_5IN83_WIDTH / 8 + 1);
  213. Height = EPD_5IN83_HEIGHT;
  214. EPD_5IN83_SendCommand(0x10);
  215. for (UWORD j = 0; j < Height; j++) {
  216. for (UWORD i = 0; i < Width; i++) {
  217. Data_Black = ~Image[i + j * Width];
  218. for(UBYTE k = 0; k < 8; k++) {
  219. if(Data_Black & 0x80)
  220. Data = 0x00;
  221. else
  222. Data = 0x03;
  223. Data <<= 4;
  224. Data_Black <<= 1;
  225. k++;
  226. if(Data_Black & 0x80)
  227. Data |= 0x00;
  228. else
  229. Data |= 0x03;
  230. Data_Black <<= 1;
  231. EPD_5IN83_SendData(Data);
  232. }
  233. }
  234. }
  235. EPD_5IN83_TurnOnDisplay();
  236. }
  237. /******************************************************************************
  238. function : Enter sleep mode
  239. parameter:
  240. ******************************************************************************/
  241. void EPD_5IN83_Sleep(void)
  242. {
  243. EPD_5IN83_SendCommand(0x02); // POWER_OFF
  244. EPD_5IN83_ReadBusy();
  245. EPD_5IN83_SendCommand(0x07); // DEEP_SLEEP
  246. EPD_5IN83_SendData(0XA5);
  247. }