关于LCD12864的显示所存在的问题

2019-07-16 04:12发布

本人在新手,刚接触LCD12864的编程,在使用LCD显示汉字时,是可以的,但是当使用LCD显示图片时,出现了乱码,如下图所示
乱码显示.png
以下是我的程序:
#include <reg51.h>     //包含头文件
#include <intrins.h>    //包含固有函数
#define uint unsigned int //定义一下方便使用
#define uchar unsigned char //定义一下方便使用
#define DATA P1 //LCD12864数据线
sbit RS=P2^2; // 数据指令 D、I 选择
sbit RW=P2^1; // 读写 选择 R、W
sbit EN=P2^0; // 读写使能
sbit cs1=P2^4; // 片选1
sbit cs2=P2^3; // 片选2
/********************************/
/* 定义图形字库   */
uchar code Hzk[]={
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x0F,0xFF,0xFF,0xF8,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x1F,0xFF,0xFF,0xFC,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x3F,0xFF,0xC3,0xFC,0x00,0x30,0x00,0x06,0x00,0xE0,0x06,0x66,0x19,0x8C,0x31,
0x00,0x3F,0xFF,0xC3,0xFC,0x00,0x30,0x00,0x06,0x00,0xE0,0x06,0x66,0x19,0x8C,0x31,
0x00,0x3F,0xFE,0x01,0xFE,0x02,0x39,0x00,0x06,0x00,0xC0,0x0C,0x62,0x19,0x8C,0x31,
0x00,0x3F,0xF8,0x00,0xFE,0x07,0x31,0x80,0xFC,0x00,0xC0,0x0C,0x62,0x19,0x8C,0x31,
0x00,0x3F,0xF0,0x00,0xFE,0x06,0x31,0x80,0x78,0x00,0xC0,0x0C,0x66,0x1D,0xDC,0x31,
0x00,0x3F,0xE0,0x01,0xFE,0x06,0x31,0x80,0x30,0x00,0xC0,0x0C,0x00,0x19,0xCC,0x30,
0x00,0x3F,0xC0,0x03,0xFE,0x07,0x39,0x80,0x38,0x00,0xC0,0x1F,0x00,0x18,0x8C,0x00,
0x00,0x3F,0xC0,0x03,0xFE,0x07,0x39,0x80,0x38,0x00,0xC0,0x1F,0x00,0x18,0x8C,0x00,
0x00,0x3F,0xC0,0x3F,0xFE,0x06,0x31,0x80,0x38,0x00,0xC0,0x3D,0x18,0x19,0x8C,0x0C,
0x00,0x3F,0x80,0x7F,0xFE,0x06,0x31,0x80,0x38,0x00,0xE0,0x3D,0x18,0x19,0x8C,0x1C,
0x00,0x3F,0x80,0xFF,0xFE,0x06,0x31,0x80,0x18,0x00,0xE0,0x3D,0x18,0x19,0x8C,0x3C,
0x00,0x3F,0x81,0xF3,0xFE,0x07,0x31,0x80,0x38,0x00,0xE0,0x3F,0x18,0x19,0xD8,0x0C,
0x00,0x3F,0x81,0xF3,0xFE,0x07,0x31,0x80,0x38,0x00,0xE0,0x3F,0x18,0x19,0xD8,0x0C,
0x00,0x3F,0x03,0xC1,0xFE,0x00,0x30,0x00,0x30,0x00,0xE0,0x3F,0x18,0x18,0x00,0x0C,
0x00,0x3F,0x03,0xC0,0xFE,0x00,0x30,0x01,0xF0,0x00,0xE0,0x35,0x18,0x1C,0x00,0x0C,
0x00,0x3F,0x03,0x80,0xFE,0x00,0x3C,0x80,0x00,0x00,0xE0,0x0C,0x18,0x0C,0x00,0x18,
0x00,0x3F,0x03,0x80,0xFE,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x3F,0x03,0x80,0xFE,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x3F,0x03,0xC0,0xFE,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x3F,0x01,0xC1,0xFE,0x1F,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
0x00,0x3F,0x81,0xFF,0xFE,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x3F,0x80,0xFF,0xFE,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x3F,0x80,0xFF,0xFE,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x3F,0x80,0x7F,0xFE,0x00,0x00,0x00,0x00,0x00,0x08,0x08,0x00,0x00,0x00,0x00,
0x00,0x3F,0xC0,0x0F,0xFE,0x00,0x00,0x00,0x00,0x00,0x18,0x18,0x00,0x00,0x00,0x00,
0x00,0x3F,0xE0,0x01,0xFE,0x01,0x01,0x80,0x00,0x60,0x10,0xD0,0x00,0x00,0x00,0x00,
0x00,0x3F,0xF0,0x01,0xFE,0x04,0x44,0x4C,0x89,0xB1,0x93,0x70,0x00,0x00,0x00,0x00,
0x00,0x3F,0xF8,0x00,0xFE,0x0C,0x4C,0x4D,0xD3,0x1B,0x12,0x30,0x78,0xE3,0xF8,0x3C,
0x00,0x3F,0xF8,0x00,0xFE,0x0C,0x4C,0x4D,0xD3,0x1B,0x12,0x30,0x78,0xE3,0xF8,0x3C,
0x00,0x3F,0xFC,0x01,0xFE,0x0F,0xCF,0xC5,0xF2,0x12,0x24,0x30,0x81,0x16,0x48,0x60,
0x00,0x3F,0xFF,0x01,0xFE,0x08,0x08,0x06,0x62,0x12,0x24,0x20,0x82,0x14,0x88,0x40,
0x00,0x3F,0xFF,0xFF,0xFE,0x08,0x08,0x06,0x63,0x26,0x26,0x60,0x83,0x24,0x88,0x40,
0x00,0x0F,0xFF,0xFF,0xFE,0x07,0x07,0x04,0x41,0xC4,0x63,0xE4,0x71,0xC0,0x92,0x39,
0x00,0x0F,0xFF,0xFF,0xFE,0x07,0x07,0x04,0x41,0xC4,0x63,0xE4,0x71,0xC0,0x92,0x39,
0x00,0x07,0xFF,0xFF,0xFC,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
};
/*状态检查,LCD是否忙*/
void CheckState()  
{
   uchar dat;//状态信息(判断是否忙)
   RS=0; // 数据指令选择,D/I(RS)="L" ,表示 DB7∽DB0 为显示指令数据
   RW=1; //R/W="H" ,E="H"数据被读到DB7∽DB0
   do{
      DATA=0x00;
      EN=1; //EN下降源
   _nop_(); //一个时钟延时
   dat=DATA;
      EN=0;
     dat=dat>>7; //仅当第7位为0时才可操作(判别busy信号)
      }while(!(dat==0x00));
}
/*写命令到LCD中*/
SendCommandToLCD(uchar com)
{
   CheckState();//状态检查,LCD是否忙
   RS=0; //向LCD发送命令。RS=0写指令,RS=1写数据
   RW=0;//R/W="L" ,E="H→L"数据被写到 IR 或 DR
   DATA=com; //com :命令
   EN=1;//EN下降源
   _nop_();
   _nop_();
   EN=0;
}
/*设置页 0xb8是页的首地址*/
void SetLine(uchar page)
{
page=0xb8|page; //1011 1xxx 0<=page<=7 设定页地址--X 0-7,8行为一页64/8=8,共8页
SendCommandToLCD(page);
}
/*设定显示开始行,0xc0是行的首地址*/
void SetStartLine(uchar startline)     
{
startline=0xc0|startline; //1100 0000
SendCommandToLCD(startline); //设置从哪行开始:0--63,一般从0 行开始显示
}
/*设定列地址--Y 0-63 ,0x40是列的首地址*/
void SetColumn(uchar column)
{
column=column &0x3f; //column最大值为64,越出 0=<column<=63
column= 0x40|column; //01xx xxxx
SendCommandToLCD(column);
}
/*开关显示,0x3f是开显示,0x3e是关显示*/
void SetOnOff(uchar onoff)   
{
onoff=0x3e|onoff; //0011 111x,onoff只能为0或者1
SendCommandToLCD(onoff);
}
/*写显示数据 */
void WriteByte(uchar dat)
{
   CheckState();//状态检查,LCD是否忙
   RS=1; //RS=0写指令,RS=1写数据
   RW=0;////R/W="L" ,E="H→L"数据被写到 IR 或 DR
   DATA=dat;//dat:显示数据
   EN=1; //EN下降源
   _nop_();
   _nop_();
   EN=0;
}
/*选择屏幕screen: 0-全屏,1-左屏,2-右屏*/
void SelectScreen(uchar screen)   
{        
   switch(screen)
   { case 0: cs1=0;//全屏
             _nop_(); _nop_(); _nop_();
             cs2=0;
             _nop_(); _nop_(); _nop_();
             break;
     case 1: cs1=0;//左屏
             _nop_(); _nop_(); _nop_();
             cs2=1;
             _nop_(); _nop_(); _nop_();
             break;
     case 2: cs1=1;//右屏
             _nop_(); _nop_(); _nop_();
             cs2=0;
             _nop_(); _nop_(); _nop_();
             break;
   }
}
/*清屏screen: 0-全屏,1-左屏,2-右*/
void ClearScreen(uchar screen)   
{
     uchar i,j;
     SelectScreen(screen);
     
for(i=0;i<8;i++) //控制页数0-7,共8页
{
         SetLine(i);
    SetColumn(0);
              for(j=0;j<64;j++)    //控制列数0-63,共64列
     {
                  WriteByte(0x00); //写点内容,列地址自动加1
              }
     }
      
}
/*延时程序*/
void delay(uint z)  
{
uint i,j;
for(i=0; i<z; i++)
   for(j = 0; j < 110; j++);
}
/*向上滚屏*//*这里用做滚动显示*/
void lcd_rol()     
{
   uint i;
for(i = 0; i < 64; i++)
{
   cs1=0; // 片选1
   SendCommandToLCD(0xc0+i);
   cs1=1;
  
     cs2=0;// 片选2
   //SendCommandToLCD(0xc0+62-i);
   SendCommandToLCD(0xc0+i);
   cs2=1; // 片选2
  
   delay(100);
}
}
/*初始化LCD*/
void InitLCD()   
{
CheckState();
SelectScreen(0);
SetOnOff(0); //关显示
SelectScreen(0);
SetOnOff(1); //开显示
SelectScreen(0);
ClearScreen(0);//清屏
SetStartLine(0); //开始行:0
}
/*显示单屏幕一页*/
void Display(uchar ss,uchar page,uchar column,uchar number)
{
int i; //ss 选屏参数,page 选页参数,column选列参数 number 区块 共计8页×2屏幕=16 取值0~15
SelectScreen(ss);
column=column&0x3f;
SetLine(page); //
SetColumn(column); //控制列
for(i=0;i<64;i++) //控制64列的数据输出
{
WriteByte(Hzk[i+64*number]); //64个数据输出
}
}
/*主函数*/
void main()
{
uint i;
   InitLCD();//初始12864   
   ClearScreen(0);//清屏   
   SetStartLine(0);//显示开始行   
   while(1)
   {   
   /*Display(选屏参数(cs0,cs1),pagr选页参数,column选列参数,number选第几汉字输出)*/
      for(i=0;i<8;i++)
      {
    Display(2,i,0,i); //左屏幕 i页,0列 i块
    _nop_();
   }
       for(i=0;i<8;i++)
      {
    Display(1,i,0,i+8); //右屏幕 i页,0列 i+8块
    _nop_();   
    }
//delay(5000);// 停住
// lcd_rol(); //滚动
}
/*这里用做滚动显示*/
/* while(1)
{
   lcd_rol();
   delay(1000);
}*/
}
请各位大侠帮忙,我试着修改过使用字模软件将图片生成数码,然后替换以上的图片数码,但是还是显示乱码,请大侠帮忙解答,谢谢!
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。