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

📄 ca12864.c

📁 51单片机驱动12864图形液晶显示的源代码
💻 C
字号:


//            CA12864 Demo Program
//***************************************************************************
//连线表:  CPU=89C52                                                        *
//RS=P2.0            R/W=P2.1               CS1=P2.7        CS2=P2.6        *
//SysClock=12MHz     DB0-DB7=P0.0-P0.7      E=/(WR*RD)      /Reset=InBoard  *
//***************************************************************************

#include <reg52.h>
#include <stdlib.h>
#include <intrins.h>
#include <stdio.h>

/********************接口定义********************/
char xdata LcmWriteCmdPort1   _at_ 0x8000;      //CS1=1 RW=0 RS=0
char xdata LcmWriteCmdPort2   _at_ 0x4000;      //CS2=1 RW=0 RS=0
char xdata LcmWriteDataPort1  _at_ 0x8100;      //CS1=1 RW=0 RS=1
char xdata LcmWriteDataPort2  _at_ 0x4100;      //CS2=1 RW=0 RS=1
char xdata LcmReadStatusPort1 _at_ 0x8200;      //CS1=1 RW=1 RS=0
char xdata LcmReadStatusPort2 _at_ 0x4200;      //CS2=1 RW=1 RS=0
sbit Key   =P3^4;

unsigned char Page;      //页 地址
unsigned char Col;      //列 地址

unsigned char code BMP1[];
unsigned char code HZ1[];

void Delay(unsigned int MS);

/***************************/
/*检查Busy                 */
/***************************/
void Busy1(void)
{
     _nop_();
     while(LcmReadStatusPort1 & 0x80);      //Status Read Bit7 = BUSY
     _nop_();
}

void Busy2(void)
{
     _nop_();
     while(LcmReadStatusPort2 & 0x80);      //Status Read Bit7 = BUSY
     _nop_();
}

/***************************/
/*写指令                   */
/***************************/
void WriteCommand1( unsigned char CommandByte )
{
     Busy1();
     LcmWriteCmdPort1 = CommandByte;
     _nop_();
}

void WriteCommand2( unsigned char CommandByte )
{
     Busy2();
     LcmWriteCmdPort2 = CommandByte;
     _nop_();
}

/***************************/
/*写数据                   */
/***************************/
void WriteData( unsigned char DataByte )
{
     unsigned char x,y;
     switch (Col&0xc0)      /*  col.and.0xC0      */
     {                  /*条件分支执行            */
           case 0:
           {
                 x = Col&0x3F|0x40;      /* col.and.0x3f.or.Set Y Address*/
                 y = Page&0x07|0xB8;      /* row.and.0x07.or.set Page      */
                 WriteCommand1(y);            //设置页面地址
                 WriteCommand1(x);            //设置列地址
                 Busy1();
                 LcmWriteDataPort1 = DataByte;
                 _nop_();
                 break;                  //完成case分支
           }
           case 0x40:
           {
                 x = Col&0x3F|0x40;      /* col.and.0x3f.or.Set Y Address*/
                 y = Page&0x07|0xB8;      /* row.and.0x07.or.set Page      */
                 WriteCommand2(y);            //设置页面地址
                 WriteCommand2(x);            //设置列地址
                 Busy2();
                 LcmWriteDataPort2 = DataByte;
                 _nop_();
                 break;                  //完成case分支
           }
     }
}

void LcmClear( void )
{
     Page = 0;
     Col  = 0;
     for(Page=0;Page<8;Page++)
           for(Col=0;Col<128;Col++)
                 WriteData(0);
}

void LcmInit( void )
{
     WriteCommand1(0x3f);      //开显示
     WriteCommand2(0x3f);
     
     WriteCommand1(0xc0);      //设置起始地址=0
     WriteCommand2(0xc0);

     WriteCommand1(0x3f);      //开显示
     WriteCommand2(0x3f);

     LcmClear();
     Col = 0;
     Page= 0;
}

void LcmPutDots( unsigned char DotByte , unsigned char n)
{
     unsigned char i,j;
     Page = 0;
     Col  = 0;
     for(Page=0;Page<8;Page++)
     {
           Col = 0;
           for(i=0;i<128/n;i++)
           {
                 for(j=0;j<n;j++)
                 {
                       WriteData( DotByte );
                       Col++;
                 }
                 DotByte = ~DotByte;
           }
           if(DotByte ==0||DotByte==0xff) DotByte=~DotByte;      //12864在显示8x8点阵时候需要换页时候取反
     }
}

void LcmPutBMP( unsigned char *puts )
{
     unsigned int X=0;
     Page = 0;
     Col  = 0;
     for(Page=0;Page<8;Page++)
     {
           for(Col=0;Col<128;Col++)
           {
                 WriteData( puts[X] );
                 X++;
           }
     }
}

void LcmPutHZ( unsigned char HZcode )
{
     unsigned char i,j;
     unsigned int  x;
     x = 0x20*HZcode;
     for(i=0;i<2;i++)
     {
           for(j=0;j<16;j++)
           {
                 WriteData(HZ1[x]);
                 x++;
                 Col++;
           }
           Page++;
           Col = Col-16;      //恢复位置
     }
     Page = Page-2;      //修正下一个汉字的起始位置
     Col  = Col+16;      //下一个汉字接在这个汉字后面
}

void Delay(unsigned int MS)
{
     unsigned char us,usn;
     while(MS!=0)
     {
           usn = 2;      //for 12M
           while(usn!=0)
           {
                 us=0xf6;
                 while (us!=0){us--;};
                 usn--;
           }
           MS--;
     }
}

void DelayKey(unsigned int Second , unsigned int MS100)
{                                    //输入精确到0.1S,是用,
     unsigned int i;
     for(i=0;i<Second*100+MS100*10;i++)
     {
           if(Key==0)
           {
                 Delay(20);
                 while(Key==0) {Delay(20);}
                 break;
           }
           else Delay(10);
     }                        
}

void Main( void )
{
     unsigned char HZ;
     Delay(20);      //等待复位
     LcmInit();
     LcmClear();
     while(1)
     {
           LcmPutBMP(BMP1);
           DelayKey(1,5);
           
           for(Page=0;Page<8;Page+=2)
           {
                 Col = 0;
                 for(HZ=0;HZ<8;HZ++) LcmPutHZ(HZ);
           }
           DelayKey(1,5);
           
           LcmPutDots(0x55,1);
           DelayKey(1,5);
           LcmPutDots(0xAA,1);
           DelayKey(1,5);
           LcmPutDots(0x33,2);
           DelayKey(1,5);
           LcmPutDots(0xCC,2);
           DelayKey(1,5);
           LcmPutDots(0x0F,4);
           DelayKey(1,5);
           LcmPutDots(0xF0,4);
           DelayKey(1,5);
           LcmPutDots(0x00,8);
           DelayKey(1,5);
           LcmPutDots(0xFF,8);
           DelayKey(1,5);
     }
}

unsigned char code BMP1[]={            //字节倒序
/*--  调入了一幅图像:E:\!Program\!BmpSample\12864.bmp  --*/
/*--  宽度x高度=128x64  --*/
0xFF,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x81,0x81,0x01,0x01,0x01,0x01,0x01,
0x01,0x01,0x01,0x01,0x81,0x81,0x81,0x81,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
0x01,0x01,0x81,0x81,0x81,0x81,0x81,0x81,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
0x01,0x01,0x81,0x81,0x81,0x81,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
0x81,0x81,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0xFF,
0xFF,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x04,0x06,0x06,0xFF,0xFF,0xFF,0xFF,0x00,0x00,0x00,0x00,0x00,
0x08,0x0E,0x07,0x03,0x03,0x03,0x07,0xFF,0xFF,0x7F,0x3E,0x00,0x00,0x00,0x00,0x1C,
0x7F,0x7F,0xFF,0xF1,0xE0,0xE0,0xC0,0xE1,0xFF,0x3F,0x1E,0x00,0x00,0x20,0x60,0xE0,
0xE0,0xE0,0xA0,0x00,0x20,0xE0,0x60,0x20,0x20,0x00,0x80,0xF0,0xF8,0xFC,0x7E,0x4E,
0x43,0xC3,0xC1,0x81,0x80,0x00,0x00,0x00,0x00,0x00,0x80,0x60,0x10,0x08,0xFE,0xFF,
0xFF,0xFF,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xFF,
0xFF,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xFF,0xFF,0xFF,0xFF,0x80,0x00,0x00,0x00,0x00,
0x00,0x80,0xC0,0xE0,0xF8,0xEC,0xE7,0xE3,0xE1,0xE0,0xE0,0x38,0x00,0x00,0x00,0x78,
0xFC,0xFE,0x83,0x01,0x03,0x03,0x8F,0xFF,0xFF,0x7F,0x3C,0x00,0x00,0x00,0x80,0x81,
0x63,0x1F,0x3F,0xFE,0xFD,0xF0,0xC0,0x80,0x00,0x00,0x1F,0x7F,0xFF,0xFF,0xC0,0x00,
0x00,0x81,0xFF,0xFF,0x7F,0x3E,0x00,0x00,0x1E,0x1D,0x1C,0x1C,0x1C,0x1C,0xFF,0xFF,
0xFF,0xFF,0x1C,0x1C,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xFF,
0xFF,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x00,0x00,
0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x01,0x01,0x01,0x01,0x01,0x01,0x00,0x00,0x00,0x00,0x00,0x01,0x01,0x01,
0x01,0x01,0x00,0x01,0x01,0x01,0x01,0x01,0x01,0x00,0x00,0x00,0x00,0x00,0x01,0x01,
0x01,0x01,0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,0x01,
0x01,0x81,0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xFF,
0xFF,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x80,0xF8,0xC0,
0x00,0x84,0xCE,0xFE,0x7F,0xF3,0xC3,0x03,0x07,0x0E,0x7C,0xF8,0x80,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x88,0xD8,0x7C,0x3C,0x3C,0x3E,0xEE,0x8E,
0x84,0x80,0x00,0x60,0x70,0x3A,0x5E,0x8E,0x0C,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x80,0xC0,0xFE,0xFC,0xC0,0x80,0xC0,0xC8,0xF8,0xF8,0x20,0xA4,0xFE,
0x3C,0x30,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xC0,0xC0,
0xC0,0xE1,0xE3,0xFF,0xFE,0xFC,0xF8,0x38,0x10,0x0E,0x0E,0x0C,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xFF,
0xFF,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xF8,0xFF,0x0F,0x7B,
0x7C,0xEF,0x37,0x3E,0xFF,0xE7,0x67,0x0E,0x0E,0x0C,0x00,0xFF,0xFF,0x00,0x00,0x00,
0x00,0x00,0x00,0x10,0x30,0x70,0x78,0x3C,0xFF,0xFF,0x0E,0x06,0x07,0x03,0xFF,0x19,
0x07,0x01,0x00,0x20,0x18,0x0C,0x8E,0xC7,0xFB,0x70,0x20,0x00,0x00,0x00,0x00,0x02,
0x0E,0x8F,0xCF,0xEF,0xFF,0x3F,0x19,0x04,0x04,0x1C,0x7D,0xEF,0xFF,0xFB,0xEB,0xB1,
0x9F,0x8E,0x80,0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x38,0x3D,
0xFB,0xFD,0xFD,0x9C,0xC8,0x80,0x07,0x1E,0x78,0xE0,0x80,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xFF,
0xFF,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,0x07,0x0E,0x1C,
0x18,0x19,0x1A,0x0F,0x1B,0x1B,0x3B,0x3A,0x38,0x38,0x3E,0x1F,0x07,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x02,0x07,0x01,0x00,0x00,0x00,0x00,0x0F,0x00,
0x10,0x10,0x18,0x0C,0x0E,0x07,0x03,0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x01,0x01,0x00,0x1F,0x20,0x04,0x06,0x16,0x12,0x1B,0x0F,0x07,0x01,0x01,0x05,
0x0D,0x1D,0x19,0x01,0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x0F,0x07,
0x03,0x03,0x01,0x01,0x01,0x01,0x00,0x00,0x00,0x00,0x03,0x0E,0x1C,0x38,0x3F,0x60,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xFF,
0xFF,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,
0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,
0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,
0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,
0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,
0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,
0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,
0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0xFF,
};

unsigned char code HZ1[]={      //字节颠倒
/*--  文字:  欢  --*/
/*--  宋体12;  此字体下对应的点阵为:宽x高=16x16   --*/
0x14,0x24,0x44,0x84,0x64,0x1C,0x20,0x18,0x0F,0xE8,0x08,0x08,0x28,0x18,0x08,0x00,
0x20,0x10,0x4C,0x43,0x43,0x2C,0x20,0x10,0x0C,0x03,0x06,0x18,0x30,0x60,0x20,0x00,

/*--  文字:  迎  --*/
/*--  宋体12;  此字体下对应的点阵为:宽x高=16x16   --*/
0x40,0x41,0xCE,0x04,0x00,0xFC,0x04,0x02,0x02,0xFC,0x04,0x04,0x04,0xFC,0x00,0x00,
0x40,0x20,0x1F,0x20,0x40,0x47,0x42,0x41,0x40,0x5F,0x40,0x42,0x44,0x43,0x40,0x00,

/*--  文字:  光  --*/
/*--  宋体12;  此字体下对应的点阵为:宽x高=16x16   --*/
0x00,0x40,0x42,0x44,0x5C,0xC8,0x40,0x7F,0x40,0xC0,0x50,0x4E,0x44,0x60,0x40,0x00,
0x00,0x80,0x40,0x20,0x18,0x07,0x00,0x00,0x00,0x3F,0x40,0x40,0x40,0x40,0x78,0x00,

/*--  文字:  临  --*/
/*--  宋体12;  此字体下对应的点阵为:宽x高=16x16   --*/
0x00,0xF8,0x00,0x00,0xFE,0x40,0x30,0x8F,0x0A,0x08,0x18,0x68,0x08,0x88,0x08,0x00,
0x00,0x1F,0x00,0x00,0x7F,0x00,0x00,0x7F,0x21,0x21,0x3F,0x21,0x21,0x7F,0x01,0x00,

/*--  文字:  松  --*/
/*--  宋体12;  此字体下对应的点阵为:宽x高=16x16   --*/
0x08,0x08,0xC8,0xFF,0x48,0x88,0x88,0x60,0x1E,0x84,0x00,0x0F,0x30,0xC0,0x80,0x00,
0x04,0x03,0x00,0xFF,0x00,0x21,0x30,0x28,0x26,0x23,0x21,0x20,0x28,0x71,0x20,0x00,

/*--  文字:  山  --*/
/*--  宋体12;  此字体下对应的点阵为:宽x高=16x16   --*/
0x00,0x00,0xE0,0x00,0x00,0x00,0x00,0xFF,0x00,0x00,0x00,0x00,0x00,0xE0,0x00,0x00,
0x00,0x20,0x7F,0x20,0x20,0x20,0x20,0x3F,0x20,0x20,0x20,0x20,0x20,0x7F,0x00,0x00,

/*--  文字:  电  --*/
/*--  宋体12;  此字体下对应的点阵为:宽x高=16x16   --*/
0x00,0x00,0xF8,0x48,0x48,0x48,0x48,0xFF,0x48,0x48,0x48,0x48,0xF8,0x00,0x00,0x00,
0x00,0x00,0x0F,0x04,0x04,0x04,0x04,0x3F,0x44,0x44,0x44,0x44,0x4F,0x40,0x70,0x00,

/*--  文字:  子  --*/
/*--  宋体12;  此字体下对应的点阵为:宽x高=16x16   --*/
0x00,0x00,0x02,0x02,0x02,0x02,0x02,0xE2,0x12,0x0A,0x06,0x02,0x00,0x80,0x00,0x00,
0x01,0x01,0x01,0x01,0x01,0x41,0x81,0x7F,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x00,
};

⌨️ 快捷键说明

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