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

📄 lcm_display.c

📁 1.5" CSTN LCD,使用Sitronix ST7687驱动IC,4线串口。程序包括LCD Driver初始化
💻 C
字号:
//************************************************************
#include "IO_Define.h"
#include "Constant.h"
#include "LCM_Display.h"
#include "ascii.h"
#include "delay.h"
#include <INTRINS.H>
#include <STRING.H>
#include <MATH.H>
/***************************************************************************
** Function Name        : Write_CMD
** Function Description : Send Command
** Input Parameter      : Command Byte
** Return Value         : No
****************************************************************************/
void Write_CMD(unsigned char data CmdData)
{
    unsigned char i,temp;
	
    SFRPAGE = CONFIG_PAGE;
	
	pLCD_A0=1;
    pLCD_CS=0;
	temp = CmdData;
	
	for(i=0;i<7;i++)
	{
	    if(temp&0x80)
			pLCD_SDA=1;
		else
		  pLCD_SDA=0;
		  _nop_();
		  _nop_();
	    pLCD_SCK=0;
		_nop_();
		_nop_();
		_nop_();
		_nop_(); 
	    pLCD_SCK=1;
		temp = temp<<1;
	}

	pLCD_A0=0;
	if(temp&0x80)
	pLCD_SDA=1;
	else
	  pLCD_SDA=0;
	_nop_();
	_nop_(); 
	pLCD_SCK=0;
	_nop_();
	_nop_();
	_nop_();
	_nop_(); 
	pLCD_SCK=1;
	_nop_();
	_nop_(); 
	pLCD_CS=1;

}

/***************************************************************************
** Function Name        : Write_DAT
** Function Description : Send Data
** Input Parameter      : H_Byte, L_Byte
** Return Value         : No
****************************************************************************/
void Write_CMD_Data(unsigned char data CmdData)
{
    unsigned char i,temp;
    SFRPAGE = CONFIG_PAGE;
	
	pLCD_A0=0;
    pLCD_CS=0;
	temp = CmdData;
	for(i=0;i<7;i++)
	  {
	    if(temp&0x80)
		  pLCD_SDA=1;
		else
		  pLCD_SDA=0;
		_nop_();
		_nop_();
	    pLCD_SCK=0;
		_nop_();
		_nop_();
		_nop_();
		_nop_(); 
	    pLCD_SCK=1;
		temp = temp<<1;
	  }
	pLCD_A0=1;
	if(temp&0x80)
	   pLCD_SDA=1;
	else
	   pLCD_SDA=0;
	_nop_();
	_nop_();
	pLCD_SCK=0;
	_nop_();
	_nop_();
	_nop_();
	_nop_(); 
	pLCD_SCK=1;
	_nop_();
	_nop_();
    pLCD_CS=1;
	
}

/***************************************************************************
** Function Name        : Write_RAM_Data()
** Function Description : Write data to GRAM
** Input Parameter      : H_Byte, L_Byte
** Return Value         : No
****************************************************************************/
void Write_RAM_Data(unsigned char data HigherByte, unsigned char LowerByte)
{
    unsigned char i,temp;
    
	SFRPAGE = CONFIG_PAGE;
	
	pLCD_A0=1;
    pLCD_CS=0;
	temp = HigherByte;
	for(i=0;i<8;i++)
	  {
	    if(temp&0x80)
		  pLCD_SDA=1;
		else
		  pLCD_SDA=0;
		_nop_();
		_nop_();
	    pLCD_SCK=0;
		_nop_();
		_nop_(); 
	    pLCD_SCK=1;
		
		temp = temp<<1;
	  }
	  
	temp = LowerByte;
	for(i=0;i<8;i++)
	  {
	    if(temp&0x80)
		  pLCD_SDA=1;
		else
		  pLCD_SDA=0;
		_nop_();
		_nop_(); 
	    pLCD_SCK=0;
		_nop_();
		_nop_(); 
	    pLCD_SCK=1;
		
		temp = temp<<1;
	  }	
    pLCD_CS=1;
	
}

/***************************************************************************
** Function Name        : Read_DAT
** Function Description : DDRAM+1
** Input Parameter      : No
** Return Value         : No
****************************************************************************/
/*
void Read_Data(void)
{
  SFRPAGE = CONFIG_PAGE;  
  pLCD_HIG_BUS = 0x00;
  pLCD_LOW_BUS = 0xFF;

  pLCD_CS = 0;
  pLCD_WR = 1;
  pLCD_RS = 1;

  pLCD_RD = 0;  _nop_();  _nop_();
  pLCD_RD = 1;  _nop_();  _nop_();
                   
  pLCD_CS = 1;
}
*/
/***************************************************************************
** Function Name        : Driver_Initial
** Function Description : Driver Initial
** Input Parameter      : No
** Return Value         : No
****************************************************************************/
void Driver_Initial(void)
{
  Driver_Reset();

  Write_Cmd(0x01); 
  Delay(200); // Delay 100ms

  Write_Cmd(0xD7); 
  Write_CMD_Data(0x9F);

  Write_Cmd(0xE0); 
  Write_CMD_Data(0x00);
  Delay(110); // Delay 100ms

  Write_Cmd(0xfa); 
  Write_CMD_Data(0x01);
  Delay(110); // Delay 100ms

  Write_Cmd(0xE3); 
  Delay(110); // Delay 100ms

  Write_Cmd(0xE1); 
  Delay(20); // Delay 10ms

//************* ****************//  
  Write_Cmd(0x28); 

  Write_Cmd(0x11);
  Delay(110); // Delay 100ms 

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

  Write_Cmd(0xC0); 
  Write_CMD_Data(0x00); //08
  Write_CMD_Data(0x01);   

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

  Write_Cmd(0xC3); 
  Write_CMD_Data(0x03);

  Write_Cmd(0xC4); 
  Write_CMD_Data(0x07);  

  Write_Cmd(0xC5); 
  Write_CMD_Data(0x01);

  Write_Cmd(0xCB); 
  Write_CMD_Data(0x01);  

  Write_Cmd(0x36); 
  Write_CMD_Data(0x80);

  Write_Cmd(0xb5); //N_Line inverse
  Write_CMD_Data(0x89);


  Write_Cmd(0xD0); 
  Write_CMD_Data(0x1D);

  Write_Cmd(0xb7); 
  Write_CMD_Data(0x40);

  Write_Cmd(0x25); 
  Write_CMD_Data(0x3f);  

  Write_Cmd(0x3A); 
  Write_CMD_Data(0x05); 

  Write_Cmd(0xb0); 
  Write_CMD_Data(0x7f); 

  Write_Cmd(0xf0); 	//Frequency set
  Write_CMD_Data(0x07);
  Write_CMD_Data(0x0c);
  Write_CMD_Data(0x0c);
  Write_CMD_Data(0x15);

  Write_Cmd(0xf9); 
  Write_CMD_Data(0x00);
  Write_CMD_Data(0x05);
  Write_CMD_Data(0x08);
  Write_CMD_Data(0x0a);
  Write_CMD_Data(0x0c);
  Write_CMD_Data(0x0e);
  Write_CMD_Data(0x10);
  Write_CMD_Data(0x11);
  Write_CMD_Data(0x12);
  Write_CMD_Data(0x13);
  Write_CMD_Data(0x14);
  Write_CMD_Data(0x15);
  Write_CMD_Data(0x16);
  Write_CMD_Data(0x18);
  Write_CMD_Data(0x1a);
  Write_CMD_Data(0x1b);

  Write_Cmd(0xf4); 
  Write_CMD_Data(0x00);
  Write_CMD_Data(0x00);
  Write_CMD_Data(0x00);
  Write_CMD_Data(0x00);
  Write_CMD_Data(0x33);
  Write_CMD_Data(0x55);
  Write_CMD_Data(0x55);
  Write_CMD_Data(0x55);


  Write_Cmd(0x29);  

//  Write_Cmd(0x29);
}

/***************************************************************************
** Function Name        : Driver_Reset
** Function Description : Reset LCM
** Input Parameter      : No
** Return Value         : No
****************************************************************************/
void Driver_Reset(void)
{  
  pLCD_RST = 0;
  Delay(150);

  pLCD_RST = 1;
  Delay(150);
  pLCD_SCK=0;
}

/***************************************************************************
** Function Name        : Display_Off
** Function Description : Reset LCM
** Input Parameter      : No
** Return Value         : No
****************************************************************************/
void Display_Off(void)
{  
  Write_Cmd(0x28);  // display off
  Write_Cmd(0x10);  // Sleep in
    
}
/***************************************************************************
** Function Name        : Define_Window
** Function Description : Define Window
** Input Parameter      : X0, Y0, X1, Y1
** Return Value         : No
****************************************************************************/
void Define_Window(unsigned char data X0, unsigned char data Y0, unsigned char data X1, unsigned char data Y1)
{
	Write_Cmd(0x2A); 
	Write_CMD_Data(X0);
	Write_CMD_Data(X1);
	
	Write_Cmd(0x2B); 
	Write_CMD_Data(Y0);
	Write_CMD_Data(Y1);
}

/***************************************************************************
** Function Name        : Clear_Window
** Function Description : Clear Window
** Input Parameter      : X0, Y0, X1, Y1, Fill_Color
** Return Value         : No
****************************************************************************/
void LCD_Clear_Window(unsigned char data X0, unsigned char data Y0, 
                  unsigned char data width, unsigned char data height, unsigned int data Fill_Color) 
{
  unsigned char data i, j;
  unsigned char data Higher_Byte, Lower_Byte;

  Lower_Byte  = Fill_Color;
  Higher_Byte = Fill_Color >> 8;
  
  Define_Window(X0, Y0, X0+width-1, Y0+height-1);
  Write_Cmd(0x2c);
  
  for (i=0; i<height; i++)
     {
	   for (j=0; j<width; j++)
	     {
		  Write_RAM_Data(Higher_Byte, Lower_Byte);
		 }
	 }
}

/**********************************************************************
Name       :  Display_Char     
Parameters :  X0, Y0, font_color, print_char                   
Returns    :  No      
Description:  Display an ascii character on location specified by x y coordinate
**********************************************************************/
void Display_Char (unsigned char data X0, unsigned char data Y0, unsigned int data font_color, unsigned char print_char)
{
  unsigned int data Char_Address;
  unsigned int data x;
  unsigned char data j, m, CHAR;
  unsigned char data higher_byte, lower_byte;

  higher_byte = (font_color >>8) & 0xff;
  lower_byte  = font_color & 0xff; 
  x  = X0;
  Char_Address = (print_char - 32) * 7; //Get character start address in ascii table

  for (j=0; j<7; j++)
      {
        CHAR = ASCII_TABLE[Char_Address++];
	    for (m = 0; m<5; m++)
		   {
	         if ((CHAR & 0x80) == 0x80)
			    {
				 Define_Window (X0, Y0, X0, Y0);
                 Write_Cmd(0x2c);
                 Write_RAM_Data(higher_byte, lower_byte);
				}
             X0++;
             CHAR= CHAR<<1;
            }
		  X0 = x;
		  Y0++;
	  }
}
/***************************************************************************
** Function Name        : Display_String
** Function Description : Display String
** Input Parameter      : X0, Y0, Font_Color, *Print_String
** Return Value         : No
****************************************************************************/
void Display_String (unsigned char data X0, unsigned char data Y0, 
                     unsigned int data Font_Color, unsigned char *Print_String)
{
  unsigned char data i,j,m,CharLength, CHAR;
  unsigned int  data Char_Address;
  unsigned char data Higher_Byte, Lower_Byte;
  unsigned int data x,y;

  x = X0;
  y = Y0;
  
  Lower_Byte  = Font_Color & 0xFF;
  Higher_Byte = (Font_Color >>8) & 0xFF;
 

  CharLength = strlen(Print_String);  // Get string length

  for (i=0; i<CharLength; i++)
  {
    Char_Address = (Print_String[i] - 32) * 7; //Get character start address in ascii table

    for (j=0; j<7; j++)        // Font height is 7
    {
      CHAR = ASCII_TABLE[Char_Address++];
	  for (m = 0; m<5; m++)
      {
	    if((CHAR & 0x80) == 0x80)
         {
	      Define_Window (X0, Y0, X0, Y0);		  
          Write_Cmd(0x2c);
          Write_RAM_Data(Higher_Byte, Lower_Byte);
         }
        X0++;
        CHAR = CHAR<<1;
      }
	  X0 = x;
	  Y0++;
    }
	Y0 = y;
	x += 6;
	if(x > (x_end - 4))
	 {
	  x = x_start; //x_start+1;
      Y0 += 8;
	  if(Y0 > (y_end - 7))
		Y0 =y_start+1;
	}
	X0 = x;      
  }
}


/*F***********************************************************
** Function Name        : 
** Function Description : 
** Input Parameter      : 
** Return Value         : 
*************************************************************/
void Draw_Line(int data X0, int data Y0, int data X1, int data Y1, unsigned int data color)
{
 unsigned int data length1,length2;
 unsigned int data i;
// unsigned char byte1, byte2, byte3;
 int data  x, y;
// unsigned int data temp;

   if(X0==X1 && Y0==Y1)                             //if only have one point
     {
	//  temp = Y0;
	  Define_Window(X0, Y0, X1, Y1);
	  Write_Cmd(0x2c);
	  Write_RAM_Data(color>>8, color);
     }

   else                                           //line between two point
    {
     length1=abs(X1-X0)+1;
     length2=abs(Y1-Y0)+1;
     if(length1>length2)
       for(i=0;i<length1;i++)
         {
		  //x=X0 + i*(X1-X0)/length1;
		  x=X0 + i;
	      y=Y0+(Y1-Y0)*(x-X0)/(X1-X0);
	      Define_Window(x,y,x,y);
	      Write_Cmd(0x2c);
	      Write_RAM_Data(color>>8, color);
         }
     else
       for(i=0;i<length2;i++)
         {
		  //y=Y0+i*(Y1-Y0)/length2;
		  y=Y0+i;
		  x=X0+(X1-X0)*(y-Y0)/(Y1-Y0);
	      Define_Window(x,y,x,y);
	      Write_Cmd(0x2c);
	      Write_RAM_Data(color>>8, color);		  
         }
     }

}

/*F***********************************************************
** Function Name        : 
** Function Description : 
** Input Parameter      : 
** Return Value         : 
*************************************************************/
void Draw_Rectangle(unsigned char data X0, unsigned char data Y0, unsigned char data X1, unsigned char data Y1, unsigned int data color)
{
 Draw_Line(X0,Y0, X1, Y0, color);
 Draw_Line(X1,Y0, X1, Y1, color);
 Draw_Line(X0,Y0, X0, Y1, color);
 Draw_Line(X0,Y1, X1, Y1, color);
}

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

⌨️ 快捷键说明

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