1 /*****************************************************************************
2 * | File : EPD_1in54b.c
3 * | Author : Waveshare team
4 * | Function : 1.54inch e-paper b
10 * -----------------------------------------------------------------------------
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 * -----------------------------------------------------------------------------
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
64 * -----------------------------------------------------------------------------
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
70 * EPD_RST -> EPD_RST_PIN
71 * EPD_DC -> EPD_DC_PIN
72 * EPD_CS -> EPD_CS_PIN
73 * EPD_BUSY -> EPD_BUSY_PIN
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:
82 # The above copyright notice and this permission notice shall be included in
83 # all copies or substantial portions of the Software.
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
93 ******************************************************************************/
94 #include "EPD_1in54b.h"
97 const unsigned char EPD_1IN54B_lut_vcom0
[] = {0x0E, 0x14, 0x01, 0x0A, 0x06, 0x04, 0x0A, 0x0A, 0x0F, 0x03, 0x03, 0x0C, 0x06, 0x0A, 0x00};
98 const unsigned char EPD_1IN54B_lut_w
[] = {0x0E, 0x14, 0x01, 0x0A, 0x46, 0x04, 0x8A, 0x4A, 0x0F, 0x83, 0x43, 0x0C, 0x86, 0x0A, 0x04};
99 const unsigned char EPD_1IN54B_lut_b
[] = {0x0E, 0x14, 0x01, 0x8A, 0x06, 0x04, 0x8A, 0x4A, 0x0F, 0x83, 0x43, 0x0C, 0x06, 0x4A, 0x04};
100 const unsigned char EPD_1IN54B_lut_g1
[] = {0x8E, 0x94, 0x01, 0x8A, 0x06, 0x04, 0x8A, 0x4A, 0x0F, 0x83, 0x43, 0x0C, 0x06, 0x0A, 0x04};
101 const unsigned char EPD_1IN54B_lut_g2
[] = {0x8E, 0x94, 0x01, 0x8A, 0x06, 0x04, 0x8A, 0x4A, 0x0F, 0x83, 0x43, 0x0C, 0x06, 0x0A, 0x04};
102 const unsigned char EPD_1IN54B_lut_vcom1
[] = {0x03, 0x1D, 0x01, 0x01, 0x08, 0x23, 0x37, 0x37, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
103 const unsigned char EPD_1IN54B_lut_red0
[] = {0x83, 0x5D, 0x01, 0x81, 0x48, 0x23, 0x77, 0x77, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
104 const unsigned char EPD_1IN54B_lut_red1
[] = {0x03, 0x1D, 0x01, 0x01, 0x08, 0x23, 0x37, 0x37, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
106 /******************************************************************************
107 function : Software reset
109 ******************************************************************************/
110 static void EPD_1IN54B_Reset(void)
112 DEV_Digital_Write(EPD_RST_PIN
, 1);
114 DEV_Digital_Write(EPD_RST_PIN
, 0);
116 DEV_Digital_Write(EPD_RST_PIN
, 1);
120 /******************************************************************************
121 function : send command
123 Reg : Command register
124 ******************************************************************************/
125 static void EPD_1IN54B_SendCommand(UBYTE Reg
)
127 DEV_Digital_Write(EPD_DC_PIN
, 0);
128 DEV_Digital_Write(EPD_CS_PIN
, 0);
129 DEV_SPI_WriteByte(Reg
);
130 DEV_Digital_Write(EPD_CS_PIN
, 1);
133 /******************************************************************************
137 ******************************************************************************/
138 static void EPD_1IN54B_SendData(UBYTE Data
)
140 DEV_Digital_Write(EPD_DC_PIN
, 1);
141 DEV_Digital_Write(EPD_CS_PIN
, 0);
142 DEV_SPI_WriteByte(Data
);
143 DEV_Digital_Write(EPD_CS_PIN
, 1);
146 /******************************************************************************
147 function : Wait until the busy_pin goes LOW
149 ******************************************************************************/
150 static void EPD_1IN54B_ReadBusy(void)
152 EPD_Busy_WaitUntil(1,0);
153 // unsigned char count = 100;
154 // Debug("e-Paper busy\r\n");
156 // if(DEV_Digital_Read(EPD_BUSY_PIN) == 1)
160 // Debug("error: e-Paper busy timeout!!!\r\n");
164 // DEV_Delay_ms(100);
166 // DEV_Delay_ms(200);
167 // Debug("e-Paper busy release\r\n");
170 /******************************************************************************
171 function : Set the look-up black and white tables
173 ******************************************************************************/
174 static void EPD_1IN54B_SetLutBw(void)
177 EPD_1IN54B_SendCommand(0x20);// g vcom
178 for(count
= 0; count
< 15; count
++) {
179 EPD_1IN54B_SendData(EPD_1IN54B_lut_vcom0
[count
]);
181 EPD_1IN54B_SendCommand(0x21);// g ww --
182 for(count
= 0; count
< 15; count
++) {
183 EPD_1IN54B_SendData(EPD_1IN54B_lut_w
[count
]);
185 EPD_1IN54B_SendCommand(0x22);// g bw r
186 for(count
= 0; count
< 15; count
++) {
187 EPD_1IN54B_SendData(EPD_1IN54B_lut_b
[count
]);
189 EPD_1IN54B_SendCommand(0x23);// g wb w
190 for(count
= 0; count
< 15; count
++) {
191 EPD_1IN54B_SendData(EPD_1IN54B_lut_g1
[count
]);
193 EPD_1IN54B_SendCommand(0x24);// g bb b
194 for(count
= 0; count
< 15; count
++) {
195 EPD_1IN54B_SendData(EPD_1IN54B_lut_g2
[count
]);
199 /******************************************************************************
200 function : Set the look-up red tables
202 ******************************************************************************/
203 static void EPD_1IN54B_SetLutRed(void)
206 EPD_1IN54B_SendCommand(0x25);
207 for(count
= 0; count
< 15; count
++) {
208 EPD_1IN54B_SendData(EPD_1IN54B_lut_vcom1
[count
]);
210 EPD_1IN54B_SendCommand(0x26);
211 for(count
= 0; count
< 15; count
++) {
212 EPD_1IN54B_SendData(EPD_1IN54B_lut_red0
[count
]);
214 EPD_1IN54B_SendCommand(0x27);
215 for(count
= 0; count
< 15; count
++) {
216 EPD_1IN54B_SendData(EPD_1IN54B_lut_red1
[count
]);
220 /******************************************************************************
221 function : Initialize the e-Paper register
223 ******************************************************************************/
224 void EPD_1IN54B_Init(UBYTE mode
)
228 EPD_1IN54B_SendCommand(0x01);// POWER_SETTING
229 EPD_1IN54B_SendData(0x07);
230 EPD_1IN54B_SendData(0x00);
231 EPD_1IN54B_SendData(0x08);
232 EPD_1IN54B_SendData(0x00);
233 EPD_1IN54B_SendCommand(0x06);// BOOSTER_SOFT_START
234 EPD_1IN54B_SendData(0x07);
235 EPD_1IN54B_SendData(0x07);
236 EPD_1IN54B_SendData(0x07);
237 EPD_1IN54B_SendCommand(0x04);// POWER_ON
239 EPD_1IN54B_ReadBusy();
241 EPD_1IN54B_SendCommand(0X00);// PANEL_SETTING
242 EPD_1IN54B_SendData(0xcf);
243 EPD_1IN54B_SendCommand(0X50);// VCOM_AND_DATA_INTERVAL_SETTING
244 EPD_1IN54B_SendData(0x37);// 0xF0
245 EPD_1IN54B_SendCommand(0x30);// PLL_CONTROL
246 EPD_1IN54B_SendData(0x39);
247 EPD_1IN54B_SendCommand(0x61);// TCON_RESOLUTION set x and y
248 EPD_1IN54B_SendData(0xC8);// 200
249 EPD_1IN54B_SendData(0x00);// y High eight: 0
250 EPD_1IN54B_SendData(0xC8);// y Low eight: 200
251 EPD_1IN54B_SendCommand(0x82);// VCM_DC_SETTING_REGISTER
252 EPD_1IN54B_SendData(0x0E);
254 EPD_1IN54B_SetLutBw();
255 EPD_1IN54B_SetLutRed();
258 /******************************************************************************
259 function : Clear screen
261 ******************************************************************************/
262 void EPD_1IN54B_Clear(void)
265 Width
= (EPD_1IN54B_WIDTH
% 8 == 0)? (EPD_1IN54B_WIDTH
/ 8 ): (EPD_1IN54B_WIDTH
/ 8 + 1);
266 Height
= EPD_1IN54B_HEIGHT
;
269 EPD_1IN54B_SendCommand(0x10);// DATA_START_TRANSMISSION_1
271 for(UWORD i
= 0; i
< Height
; i
++) {
272 for(UWORD i
= 0; i
< Width
; i
++) {
273 EPD_1IN54B_SendData(0xFF);
274 EPD_1IN54B_SendData(0xFF);
280 EPD_1IN54B_SendCommand(0x13);// DATA_START_TRANSMISSION_2
282 for(UWORD i
= 0; i
< Height
; i
++) {
283 for(UWORD i
= 0; i
< Width
; i
++) {
284 EPD_1IN54B_SendData(0xFF);
289 EPD_1IN54B_SendCommand(0x12);// DISPLAY_REFRESH
290 EPD_1IN54B_ReadBusy();
293 /******************************************************************************
294 function : Sends the image buffer in RAM to e-Paper and displays
296 ******************************************************************************/
297 void EPD_1IN54B_Display(const UBYTE
*blackimage
, const UBYTE
*redimage
)
301 Width
= (EPD_1IN54B_WIDTH
% 8 == 0)? (EPD_1IN54B_WIDTH
/ 8 ): (EPD_1IN54B_WIDTH
/ 8 + 1);
302 Height
= EPD_1IN54B_HEIGHT
;
304 EPD_1IN54B_SendCommand(0x10);// DATA_START_TRANSMISSION_1
305 for (UWORD j
= 0; j
< Height
; j
++) {
306 for (UWORD i
= 0; i
< Width
; i
++) {
308 for (int bit
= 0; bit
< 4; bit
++) {
309 if ((blackimage
[i
+ j
* Width
] & (0x80 >> bit
)) != 0) {
310 Temp
|= 0xC0 >> (bit
* 2);
313 EPD_1IN54B_SendData(Temp
);
315 for (int bit
= 4; bit
< 8; bit
++) {
316 if ((blackimage
[i
+ j
* Width
] & (0x80 >> bit
)) != 0) {
317 Temp
|= 0xC0 >> ((bit
- 4) * 2);
320 EPD_1IN54B_SendData(Temp
);
325 EPD_1IN54B_SendCommand(0x13);// DATA_START_TRANSMISSION_2
326 for (UWORD j
= 0; j
< Height
; j
++) {
327 for (UWORD i
= 0; i
< Width
; i
++) {
328 EPD_1IN54B_SendData(redimage
[i
+ j
* Width
]);
334 EPD_1IN54B_SendCommand(0x12);// DISPLAY_REFRESH
335 EPD_1IN54B_ReadBusy();
338 /******************************************************************************
339 function : Enter sleep mode
341 ******************************************************************************/
342 void EPD_1IN54B_Sleep(void)
344 EPD_1IN54B_SendCommand(0x50);// VCOM_AND_DATA_INTERVAL_SETTING
345 EPD_1IN54B_SendData(0x17);
346 EPD_1IN54B_SendCommand(0x82);// VCM_DC_SETTING_REGISTER
347 EPD_1IN54B_SendData(0x00);
348 EPD_1IN54B_SendCommand(0x01);// POWER_SETTING
349 EPD_1IN54B_SendData(0x02);
350 EPD_1IN54B_SendData(0x00);
351 EPD_1IN54B_SendData(0x00);
352 EPD_1IN54B_SendData(0x00);
353 EPD_1IN54B_ReadBusy();
356 EPD_1IN54B_SendCommand(0x02);// POWER_OFF