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

📄 lcd-cag12864i.c

📁 LCD128*64液晶驱动程序
💻 C
字号:
/*********************************************
//128*64液晶显示程序,串口
//模块编写者:
//模块测试者:xpf
//模块最后修改时间:2008.07.29
*********************************************/
#include  <msp430x14x.h>
//数据类型重命名
typedef unsigned char   uchar;
typedef unsigned int    uint;
typedef unsigned long   ulong;
typedef signed char     schar;
typedef signed int      sint;
typedef signed long     slong;

#define LCDDIR P3DIR
#define LCDOUT P3OUT
#define LCDRS  BIT1
#define LCDCS  BIT0
#define LCDSID BIT2
#define LCDSCK BIT3

#define LCDRSTDIR  P4DIR
#define LCDRSTOUT  P4OUT
#define LCDRST     BIT7

//函数声明,函数包含于main模块中
extern void DELAYNMS(int delaycount);//函数用途:延时nms
extern void DELAYNUS(int delaycount); //函数用途:延时nus

void WRTIECOMTOLCD(uint ui_command); //函数用途:向LCD写命令
void WRTIEDATATOLCD(uint ui_data); //函数用途:向LCD写数据
void CLRLCD(void); //函数用途:清屏
void InitLCD(void); //函数用途:初始化 LCD
void WRTIEWORDTOLCD(int x,int y,uchar *data); //函数用途:向液晶指定位置输出汉字
void WRTIECHARTOLCD(int x,int y,uchar *data); //函数用途:向液晶指定位置输出字符
void Circle(int x,int y,uchar *data,uchar uc_byte,int i_temp);   //函数用途:循环显示汉字
void CLRPRELCD(int x,int y,int i_byte);   //函数用途:部分清屏
void WRITELINETOLCD(int x,int y,uchar uc_nbyte);    //函数功能:从指定的位置开始画线
const uchar ComTable[8]={3,2,1,0,7,6,5,4,};

/*************************************************
//函数名称:WRTIECOMTOLCD()
//函数功能:向LCD写命令
//调用函数:无
//全局变量:
//输入:command,准备写入的命令字
//返回:无
//函数编写者:xpf
//函数测试者:            函数最后修改时间:
*************************************************/
void WRTIECOMTOLCD(uint ui_command)  
{
     uint ui_i; 
     uint ui_temp = 0x80;
     LCDOUT |= (LCDSID + LCDSCK);
     LCDOUT &= ~(LCDCS + LCDRS);    //RS为低写入指令
     LCDDIR |= (LCDSID + LCDCS + LCDSCK + LCDRS);
     for (ui_i=0; ui_i<8; ui_i++)
     { 
         if ( (ui_command & ui_temp)!=0 )
         {
            LCDOUT |= LCDSID;   //写入1
         }
         else
         {
            LCDOUT &= ~LCDSID;  //写入0
         }
         LCDOUT &= ~LCDSCK;   
         LCDOUT |= LCDSCK;  //上升沿写入数据
         ui_temp >>= 1;
     }   
     LCDOUT |= LCDCS;
}

/*************************************************
//函数名称:WRTIEDATATOLCD()
//函数功能:向LCD写数据
//调用函数:无
//全局变量:
//输入:ui_data,准备写入的数据
//返回:无
//函数编写者:xpf
//函数测试者:            函数最后修改时间:
*************************************************/
void WRTIEDATATOLCD(uint ui_data) 
{
     uint ui_i; 
     uint ui_temp = 0x80;
     LCDOUT |= (LCDSID + LCDSCK + LCDRS); //RS为高写入数据
     LCDOUT &= ~LCDCS;
     LCDDIR |= (LCDSID + LCDCS + LCDSCK + LCDRS);
     for (ui_i=0; ui_i<8; ui_i++)
     { 
         if ( (ui_data & ui_temp) != 0 )
         {
            LCDOUT |= LCDSID;   //写入1
         }
         else
         {
            LCDOUT &= ~LCDSID;  //写入0 
         }
         LCDOUT &= ~LCDSCK;   
         LCDOUT |= LCDSCK;  //上升沿写入
         ui_temp >>= 1;
     }   
}

/*************************************************
//函数名称:CLRLCD()
//函数功能:清屏操作
//调用函数:WRTIECOMTOLCD
//全局变量:
//输入:ui_data,准备写入的数据
//返回:无
//函数编写者:xpf
//函数测试者:            函数最后修改时间:
*************************************************/
void CLRLCD(void)   //清屏
{
     uint ui_i,ui_j;
     WRTIECOMTOLCD(0x40);  //Set Display Start Line = com0
     for (ui_i=0; ui_i<8; ui_i++)
     {
         WRTIECOMTOLCD(0xB0+ComTable[ui_i]);   //Set Page Address
         WRTIECOMTOLCD(0x10);    //Set Column Address = 0
         WRTIECOMTOLCD(0x01);    //Colum from 1 -> 129 auto add
         for (ui_j=0; ui_j<128; ui_j++)
         {
            WRTIEDATATOLCD(0x00);
         }
     }
}

/*************************************************
//函数名称:InitLCD()
//函数功能:初始化LCD
//调用函数:WRTIECOMTOLCD
//全局变量:
//输入:
//返回:无
//函数编写者:xpf
//函数测试者:            函数最后修改时间:
*************************************************/
void InitLCD(void)    
{
        
    LCDRSTOUT &= ~LCDRST;
    LCDRSTDIR |= LCDRST;
    DELAYNUS(100);
    LCDRSTOUT |= LCDRST;
    DELAYNUS(10);
    WRTIECOMTOLCD(0xAE);	//Display OFF
    WRTIECOMTOLCD(0xA2);	//1/64 Duty 1/9 Bias
    WRTIECOMTOLCD(0xA0);	//ADC select  --> right
    WRTIECOMTOLCD(0xC0);	//com1 --> com64
    WRTIECOMTOLCD(0x2f);	//V0 regulator Rb/Ra=111B
    WRTIECOMTOLCD(0x81);	//Sets V0
    WRTIECOMTOLCD(0x24);	//对比度调节
    WRTIECOMTOLCD(0x24);	//voltage follower ON  regulator ON  booster ON
    WRTIECOMTOLCD(0xA6);	//Normal Display (not reverse dispplay)
    WRTIECOMTOLCD(0xA4);	//Entire Display Disable
    CLRLCD();                 //Clear LCD
    WRTIECOMTOLCD(0xAF);	//Display ON
    WRTIECOMTOLCD(0x40);	//Set Display Start Line = com0
    WRTIECOMTOLCD(0xB0);	//Set Page Address = 0
    WRTIECOMTOLCD(0x10);	//Set Column Address 4 higher bits = 0
    WRTIECOMTOLCD(0x01);	//Set Column Address 4 lower bits = 1 , from IC SEG1 -> SEG129
}

/*************************************************
//函数名称:WRITEBMPTOLCD()
//函数功能:输出图像128X64
//调用函数:WRTIECOMTOLCD,WRTIEDATATOLCD
//全局变量:
//输入:
//返回:无
//函数编写者:xpf
//函数测试者:            函数最后修改时间:
*************************************************/
void WRITEBMPTOLCD(uchar *data)    //输出图像128X64
{
     uint ui_i,ui_j;
     WRTIECOMTOLCD(0x40);  //Set Display Start Line = com0
     for (ui_i=0; ui_i<8; ui_i++)
     {
         WRTIECOMTOLCD(0xB0+ComTable[ui_i]);   ////Set Page Address
         WRTIECOMTOLCD(0x10);    //Set Column Address = 0
         WRTIECOMTOLCD(0x01);    //Colum from 1 -> 129 auto add
         for (ui_j=0; ui_j<128; ui_j++)
         {
            WRTIEDATATOLCD(data[ui_i*128+ui_j]);
         }
     }
}

/*************************************************
//函数名称:WRITELINETOLCD()
//函数功能:指定的位置开始画线
//调用函数:WRTIECOMTOLCD,WRTIEDATATOLCD
//全局变量:
//输入:
//返回:无
//函数编写者:xpf
//函数测试者:            函数最后修改时间:
*************************************************/
void WRITELINETOLCD(int x,int y,uchar uc_nbyte)    //画线
{
     uint ui_j;
     if (y > 6)   //确保y在0,6之间
     {
        y = 6;
     }
     else if (y < 0)
     {
        y = 0;  
     }   
     WRTIECOMTOLCD(0x40);  //Set Display Start Line = com0
     //for (ui_i=0; ui_i<8; ui_i++)
     //{
         WRTIECOMTOLCD(0xB0+ComTable[y]);   ////Set Page Address
         WRTIECOMTOLCD(0x10+((x&0xf0)>>4));    //Set Column Address High Byte
         WRTIECOMTOLCD(0x01+(x&0xf));        //Low Byte  Colum from 1 -> 129 auto add
         for (ui_j=0; ui_j<uc_nbyte; ui_j++)
         {
            WRTIEDATATOLCD(0x01);
         }
     //}
}


/*************************************************
//函数名称:WRTIECHARTOLCD()
//函数功能:初始化LCD
//调用函数:WRTIECOMTOLCD,WRTIEDATATOLCD
//全局变量:
//输入:
//返回:无
//函数编写者:xpf
//函数测试者:            函数最后修改时间:
*************************************************/
void WRTIECHARTOLCD(int x,int y,uchar *data)  //向液晶指定位置输出字符
//输入参数:y为 page位置,page 在0到7之间,在这里y在0,6之间
//          x为点阵位置
{
    uint ui_j;
    if (y > 6)   //确保y在0,6之间
    {
        y = 6;
    }
    else if (y < 0)
    {
        y = 0;  
    }
    
    WRTIECOMTOLCD(0x40);  //Set Display Start Line = com0    
    WRTIECOMTOLCD(0xB0+ComTable[y]);  //Set Page Address    //写字的上半部
    WRTIECOMTOLCD(0x10+((x&0xf0)>>4));    //Set Column Address High Byte
    WRTIECOMTOLCD(0x01+(x&0xf));        //Low Byte  Colum from 1 -> 129 auto add
    for (ui_j=0; ui_j<8; ui_j++)
    {
        WRTIEDATATOLCD(data[ui_j]);
    }
    
    WRTIECOMTOLCD(0xB0+ComTable[y+1]);  //Set Page Address    //写字的下半部
    WRTIECOMTOLCD(0x10+((x&0xf0)>>4));  //Set Column Address High Byte
    WRTIECOMTOLCD(0x01+(x&0xf));        //Low Byte  Colum from 1 -> 129 auto add
    for (ui_j=0; ui_j<8; ui_j++)
    {
        WRTIEDATATOLCD( data[ui_j+8] );
    }   
}

/*************************************************
//函数名称:WRTIEWORDTOLCD()
//函数功能:向液晶指定位置输出汉字
//调用函数:WRTIECOMTOLCD,WRTIEDATATOLCD
//全局变量:
//输入:
//返回:无
//函数编写者:xpf
//函数测试者:            函数最后修改时间:
*************************************************/
void WRTIEWORDTOLCD(int x,int y,uchar *data)  //向液晶指定位置输出汉字
//输入参数:y page位置
//          x 点阵位置
{
     uint ui_j;
     WRTIECOMTOLCD(0x40);  //Set Display Start Line = com0
     
     WRTIECOMTOLCD(0xB0+ComTable[y]);  //Set Page Address    //写字的上半部
     WRTIECOMTOLCD(0x10+((x&0xf0)>>4));   //Set Column Address High Byte
     WRTIECOMTOLCD(0x01+(x&0xf));        //Low Byte  Colum from 1 -> 129 auto add
     for (ui_j=0; ui_j<16; ui_j++)
     {
        WRTIEDATATOLCD(data[ui_j]);
     }
     
     WRTIECOMTOLCD(0xB0+ComTable[y+1]);  //Set Page Address    //写字的下半部
     WRTIECOMTOLCD(0x10+((x&0xf0)>>4));    //Set Column Address High Byte
     WRTIECOMTOLCD(0x01+(x&0xf));        //Low Byte  Colum from 1 -> 129 auto add
     for (ui_j=0; ui_j<16; ui_j++)
     {
        WRTIEDATATOLCD(data[ui_j+16]);
     }   
}

/*************************************************
//函数名称:Circle()
//函数功能:
//调用函数:WRTIECOMTOLCD
//全局变量:
//输入:ui_data,准备写入的数据
//返回:无
//函数编写者:xpf
//函数测试者:            函数最后修改时间:
*************************************************/
void Circle(int x,int y,uchar *data,uchar uc_byte,int i_temp)   //清屏
{
     uint ui_i,ui_j;
     WRTIECOMTOLCD(0x40);  //Set Display Start Line = com0
     //
            
     //添加
     WRTIECOMTOLCD(0xB0+ComTable[y]);  //Set Page Address    //写字的上半部
     WRTIECOMTOLCD(0x10);   //Set Column Address High Byte
     WRTIECOMTOLCD(0x01);        //Low Byte  Colum from 1 -> 129 auto add   
     for (ui_j=0; ui_j<i_temp; ui_j++)
     {
        WRTIEDATATOLCD(0x00);
     }
    //添加

     //
     WRTIECOMTOLCD(0xB0+ComTable[y]);  //Set Page Address    //写字的上半部
     WRTIECOMTOLCD(0x10+((x&0xf0)>>4));   //Set Column Address High Byte
     WRTIECOMTOLCD(0x01+(x&0xf));        //Low Byte  Colum from 1 -> 129 auto add
     for(ui_i=0 ; ui_i< uc_byte; ui_i++)
     {
         for (ui_j=0; ui_j<16; ui_j++)
         {
            if( (i_temp + ui_i*ui_j)<112 )
            {
                WRTIEDATATOLCD(data[ui_j+16*2*ui_i]);
            }
         }
     }
     
     //添加
     WRTIECOMTOLCD(0xB0+ComTable[y+1]);  //Set Page Address    //写字的上半部
     WRTIECOMTOLCD(0x10);   //Set Column Address High Byte
     WRTIECOMTOLCD(0x01);        //Low Byte  Colum from 1 -> 129 auto add 
 
      for (ui_j=0; ui_j<i_temp; ui_j++)
     {
        WRTIEDATATOLCD(0x00);
     }     
     //添加
     
      
     WRTIECOMTOLCD(0xB0+ComTable[y+1]);  //Set Page Address    //写字的下半部
     WRTIECOMTOLCD(0x10+((x&0xf0)>>4));    //Set Column Address High Byte
     WRTIECOMTOLCD(0x01+(x&0xf));        //Low Byte  Colum from 1 -> 129 auto add
     
     for(ui_i=0 ; ui_i< uc_byte; ui_i++)
     {
         for (ui_j=0; ui_j<16; ui_j++)
         {
             if( (i_temp + ui_i*ui_j)<112 )
             {
                WRTIEDATATOLCD(data[ui_j+16*(2*ui_i+1)]);
             }
         }   
     }
}

///

void CLRPRELCD(int x,int y,int i_byte)   //清屏
{
     uint ui_j;
     WRTIECOMTOLCD(0x40);  //Set Display Start Line = com0
     //
     WRTIECOMTOLCD(0xB0+ComTable[y]);  //Set Page Address    //写字的上半部
     WRTIECOMTOLCD(0x10+((x&0xf0)>>4));   //Set Column Address High Byte
     WRTIECOMTOLCD(0x01+(x&0xf));        //Low Byte  Colum from 1 -> 129 auto add 
    //     

     for (ui_j=0; ui_j<i_byte; ui_j++)
     {
        WRTIEDATATOLCD(0x00);
     }

     
     WRTIECOMTOLCD(0xB0+ComTable[y+1]);  //Set Page Address    //写字的下半部
     WRTIECOMTOLCD(0x10+((x&0xf0)>>4));    //Set Column Address High Byte
     WRTIECOMTOLCD(0x01+(x&0xf));        //Low Byte  Colum from 1 -> 129 auto add
     for (ui_j=0; ui_j<i_byte; ui_j++)
     {
        WRTIEDATATOLCD(0x00);
     }   
}

⌨️ 快捷键说明

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