>_<:因为要驱动彩屏,所以这里采用STC90C516RD+单片机[51的89C52也行,就是可能内存在接下来的实验中可能会内存不够],晶振尽量采用高一点的,这里采用24MHz的。
>_<:这里的引脚连接为:
- sbit CS=P2^2; //片选
- sbit RES=P2^1; //复位
- sbit RS=P2^4; //数据/命令选择
- sbit RW=P2^5;
- 数据口使用P0
>_<:全部代码:
8x16.h
gb1616.h
1 #include"reg52.h" 2 3 #define WINDOW_XADDR_START 0x0050 // Horizontal Start Address Set 4 #define WINDOW_XADDR_END 0x0051 // Horizontal End Address Set 5 #define WINDOW_YADDR_START 0x0052 // Vertical Start Address Set 6 #define WINDOW_YADDR_END 0x0053 // Vertical End Address Set 7 #define GRAM_XADDR 0x0020 // GRAM Horizontal Address Set 8 #define GRAM_YADDR 0x0021 // GRAM Vertical Address Set 9 #define GRAMWR 0x0022 // memory write 10 11 sbit CS=P2^2; //片选 12 sbit RES=P2^1; //复位 13 sbit RS=P2^4; //数据/命令选择 14 sbit RW=P2^5; 15 //数据口使用P0 16 //====================================================// 17 void main (void); 18 void ILI9325_Initial(void); 19 void show_colour_bar (void); 20 void Write_Cmd_Data(unsigned char x, unsigned int y); 21 void Write_Cmd(unsigned char DH,unsigned char DL); 22 void Write_Data(unsigned char DH,unsigned char DL); 23 void delayms(unsigned int tt); 24 void show_photo(void); 25 void Show_RGB (unsigned int x0,unsigned int x1,unsigned int y0,unsigned int y1,unsigned int Color); 26 unsigned char code pic[]; 27 void Write_Data_U16(unsigned int y); 28 static void LCD_SetPos(unsigned int x0,unsigned int x1,unsigned int y0,unsigned int y1); 29 void ClearScreen(unsigned int bColor); 30 void LCD_PutChar8x16(unsigned short x, unsigned short y, char c, unsigned int fColor, unsigned int bColor); 31 void LCD_PutChar(unsigned short x, unsigned short y, char c, unsigned int fColor, unsigned int bColor); 32 void LCD_PutString(unsigned short x, unsigned short y, char *s, unsigned int fColor, unsigned int bColor); 33 34 void LCD_PutChar8x8(unsigned short x, unsigned short y, char c, unsigned int fColor, unsigned int bColor); 35 void PutGB1616(unsigned short x, unsigned short y, unsigned char c[2], unsigned int fColor,unsigned int bColor); 36 //================================================// 37 unsigned int w,o; 38 void main(void) 39 { 40 CS=1; 41 delayms(5); 42 RES=0; 43 delayms(5); 44 RES=1; 45 delayms(5); 46 ILI9325_Initial(); 47 48 while(1) 49 { 50 /*Show_RGB(0x0000); 51 Show_RGB(0xffff); 52 Show_RGB(0xf800); 53 Show_RGB(0x07e0); 54 Show_RGB(0x001f);*/ 55 56 //LCD_SetPos(48,68,29,69); 57 ClearScreen(0xf800);//用某种颜色刷屏 58 //Show_RGB (0,240,0,320,0xf800); 59 /*Show_RGB (0,120,0,160,0x001f); 60 Show_RGB (0,120,161,320,0xffff); 61 Show_RGB (121,240,161,320,0x07e0); 62 Show_RGB (121,240,0,160,0x0000); 63 */ 64 show_colour_bar();//显示7个横条的函数 65 66 67 //这里仅支持几个汉字,在gb1616里有定义,想更多要找汉字库 68 LCD_PutString(40,140,"1234567890",0x001f,0xf800);//放置string,位置,tring,前景,背景 69 LCD_PutString(0,160,"abcdefghijklmnopqistuvwxyz",0x001f,0xf800); 70 LCD_PutString(0,180,"`,./<>';:[]{}\|?)(-=+*&^%$",0x001f,0xf800); 71 /**/ 72 //show_colour_bar(); 73 //show_photo(); 74 while(1); 75 } 76 } 77 /* 78 用某种颜色刷屏 79 参数:4位16进制数 80 */ 81 void ClearScreen(unsigned int bColor) 82 { 83 unsigned int i,j; 84 LCD_SetPos(0,240,0,320);//定义显示窗体,x0,x1,y0,y1 85 for (i=0;i<320;i++) 86 { 87 for (j=0;j<240;j++) 88 Write_Data_U16(bColor); 89 } 90 } 91 92 ///////////////////////////////////////////////////////////// 93 #include "8X16.h" 94 void LCD_PutChar8x16(unsigned short x, unsigned short y, char c, unsigned int fColor, unsigned int bColor) 95 { 96 unsigned int i,j; 97 LCD_SetPos(x,x+8-1,y,y+16-1); 98 for(i=0; i<16;i++) { 99 unsigned char m=Font8x16[c*16+i]; 100 for(j=0;j<8;j++) { 101 if((m&0x80)==0x80) { 102 Write_Data_U16(fColor); 103 } 104 else { 105 Write_Data_U16(bColor); 106 } 107 m<<=1; 108 } 109 } 110 } 111 void LCD_PutChar(unsigned short x, unsigned short y, char c, unsigned int fColor, unsigned int bColor) { 112 LCD_PutChar8x16( x, y, c, fColor, bColor ); 113 } 114 ///////////////////////////////////////////////////////////// 115 #include "GB1616.h" //16*16汉字字模 116 117 void PutGB1616(unsigned short x, unsigned short y, unsigned char c[2], unsigned int fColor,unsigned int bColor){ 118 unsigned int i,j,k; 119 120 LCD_SetPos(x, x+16-1,y, y+16-1); 121 122 for (k=0;k<64;k++) { //64标示自建汉字库中的个数,循环查询内码 123 if ((codeGB_16[k].Index[0]==c[0])&&(codeGB_16[k].Index[1]==c[1])){ 124 for(i=0;i<32;i++) { 125 unsigned short m=codeGB_16[k].Msk[i]; 126 for(j=0;j<8;j++) { 127 if((m&0x80)==0x80) { 128 Write_Data_U16(fColor); 129 } 130 else { 131 Write_Data_U16(bColor); 132 } 133 m<<=1; 134 } 135 } 136 } 137 } 138 } 139 /* 140 在屏上显示字符串 141 参数:x,y,*s,fColor,bColor 142 实现:遍历字符串,对于每一个字符判断是汉字还是字符,分别用PutGB1616(...)或LCD_PutChar(...)实现显示 143 */ 144 void LCD_PutString(unsigned short x, unsigned short y, unsigned char *s, unsigned int fColor, unsigned int bColor) { 145 unsigned char l=0; 146 while(*s){//遍历字符串 147 if(*s < 0x80)//非汉字 148 { 149 LCD_PutChar(x+l*8,y,*s,fColor,bColor); 150 s++;l++;//++1 151 } 152 else//汉字 153 { 154 PutGB1616(x+l*8,y,(unsigned char*)s,fColor,bColor); 155 s+=2;l+=2;//++2 156 } 157 } 158 } 159 160 //==================== 显示R G B 顔色 ====================// 161 /* 162 在特定矩形内显示颜色 163 参数:x0,x1,y0,y1,16进制颜色 164 */ 165 void Show_RGB (unsigned int x0,unsigned int x1,unsigned int y0,unsigned int y1,unsigned int Color) 166 { 167 unsigned int i,j; 168 //address_set(); 169 LCD_SetPos(x0,x1,y0,y1);//定义显示窗体[矩形] 170 for (i=y0;i<=y1;i++) 171 { 172 for (j=x0;j<=x1;j++) 173 Write_Data_U16(Color); 174 175 } 176 } 177 //====================== 显示彩条 ======================// 178 /* 179 显示7个彩条 180 */ 181 void show_colour_bar (void) 182 { 183 int V,H; 184 LCD_SetPos(0,240,0,320);//320x240 185 186 for(H=0;H<240;H++) 187 for(V=0;V<40;V++) 188 Write_Data(0xf8,0x00); 189 for(H=0;H<240;H++) 190 for(V=40;V<80;V++) 191 Write_Data(0x07,0xe0); 192 for(H=0;H<240;H++) 193 for(V=80;V<120;V++) 194 Write_Data(0x00,0x1f); 195 for(H=0;H<240;H++) 196 for(V=120;V<160;V++) 197 Write_Data(0xff,0xe0); 198 for(H=0;H<240;H++) 199 for(V=160;V<200;V++) 200 Write_Data(0xf8,0x1f); 201 for(H=0;H<240;H++) 202 for(V=200;V<240;V++) 203 Write_Data(0x07,0xff); 204 for(H=0;H<240;H++) 205 for(V=240;V<280;V++) 206 Write_Data(0xff,0xff); 207 for(H=0;H<240;H++) 208 for(V=280;V<320;V++) 209 Write_Data(0x00,0x00); 210 } 211 212 void show_photo(void) 213 { 214 unsigned char j; 215 unsigned int i; 216 unsigned long s=0; 217 218 LCD_SetPos(0,240,0,320);//320x240 219 220 for (i=0;i<75;i++) 221 { 222 for (j=0;j<240;j++) 223 Write_Data(0xff,0xff); 224 225 } 226 227 for (i=0;i<170;i++) 228 { 229 for (j=0;j<55;j++) 230 Write_Data(0xff,0xff); 231 232 for (j=0;j<130;j++) 233 Write_Data(pic[s++],pic[s++]); 234 235 for (j=0;j<55;j++) 236 Write_Data(0xff,0xff); 237 } 238 239 for (i=0;i<75;i++) 240 { 241 for (j=0;j<240;j++) 242 Write_Data(0xff,0xff); 243 244 } 245 246 } 247 248 //=======================================================// 249 void Write_Cmd_Data(unsigned char x,unsigned int y) 250 { 251 unsigned char m,n; 252 m=y>>8; 253 n=y; 254 Write_Cmd(0x00,x); 255 Write_Data(m,n); 256 } 257 /* 258 用4位16进制写数据 259 参数:4位16进制:如:0xf800 260 */ 261 void Write_Data_U16(unsigned int y) 262 { 263 unsigned char m,n; 264 m=y>>8; 265 n=y; 266 Write_Data(m,n);//H,L 267 } 268 /* 269 写命令函数 270 参数:DH,DL 271 功能: 272 */ 273 void Write_Cmd(unsigned char DH,unsigned char DL) 274 { 275 CS=0; 276 RS=0; 277 278 P0=DH; 279 RW=0; 280 RW=1; 281 282 P0=DL; 283 RW=0; 284 RW=1; 285 286 CS=1; 287 } 288 /* 289 写数据函数 290 参数:DH,DL 291 功能: 292 */ 293 void Write_Data(unsigned char DH,unsigned char DL) 294 { 295 CS=0; 296 RS=1; 297 298 P0=DH; 299 RW=0; 300 RW=1; 301 302 P0=DL; 303 RW=0; 304 RW=1; 305 306 CS=1; 307 } 308 //======================================================= 309 void delayms(unsigned int count) 310 { 311 int i,j; 312 for(i=0;i<count;i++) 313 { 314 for(j=0;j<260;j++); 315 } 316 } 317 318 319 320 //===================== 初始化代码 =======================// 321 322 void ILI9325_Initial(void) 323 { 324 delayms(50); 325 Write_Cmd_Data(0x0001,0x0100); 326 Write_Cmd_Data(0x0002,0x0700); 327 Write_Cmd_Data(0x0003,0x1030); 328 Write_Cmd_Data(0x0004,0x0000); 329 Write_Cmd_Data(0x0008,0x0207); 330 Write_Cmd_Data(0x0009,0x0000); 331 Write_Cmd_Data(0x000A,0x0000); 332 Write_Cmd_Data(0x000C,0x0000); 333 Write_Cmd_Data(0x000D,0x0000); 334 Write_Cmd_Data(0x000F,0x0000); 335 //power on sequence VGHVGL 336 Write_Cmd_Data(0x0010,0x0000); 337 Write_Cmd_Data(0x0011,0x0007); 338 Write_Cmd_Data(0x0012,0x0000); 339 Write_Cmd_Data(0x0013,0x0000); 340 //vgh 341 Write_Cmd_Data(0x0010,0x1290); 342 Write_Cmd_Data(0x0011,0x0227); 343 //delayms(100); 344 //vregiout 345 Write_Cmd_Data(0x0012,0x001d); //0x001b 346 //delayms(100); 347 //vom amplitude 348 Write_Cmd_Data(0x0013,0x1500); 349 //delayms(100); 350 //vom H 351 Write_Cmd_Data(0x0029,0x0018); 352 Write_Cmd_Data(0x002B,0x000D); 353 354 //gamma 355 Write_Cmd_Data(0x0030,0x0004); 356 Write_Cmd_Data(0x0031,0x0307); 357 Write_Cmd_Data(0x0032,0x0002);// 0006 358 Write_Cmd_Data(0x0035,0x0206); 359 Write_Cmd_Data(0x0036,0x0408); 360 Write_Cmd_Data(0x0037,0x0507); 361 Write_Cmd_Data(0x0038,0x0204);//0200 362 Write_Cmd_Data(0x0039,0x0707); 363 Write_Cmd_Data(0x003C,0x0405);// 0504 364 Write_Cmd_Data(0x003D,0x0F02); 365 //ram 366 Write_Cmd_Data(0x0050,0x0000); 367 Write_Cmd_Data(0x0051,0x00EF); 368 Write_Cmd_Data(0x0052,0x0000); 369 Write_Cmd_Data(0x0053,0x013F); 370 Write_Cmd_Data(0x0060,0xA700); 371 Write_Cmd_Data(0x0061,0x0001); 372 Write_Cmd_Data(0x006A,0x0000); 373 // 374 Write_Cmd_Data(0x0080,0x0000); 375 Write_Cmd_Data(0x0081,0x0000); 376 Write_Cmd_Data(0x0082,0x0000); 377 Write_Cmd_Data(0x0083,0x0000); 378 Write_Cmd_Data(0x0084,0x0000); 379 Write_Cmd_Data(0x0085,0x0000); 380 // 381 Write_Cmd_Data(0x0090,0x0010); 382 Write_Cmd_Data(0x0092,0x0600); 383 Write_Cmd_Data(0x0093,0x0003); 384 Write_Cmd_Data(0x0095,0x0110); 385 Write_Cmd_Data(0x0097,0x0000); 386 Write_Cmd_Data(0x0098,0x0000); 387 Write_Cmd_Data(0x0007,0x0133); 388 // Write_Cmd_Data(0x0022);// 389 } 390 /*===========================================================*/ 391 392 /************************************************************* 393 函数名称:LCD_DefineDispWindow 394 功 能:定义显示窗体 395 参 数: 396 x0: 窗体中X坐标中较小者 397 x1: 窗体中X坐标中较大者 398 y0: 窗体中Y坐标中较小者 399 y1: 窗体中Y坐标中较大者 400 返 回 值:无 401 *************************************************************/ 402 static void LCD_SetPos(unsigned int x0,unsigned int x1,unsigned int y0,unsigned int y1) 403 { 404 Write_Cmd_Data(WINDOW_XADDR_START,x0); 405 Write_Cmd_Data(WINDOW_XADDR_END,x1); 406 Write_Cmd_Data(WINDOW_YADDR_START,y0); 407 Write_Cmd_Data(WINDOW_YADDR_END,y1); 408 Write_Cmd_Data(GRAM_XADDR,x0); 409 Write_Cmd_Data(GRAM_YADDR,y0); 410 Write_Cmd (0x00,0x22);//LCD_WriteCMD(GRAMWR); 411 }