技术热线: 4007-888-234

128x64 LCD图形显示程序例子

更新时间: 2019-03-23
阅读量:1953

128x64 LCD图形显示程序例子
 
这是一个在LCD128*64上显示图形的一个程序,一直以来都总是不知道怎么搞,现在终于都通了,想当初在网上总找,要不是程序不全,就是无法运行,或者是C51汇编语言,让人不爽,这样的痛苦不可以再让别人难受了。在LCD上显示一幅图,是“我的电脑,我的文档”。IC用PIC16F877A,晶振4MHZ,采用并行8线,LCD:TS12864-3,接法在程序中有定义,只要将此程序COPY到你的PICC里编译下载后,在目标板上即可以看到图画了!

     不知道可不可以加酷?

#i nclude "pic.h"
#define  RS   RC0
#define  RW   RC1
#define  E    RC2
#define  PSB  RC3
#define  RST  RC4
//PORTD=DATA;
unsigned char COUNT3,COUNT2,COUNT1,COUNT,LCD_X,LCD_Y,LCD_DATA1,LCD_DATA2,LCD_DATA,F3,R1,F0;
const unsigned char TU_TAB1[]={
        // 16  15   14   13   12   11   10   9     8   7     6   5    4    3    2    1
         0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
       0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,
       0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,
       0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,
       0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,
       0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,
       0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x3F,0xFF,0xF0,0x00,0x01,
       0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x40,0x00,0x18,0x00,0x01,
       0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xBF,0xFF,0xDC,0x00,0x01,
       0x80,0x00,0x1F,0xFF,0xFF,0x00,0x00,0x00,0x00,0x00,0x00,0xBF,0xFF,0xBC,0x00,0x01,
       0x80,0x00,0x30,0x00,0x01,0x80,0x00,0x00,0x00,0x00,0x01,0x7F,0xFF,0xB8,0x00,0x01,
       0x80,0x00,0x30,0x00,0x00,0x80,0x00,0x00,0x00,0x00,0x01,0x7F,0xFF,0x78,0x00,0x01,
       0x80,0x00,0x19,0xFF,0xFE,0xC0,0x00,0x00,0x00,0x00,0x02,0xFF,0xFF,0x70,0x00,0x01,
       0x80,0x00,0x18,0x00,0x00,0x40,0x00,0x00,0x00,0x00,0x02,0xFF,0xFE,0xF0,0x00,0x01,
       0x80,0x00,0x0C,0x01,0xFF,0x60,0x00,0x00,0x00,0x00,0x05,0xFF,0xFE,0xE0,0x00,0x01,
    0x80,0x00,0x0C,0x7F,0xD0,0x20,0x00,0x00,0x00,0x00,0x05,0xFF,0xFD,0xE0,0x00,0x01,
    0x80,0x00,0x06,0x00,0x0F,0xB0,0x00,0x00,0x00,0x00,0x0B,0xFF,0xFD,0xC0,0x00,0x01,
    0x80,0x00,0x06,0x03,0xFC,0x10,0x00,0x00,0x00,0x00,0x0B,0xFF,0xFB,0xC0,0x00,0x01,
    0x80,0x00,0x03,0x1F,0x00,0x18,0x00,0x00,0x00,0x00,0x17,0xFF,0xFB,0x80,0x00,0x01,
    0x80,0x00,0x03,0x00,0x00,0x08,0x00,0x00,0x00,0x00,0x17,0xFF,0xF7,0x80,0x00,0x01,
    0x80,0x00,0x01,0x80,0x00,0x0C,0x00,0x00,0x00,0x00,0x17,0xFF,0xF7,0x00,0x00,0x01,
    0x80,0x00,0x01,0x80,0x00,0x04,0x00,0x00,0x00,0x00,0x09,0xFF,0xEF,0x00,0x00,0x01,
    0x80,0x00,0x00,0xC0,0x00,0xF6,0x00,0x00,0x00,0x00,0x06,0x7F,0xEE,0x00,0x00,0x01,
    0x80,0x00,0x00,0xC0,0x03,0xC2,0x00,0x00,0x00,0x00,0x01,0x9F,0xDE,0x00,0x00,0x01,
    0x80,0x00,0x00,0x60,0x00,0x3B,0x00,0x00,0x00,0x00,0x00,0x67,0xDF,0x00,0x00,0x01,
    0x80,0x00,0x00,0x60,0x00,0xE3,0x00,0x00,0x00,0x00,0x00,0x19,0xBF,0x00,0x00,0x01,
    0x80,0x00,0x00,0x30,0x03,0x8E,0x00,0x00,0x00,0x00,0x00,0x06,0x3F,0x00,0x00,0x01,
    0x80,0x00,0x00,0x30,0x06,0x3C,0x00,0x00,0x00,0x00,0x00,0x01,0xFF,0x00,0x00,0x01,
    0x80,0x00,0x00,0x18,0x00,0xF0,0x00,0x00,0x00,0x00,0x00,0x00,0xFF,0x00,0x00,0x01,
    0x80,0x00,0x00,0x18,0x03,0xC0,0x00,0x00,0x00,0x00,0x00,0x0F,0x7F,0x80,0x00,0x01,
       0x80,0x00,0x00,0x0C,0x0F,0x00,0x00,0x00,0x00,0x00,0x00,0x30,0x1F,0xC0,0x00,0x01,
    0x80,0x00,0x00,0x0C,0x3C,0x00,0x00,0x00,0x00,0x00,0x00,0x40,0x07,0xE0,0x00,0x01,
    0x80,0x00,0x00,0x06,0xF0,0x00,0x00,0x00,0x00,0x00,0x00,0xE0,0x1F,0xE0,0x00,0x01,
    0x80,0x00,0x00,0x07,0xC0,0x00,0x00,0x00,0x00,0x00,0x00,0xF8,0x3F,0xC0,0x00,0x01,
    0x80,0x00,0x00,0x03,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x7E,0xFF,0x00,0x00,0x01,
    0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x1F,0xFC,0x00,0x00,0x01,
    0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x07,0xF0,0x00,0x00,0x01,
    0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,0xC0,0x00,0x00,0x01,
    0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,
    0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,
    0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,
    0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,
    0x80,0x05,0x40,0x88,0x04,0x01,0x08,0x00,0x00,0x0A,0x81,0x10,0x04,0x00,0x20,0x01,
    0x80,0x79,0x21,0x08,0x02,0x21,0x49,0x00,0x00,0xF2,0x42,0x10,0x04,0x47,0x10,0x01,
    0x80,0x09,0x07,0xDF,0x7F,0xF7,0xAA,0x00,0x00,0x12,0x0F,0xBE,0x7F,0xE5,0xFE,0x01,
    0x80,0x7F,0xF4,0x51,0x08,0x81,0x2C,0x00,0x00,0xFF,0xE8,0xA2,0x44,0x47,0x00,0x01,
    0x80,0x09,0x04,0x61,0x08,0x81,0x7F,0x00,0x00,0x12,0x08,0xC2,0x7F,0xC5,0x4A,0x01,
    0x80,0x0B,0x24,0x51,0x08,0x83,0x81,0x00,0x00,0x16,0x48,0xA2,0x44,0x45,0x6A,0x01,
    0x80,0x1D,0x47,0xC9,0x05,0x05,0x01,0x00,0x00,0x3A,0x8F,0x92,0x44,0x47,0x52,0x01,
    0x80,0x68,0x84,0x49,0x05,0x05,0x7F,0x00,0x00,0xD1,0x08,0x92,0x7F,0xC5,0x6A,0x01,
       0x80,0x09,0x94,0x41,0x02,0x01,0x01,0x00,0x00,0x13,0x28,0x82,0x44,0x05,0x4A,0x01,
    0x80,0x0E,0x57,0xC1,0x0D,0x81,0x01,0x00,0x00,0x1C,0xAF,0x82,0x04,0x29,0x42,0x01,
       0x80,0x38,0x34,0x4E,0x70,0x71,0x7F,0x00,0x00,0x70,0x68,0x9C,0x03,0xEB,0x7E,0x01,
       0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,
       0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,
       0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,
       0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,
       0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,
       0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,
       0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,
       0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,
       0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,
       0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,
       0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF
         };

//*************delay_ms********************************
void delay_10us(unsigned char x)
{
  while(x--);
}
void delay_ms(unsigned char cnt)
{
    unsigned char i;
do {
  i = 4;
  do {
   delay_10us(39);
  } while(--i);
} while(--cnt);
}

//***********检查是否LCD忙碌的子程序**********************
void check_busy(void)
{
  TRISD=0xff;//在此处设为输入。
  PORTD=0xff;
  RS=0;
  RW=1;
  while (1)
   {
    NOP();NOP();NOP();
    E =1;
    NOP();NOP();NOP();
    if (RD7==0) break;
    NOP();NOP();NOP();
    E =0;
   }
  E =0;     
  TRISD=0X00;//还原为输出。
  RC7=!RC7;
  //delay_ms(50);
}
//**********数据写命令***************************
void send_com(unsigned char command )
{
  check_busy();
  RS=0;
  RW=0;
  PORTD=command;
  E =1;
  NOP();
  NOP();
  E =0;
}
//**********LCD初始化时的送命令******************
void send_com_init(unsigned char command_init )
{
  RS=0;
  RW=0;
  PORTD=command_init;
  E =1;
  NOP();
  NOP();
  E =0;
}
//**********写数据***************************
void send_data(unsigned char data )
{
  check_busy();
  RS=1;
  RW=0;
  PORTD=data;
  E =1;
  NOP();
  NOP();
  E =0;
}
//***********读数据****************************
unsigned char read_data(void)
{
  unsigned char read_data;
  check_busy();
  TRISD=0XFF;
  RS=1;
  RW=1;
  E =1;
  NOP();
  read_data=PORTD;
  E =0;
  TRISD=0x00;//还原为输出。
  return (read_data);
}
//***********清屏*****************************
void clear_LCD(void)
{
  send_com(0x01);
  send_com(0x34);
  send_com(0x30);
}
//**********LCD初始化************************
void initial_LCD(void)
{  
   PSB=1;
   RST=0;//Reset.
   NOP();
   NOP();
   RST=1;
   NOP();
   delay_10us(10);
   send_com_init(0x30);//30H--基本指令操作
   delay_10us(4);
   send_com_init(0x0c);//开显示,关光标,不闪烁。
   delay_10us(10);
   send_com_init(0x01);//清除显示
   delay_ms(10);
   send_com_init(0x06);//指定在资料写入或读取时,光标的移动方向,DDRAM的地址计数器(AC)加1。
   
}
//**********写准备***************************
void WR_ZB(void)
{
   send_com(0x34);
   send_com(LCD_Y);
   send_com(LCD_X);
   send_com(0x30);
}
//*********取数送显示******************************
void QUSHU(const unsigned char *shu)
{
   for (;COUNT!=0;COUNT--)
   {
     send_data(*shu++);
     delay_ms(80);
   }  
}
//*********Flash******************************
void flash(void)
{
   send_com(0x08);//关闭显示
   delay_ms(250);
   send_com(0x0c);//开显示,关光标,不闪烁。
   delay_ms(250);
   send_com(0x08);//关闭显示
   delay_ms(250);
   send_com(0x0c);//开显示,关光标,不闪烁。
   delay_ms(250);
   send_com(0x08);//关闭显示
   delay_ms(250);
}
//**********显示图形子程序*********************
void PHO_DISP(const unsigned char *s)
{
   COUNT3=0X02;
   LCD_X=0X80;
   for (;COUNT3!=0;COUNT3--)
   {
       LCD_Y=0X80;
       COUNT2=0X20;//32
       for (;COUNT2!=0;COUNT2--)
       {
         COUNT1=0X10;//16
         WR_ZB();
         for (;COUNT1!=0;COUNT1--)
          {
            send_data(*s++);
            
          }
         LCD_Y+=1;
       }
       LCD_X=0X88;
   }
   send_com(0x36);
   send_com(0x30);
}
//******************显示点阵子程序************
void LAT_DISP(void)
{
   COUNT3=0X02;
   LCD_X=0X80;
   for (;COUNT3!=0;COUNT--)
    {
       LCD_Y=0X80;
       F0=0;
       COUNT2=0X20;
       for (;COUNT2!=0;COUNT--)
         {
           COUNT1=0X10;
           WR_ZB();
           if (F0!=0)
           {
             LCD_DATA=LCD_DATA2;
           }
           else LCD_DATA=LCD_DATA1;
           for (;COUNT1!=0;COUNT1--)
             {
              send_data(LCD_DATA);
             }
           LCD_Y+=1;
           F0=!F0;
         }
       LCD_X=0X88;
    }
   
   send_com(0x36);
   send_com(0x30);
}
 
void main(void)
{
   TRISC=0X00;
   TRISD=0X00;   
   PORTD=0X00;
   PORTC=0X00;
   initial_LCD();
   while (1)
   {   
       clear_LCD();
       PHO_DISP(TU_TAB1);//显示图形
       while (1);     
   }
}