Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: mbed-STM32F103C8T6 SHT21_ncleee
st7789v.cpp
00001 #include "main.h" 00002 00003 void ST7789V_CTRL_Write(unsigned char data) 00004 { 00005 lcdBus.write(data); 00006 } 00007 00008 void ST7789V_writeCommand(unsigned char command) 00009 { 00010 TFTCS=0; 00011 TFTDC = 0; 00012 ST7789V_CTRL_Write(command); 00013 TFTWR = 0; 00014 TFTWR = 1; 00015 TFTCS=1; 00016 } 00017 00018 void ST7789V_writeData(unsigned char data) 00019 { 00020 TFTCS=0; 00021 TFTDC = 1; 00022 ST7789V_CTRL_Write(data); 00023 TFTWR = 0; 00024 TFTWR = 1; 00025 TFTCS=1; 00026 } 00027 00028 void ST7789V_writeOneDot(unsigned int color) 00029 { 00030 TFTCS=0; 00031 TFTRD=1; 00032 TFTDC=1; 00033 00034 ST7789V_CTRL_Write(color>>8); 00035 TFTWR=0; 00036 TFTWR=1; 00037 00038 ST7789V_CTRL_Write(color); 00039 TFTWR=0; 00040 TFTWR=1; 00041 00042 TFTCS=1; 00043 } 00044 00045 void ST7789V_Init(void) 00046 { 00047 TFTDC = 0; 00048 TFTRD = 1; 00049 TFTWR = 0; 00050 00051 #if 0 00052 TFTRST = 0; // reset the chip[] 00053 wait_us(100); 00054 TFTRST = 1; // take it out of reset 00055 #endif 00056 00057 wait_us(100); 00058 ST7789V_writeCommand(0x11); /* exit SLEEP mode*/ 00059 wait_us(300); 00060 ST7789V_writeCommand(0x36); 00061 ST7789V_writeData(0x00); /* MADCTL: memory data access control*/ 00062 ST7789V_writeCommand(0x3A); 00063 ST7789V_writeData(0x05); /* COLMOD: Interface Pixel format*/ 00064 ST7789V_writeCommand(0xB2); 00065 ST7789V_writeData(0x0C); 00066 ST7789V_writeData(0x0C); 00067 ST7789V_writeData(0x00); 00068 ST7789V_writeData(0x33); 00069 ST7789V_writeData(0x33); /*PORCTRK: Porch setting*/ 00070 ST7789V_writeCommand(0xB7); 00071 ST7789V_writeData(0x75); /*GCTRL: Gate Control*/ 00072 ST7789V_writeCommand(0xBB); 00073 ST7789V_writeData(0x3d); /*VCOMS: VCOM setting*/ 00074 //ST7789V_writeCommand(0xC0); 00075 //ST7789V_writeData(0x2C); /*LCMCTRL: LCM Control*/ 00076 ST7789V_writeCommand(0xC2); 00077 ST7789V_writeData(0x01); 00078 //ST7789V_writeData(0xFF); /*VDVVRHEN: VDV and VRH Command Enable*/ 00079 ST7789V_writeCommand(0xC3); 00080 ST7789V_writeData(0x19); /*VRHS: VRH Set*/ 00081 ST7789V_writeCommand(0xC4); 00082 ST7789V_writeData(0x20); /*VDVS: VDV Set*/ 00083 ST7789V_writeCommand(0xC6); 00084 ST7789V_writeData(0x0F); /*FRCTRL2: Frame Rate control in normal mode*/ 00085 ST7789V_writeCommand(0xD0); 00086 ST7789V_writeData(0xA4); 00087 ST7789V_writeData(0xA1); /*PWCTRL1: Power Control 1*/ 00088 ST7789V_writeCommand(0xD6); 00089 ST7789V_writeData(0xA1); 00090 ST7789V_writeCommand(0xE0); 00091 ST7789V_writeData(0x70); 00092 ST7789V_writeData(0x04); 00093 ST7789V_writeData(0x08); 00094 ST7789V_writeData(0x09); 00095 ST7789V_writeData(0x09); 00096 ST7789V_writeData(0x05); 00097 ST7789V_writeData(0x2A); 00098 ST7789V_writeData(0x33); 00099 ST7789V_writeData(0x41); 00100 ST7789V_writeData(0x07); 00101 ST7789V_writeData(0x13); 00102 ST7789V_writeData(0x13); 00103 ST7789V_writeData(0x29); 00104 ST7789V_writeData(0x0F); /*PVGAMCTRL: Positive Voltage Gamma control*/ 00105 ST7789V_writeCommand(0xE1); 00106 ST7789V_writeData(0x70); 00107 ST7789V_writeData(0x03); 00108 ST7789V_writeData(0x09); 00109 ST7789V_writeData(0x0A); 00110 ST7789V_writeData(0x09); 00111 ST7789V_writeData(0x06); 00112 ST7789V_writeData(0x2B); 00113 ST7789V_writeData(0x34); 00114 ST7789V_writeData(0x41); 00115 ST7789V_writeData(0x07); 00116 ST7789V_writeData(0x12); 00117 ST7789V_writeData(0x14); 00118 ST7789V_writeData(0x28); 00119 ST7789V_writeData(0x2E); /*NVGAMCTRL: Negative Voltage Gamma control*/ 00120 ST7789V_writeCommand(0x21); 00121 ST7789V_writeCommand(0x29); 00122 ST7789V_writeCommand(0x2A); 00123 ST7789V_writeData(0x00); 00124 ST7789V_writeData(0x00); 00125 ST7789V_writeData(0x00); 00126 ST7789V_writeData(0xEF); /*X address set*/ 00127 ST7789V_writeCommand(0x2B); 00128 ST7789V_writeData(0x00); 00129 ST7789V_writeData(0x00); 00130 ST7789V_writeData(0x00); 00131 ST7789V_writeData(0xEF); /*Y address set*/ 00132 ST7789V_writeCommand(0x2C); 00133 //wait_us(10); 00134 //ST7789V_writeCommand(0x29); /*Enable Display*/ 00135 } 00136 00137 void BlockWrite(unsigned int Xstart,unsigned int Xend,unsigned int Ystart,unsigned int Yend) 00138 { 00139 ST7789V_writeCommand(0x2a); 00140 ST7789V_writeData((Xstart)>>8); 00141 ST7789V_writeData((Xstart)&0xff); 00142 00143 ST7789V_writeData((Xend)>>8); 00144 ST7789V_writeData((Xend)&0xff); 00145 00146 ST7789V_writeCommand(0x2b); 00147 ST7789V_writeData((Ystart+40)>>8); 00148 00149 ST7789V_writeData((Ystart+40)&0xff); 00150 ST7789V_writeData((Yend+40)>>8); 00151 00152 ST7789V_writeData((Yend+40)&0xff); 00153 ST7789V_writeCommand(0x2c); 00154 } 00155 00156 unsigned char ToOrd(unsigned char ch) 00157 { 00158 if(ch<32) 00159 { 00160 ch=95; 00161 } 00162 else if((ch>=32)&&(ch<=47)) //(32~47)空格~/ 00163 { 00164 ch=(ch-32)+10+62; 00165 } 00166 else if((ch>=48)&&(ch<=57))//(48~57)0~9 00167 { 00168 ch=ch-48; 00169 } 00170 else if((ch>=58)&&(ch<=64))//(58~64):~@ 00171 { 00172 ch=(ch-58)+10+62+16; 00173 } 00174 else if((ch>=65)&&(ch<=126))//(65~126)A~~ 00175 { 00176 ch=(ch-65)+10; 00177 } 00178 else if(ch>126) 00179 { 00180 ch=95; 00181 } 00182 00183 return ch; 00184 } 00185 00186 void WriteOneDot(unsigned int color) 00187 { 00188 ST7789V_writeOneDot(color); 00189 } 00190 00191 void DispOneChar(unsigned char ord,unsigned int Xstart,unsigned int Ystart,unsigned int TextColor,unsigned int BackColor) // ord:0~95 00192 { 00193 unsigned char i,j; 00194 unsigned char *p; 00195 unsigned char dat; 00196 unsigned int index; 00197 00198 BlockWrite(Xstart,Xstart+(FONT_W-1),Ystart,Ystart+(FONT_H-1)); 00199 00200 index = ord; 00201 00202 if(index>95) //95:ASCII CHAR NUM 00203 index=95; 00204 00205 index = index*((FONT_W/8)*FONT_H); 00206 00207 p = ascii; 00208 p = p+index; 00209 00210 for(i=0;i<(FONT_W/8*FONT_H);i++) 00211 { 00212 dat=*p++; 00213 for(j=0;j<8;j++) 00214 { 00215 if((dat<<j)&0x80) 00216 { 00217 WriteOneDot(TextColor); 00218 } 00219 else 00220 { 00221 WriteOneDot(BackColor); 00222 } 00223 } 00224 } 00225 } 00226 00227 void DispStr(unsigned char *str,unsigned int Xstart,unsigned int Ystart,unsigned int TextColor,unsigned int BackColor) 00228 { 00229 00230 while(!(*str=='\0')) 00231 { 00232 DispOneChar(ToOrd(*str++),Xstart,Ystart,TextColor,BackColor); 00233 00234 if(Xstart>((COL-1)-FONT_W)) 00235 { 00236 Xstart=0; 00237 Ystart=Ystart+FONT_H; 00238 } 00239 else 00240 { 00241 Xstart=Xstart+FONT_W; 00242 } 00243 00244 if(Ystart>((ROW-1)-FONT_H)) 00245 { 00246 Ystart=0; 00247 } 00248 } 00249 BlockWrite(0,COL-1,0,ROW-1); 00250 } 00251 00252 void DispInt(unsigned int i,unsigned int Xstart,unsigned int Ystart,unsigned int TextColor,unsigned int BackColor) 00253 { 00254 if(Xstart>((COL-1)-FONT_W*4)) 00255 { 00256 Xstart=(COL-1)-FONT_W*4; 00257 } 00258 if(Ystart>((ROW-1)-FONT_H)) 00259 { 00260 Ystart=(Ystart-1)-FONT_H; 00261 } 00262 00263 DispOneChar((i>>12)%16,Xstart,Ystart,TextColor,BackColor); //ID value 00264 DispOneChar((i>>8)%16,Xstart+FONT_W,Ystart,TextColor,BackColor); 00265 DispOneChar((i>>4)%16,Xstart+FONT_W*2,Ystart,TextColor,BackColor); 00266 DispOneChar(i%16,Xstart+FONT_W*3,Ystart,TextColor,BackColor); 00267 00268 BlockWrite(0,COL-1,0,ROW-1); 00269 } 00270 00271 void DispColor(unsigned int color) 00272 { 00273 unsigned int i,j; 00274 00275 BlockWrite(0,COL-1,0,ROW-1); 00276 00277 TFTCS=0; 00278 TFTDC=1; 00279 TFTRD=1; 00280 00281 for(i=0;i<ROW;i++) 00282 { 00283 for(j=0;j<COL;j++) 00284 { 00285 WriteOneDot(color); 00286 } 00287 } 00288 00289 TFTCS=1; 00290 } 00291 00292 void DispBand(void) 00293 { 00294 unsigned int i,j,k; 00295 //unsigned int color[8]={0x001f,0x07e0,0xf800,0x07ff,0xf81f,0xffe0,0x0000,0xffff}; 00296 unsigned int color[8]={0xf800,0xf800,0x07e0,0x07e0,0x001f,0x001f,0xffff,0xffff};//0x94B2 00297 //unsigned int gray16[]={0x0000,0x1082,0x2104,0x3186,0x42,0x08,0x528a,0x630c,0x738e,0x7bcf,0x9492,0xa514,0xb596,0xc618,0xd69a,0xe71c,0xffff}; 00298 00299 BlockWrite(0,COL-1,0,ROW-1); 00300 00301 TFTCS=0; 00302 TFTRD=1; 00303 TFTDC=1; 00304 00305 for(i=0;i<8;i++) 00306 { 00307 for(j=0;j<ROW/8;j++) 00308 { 00309 for(k=0;k<COL;k++) 00310 { 00311 WriteOneDot(color[i]); 00312 } 00313 } 00314 } 00315 for(j=0;j<ROW%8;j++) 00316 { 00317 for(k=0;k<COL;k++) 00318 { 00319 WriteOneDot(color[7]); 00320 } 00321 } 00322 00323 TFTCS=1; 00324 } 00325 00326 void DispPic(unsigned int *picture) 00327 { 00328 unsigned int *p; 00329 unsigned int i,j; //i-row,j-col 00330 unsigned char n,k; //n-row repeat count,k-col repeat count 00331 00332 BlockWrite(0,COL-1,0,ROW-1); 00333 00334 TFTCS =0; 00335 TFTDC =1; 00336 TFTRD =1; 00337 00338 for(n=0;n<ROW/PIC_HEIGHT;n++) //n-row repeat count 00339 { 00340 for(i=0;i<PIC_HEIGHT;i++) 00341 { 00342 p=picture; 00343 for(k=0;k<COL/PIC_WIDTH;k++) //k-col repeat count 00344 { 00345 for(j=0;j<PIC_WIDTH;j++) 00346 { 00347 WriteOneDot(*(p+i*PIC_HEIGHT+j)); 00348 } 00349 } 00350 00351 p=picture; 00352 for(j=0;j<COL%PIC_WIDTH;j++) 00353 { 00354 WriteOneDot(*(p+i*PIC_HEIGHT+j)); 00355 } 00356 } 00357 } 00358 00359 for(i=0;i<ROW%PIC_HEIGHT;i++) 00360 { 00361 p=picture; 00362 for(k=0;k<COL/PIC_WIDTH;k++) //k-col repeat count 00363 { 00364 for(j=0;j<PIC_WIDTH;j++) 00365 { 00366 WriteOneDot(*(p+i*PIC_HEIGHT+j)); 00367 } 00368 } 00369 00370 p=picture; 00371 for(j=0;j<COL%PIC_WIDTH;j++) 00372 { 00373 WriteOneDot(*(p+i*PIC_HEIGHT+j)); 00374 } 00375 } 00376 TFTCS=1; 00377 } 00378 00379 void LCD_block_test(){ 00380 BlockWrite(0,COL-1,0,ROW-1); 00381 DispColor(GREEN); 00382 wait(2); 00383 DispColor(BLUE); 00384 wait(2); 00385 DispColor(RED); 00386 wait(2); 00387 } 00388 00389 00390
Generated on Wed Jul 13 2022 01:09:48 by
1.7.2