C51 COMPILER V9.59.0.0 MAIN 06/10/2021 18:14:47 PAGE 1 C51 COMPILER V9.59.0.0, COMPILATION OF MODULE MAIN OBJECT MODULE PLACED IN .\list\main.obj COMPILER INVOKED BY: C:\Keil_v5\C51\BIN\C51.EXE main.c LARGE OMF2 OPTIMIZE(8,SPEED) BROWSE DEBUG PRINT(.\list\main.lst) -TABS(2) OBJECT(.\list\main.obj) line level source 1 /*---------------------------------------------------------------------*/ 2 /* --- STC MCU Limited ------------------------------------------------*/ 3 /* --- STC 1T Series MCU Demo Programme -------------------------------*/ 4 /* --- Mobile: (86)13922805190 ----------------------------------------*/ 5 /* --- Fax: 86-0513-55012956,55012947,55012969 ------------------------*/ 6 /* --- Tel: 86-0513-55012928,55012929,55012966 ------------------------*/ 7 /* --- Web: www.STCMCU.com --------------------------------------------*/ 8 /* --- Web: www.STCMCUDATA.com ---------------------------------------*/ 9 /* --- QQ: 800003751 -------------------------------------------------*/ 10 /* 如果要在程序中使用此代码,请在程序中注明使用了STC的资料及程序 */ 11 /*---------------------------------------------------------------------*/ 12 13 #include "STC8A_BMM.h" 14 #include "STC8A_GPIO.h" 15 #include "STC8A_LCM.h" 16 #include "STC8A_NVIC.h" 17 #include "STC8A_Delay.h" 18 #include "STC8A_Switch.h" 19 #include "font.h" 20 21 /************* 功能说明 *************** 22 23 LCM接口+BMM驱动液晶屏程序 24 25 8bit I8080模式, P2口接D8~D15 26 27 sbit LCD_RS = P4^1; //数据/命令切换 28 sbit LCD_WR = P3^6; //写控制 29 sbit LCD_RD = P3^7; //读控制 30 sbit LCD_CS = P3^4; //片选 31 sbit LCD_RESET = P3^5; //复位 32 33 LCM指令通过中断方式等待发送完成 34 35 BMM设置长度256字节,通过中断方式判断传输完成 36 37 下载时, 选择时钟 22.1184MHz (可以在配置文件"config.h"中修改). 38 39 ******************************************/ 40 41 /************* 本地常量声明 **************/ 42 43 #define LCD_DataPort P2 //8位数据口 44 45 //支持横竖屏快速定义切换 46 #define USE_HORIZONTAL 1 //定义液晶屏显示方向 0-竖屏,1-横屏 47 48 //画笔颜色 49 #define WHITE 0xFFFF 50 #define BLACK 0x0000 51 #define BLUE 0x001F 52 #define BRED 0XF81F 53 #define GRED 0XFFE0 54 #define GBLUE 0X07FF C51 COMPILER V9.59.0.0 MAIN 06/10/2021 18:14:47 PAGE 2 55 #define RED 0xF800 56 #define MAGENTA 0xF81F 57 #define GREEN 0x07E0 58 #define CYAN 0x7FFF 59 #define YELLOW 0xFFE0 60 #define BROWN 0XBC40 //棕色 61 #define BRRED 0XFC07 //棕红色 62 #define GRAY 0X8430 //灰色 63 64 /************* 本地变量声明 **************/ 65 66 u16 LCD_W; //LCD 宽度 67 u16 LCD_H; //LCD 高度 68 69 u16 POINT_COLOR=0x0000; //画笔颜色 70 71 u16 LCM_Cnt; 72 u16 xdata Buffer[8]={0x11,0x22,0x33,0x44,0x55,0x66,0x77,0x88}; 73 u16 xdata Color[256]; 74 bit BmmLcmFlag; 75 bit LcmFlag; 76 77 /************* 本地函数声明 **************/ 78 79 void Test_Color(void); 80 void LCD_WR_DATA_16Bit(u16 Data); 81 void LCD_SetWindows(u16 xStar, u16 yStar,u16 xEnd,u16 yEnd); 82 u16 LCD_Read_ID(void); 83 void Show_Str(u16 x, u16 y, u16 fc, u16 bc, u8 *str,u8 size,u8 mode); 84 void LCD_Init(void); 85 86 /************* 外部函数和变量声明 *****************/ 87 88 89 /******************** IO口配置 ********************/ 90 void GPIO_config(void) 91 { 92 1 P2_MODE_OUT_PP(GPIO_Pin_All); //P2 设置成推挽输出 93 1 P3_MODE_OUT_PP(GPIO_Pin_HIGH); //P3.4~P3.7口设置成推挽输出 94 1 P4_MODE_OUT_PP(GPIO_Pin_1); //P4.1 设置成推挽输出 95 1 96 1 LCM_CTRL_SW(LCM_CTRL_P41_P37_P36); //LCM_CTRL_P41_P44_P43,LCM_CTRL_P41_P37_P36,LCM_CTRL_P41_P42_P40,LCM_C -TRL_P40_P37_P36 97 1 LCM_DATA_SW(LCM_D8_NA_P2); //LCM_D8_NA_P2,LCM_D8_NA_P6 / LCM_D16_P2_P0,LCM_D16_P6_P2,LCM_D16_P2_P7,LCM_D -16_P6_P7 98 1 } 99 100 /******************** LCM 配置 ********************/ 101 void LCM_config(void) 102 { 103 1 LCM_InitTypeDef LCM_InitStructure; //结构定义 104 1 105 1 LCM_InitStructure.LCM_Enable = ENABLE; //LCM接口使能 ENABLE,DISABLE 106 1 LCM_InitStructure.LCM_Mode = MODE_I8080; //LCM接口模式 MODE_I8080,MODE_M6800 107 1 LCM_InitStructure.LCM_Bit_Wide = BIT_WIDE_8; //LCM数据宽度 BIT_WIDE_8,BIT_WIDE_16 108 1 LCM_InitStructure.LCM_Setup_Time = 2; //LCM通信数据建立时间 0~7 109 1 LCM_InitStructure.LCM_Hold_Time = 1; //LCM通信数据保持时间 0~3 110 1 LCM_Inilize(&LCM_InitStructure); //初始化 111 1 NVIC_LCM_Init(ENABLE,Priority_0); //中断使能, ENABLE/DISABLE; 优先级(低到高) Priority_0,Priority_1,Prior -ity_2,Priority_3 112 1 } 113 C51 COMPILER V9.59.0.0 MAIN 06/10/2021 18:14:47 PAGE 3 114 /******************** BMM 配置 ********************/ 115 void BMM_config(void) 116 { 117 1 BMM_LCM_InitTypeDef BMM_LCM_InitStructure; //结构定义 118 1 119 1 BMM_LCM_InitStructure.BMM_Enable = ENABLE; //BMM使能 ENABLE,DISABLE 120 1 BMM_LCM_InitStructure.BMM_Length = 255; //BMM传输总字节数 (0~255) + 1 121 1 BMM_LCM_InitStructure.BMM_Tx_Buffer = &Color; //发送数据存储地址 122 1 BMM_LCM_InitStructure.BMM_Rx_Buffer = &Buffer; //接收数据存储地址 123 1 BMM_LCM_Inilize(&BMM_LCM_InitStructure); //初始化 124 1 NVIC_BMM_LCM_Init(ENABLE,Priority_0,Priority_0); //中断使能, ENABLE/DISABLE; 优先级(低到高) Priority_0~P -riority_3; 总线优先级(低到高) Priority_0~Priority_3 125 1 } 126 127 /******************** task A **************************/ 128 void main(void) 129 { 130 1 GPIO_config(); 131 1 LCM_config(); 132 1 BMM_config(); 133 1 EA = 1; 134 1 LCD_Init(); //LCM初始化 135 1 136 1 while (1) 137 1 { 138 2 Test_Color(); 139 2 } 140 1 } 141 142 void LCD_Fill(u16 sx,u16 sy,u16 ex,u16 ey,u16 color) 143 { 144 1 u16 i,j; 145 1 u16 width=ex-sx+1; //得到填充的宽度 146 1 u16 height=ey-sy+1; //高度 147 1 LCD_SetWindows(sx,sy,ex,ey);//设置显示窗口 148 1 149 1 for(j=0,i=0;i<256;i++) 150 1 { 151 2 Color[i] = color; 152 2 } 153 1 LCM_Cnt = 600; 154 1 LCD_CS=0; 155 1 BMM_LCM_TRIG_WD(); //Write dat 156 1 // P40 = 0; 157 1 } 158 159 void Test_Color(void) 160 { 161 1 u16 lcd_id; 162 1 u8 buf[10] = {0}; 163 1 164 1 // P40 = 0; 165 1 LCD_Fill(0,0,LCD_W,LCD_H,WHITE); 166 1 // P40 = 1; 167 1 while(!LCD_CS); 168 1 169 1 SET_LCM_BMM_LEN(0x01); //Exe 2(n+1) bytes 170 1 lcd_id = LCD_Read_ID(); 171 1 sprintf((char *)buf,"ID:0x%x",lcd_id); 172 1 Show_Str(50,25,BLUE,YELLOW,buf,16,1); 173 1 SET_LCM_BMM_LEN(0xff); //Exe 256(n+1) bytes 174 1 C51 COMPILER V9.59.0.0 MAIN 06/10/2021 18:14:47 PAGE 4 175 1 delay_ms(800); 176 1 // P40 = 0; 177 1 LCD_Fill(0,0,LCD_W,LCD_H,RED); 178 1 // P40 = 1; 179 1 delay_ms(800); 180 1 // P40 = 0; 181 1 LCD_Fill(0,0,LCD_W,LCD_H,GREEN); 182 1 // P40 = 1; 183 1 delay_ms(800); 184 1 // P40 = 0; 185 1 LCD_Fill(0,0,LCD_W,LCD_H,BLUE); 186 1 // P40 = 1; 187 1 delay_ms(800); 188 1 } 189 190 void Write_Cmd(unsigned char DH,unsigned char DL) 191 { 192 1 SET_LCM_DAT_LOW(DH); 193 1 LCD_CS = 0; 194 1 LcmFlag = 1; 195 1 LCM_WRITE_CMD(); //Enable interface, write command out 196 1 while(LcmFlag); 197 1 198 1 SET_LCM_DAT_LOW(DL); 199 1 LcmFlag = 1; 200 1 LCM_WRITE_CMD(); //Enable interface, write command out 201 1 while(LcmFlag); 202 1 LCD_CS = 1 ; 203 1 } 204 205 void Write_Data(unsigned char DH,unsigned char DL) 206 { 207 1 SET_LCM_DAT_LOW(DH); 208 1 LCD_CS = 0; 209 1 LcmFlag = 1; 210 1 LCM_WRITE_DAT(); //Enable interface, write data out 211 1 while(LcmFlag); 212 1 213 1 SET_LCM_DAT_LOW(DL); 214 1 LcmFlag = 1; 215 1 LCM_WRITE_DAT(); //Enable interface, write data out 216 1 while(LcmFlag); 217 1 LCD_CS = 1 ; 218 1 } 219 220 void LCD_WR_REG(u8 Reg) 221 { 222 1 SET_LCM_DAT_LOW(Reg); 223 1 LCD_CS = 0; 224 1 LcmFlag = 1; 225 1 LCM_WRITE_CMD(); //Enable interface, write command out 226 1 while(LcmFlag); 227 1 LCD_CS = 1 ; 228 1 } 229 230 u16 LCD_RD_DATA(void) 231 { 232 1 //BMM Read 233 1 LCD_CS=0; 234 1 BmmLcmFlag = 1; 235 1 BMM_LCM_TRIG_RD(); //Read data 236 1 while(BmmLcmFlag); C51 COMPILER V9.59.0.0 MAIN 06/10/2021 18:14:47 PAGE 5 237 1 LCD_CS=1; 238 1 return (Buffer[0]); 239 1 } 240 241 void LCD_WR_REG16(u8 Reg, u16 dat) 242 { 243 1 Write_Cmd(0x00,Reg); 244 1 Write_Data((dat>>8)&0xFF,dat&0xFF); 245 1 } 246 247 void LCD_WR_DATA_16Bit(u16 Data) 248 { 249 1 Write_Data((Data>>8)&0xFF,Data&0xFF); 250 1 } 251 252 /***************************************************************************** 253 * @name :u16 LCD_Read_ID(void) 254 * @date :2020-12-11 255 * @function :Read ID 256 * @parameters :None 257 * @retvalue :ID value 258 ******************************************************************************/ 259 u16 LCD_Read_ID(void) 260 { 261 1 u16 val; 262 1 263 1 LCD_WR_REG(0x00); //ID: Status Read (RS) 264 1 val = LCD_RD_DATA(); 265 1 return (val); 266 1 } 267 268 /***************************************************************************** 269 * @name :void LCDReset(void) 270 * @date :2018-08-09 271 * @function :Reset LCD screen 272 * @parameters :None 273 * @retvalue :None 274 ******************************************************************************/ 275 void LCDReset(void) 276 { 277 1 LCD_CS=1; 278 1 delay_ms(50); 279 1 LCD_RESET=0; 280 1 delay_ms(150); 281 1 LCD_RESET=1; 282 1 delay_ms(50); 283 1 } 284 285 /***************************************************************************** 286 * @name :void LCD_Init(void) 287 * @date :2018-08-09 288 * @function :Initialization LCD screen 289 * @parameters :None 290 * @retvalue :None 291 ******************************************************************************/ 292 void LCD_Init(void) 293 { 294 1 LCDReset(); //初始化之前复位 295 1 delay_ms(150); //根据不同晶振速度可以调整延时,保障稳定显示 296 1 //*************2.4inch ILI9325初始化**********// 297 1 LCD_WR_REG16(0x00E5,0x78F0); 298 1 LCD_WR_REG16(0x0001,0x0100); C51 COMPILER V9.59.0.0 MAIN 06/10/2021 18:14:47 PAGE 6 299 1 LCD_WR_REG16(0x0002,0x0700); 300 1 LCD_WR_REG16(0x0003,0x1030); 301 1 LCD_WR_REG16(0x0004,0x0000); 302 1 LCD_WR_REG16(0x0008,0x0202); 303 1 LCD_WR_REG16(0x0009,0x0000); 304 1 LCD_WR_REG16(0x000A,0x0000); 305 1 LCD_WR_REG16(0x000C,0x0000); 306 1 LCD_WR_REG16(0x000D,0x0000); 307 1 LCD_WR_REG16(0x000F,0x0000); 308 1 //power on sequence VGHVGL 309 1 LCD_WR_REG16(0x0010,0x0000); 310 1 LCD_WR_REG16(0x0011,0x0007); 311 1 LCD_WR_REG16(0x0012,0x0000); 312 1 LCD_WR_REG16(0x0013,0x0000); 313 1 LCD_WR_REG16(0x0007,0x0000); 314 1 //vgh 315 1 LCD_WR_REG16(0x0010,0x1690); 316 1 LCD_WR_REG16(0x0011,0x0227); 317 1 delay_ms(10); 318 1 //vregiout 319 1 LCD_WR_REG16(0x0012,0x009D); //0x001b 320 1 delay_ms(10); 321 1 //vom amplitude 322 1 LCD_WR_REG16(0x0013,0x1900); 323 1 delay_ms(10); 324 1 //vom H 325 1 LCD_WR_REG16(0x0029,0x0025); 326 1 LCD_WR_REG16(0x002B,0x000D); 327 1 //gamma 328 1 LCD_WR_REG16(0x0030,0x0007); 329 1 LCD_WR_REG16(0x0031,0x0303); 330 1 LCD_WR_REG16(0x0032,0x0003); //0006 331 1 LCD_WR_REG16(0x0035,0x0206); 332 1 LCD_WR_REG16(0x0036,0x0008); 333 1 LCD_WR_REG16(0x0037,0x0406); 334 1 LCD_WR_REG16(0x0038,0x0304); //0200 335 1 LCD_WR_REG16(0x0039,0x0007); 336 1 LCD_WR_REG16(0x003C,0x0602); //0504 337 1 LCD_WR_REG16(0x003D,0x0008); 338 1 //ram 339 1 LCD_WR_REG16(0x0050,0x0000); 340 1 LCD_WR_REG16(0x0051,0x00EF); 341 1 LCD_WR_REG16(0x0052,0x0000); 342 1 LCD_WR_REG16(0x0053,0x013F); 343 1 LCD_WR_REG16(0x0060,0xA700); 344 1 LCD_WR_REG16(0x0061,0x0001); 345 1 LCD_WR_REG16(0x006A,0x0000); 346 1 // 347 1 LCD_WR_REG16(0x0080,0x0000); 348 1 LCD_WR_REG16(0x0081,0x0000); 349 1 LCD_WR_REG16(0x0082,0x0000); 350 1 LCD_WR_REG16(0x0083,0x0000); 351 1 LCD_WR_REG16(0x0084,0x0000); 352 1 LCD_WR_REG16(0x0085,0x0000); 353 1 // 354 1 LCD_WR_REG16(0x0090,0x0010); 355 1 LCD_WR_REG16(0x0092,0x0600); 356 1 357 1 LCD_WR_REG16(0x0007,0x0133); 358 1 LCD_WR_REG16(0x0000,0x0022); 359 1 360 1 //设置LCD属性参数 C51 COMPILER V9.59.0.0 MAIN 06/10/2021 18:14:47 PAGE 7 361 1 #if USE_HORIZONTAL==1 362 1 LCD_W = 320; 363 1 LCD_H = 240; 364 1 LCD_WR_REG16(0x0003, 0x1038); // set GRAM write direction and BGR=1. 365 1 LCD_WR_REG16(0x0001, 0x0000); // set SS and SM bit 366 1 #else LCD_W = 240; LCD_H = 320; LCD_WR_REG16(0x0003, 0x1030); // set GRAM write direction and BGR=1. LCD_WR_REG16(0x0001, 0x0100); // set SS and SM bit #endif 372 1 } 373 374 #define WINDOW_XADDR_START 0x0050 // Horizontal Start Address Set 375 #define WINDOW_XADDR_END 0x0051 // Horizontal End Address Set 376 #define WINDOW_YADDR_START 0x0052 // Vertical Start Address Set 377 #define WINDOW_YADDR_END 0x0053 // Vertical End Address Set 378 #define GRAM_XADDR 0x0020 // GRAM Horizontal Address Set 379 #define GRAM_YADDR 0x0021 // GRAM Vertical Address Set 380 #define GRAMWR 0x0022 // memory write 381 382 void LCD_SetWindows(u16 xStar0, u16 yStar0,u16 xEnd0,u16 yEnd0) 383 { 384 1 u16 xStar; u16 yStar;u16 xEnd;u16 yEnd; 385 1 #if USE_HORIZONTAL==1 386 1 xStar = yStar0; 387 1 xEnd = yEnd0; 388 1 yStar = xStar0; 389 1 yEnd = xEnd0; 390 1 #else xStar = xStar0; xEnd = xEnd0; yStar = yStar0; yEnd = yEnd0; #endif 396 1 397 1 LCD_WR_REG16(WINDOW_XADDR_START,xStar); 398 1 LCD_WR_REG16(WINDOW_XADDR_END,xEnd); 399 1 LCD_WR_REG16(WINDOW_YADDR_START,yStar); 400 1 LCD_WR_REG16(WINDOW_YADDR_END,yEnd); 401 1 LCD_WR_REG16(GRAM_XADDR,xStar); 402 1 LCD_WR_REG16(GRAM_YADDR,yStar); 403 1 LCD_WR_REG(GRAMWR); 404 1 } 405 406 /***************************************************************************** 407 * @name :void LCD_DrawPoint(u16 x,u16 y) 408 * @date :2018-08-09 409 * @function :Write a pixel data at a specified location 410 * @parameters :x:the x coordinate of the pixel 411 y:the y coordinate of the pixel 412 * @retvalue :None 413 ******************************************************************************/ 414 void LCD_DrawPoint(u16 x,u16 y) 415 { 416 1 LCD_SetWindows(x,y,x,y);//设置光标位置 417 1 LCD_WR_DATA_16Bit(POINT_COLOR); 418 1 } 419 420 /***************************************************************************** 421 * @name :void LCD_ShowChar(u16 x,u16 y,u16 fc, u16 bc, u8 num,u8 size,u8 mode) 422 * @date :2018-08-09 C51 COMPILER V9.59.0.0 MAIN 06/10/2021 18:14:47 PAGE 8 423 * @function :Display a single English character 424 * @parameters :x:the beginning x coordinate of the Character display position 425 y:the beginning y coordinate of the Character display position 426 fc:the color value of display character 427 bc:the background color of display character 428 num:the ascii code of display character(0~94) 429 size:the size of display character 430 mode:0-no overlying,1-overlying 431 * @retvalue :None 432 ******************************************************************************/ 433 void LCD_ShowChar(u16 x,u16 y,u16 fc, u16 bc, u8 num,u8 size,u8 mode) 434 { 435 1 u8 temp; 436 1 u8 pos,t; 437 1 u16 colortemp=POINT_COLOR; 438 1 439 1 num=num-' ';//得到偏移后的值 440 1 LCD_SetWindows(x,y,x+size/2-1,y+size-1);//设置单个文字显示窗口 441 1 if(!mode) //非叠加方式 442 1 { 443 2 for(pos=0;pos>=1; 452 4 } 453 3 } 454 2 } 455 1 else//叠加方式 456 1 { 457 2 for(pos=0;pos>=1; 466 4 } 467 3 } 468 2 } 469 1 POINT_COLOR=colortemp; 470 1 LCD_SetWindows(0,0,LCD_W-1,LCD_H-1);//恢复窗口为全屏 471 1 } 472 473 /***************************************************************************** 474 * @name :void Show_Str(u16 x, u16 y, u16 fc, u16 bc, u8 *str,u8 size,u8 mode) 475 * @date :2018-08-09 476 * @function :Display Chinese and English strings 477 * @parameters :x:the beginning x coordinate of the Chinese and English strings 478 y:the beginning y coordinate of the Chinese and English strings 479 fc:the color value of Chinese and English strings 480 bc:the background color of Chinese and English strings 481 str:the start address of the Chinese and English strings 482 size:the size of Chinese and English strings 483 mode:0-no overlying,1-overlying 484 * @retvalue :None C51 COMPILER V9.59.0.0 MAIN 06/10/2021 18:14:47 PAGE 9 485 ******************************************************************************/ 486 void Show_Str(u16 x, u16 y, u16 fc, u16 bc, u8 *str,u8 size,u8 mode) 487 { 488 1 u16 x0=x; 489 1 u8 bHz=0; //字符或者中文 490 1 while(*str!=0)//数据未结束 491 1 { 492 2 if(!bHz) 493 2 { 494 3 if(x>(LCD_W-size/2)||y>(LCD_H-size)) 495 3 return; 496 3 if(*str>0x80) bHz=1;//中文 497 3 else //字符 498 3 { 499 4 if(*str==0x0D)//换行符号 500 4 { 501 5 y+=size; 502 5 x=x0; 503 5 str++; 504 5 } 505 4 else 506 4 { 507 5 if(size>16)//字库中没有集成12X24 16X32的英文字体,用8X16代替 508 5 { 509 6 LCD_ShowChar(x,y,fc,bc,*str,16,mode); 510 6 x+=8; //字符,为全字的一半 511 6 } 512 5 else 513 5 { 514 6 LCD_ShowChar(x,y,fc,bc,*str,size,mode); 515 6 x+=size/2; //字符,为全字的一半 516 6 } 517 5 } 518 4 str++; 519 4 } 520 3 } 521 2 else//中文 522 2 { 523 3 // if(x>(lcddev.width-size)||y>(lcddev.height-size)) 524 3 // return; 525 3 // bHz=0;//有汉字库 526 3 // if(size==32) 527 3 // GUI_DrawFont32(x,y,fc,bc,str,mode); 528 3 // else if(size==24) 529 3 // GUI_DrawFont24(x,y,fc,bc,str,mode); 530 3 // else 531 3 // GUI_DrawFont16(x,y,fc,bc,str,mode); 532 3 533 3 str+=2; 534 3 x+=size;//下一个汉字偏移 535 3 } 536 2 } 537 1 } MODULE INFORMATION: STATIC OVERLAYABLE CODE SIZE = 2442 ---- CONSTANT SIZE = 2678 ---- XDATA SIZE = 536 85 PDATA SIZE = ---- ---- DATA SIZE = ---- ---- IDATA SIZE = ---- ---- C51 COMPILER V9.59.0.0 MAIN 06/10/2021 18:14:47 PAGE 10 BIT SIZE = 2 ---- EDATA SIZE = ---- ---- HDATA SIZE = ---- ---- XDATA CONST SIZE = ---- ---- FAR CONST SIZE = ---- ---- END OF MODULE INFORMATION. C51 COMPILATION COMPLETE. 0 WARNING(S), 0 ERROR(S)