EPD_10in2b.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. /*****************************************************************************
  2. * | File : EPD_10in2b.c
  3. * | Author : Waveshare team
  4. * | Function : 10.2inch b e-paper test demo
  5. * | Info :
  6. *----------------
  7. * | This version: V1.0
  8. * | Date : 2021-01-20
  9. * | Info :
  10. * -----------------------------------------------------------------------------
  11. #
  12. # Permission is hereby granted, free of charge, to any person obtaining a copy
  13. # of this software and associated documnetation files (the "Software"), to deal
  14. # in the Software without restriction, including without limitation the rights
  15. # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  16. # copies of the Software, and to permit persons to whom the Software is
  17. # furished to do so, subject to the following conditions:
  18. #
  19. # The above copyright notice and this permission notice shall be included in
  20. # all copies or substantial portions of the Software.
  21. #
  22. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  23. # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  24. # FITNESS OR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  25. # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  26. # LIABILITY WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  27. # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  28. # THE SOFTWARE.
  29. #
  30. ******************************************************************************/
  31. #include "EPD_10in2b.h"
  32. #include "Debug.h"
  33. /******************************************************************************
  34. function : Software reset
  35. parameter:
  36. ******************************************************************************/
  37. static void EPD_10IN2b_Reset(void)
  38. {
  39. DEV_Digital_Write(EPD_RST_PIN, 1);
  40. DEV_Delay_ms(100);
  41. DEV_Digital_Write(EPD_RST_PIN, 0);
  42. DEV_Delay_ms(2);
  43. DEV_Digital_Write(EPD_RST_PIN, 1);
  44. DEV_Delay_ms(100);
  45. }
  46. /******************************************************************************
  47. function : send command
  48. parameter:
  49. Reg : Command register
  50. ******************************************************************************/
  51. static void EPD_10IN2b_SendCommand(UBYTE Reg)
  52. {
  53. DEV_Digital_Write(EPD_DC_PIN, 0);
  54. DEV_Digital_Write(EPD_CS_PIN, 0);
  55. DEV_SPI_WriteByte(Reg);
  56. DEV_Digital_Write(EPD_CS_PIN, 1);
  57. }
  58. /******************************************************************************
  59. function : send data
  60. parameter:
  61. Data : Write data
  62. ******************************************************************************/
  63. static void EPD_10IN2b_SendData(UBYTE Data)
  64. {
  65. DEV_Digital_Write(EPD_DC_PIN, 1);
  66. DEV_Digital_Write(EPD_CS_PIN, 0);
  67. DEV_SPI_WriteByte(Data);
  68. DEV_Digital_Write(EPD_CS_PIN, 1);
  69. }
  70. /******************************************************************************
  71. function : Wait until the busy_pin goes LOW
  72. parameter:
  73. ******************************************************************************/
  74. void EPD_10IN2b_ReadBusy(void)
  75. {
  76. Debug("e-Paper busy\r\n");
  77. while(1)
  78. { //=1 BUSY
  79. if(DEV_Digital_Read(EPD_BUSY_PIN)==0)
  80. break;
  81. DEV_Delay_ms(20);
  82. }
  83. DEV_Delay_ms(20);
  84. Debug("e-Paper busy release\r\n");
  85. }
  86. /******************************************************************************
  87. function : Turn On Display
  88. parameter:
  89. ******************************************************************************/
  90. static void EPD_10IN2b_TurnOnDisplay(void)
  91. {
  92. EPD_10IN2b_SendCommand(0x22); //Display Update Control
  93. EPD_10IN2b_SendData(0xC7);
  94. EPD_10IN2b_SendCommand(0x20); //Activate Display Update Sequence
  95. EPD_10IN2b_ReadBusy();
  96. }
  97. /******************************************************************************
  98. function : Setting the display window
  99. parameter:
  100. ******************************************************************************/
  101. static void EPD_10IN2b_SetWindows(UWORD Xstart, UWORD Ystart, UWORD Xend, UWORD Yend)
  102. {
  103. EPD_10IN2b_SendCommand(0x44); // SET_RAM_X_ADDRESS_START_END_POSITION
  104. EPD_10IN2b_SendData(Xstart & 0xFF);
  105. EPD_10IN2b_SendData((Xstart>>8) & 0x03);
  106. EPD_10IN2b_SendData(Xend & 0xFF);
  107. EPD_10IN2b_SendData((Xend>>8) & 0x03);
  108. EPD_10IN2b_SendCommand(0x45); // SET_RAM_Y_ADDRESS_START_END_POSITION
  109. EPD_10IN2b_SendData(Ystart & 0xFF);
  110. EPD_10IN2b_SendData((Ystart>>8) & 0x03);
  111. EPD_10IN2b_SendData(Yend & 0xFF);
  112. EPD_10IN2b_SendData((Yend>>8) & 0x03);
  113. }
  114. /******************************************************************************
  115. function : Set Cursor
  116. parameter:
  117. ******************************************************************************/
  118. static void EPD_10IN2b_SetCursor(UWORD Xstart, UWORD Ystart)
  119. {
  120. EPD_10IN2b_SendCommand(0x4E); // SET_RAM_X_ADDRESS_COUNTER
  121. EPD_10IN2b_SendData(Xstart & 0xFF);
  122. EPD_10IN2b_SendData((Xstart>>8) & 0x03);
  123. EPD_10IN2b_SendCommand(0x4F); // SET_RAM_Y_ADDRESS_COUNTER
  124. EPD_10IN2b_SendData(Ystart & 0xFF);
  125. EPD_10IN2b_SendData((Ystart>>8) & 0x03);
  126. }
  127. /******************************************************************************
  128. function : Initialize the e-Paper register
  129. parameter:
  130. ******************************************************************************/
  131. void EPD_10IN2b_Init(void)
  132. {
  133. EPD_10IN2b_Reset();
  134. DEV_Delay_ms(100);
  135. EPD_10IN2b_ReadBusy();
  136. EPD_10IN2b_SendCommand(0x12); //SWRESET
  137. EPD_10IN2b_ReadBusy();
  138. EPD_10IN2b_SendCommand(0x0C); //set soft start
  139. EPD_10IN2b_SendData(0xAE);
  140. EPD_10IN2b_SendData(0xC7);
  141. EPD_10IN2b_SendData(0xC3);
  142. EPD_10IN2b_SendData(0xC0);
  143. EPD_10IN2b_SendData(0x80);
  144. EPD_10IN2b_SendCommand(0x01); // drive output control
  145. EPD_10IN2b_SendData(0xA7); // Y
  146. EPD_10IN2b_SendData(0x02); // Y
  147. EPD_10IN2b_SendData(0x00);
  148. EPD_10IN2b_SendCommand(0x11); // data entry mode
  149. EPD_10IN2b_SendData(0x03); // X-mode x+ y-
  150. EPD_10IN2b_SetWindows(0, 0, EPD_10IN2b_WIDTH-1, EPD_10IN2b_HEIGHT-1);
  151. EPD_10IN2b_SendCommand(0x3C); // Border Border setting
  152. EPD_10IN2b_SendData(0x01);
  153. EPD_10IN2b_SendCommand(0x18); // use the internal temperature sensor
  154. EPD_10IN2b_SendData(0x80);
  155. EPD_10IN2b_SendCommand(0x22);
  156. EPD_10IN2b_SendData(0xB1);
  157. EPD_10IN2b_SendCommand(0x20);
  158. EPD_10IN2b_SetCursor(0, 0);
  159. EPD_10IN2b_ReadBusy();
  160. }
  161. /******************************************************************************
  162. function : Clear screen
  163. parameter:
  164. ******************************************************************************/
  165. void EPD_10IN2b_Clear(void)
  166. {
  167. UWORD i, j;
  168. UWORD height = EPD_10IN2b_HEIGHT;
  169. UWORD width = EPD_10IN2b_WIDTH/8;
  170. EPD_10IN2b_SendCommand(0x24); //write RAM for black(0)/white (1)
  171. for(i=0; i<height; i++)
  172. {
  173. for(j=0; j<width; j++)
  174. EPD_10IN2b_SendData(0xff);
  175. }
  176. EPD_10IN2b_SendCommand(0x26); //write RAM for black(0)/white (1)
  177. for(i=0; i<height; i++)
  178. {
  179. for(j=0; j<width; j++)
  180. EPD_10IN2b_SendData(0x00);
  181. }
  182. EPD_10IN2b_TurnOnDisplay();
  183. }
  184. /******************************************************************************
  185. function : Sends the image buffer in RAM to e-Paper and displays
  186. parameter:
  187. ******************************************************************************/
  188. void EPD_10IN2b_Display(UBYTE *Image, UBYTE *RedImage)
  189. {
  190. UWORD i, j;
  191. UWORD height = EPD_10IN2b_HEIGHT;
  192. UWORD width = EPD_10IN2b_WIDTH/8;
  193. EPD_10IN2b_SendCommand(0x24); //write RAM for black(0)/white (1)
  194. for(i=0; i<height; i++)
  195. {
  196. for(j=0; j<width; j++)
  197. EPD_10IN2b_SendData(Image[j + i*width]);
  198. }
  199. EPD_10IN2b_SendCommand(0x26); //write RAM for black(0)/white (1)
  200. for(i=0; i<height; i++)
  201. {
  202. for(j=0; j<width; j++)
  203. EPD_10IN2b_SendData(~RedImage[j + i*width]);
  204. }
  205. EPD_10IN2b_TurnOnDisplay();
  206. }
  207. /******************************************************************************
  208. function : Enter sleep mode
  209. parameter:
  210. ******************************************************************************/
  211. void EPD_10IN2b_Sleep(void)
  212. {
  213. EPD_10IN2b_SendCommand(0x10); //enter deep sleep
  214. EPD_10IN2b_SendData(0x01);
  215. DEV_Delay_ms(100);
  216. }