C51 COMPILER V9.01   LCD                                                                   07/18/2014 12:11:01 PAGE 1   


C51 COMPILER V9.01, COMPILATION OF MODULE LCD
OBJECT MODULE PLACED IN LCD.obj
COMPILER INVOKED BY: C:\Keil\C51\BIN\C51.EXE LCD\LCD.c LARGE BROWSE DEBUG OBJECTEXTEND PRINT(.\LCD.lst) OBJECT(LCD.obj)

line level    source

   1          #include <reg51.h>
   2          #include <intrins.h>
   3          #include <font\font.h>
   4          #include <sys\sys.h>
   5          #include <lcd\lcd.h>
   6          /* 
   7          代码测试环境:单片机STC12LE5A60S2,晶振33M  单片机工作电压3.3V
   8          程序默认IO连接方式：
   9          sbit LCD_CS     =P0^0;     //片选       
  10          sbit LCD_DC     =P0^2;    //数据/命令切换
  11          sbit LCD_SDI    =P1^5;    //数据
  12          sbit LCD_SCK    =P1^7;    //时钟
  13          sbit LCD_REST   =P0^1;    //复位  
  14          触摸功能连接方式：(不使用触摸可不连接)
  15          T_CLK-P1^7;  T_CS-P1^4;  T_DIN-P3^0;  T_OUT-P3^1;  T_IRQ-P3^4;
  16          */      
  17          u16 BACK_COLOR, POINT_COLOR;   //背景色，画笔色
  18          unsigned char bdata bitdata;
  19          sbit bit7=bitdata^7;
  20          sbit bit6=bitdata^6;
  21          sbit bit5=bitdata^5;
  22          sbit bit4=bitdata^4;
  23          sbit bit3=bitdata^3;
  24          sbit bit2=bitdata^2;
  25          sbit bit1=bitdata^1;
  26          sbit bit0=bitdata^0;
  27          void LCD_Writ_Bus(char da)   //串行数据写入
  28          {       
  29   1              bitdata=da;
  30   1              LCD_SDI=bit7;LCD_SCK=0;LCD_SCK=1;
  31   1              LCD_SDI=bit6;LCD_SCK=0;LCD_SCK=1;
  32   1              LCD_SDI=bit5;LCD_SCK=0;LCD_SCK=1;
  33   1              LCD_SDI=bit4;LCD_SCK=0;LCD_SCK=1;
  34   1              LCD_SDI=bit3;LCD_SCK=0;LCD_SCK=1;
  35   1              LCD_SDI=bit2;LCD_SCK=0;LCD_SCK=1;
  36   1              LCD_SDI=bit1;LCD_SCK=0;LCD_SCK=1;
  37   1              LCD_SDI=bit0;LCD_SCK=0;LCD_SCK=1; 
  38   1      } 
  39          void LCD_WR_DATA8(char da) //发送数据-8位参数
  40          {
  41   1          LCD_DC=1;
  42   1              LCD_Writ_Bus(da);  
  43   1      }  
  44           void LCD_WR_DATA(int da)
  45          {
  46   1          LCD_DC=1;
  47   1              LCD_Writ_Bus(da>>8);
  48   1          LCD_Writ_Bus(da);
  49   1      }         
  50          void LCD_WR_REG(char da)         
  51          {       
  52   1          LCD_DC=0;
  53   1              LCD_Writ_Bus(da);
  54   1      }
  55           void LCD_WR_REG_DATA(int reg,int da)
C51 COMPILER V9.01   LCD                                                                   07/18/2014 12:11:01 PAGE 2   

  56          {
  57   1          LCD_WR_REG(reg);
  58   1              LCD_WR_DATA(da);
  59   1      }
  60          void Address_set(unsigned int x1,unsigned int y1,unsigned int x2,unsigned int y2)
  61          { 
  62   1              LCD_WR_REG(0x2a);
  63   1         LCD_WR_DATA8(x1>>8);
  64   1         LCD_WR_DATA8(x1);
  65   1         LCD_WR_DATA8(x2>>8);
  66   1         LCD_WR_DATA8(x2);
  67   1        
  68   1         LCD_WR_REG(0x2b);
  69   1         LCD_WR_DATA8(y1>>8);
  70   1         LCD_WR_DATA8(y1);
  71   1         LCD_WR_DATA8(y2>>8);
  72   1         LCD_WR_DATA8(y2);
  73   1      
  74   1         LCD_WR_REG(0x2C);                                                                                     
  75   1      }
  76          void Lcd_Init(void)
  77          {
  78   1      
  79   1      //调用一次这些函数，免得编译的时候提示警告
  80   1              LCD_CS =1;
  81   1              if(LCD_CS==0)
  82   1              {
  83   2                 LCD_WR_REG_DATA(0,0);
  84   2                 LCD_ShowString(0,0," ");
  85   2                 LCD_ShowNum(0,0,0,0);
  86   2                 LCD_Show2Num(0,0,0,0);
  87   2                 LCD_DrawPoint_big(0,0);
  88   2                 LCD_DrawRectangle(0,0,0,0);
  89   2                 Draw_Circle(0,0,0);
  90   2               }
  91   1              LCD_REST=0;
  92   1              delayms(20);
  93   1              LCD_REST=1;
  94   1              delayms(20);
  95   1              LCD_CS =0;  //打开片选使能
  96   1      
  97   1              LCD_WR_REG(0xCB);  
  98   1          LCD_WR_DATA8(0x39); 
  99   1          LCD_WR_DATA8(0x2C); 
 100   1          LCD_WR_DATA8(0x00); 
 101   1          LCD_WR_DATA8(0x34); 
 102   1          LCD_WR_DATA8(0x02); 
 103   1      
 104   1          LCD_WR_REG(0xCF);  
 105   1          LCD_WR_DATA8(0x00); 
 106   1          LCD_WR_DATA8(0XC1); 
 107   1          LCD_WR_DATA8(0X30); 
 108   1              
 109   1              LCD_WR_REG(0xE8);  
 110   1          LCD_WR_DATA8(0x85); 
 111   1          LCD_WR_DATA8(0x00); 
 112   1          LCD_WR_DATA8(0x78); 
 113   1              
 114   1              LCD_WR_REG(0xEA);  
 115   1          LCD_WR_DATA8(0x00); 
 116   1          LCD_WR_DATA8(0x00); 
 117   1              
C51 COMPILER V9.01   LCD                                                                   07/18/2014 12:11:01 PAGE 3   

 118   1              LCD_WR_REG(0xED);  
 119   1          LCD_WR_DATA8(0x64); 
 120   1          LCD_WR_DATA8(0x03); 
 121   1          LCD_WR_DATA8(0X12); 
 122   1          LCD_WR_DATA8(0X81); 
 123   1      
 124   1          LCD_WR_REG(0xF7);  
 125   1          LCD_WR_DATA8(0x20); 
 126   1         
 127   1          LCD_WR_REG(0xC0);    //Power control 
 128   1          LCD_WR_DATA8(0x23);   //VRH[5:0] 
 129   1         
 130   1          LCD_WR_REG(0xC1);    //Power control 
 131   1          LCD_WR_DATA8(0x10);   //SAP[2:0];BT[3:0] 
 132   1       
 133   1          LCD_WR_REG(0xC5);    //VCM control 
 134   1          LCD_WR_DATA8(0x3e); //对比度调节
 135   1          LCD_WR_DATA8(0x28); 
 136   1       
 137   1          LCD_WR_REG(0xC7);    //VCM control2 
 138   1          LCD_WR_DATA8(0x86);  //--
 139   1       
 140   1          LCD_WR_REG(0x36);    // Memory Access Control 
 141   1          LCD_WR_DATA8(0x48); //         //48 68竖屏//28 E8 横屏
 142   1      
 143   1          LCD_WR_REG(0x3A);    
 144   1          LCD_WR_DATA8(0x55); 
 145   1      
 146   1          LCD_WR_REG(0xB1);    
 147   1          LCD_WR_DATA8(0x00);  
 148   1          LCD_WR_DATA8(0x18); 
 149   1       
 150   1          LCD_WR_REG(0xB6);    // Display Function Control 
 151   1          LCD_WR_DATA8(0x08); 
 152   1          LCD_WR_DATA8(0x82);
 153   1          LCD_WR_DATA8(0x27);  
 154   1              
 155   1              LCD_WR_REG(0xF2);    // 3Gamma Function Disable 
 156   1          LCD_WR_DATA8(0x00); 
 157   1              
 158   1              LCD_WR_REG(0x26);    //Gamma curve selected 
 159   1          LCD_WR_DATA8(0x01); 
 160   1       
 161   1              LCD_WR_REG(0xE0);    //Set Gamma 
 162   1          LCD_WR_DATA8(0x0F); 
 163   1          LCD_WR_DATA8(0x31); 
 164   1          LCD_WR_DATA8(0x2B); 
 165   1          LCD_WR_DATA8(0x0C); 
 166   1          LCD_WR_DATA8(0x0E); 
 167   1          LCD_WR_DATA8(0x08); 
 168   1          LCD_WR_DATA8(0x4E); 
 169   1          LCD_WR_DATA8(0xF1); 
 170   1          LCD_WR_DATA8(0x37); 
 171   1          LCD_WR_DATA8(0x07); 
 172   1          LCD_WR_DATA8(0x10); 
 173   1          LCD_WR_DATA8(0x03); 
 174   1          LCD_WR_DATA8(0x0E); 
 175   1          LCD_WR_DATA8(0x09); 
 176   1          LCD_WR_DATA8(0x00); 
 177   1      
 178   1          LCD_WR_REG(0XE1);    //Set Gamma 
 179   1          LCD_WR_DATA8(0x00); 
C51 COMPILER V9.01   LCD                                                                   07/18/2014 12:11:01 PAGE 4   

 180   1          LCD_WR_DATA8(0x0E); 
 181   1          LCD_WR_DATA8(0x14); 
 182   1          LCD_WR_DATA8(0x03); 
 183   1          LCD_WR_DATA8(0x11); 
 184   1          LCD_WR_DATA8(0x07); 
 185   1          LCD_WR_DATA8(0x31); 
 186   1          LCD_WR_DATA8(0xC1); 
 187   1          LCD_WR_DATA8(0x48); 
 188   1          LCD_WR_DATA8(0x08); 
 189   1          LCD_WR_DATA8(0x0F); 
 190   1          LCD_WR_DATA8(0x0C); 
 191   1          LCD_WR_DATA8(0x31); 
 192   1          LCD_WR_DATA8(0x36); 
 193   1          LCD_WR_DATA8(0x0F); 
 194   1      
 195   1          LCD_WR_REG(0x11);    //Exit Sleep 
 196   1          delayms(120); 
 197   1                              
 198   1           LCD_WR_REG(0x29);    //Display on 
 199   1           LCD_WR_REG(0x2c); 
 200   1      
 201   1      }
 202          //清屏函数
 203          //Color:要清屏的填充色
 204          void LCD_Clear(u16 Color)
 205          {
 206   1              u16 i,j;        
 207   1              Address_set(0,0,LCD_W-1,LCD_H-1);
 208   1          for(i=0;i<LCD_W;i++)
 209   1               {
 210   2                for (j=0;j<LCD_H;j++)
 211   2                      {
 212   3                      LCD_WR_DATA(Color);                              
 213   3                  }
 214   2      
 215   2                }
 216   1      }
 217          //在指定位置显示一个汉字(32*33大小)
 218          //dcolor为内容颜色，gbcolor为背静颜色
 219          void showhanzi(unsigned int x,unsigned int y,unsigned char index)       
 220          {  
 221   1              unsigned char i,j;
 222   1              unsigned char *temp=hanzi;    
 223   1          Address_set(x,y,x+31,y+31); //设置区域      
 224   1              temp+=index*128;        
 225   1              for(j=0;j<128;j++)
 226   1              {
 227   2                      for(i=0;i<8;i++)
 228   2                      {                    
 229   3                              if((*temp&(1<<i))!=0)
 230   3                              {
 231   4                                      LCD_WR_DATA(POINT_COLOR);
 232   4                              } 
 233   3                              else
 234   3                              {
 235   4                                      LCD_WR_DATA(BACK_COLOR);
 236   4                              }   
 237   3                      }
 238   2                      temp++;
 239   2               }
 240   1      }
 241          //画点
C51 COMPILER V9.01   LCD                                                                   07/18/2014 12:11:01 PAGE 5   

 242          //POINT_COLOR:此点的颜色
 243          void LCD_DrawPoint(u16 x,u16 y)
 244          {
 245   1              Address_set(x,y,x,y);//设置光标位置 
 246   1              LCD_WR_DATA(POINT_COLOR);           
 247   1      }        
 248          //画一个大点
 249          //POINT_COLOR:此点的颜色
 250          void LCD_DrawPoint_big(u16 x,u16 y)
 251          {
 252   1              LCD_Fill(x-1,y-1,x+1,y+1,POINT_COLOR);
 253   1      } 
 254          //在指定区域内填充指定颜色
 255          //区域大小:
 256          //  (xend-xsta)*(yend-ysta)
 257          void LCD_Fill(u16 xsta,u16 ysta,u16 xend,u16 yend,u16 color)
 258          {          
 259   1              u16 i,j; 
 260   1              Address_set(xsta,ysta,xend,yend);      //设置光标位置 
 261   1              for(i=ysta;i<=yend;i++)
 262   1              {                                                                                                                       
 263   2                      for(j=xsta;j<=xend;j++)LCD_WR_DATA(color);//设置光标位置            
 264   2              }                                                   
 265   1      }  
 266          //画线
 267          //x1,y1:起点坐标
 268          //x2,y2:终点坐标  
 269          void LCD_DrawLine(u16 x1, u16 y1, u16 x2, u16 y2)
 270          {
 271   1              u16 t; 
 272   1              int xerr=0,yerr=0,delta_x,delta_y,distance; 
 273   1              int incx,incy,uRow,uCol; 
 274   1      
 275   1              delta_x=x2-x1; //计算坐标增量 
 276   1              delta_y=y2-y1; 
 277   1              uRow=x1; 
 278   1              uCol=y1; 
 279   1              if(delta_x>0)incx=1; //设置单步方向 
 280   1              else if(delta_x==0)incx=0;//垂直线 
 281   1              else {incx=-1;delta_x=-delta_x;} 
 282   1              if(delta_y>0)incy=1; 
 283   1              else if(delta_y==0)incy=0;//水平线 
 284   1              else{incy=-1;delta_y=-delta_y;} 
 285   1              if( delta_x>delta_y)distance=delta_x; //选取基本增量坐标轴 
 286   1              else distance=delta_y; 
 287   1              for(t=0;t<=distance+1;t++ )//画线输出 
 288   1              {  
 289   2                      LCD_DrawPoint(uRow,uCol);//画点 
 290   2                      xerr+=delta_x ; 
 291   2                      yerr+=delta_y ; 
 292   2                      if(xerr>distance) 
 293   2                      { 
 294   3                              xerr-=distance; 
 295   3                              uRow+=incx; 
 296   3                      } 
 297   2                      if(yerr>distance) 
 298   2                      { 
 299   3                              yerr-=distance; 
 300   3                              uCol+=incy; 
 301   3                      } 
 302   2              }  
 303   1      }    
C51 COMPILER V9.01   LCD                                                                   07/18/2014 12:11:01 PAGE 6   

 304          //画矩形
 305          void LCD_DrawRectangle(u16 x1, u16 y1, u16 x2, u16 y2)
 306          {
 307   1              LCD_DrawLine(x1,y1,x2,y1);
 308   1              LCD_DrawLine(x1,y1,x1,y2);
 309   1              LCD_DrawLine(x1,y2,x2,y2);
 310   1              LCD_DrawLine(x2,y1,x2,y2);
 311   1      }
 312          //在指定位置画一个指定大小的圆
 313          //(x,y):中心点
 314          //r    :半径
 315          void Draw_Circle(u16 x0,u16 y0,u8 r)
 316          {
 317   1              int a,b;
 318   1              int di;
 319   1              a=0;b=r;          
 320   1              di=3-(r<<1);             //判断下个点位置的标志
 321   1              while(a<=b)
 322   1              {
 323   2                      LCD_DrawPoint(x0-b,y0-a);             //3           
 324   2                      LCD_DrawPoint(x0+b,y0-a);             //0           
 325   2                      LCD_DrawPoint(x0-a,y0+b);             //1       
 326   2                      LCD_DrawPoint(x0-b,y0-a);             //7           
 327   2                      LCD_DrawPoint(x0-a,y0-b);             //2             
 328   2                      LCD_DrawPoint(x0+b,y0+a);             //4               
 329   2                      LCD_DrawPoint(x0+a,y0-b);             //5
 330   2                      LCD_DrawPoint(x0+a,y0+b);             //6 
 331   2                      LCD_DrawPoint(x0-b,y0+a);             
 332   2                      a++;
 333   2                      //使用Bresenham算法画圆     
 334   2                      if(di<0)di +=4*a+6;       
 335   2                      else
 336   2                      {
 337   3                              di+=10+4*(a-b);   
 338   3                              b--;
 339   3                      } 
 340   2                      LCD_DrawPoint(x0+a,y0+b);
 341   2              }
 342   1      } 
 343          //在指定位置显示一个字符
 344          
 345          //num:要显示的字符:" "--->"~"
 346          //mode:叠加方式(1)还是非叠加方式(0)
 347          //在指定位置显示一个字符
 348          
 349          //num:要显示的字符:" "--->"~"
 350          
 351          //mode:叠加方式(1)还是非叠加方式(0)
 352          void LCD_ShowChar(u16 x,u16 y,u8 num,u8 mode)
 353          {
 354   1          u8 temp;
 355   1          u8 pos,t;
 356   1              u16 x0=x;
 357   1              u16 colortemp=POINT_COLOR;      
 358   1          if(x>LCD_W-16||y>LCD_H-16)return;       
 359   1              //设置窗口                 
 360   1              num=num-' ';//得到偏移后的值
 361   1              Address_set(x,y,x+8-1,y+16-1);      //设置光标位置 
 362   1              if(!mode) //非叠加方式
 363   1              {
 364   2                      for(pos=0;pos<16;pos++)
 365   2                      { 
C51 COMPILER V9.01   LCD                                                                   07/18/2014 12:11:01 PAGE 7   

 366   3                              temp=asc2_1608[(u16)num*16+pos];                 //调用1608字体
 367   3                              for(t=0;t<8;t++)
 368   3                          {                 
 369   4                              if(temp&0x01)POINT_COLOR=colortemp;
 370   4                                      else POINT_COLOR=BACK_COLOR;
 371   4                                      LCD_WR_DATA(POINT_COLOR);       
 372   4                                      temp>>=1; 
 373   4                                      x++;
 374   4                          }
 375   3                              x=x0;
 376   3                              y++;
 377   3                      }       
 378   2              }else//叠加方式
 379   1              {
 380   2                      for(pos=0;pos<16;pos++)
 381   2                      {
 382   3                          temp=asc2_1608[(u16)num*16+pos];             //调用1608字体
 383   3                              for(t=0;t<8;t++)
 384   3                          {                 
 385   4                              if(temp&0x01)LCD_DrawPoint(x+t,y+pos);//画一个点     
 386   4                              temp>>=1; 
 387   4                          }
 388   3                      }
 389   2              }
 390   1              POINT_COLOR=colortemp;                            
 391   1      }   
 392          //m^n函数
 393          u32 mypow(u8 m,u8 n)
 394          {
 395   1              u32 result=1;    
 396   1              while(n--)result*=m;    
 397   1              return result;
 398   1      }                        
 399          //显示2个数字
 400          //x,y :起点坐标  
 401          //len :数字的位数
 402          //color:颜色
 403          //num:数值(0~4294967295);       
 404          void LCD_ShowNum(u16 x,u16 y,u32 num,u8 len)
 405          {               
 406   1              u8 t,temp;
 407   1              u8 enshow=0;
 408   1              num=(u16)num;
 409   1              for(t=0;t<len;t++)
 410   1              {
 411   2                      temp=(num/mypow(10,len-t-1))%10;
 412   2                      if(enshow==0&&t<(len-1))
 413   2                      {
 414   3                              if(temp==0)
 415   3                              {
 416   4                                      LCD_ShowChar(x+8*t,y,' ',0);
 417   4                                      continue;
 418   4                              }else enshow=1; 
 419   3                               
 420   3                      }
 421   2                      LCD_ShowChar(x+8*t,y,temp+48,0); 
 422   2              }
 423   1      } 
 424          //显示2个数字
 425          //x,y:起点坐标
 426          //num:数值(0~99);        
 427          void LCD_Show2Num(u16 x,u16 y,u16 num,u8 len)
C51 COMPILER V9.01   LCD                                                                   07/18/2014 12:11:01 PAGE 8   

 428          {               
 429   1              u8 t,temp;                                                 
 430   1              for(t=0;t<len;t++)
 431   1              {
 432   2                      temp=(num/mypow(10,len-t-1))%10;
 433   2                      LCD_ShowChar(x+8*t,y,temp+'0',0); 
 434   2              }
 435   1      } 
 436          //显示字符串
 437          //x,y:起点坐标  
 438          //*p:字符串起始地址
 439          //用16字体
 440          void LCD_ShowString(u16 x,u16 y,const u8 *p)
 441          {         
 442   1          while(*p!='\0')
 443   1          {       
 444   2              if(x>LCD_W-16){x=0;y+=16;}
 445   2              if(y>LCD_H-16){y=x=0;LCD_Clear(RED);}
 446   2              LCD_ShowChar(x,y,*p,0);
 447   2              x+=8;
 448   2              p++;
 449   2          }  
 450   1      }
 451          
 452          
 453          
 454          
 455          
 456          
 457          
 458          
 459          
 460          
 461          
 462          


MODULE INFORMATION:   STATIC OVERLAYABLE
   CODE SIZE        =   3429    ----
   CONSTANT SIZE    =      2    ----
   XDATA SIZE       =      4     126
   PDATA SIZE       =   ----    ----
   DATA SIZE        =      1    ----
   IDATA SIZE       =   ----    ----
   BIT SIZE         =   ----    ----
END OF MODULE INFORMATION.


C51 COMPILATION COMPLETE.  0 WARNING(S),  0 ERROR(S)
