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

📄 lcd.c

📁 128*64点阵的LCD LCD控制器 KS0107 开发平台 Luminary 的 LM3S8962 ,开发软件IAR 4.42. 该程序包括对LCD显示全角汉字,半角英文,滚动显示等功能.
💻 C
📖 第 1 页 / 共 2 页
字号:
/**--------------------------------File Info--------------------------------------------------------
**
** File name:               lcd.c
** Latest modified Date:    2008.05.23
** Latest Version:          v1.0
** Descriptions:            The lcd draw function
**
**--------------------------------------------------------------------------------------------------------
** Modified by:              X
** Modified date:            2008.05.22
** Version:                 v1.1
** Descriptions:            The original version is Running In 51 System 
**
*********************************************************************************************************/

#include  <hw_types.h>
#include  <hw_memmap.h>
#include  <hw_sysctl.h>
#include  <hw_gpio.h>
#include  <sysctl.h>
#include  <gpio.h>
#include  "lcd.h"

/////////////////////// 延时常数宏定义 ///////////////////////////

#define   NOP      for(int i=0;i<80;i++)

/*****************************  定义LCD数据接口  **********************************/

#define   LCD_DATA_PORT               SYSCTL_PERIPH_GPIOA   
#define   LCD_CTL_PORT                SYSCTL_PERIPH_GPIOB

#define   LCD_DATA_ADR                GPIO_PORTA_BASE
#define   LCD_CTL_ADR                 GPIO_PORTB_BASE

#define	  LCD_RS_PIN                  GPIO_PIN_0   
#define	  LCD_RW_PIN	              GPIO_PIN_1 
#define	  LCD_E_PIN	              GPIO_PIN_2 
#define	  CSA_PIN	              GPIO_PIN_3 
#define	  CSB_PIN		      GPIO_PIN_4 

#define   GPIO_ALL                    0x000000ff  // All of GPIO Select 

#define   PinSetBit(x)         GPIOPinWrite(LCD_CTL_ADR,x,x)
#define   PinClrBit(x)         GPIOPinWrite(LCD_CTL_ADR,x,~x)

////////////////////////////// 常用操作命令和参数定义 ////////////////////////////////

#define	 DISPON			0x3f			//	开启显示
#define	 DISPOFF		0x3e			//	关闭显示
#define	 DISPFIRST		0xc0			//	显示起始行定义
#define	 SETX			0x40			//	X定位设定指令(页)(0-127)
#define	 SETY			0xb8			//	Y定位设定指令(列)(0-7)
#define	 LCD_Busy		0x80			//	LCM忙判断位

///////////////////////// 显示分区边界位置 ////////////////////////////////////////

#define	 MODL		0x00		//  左区
#define	 MODR		0x40		//  左区和右区分界
#define	 LCMLIMIT	0x80		//  显示区的右边界

///////////////////////////// 全局变量定义 ////////////////////////////////////////

uchar ucCol,ucRow,ucShowData;			//  Col = x (0-127), Row = y (0-7)
uchar xy;					//  画线方向标志:1 = 水平 0 = 垂直 
uchar Base=0;                                     //  滚屏显示中使用  Base为显示的起始行 

///////////////////////// 自定义字符数组列表 ////////////////////////////////

const uchar _ASCII[];				//	ASCII常规字符点阵码表

/***************************** 中英文字符串显示函数 **********************************/

void PutEnStr(uchar *puts,uchar i)
{

  uchar j,ucDat;

  for (j=0;j<i;j++)
  {
	ucDat = puts[j];

	if(!(ucDat&0x80))
          Putedot(ucDat-0x20);	  //  Ascii码表从0x20开始 之前的20H是控制码
  }
}

void PutChStr(const uchar *puts,uchar i)
{

  uchar j;

  for (j=0;j<i;j++)
  {
    Putcdot(puts,j&0x7f);      //  只保留低7位
  }
}

/*********************************半角字符点阵码数据输出********************************/

void Putedot(uchar Order)
{
  uchar i,bakerx,bakery;		
  uint index;					 //	偏移量

  bakerx = ucCol;					//      暂存x,y坐标,已备下半个字符使用
  bakery = ucRow;
  index=Order * 0x10;				//	半角字符,每个字符16字节
                                	        //	上半个字符输出,8列
  for(i=0;i<8;i++)
  {   
    ucShowData = _ASCII[index];		              //	取点阵码,ASCII码数组
    Wrdata(ucShowData);			      //	写输出一字节数据
    index++;
    ucCol++;
    if (ucCol==LCMLIMIT)
    {
      ucCol=0;ucRow++;ucRow++;};	              //	下一列,如果列越界换行
      if (ucRow>7) 
        ucRow=0;			            //	        如果行越界,返回首行
    }			                    //	        上半个字符输出结束
    ucCol = bakerx;			    //	        列对齐
    ucRow = bakery+1;			    //	        指向下半个字符行
                                	    //	        下半个字符输出,8列
    for(i=0;i<8;i++)
    {
      ucShowData = _ASCII[index];			    //	        取点阵码
      Wrdata(ucShowData);			    //	        写输出一字节数据
      index++;
      ucCol++;
      if (ucCol==LCMLIMIT)
      { 
        ucCol=0;
        ucRow=ucRow+2;
      };		                    //	        下一列,如果列越界换行
      if (ucRow>7)
        ucRow=1;				//	如果行越界,返回首行
    }					//	下半个字符输出结束
    ucRow=bakery;	
}					//	整个字符输出结束

/*********************************全角字符点阵码数据输出********************************/

void Putcdot(const uchar * ChDat,uchar Order)
{
    uchar i,bakerx,bakery;			
    uint index;					//	偏移量
	
    bakerx = ucCol;				//	暂存x,y坐标,已备下半个字符使用*/
    bakery = ucRow;
    index=Order * 0x20;				//	每个字符32字节*/
    	                            	        //	上半个字符输出,16列*/
    for(i=0;i<16;i++)
    {
      Wrdata(ChDat[index]);			
      index++;
      ucCol++;
      if(ucCol==LCMLIMIT)
      {
        ucCol=0;
        ucRow++;
        ucRow++;
      }	
      if (ucRow>6) ucRow=0;       	
    }					           
	                                   
    ucCol = bakerx;
    ucRow = bakery+1;
    for(i=0;i<16;i++)				
    {
      Wrdata(ChDat[index]);
      index++;
      ucCol++;
      if (ucCol==LCMLIMIT)
      {
        ucCol=0;
        ucRow++;
        ucRow++;
      }	
      if(ucRow>7)
        ucRow=1;	
    }					
    ucRow = bakery;
}						

/************************************清屏,全屏幕清零***********************************/

void LCD_Cls( void )
{
    for(ucRow=0;ucRow<8;ucRow++)
      for(ucCol=0;ucCol<LCMLIMIT;ucCol++)
         Wrdata(0);
}

/*************************************数据写输出 **************************************/

void Wrdata(uchar WrDat)
{
    Locatexy();				//	坐标定位,返回时保留分区状态不变
    PinSetBit(LCD_RS_PIN);				//	数据输出
    PinClrBit(LCD_RW_PIN);				//	写输出
    BaseWrData(WrDat);			
    PinSetBit(LCD_E_PIN);	
    NOP;
    PinClrBit(LCD_E_PIN);
}

/***************************从液晶片上读数据,保留在全局变量中***************************/

uchar Rddata( void )
{
    uchar RdDat;
         
    Locatexy();				        //	坐标定位,返回时保留分区状态不变

    PinSetBit(LCD_RS_PIN);			//	数据
    PinSetBit(LCD_RW_PIN);			//	读数据
    PinSetBit(LCD_E_PIN);			
    NOP;
    RdDat=BaseRdData();		      //	虚读一次
    PinClrBit(LCD_E_PIN);
    Locatexy();			//	坐标定位,返回时保留分区状态不变
    
    PinSetBit(LCD_RS_PIN);			//	数据
    PinSetBit(LCD_RW_PIN);			//	读数据
    PinSetBit(LCD_E_PIN);	
    NOP;
    RdDat=BaseRdData();		//	从数据口读数据,真读
    PinClrBit(LCD_E_PIN);
    
    return RdDat;
}

/*********************************命令输出到左区控制口*********************************/

void WrcmdL(uchar WrDat)
{
    LCD_BusyL();				//	确定分区,返回时保留分区状态不变
    PinClrBit(LCD_RS_PIN);				//命令操作
    PinClrBit(LCD_RW_PIN);			
    BaseWrData(WrDat);			
    PinSetBit(LCD_E_PIN);
    NOP;
    PinClrBit(LCD_E_PIN);	
}


/*********************************命令输出到右区控制口********************************/

void WrcmdR(uchar WrDat)
{
    LCD_BusyR();			
    PinClrBit(LCD_RS_PIN);			   
    PinClrBit(LCD_RW_PIN);			
    BaseWrData(WrDat);			
    PinSetBit(LCD_E_PIN);
    NOP;
    PinClrBit(LCD_E_PIN);
}

/************************分区操作允许等待,返回时保留分区选择状态*************************/

void LCD_BusyL(void)
{
    PinClrBit(CSA_PIN);
    PinSetBit(CSB_PIN);				//	选择左区
    Wtcom();					//	等待使能
}

/************************************************************************************/

void LCD_BusyR(void)
{
    PinSetBit(CSA_PIN);
    PinClrBit(CSB_PIN);			//	选择右区
    Wtcom();				//	等待使能
}

/************************************************************************************/

void Wtcom(void)
{
    uchar RdDat;
    
    PinClrBit(LCD_RS_PIN);				
    PinSetBit(LCD_RW_PIN);		
    RdDat=BaseRdData();
    PinSetBit(LCD_E_PIN);
    while(RdDat & LCD_Busy)
    {
      RdDat=BaseRdData();                       //  判断等待位状态
    }                      
    PinClrBit(LCD_E_PIN);
}

/******************根据设定的坐标数据,定位LCM上的下一个操作单元位置******************/

void Locatexy(void)
{

    uchar  x,y;

    switch (ucCol&0xc0)			 // 根据列地址判断显示所在的区(左/右)			
    {								
      case 0    : LCD_BusyL();break;	
      case 0x40 : LCD_BusyR();break;	
      default   : break; 
    }

    x = ucCol&0x3F|SETX;			
    y = ucRow&0x07|SETY;			
    Wtcom();						
    PinClrBit(LCD_RS_PIN);					
    PinClrBit(LCD_RW_PIN);				
    BaseWrData(y);	  		//	写入列地址(Y)  			
    PinSetBit(LCD_E_PIN);
    PinClrBit(LCD_E_PIN);
    Wtcom();					
    PinClrBit(LCD_RS_PIN);					
    PinClrBit(LCD_RW_PIN);				
    BaseWrData(x);		    //	写入行地址(X)		
    PinSetBit(LCD_E_PIN);
    PinClrBit(LCD_E_PIN);
}

/***************************画线,只提供X或Y方向的,不支持斜线*************************/

void LineTo(uchar length)
{
    uchar xs,ys;
    if (xy)
    { 
      ys = ucCol;
      for (xs=0;xs<length;xs++)
      {
          ucCol = ys + xs;
          Point();

⌨️ 快捷键说明

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