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