1 /*****************************************************************************
2 * | File : EPD_1in54_V2.c
3 * | Author : Waveshare team
4 * | Function : 1.54inch e-paper V2
11 # Permission is hereby granted, free of charge, to any person obtaining a copy
12 # of this software and associated documnetation files (the "Software"), to deal
13 # in the Software without restriction, including without limitation the rights
14 # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
15 # copies of the Software, and to permit persons to whom the Software is
16 # furished to do so, subject to the following conditions:
18 # The above copyright notice and this permission notice shall be included in
19 # all copies or substantial portions of the Software.
21 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22 # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23 # FITNESS OR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
24 # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25 # LIABILITY WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
26 # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
29 ******************************************************************************/
30 #include "EPD_1in54_V2.h"
33 /******************************************************************************
34 function : Software reset
36 ******************************************************************************/
37 static void EPD_1IN54_V2_Reset(void)
39 DEV_Digital_Write(EPD_RST_PIN
, 1);
41 DEV_Digital_Write(EPD_RST_PIN
, 0);
43 DEV_Digital_Write(EPD_RST_PIN
, 1);
47 /******************************************************************************
48 function : send command
50 Reg : Command register
51 ******************************************************************************/
52 static void EPD_1IN54_V2_SendCommand(UBYTE Reg
)
54 DEV_Digital_Write(EPD_DC_PIN
, 0);
55 DEV_Digital_Write(EPD_CS_PIN
, 0);
56 DEV_SPI_WriteByte(Reg
);
57 DEV_Digital_Write(EPD_CS_PIN
, 1);
60 /******************************************************************************
64 ******************************************************************************/
65 static void EPD_1IN54_V2_SendData(UBYTE Data
)
67 DEV_Digital_Write(EPD_DC_PIN
, 1);
68 DEV_Digital_Write(EPD_CS_PIN
, 0);
69 DEV_SPI_WriteByte(Data
);
70 DEV_Digital_Write(EPD_CS_PIN
, 1);
73 /******************************************************************************
74 function : Wait until the busy_pin goes LOW
76 ******************************************************************************/
77 static void EPD_1IN54_V2_ReadBusy(void)
79 EPD_Busy_WaitUntil(0,0);
81 // unsigned char count = 100;
82 // Debug("e-Paper busy\r\n");
85 // // EPD_1IN54_V2_SendCommand(0x71);
86 // // busy = DEV_Digital_Read(EPD_BUSY_PIN);
87 // // busy = !(busy & 0x01);
89 // // DEV_Delay_ms(200);
90 // while(DEV_Digital_Read(EPD_BUSY_PIN) == 1) { //LOW: idle, HIGH: busy
93 // Debug("error: e-Paper busy timeout!!!\r\n");
99 // Debug("e-Paper busy release\r\n");
102 /******************************************************************************
103 function : Turn On Display full
105 ******************************************************************************/
106 static void EPD_1IN54_V2_TurnOnDisplay(void)
108 EPD_1IN54_V2_SendCommand(0x22);
109 EPD_1IN54_V2_SendData(0xF7);
110 EPD_1IN54_V2_SendCommand(0x20);
111 EPD_1IN54_V2_ReadBusy();
114 /******************************************************************************
115 function : Turn On Display part
117 ******************************************************************************/
118 static void EPD_1IN54_V2_TurnOnDisplayPart(void)
120 EPD_1IN54_V2_SendCommand(0x22);
121 EPD_1IN54_V2_SendData(0xFF);
122 EPD_1IN54_V2_SendCommand(0x20);
123 EPD_1IN54_V2_ReadBusy();
126 /******************************************************************************
127 function : Initialize the e-Paper register
129 ******************************************************************************/
130 void EPD_1IN54_V2_Init(UBYTE mode
)
132 EPD_1IN54_V2_Reset();
134 EPD_1IN54_V2_ReadBusy();
135 EPD_1IN54_V2_SendCommand(0x12); //SWRESET
136 EPD_1IN54_V2_ReadBusy();
138 EPD_1IN54_V2_SendCommand(0x01); //Driver output control
139 EPD_1IN54_V2_SendData(0xC7);
140 EPD_1IN54_V2_SendData(0x00);
141 EPD_1IN54_V2_SendData(0x01);
143 EPD_1IN54_V2_SendCommand(0x11); //data entry mode
144 EPD_1IN54_V2_SendData(0x01);
146 EPD_1IN54_V2_SendCommand(0x44); //set Ram-X address start/end position
147 EPD_1IN54_V2_SendData(0x00);
148 EPD_1IN54_V2_SendData(0x18); //0x0C-->(18+1)*8=200
150 EPD_1IN54_V2_SendCommand(0x45); //set Ram-Y address start/end position
151 EPD_1IN54_V2_SendData(0xC7); //0xC7-->(199+1)=200
152 EPD_1IN54_V2_SendData(0x00);
153 EPD_1IN54_V2_SendData(0x00);
154 EPD_1IN54_V2_SendData(0x00);
156 EPD_1IN54_V2_SendCommand(0x3C); //BorderWavefrom
157 EPD_1IN54_V2_SendData(0x01);
159 EPD_1IN54_V2_SendCommand(0x18);
160 EPD_1IN54_V2_SendData(0x80);
162 EPD_1IN54_V2_SendCommand(0x22); // //Load Temperature and waveform setting.
163 EPD_1IN54_V2_SendData(0XB1);
164 EPD_1IN54_V2_SendCommand(0x20);
166 EPD_1IN54_V2_SendCommand(0x4E); // set RAM x address count to 0;
167 EPD_1IN54_V2_SendData(0x00);
168 EPD_1IN54_V2_SendCommand(0x4F); // set RAM y address count to 0X199;
169 EPD_1IN54_V2_SendData(0xC7);
170 EPD_1IN54_V2_SendData(0x00);
171 EPD_1IN54_V2_ReadBusy();
174 /******************************************************************************
175 function : Clear screen
177 ******************************************************************************/
178 void EPD_1IN54_V2_Clear(void)
181 Width
= (EPD_1IN54_V2_WIDTH
% 8 == 0)? (EPD_1IN54_V2_WIDTH
/ 8 ): (EPD_1IN54_V2_WIDTH
/ 8 + 1);
182 Height
= EPD_1IN54_V2_HEIGHT
;
184 EPD_1IN54_V2_SendCommand(0x24);
185 for (UWORD j
= 0; j
< Height
; j
++) {
186 for (UWORD i
= 0; i
< Width
; i
++) {
187 EPD_1IN54_V2_SendData(0XFF);
190 EPD_1IN54_V2_TurnOnDisplay();
193 /******************************************************************************
194 function : Sends the image buffer in RAM to e-Paper and displays
196 ******************************************************************************/
197 void EPD_1IN54_V2_Display(UBYTE
*Image
, UBYTE
*Image2
)
200 Width
= (EPD_1IN54_V2_WIDTH
% 8 == 0)? (EPD_1IN54_V2_WIDTH
/ 8 ): (EPD_1IN54_V2_WIDTH
/ 8 + 1);
201 Height
= EPD_1IN54_V2_HEIGHT
;
204 EPD_1IN54_V2_SendCommand(0x24);
205 for (UWORD j
= 0; j
< Height
; j
++) {
206 for (UWORD i
= 0; i
< Width
; i
++) {
207 Addr
= i
+ j
* Width
;
208 EPD_1IN54_V2_SendData(Image
[Addr
]);
211 EPD_1IN54_V2_TurnOnDisplay();
214 /******************************************************************************
215 function : The image of the previous frame must be uploaded, otherwise the
216 first few seconds will display an exception.
218 ******************************************************************************/
219 void EPD_1IN54_V2_DisplayPartBaseImage(UBYTE
*Image
)
222 Width
= (EPD_1IN54_V2_WIDTH
% 8 == 0)? (EPD_1IN54_V2_WIDTH
/ 8 ): (EPD_1IN54_V2_WIDTH
/ 8 + 1);
223 Height
= EPD_1IN54_V2_HEIGHT
;
226 EPD_1IN54_V2_SendCommand(0x24);
227 for (UWORD j
= 0; j
< Height
; j
++) {
228 for (UWORD i
= 0; i
< Width
; i
++) {
229 Addr
= i
+ j
* Width
;
230 EPD_1IN54_V2_SendData(Image
[Addr
]);
233 EPD_1IN54_V2_SendCommand(0x26);
234 for (UWORD j
= 0; j
< Height
; j
++) {
235 for (UWORD i
= 0; i
< Width
; i
++) {
236 Addr
= i
+ j
* Width
;
237 EPD_1IN54_V2_SendData(Image
[Addr
]);
240 EPD_1IN54_V2_TurnOnDisplayPart();
243 /******************************************************************************
244 function : Sends the image buffer in RAM to e-Paper and displays
246 ******************************************************************************/
247 void EPD_1IN54_V2_DisplayPart(UBYTE
*Image
)
250 Width
= (EPD_1IN54_V2_WIDTH
% 8 == 0)? (EPD_1IN54_V2_WIDTH
/ 8 ): (EPD_1IN54_V2_WIDTH
/ 8 + 1);
251 Height
= EPD_1IN54_V2_HEIGHT
;
253 DEV_Digital_Write(EPD_RST_PIN
, 0);
255 DEV_Digital_Write(EPD_RST_PIN
, 1);
257 EPD_1IN54_V2_SendCommand(0x3C); //BorderWavefrom
258 EPD_1IN54_V2_SendData(0x80);
261 EPD_1IN54_V2_SendCommand(0x24);
262 for (UWORD j
= 0; j
< Height
; j
++) {
263 for (UWORD i
= 0; i
< Width
; i
++) {
264 Addr
= i
+ j
* Width
;
265 EPD_1IN54_V2_SendData(Image
[Addr
]);
268 EPD_1IN54_V2_TurnOnDisplayPart();
270 /******************************************************************************
271 function : Enter sleep mode
273 ******************************************************************************/
274 void EPD_1IN54_V2_Sleep(void)
276 EPD_1IN54_V2_SendCommand(0x10); //enter deep sleep
277 EPD_1IN54_V2_SendData(0x01);