⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 main.c

📁 Keil写的320*240的液晶显示程序。很有参考意义。
💻 C
字号:
#include "reg52.h"
#include "lcd.h"
#include <stdio.h>                //输入输出标准库
#include <intrins.h>       //内部函数
#include "Chinese_Code.h"
uchar    code   SYSTAB[8] = {0x30,0x87,0x07,0x27,0x42,0xf0,0x28,0x00};
uchar    code   SCRTAB[10] = {0x00,0x00,0xf0,0x00,0x40,0xf0,0x00,0x80,0x00,0x00};
uchar    data   DAT1, COM, COUNT1;                           // 数据单元,指令单元

/***********************/
/*    写指令代码函数   */
/***********************/
void Writecode( uchar x )
{
 // SFRPAGE=CONFIG_PAGE;
  
  COM = x;
  DATBUS = COM;             //置指令代码
  A0 = 1;                   //设置RS=1
  CS1 = 0;
  _nop_();
  Write = 0;                //WR=0
  _nop_();
  _nop_();
  _nop_();
  Write = 1;                //WR=1
  _nop_();
  _nop_();
  _nop_();
  CS1 = 1;
}
/***********************/
/* 写参数及显示数据函数*/
/***********************/
void Writedata( uchar x )
{
   
   DAT1 = x;
   DATBUS = DAT1;              //置显示数据
   A0 = 0;                     //设置RS=0
   CS1 = 0;
   _nop_();                  //地址保持时间最小 10ns
   Write = 0;                  //WR=0
   _nop_();                  //读写脉冲宽度 最小120ns
   _nop_();
   _nop_();
   Write = 1;                  //WR=1
   _nop_();                  //写数据保持时间最小 120ns
   _nop_();
   _nop_(); 
   CS1 = 1;
}						 
/***********************/
/*读参数及显示数据函数 */
/***********************/
uchar Readdata(void)
{
   A0 = 1;                     //设置RS=1
   CS1 = 0;
   _nop_();
   DATBUS = 0xff;              //使能输入
   Read = 0;                   //RD=0
   _nop_();  
   _nop_();
   _nop_();
   DAT1 = DATBUS;             //读取数据
   _nop_();  
   _nop_();
   _nop_();
   Read = 1;                   //RD=1
   _nop_();
   _nop_();
   CS1 = 1;
   return DAT1;
}     
/***********************/
/*      初始化函数     */
/***********************/
void Init_LCD()
{
   uchar i;
   Writecode( 0x40 ); 	      //SYSTEM SET 代码
   for(i = 0;i < 8;i++)
     Writedata(SYSTAB[i]);    //写入参数
   Writecode(0x44);           //SCROLL代码
   for(i = 0;i < 10;i++)
     Writedata(SCRTAB[i]);    //写入参数
   Writecode( 0x5a );           //HDOT SCR 代码
   Writedata( 0x00 );            //设置参数P1
   Writecode( 0x5b );           //OVLAY 代码
   Writedata( 0x00 );            //设置参数:显示一,三区为文本方式,二重或合成
   Clear();                   //调清屏子程序
  // Writecode(0x56);           //显示状态设置
// Writedata(0x56);           //设置参数:显示一~四区开显示,光标闪烁显示
//   Writedata(0x54);           //设置参数:显示一~四区开显示,关光标
//   Writecode(0x5d);           //光标形状设置
 //  Writedata(0x07);
 //  Writedata(0x01);
}
 
/***********************/
/*    清显示RAM函数    */
/***********************/
void Clear( void )
{
  uint i;
  Writecode( 0x4C );           //CSRDIR代码,光标地址自动增1
  Writecode( 0x46 );           //CSRW 代码,写光标地址
  Writedata( 0x00 );            //设置参数光标指针低8位
  Writedata( 0x00 );            //设置参数光标指针高8位
  Writecode( 0x42 );           //MWRITE 代码,连续写数据命令代码
  for(i = 0;i < 9600;i++)
    Writedata( 0x00 );          //写入数据0
} 

/***********************/
/*      消点函数       */
/***********************/
/*
void cleardot(uint O_X,uint O_Y) 
{
   uint i,k;
   uchar dat;
   i=O_X%8;                          //计算显示单元地址(X坐标),低位点坐标
   O_X=O_X/8;                        //高位,字节坐标,以8的整数为单位
   k=40*O_Y+O_X;                     //指针地址
   Writecode(0x4f);
   Writecode(0x46);                  //设置光标地址 
   Writedata(k%256);                 //计算光标地址 
   Writedata(k/256+0x40);
   if(i==0)                          //如果=0,则是第8位(第一点),即把第8位置1
    O_Y=0x80;
   else
   {
    O_Y=1;
    for(;i<7;i++)                   //计算显示位,最高位为1位
    {
     O_Y=O_Y*2;                     //左移1位
    }
   }
   O_Y=~O_Y;                        //按位取反
   Writecode(0x43);                 //MREAD代码 
   dat=Readdata();                  //读取当前显示数据,同时光标指针根据设置方式自动修改
   Writecode(0x46);                 //重新设置光标地址 
   Writedata(k%256);                //计算光标地址 
   Writedata(k/256+0x40);
   Writecode(0x42);                 //MWRITE 代码
   Writedata(dat&O_Y);              //写入合成数据,或合成显示点 
}
*/

/***********************/
/*     清显示区函数    */
/***********************/
void clear_displayarea( void )
{
   uint i, j;
   uchar a;
   j = 40 * 56 + 8;
   Writecode( 0x4f );           //CSRDIR 代码(下移)
   for(i = 0;i < 32;i++)          //32列
    {  
     Writecode( 0x46 );         //CSRW代码,设置光标地址
     a = j % 256;
     Writedata( a );            //设置光标地址CSR,低地址
     a = j / 256 + 0x40;
     Writedata( a );            //高地址  
     Writecode( 0x42 );         //MWRITE代码,连续显示数据             
     for(a = 0;a < 148;a++)       //148行  
      {    
       Writedata( 0x00 );        //写入数据  
      }  
     j = j + 1;
    }                         //清图形区
   j = 40 * 8 + 8;                  //第8行开始清
   for(a = 0;a < 18;a++)          //18行??????
   {
    Writecode( 0x46 );          //CSRW 代码
    Writedata( j%256 );         //设置光标地址CSR
    Writedata( j/256 );
    Writecode( 0x4c );          //CSRDIR代码,光标地址自动左移
    Writecode( 0x42 );          //MWRITE代码,连续写
    for(i = 0;i < 32;i++)         //32列
    {
      Writedata( 0x20 );        //写入数据 
    }
    j = j + 40;
   }
   for(i = 0;i < 239;i++)         //竖线        
    Displaydot( 319,i );
}
/************************/
/* 写汉字函数(图形方式) */
/************************/
void Displaytext(uchar O_X,uchar O_Y,uchar code_1)
 {
   uchar xdata count1,a;
   uint xdata i,j;
   j = 40 * O_Y + O_X;                               //计算光标地址
   Writecode( 0x4f );                            //CSRDIR 代码(下移)
   for(i = 0; i < 2;i++)
    {  
     Writecode( 0x46 );                          //CSRW代码,设置光标地址
     a = j % 256;
     Writedata( a );                             //设置光标地址CSR,低地址
     a = j / 256 + 0x40;
     Writedata( a );                             //高地址 
     Writecode( 0x42 );                          //MWRITE代码,连续显示数据             
     for(count1 = i*16;count1 < 16*(i+1);count1++)  
      {    
       Writedata(CGTAB[code_1][count1]);       //写入数据  
      }  
     j = j + 1;
    }
 }
//显示图象函数
/*
void Diplaypic()
{
   uchar SAVEPAGESFR=SFRPAGE;
   uchar xdata a,i;
   uint xdata j,count1;
   j=0;                                        //计算光标地址
   Writecode(0x4f);                            //CSRDIR 代码(下移)
   for(i=0;i<40;i++)
    {  
     Writecode(0x46);                          //CSRW代码,设置光标地址
     a=j%256;
     Writedata(a);                             //设置光标地址CSR,低地址
     a=j/256+0x40;
     Writedata(a);                             //高地址 
     Writecode(0x42);                          //MWRITE代码,连续显示数据             
     for(count1=i*240;count1<240*(i+1);count1++)  
      {    
       Writedata(InterFaceTAB[count1]);       //写入数据  
      }  
     j=j+1;
    }
   SFRPAGE=SAVEPAGESFR;  
}
*/

/************************/
/*       西文函数       */
/************************/
void Displaydata(uchar O_X,uchar O_Y,uchar datacode)
 {
  uint j;
  j=40*O_Y+O_X;
  Writecode(0x46);             //CSRW 代码
  Writedata(j%256);            //设置光标地址CSR
  Writedata(j/256);
  Writecode(0x4c);             //CSRDIR代码,光标地址自动左移
  Writecode(0x42);             //MWRITE代码,连续写
  Writedata(datacode);         //写入数据 
 }
/***********************/
/*      绘点函数       */
/***********************/
void Displaydot(uint O_X,uint O_Y)
 {
   uint i,k;
   uchar dat;
   i=O_X%8;                          //计算显示单元地址(X坐标),低位点坐标
   O_X=O_X/8;                        //高位,字节坐标,以8的整数为单位
   k=40*O_Y+O_X;                     //指针地址
   Writecode(0x4f);
   Writecode(0x46);                  //设置光标地址 
   Writedata(k%256);                 //计算光标地址 
   Writedata(k/256+0x40);
   if(i==0)                          //如果=0,则是第8位(第一点),即把第8位置1
    O_Y=0x80;
   else
   {
    O_Y=1;
    for(;i<7;i++)                   //计算显示位,最高位为1位
    {
     O_Y=O_Y*2;                     //左移1位
    }
   }
   Writecode(0x43);                 //MREAD代码 
   dat=Readdata();                  //读取当前显示数据,同时光标指针根据设置方式自动修改
   Writecode(0x46);                 //重新设置光标地址 
   Writedata(k%256);                //计算光标地址 
   Writedata(k/256+0x40);
   Writecode(0x42);                 //MWRITE 代码
   Writedata(dat|O_Y);              //写入合成数据,或合成显示点 
}
//*******横线*******//
void H_line(uint x,uchar y,uint lenth)
{
  uint i;
  for(i=x;i<lenth;i++)        //横线
   Displaydot(i,y); 
}
//*******竖线******//
void L_line(uint x,uchar y,uint lenth)
{
  uint i;
  for(i=y;i<lenth;i++)        //竖线
   Displaydot(x,i);          
}

//*********画矩形框********//
void  Rect_line(uint x,uchar y,uint lenthx,uchar lenthy)
{
  uint i;
  for(i=x;i<lenthx;i++)        //横线
   Displaydot(i,y); 
  for(i=x;i<lenthx;i++)        //横线
   Displaydot(i,y+lenthy); 
  for(i=y;i<lenthy;i++)        //竖线
   Displaydot(x,i); 
  for(i=y;i<lenthy;i++)         //竖线
   Displaydot(x+lenthx,i);     
}

//**********显示连续显示n个汉字函数**********//
//**坐标,p为汉字库里的首个显示汉字的偏移量**//
void DisplayStrC(uchar x,uchar y,uchar code_1,uchar lenth)
{   
  uchar xdata count1,a,k;
  uint xdata i,j;
  j=40*y+x;                                  //计算光标地址
  Writecode(0x4f);                               //CSRDIR 代码(下移)

  for(k=0;k<lenth;k++)
   {
     for(i=0;i<2;i++)
      {  
       Writecode(0x46);                          //CSRW代码,设置光标地址
       a=j%256;
       Writedata(a);                             //设置光标地址CSR,低地址
       a=j/256+0x40;
       Writedata(a);                             //高地址 
       Writecode(0x42);                          //MWRITE代码,连续显示数据             
     for(count1=i*16;count1<16*(i+1);count1++)    
       Writedata(CGTAB[code_1][count1]);         //写入数据     
     j=j+1;
   }
  code_1=code_1+1;                               //指向下一个字
   }
}

//**********显示字符串函数**********//
void DisplayStr(uchar x,uchar y,uchar *p,uchar lenth)
{
  uchar i;
  uint j;
  j=40*y+x;
  Writecode(0x46);             //CSRW 代码
  Writedata(j%256);            //设置光标地址CSR
  Writedata(j/256);
  Writecode(0x4c);             //CSRDIR代码,光标地址自动左移
  Writecode(0x42);             //MWRITE代码,连续写
  for(i=0;i<lenth;i++)
    Writedata(*(p+i));           //写入数据 
}
void Delay(uchar x)
{
    uint i=655; 
    while(x--)
        while(i--);  
}
main()
{
    Delay(2); 
	Init_LCD();
	Writecode(0x42);              //MWRITE 代码
	Rect_line(0,0,319,239);
    DisplayStrC(1,3,0,2);         //文件
	H_line(0,21,319);
    H_line(0,20,300);
    H_line(0,50,300);
    H_line(0,30,300);
	H_line(0,60,300);
	while(1);
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -