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

📄 test1.c

📁 TFT彩色液晶屏的SPI接口的驱动程序
💻 C
字号:
/***************************************************************
* Drscription	: TFT Lcd demo
* Driver		: ILI9320 or compatible
* MCU			: stc89LE52 
* Voltage		: 3.3V
* Interface		: 16bits parallel interface
*
***************************************************************
*
* Author 		: zjq in ShenZhen 
* Data			: 04-06-08
* Email			: Crazyzjq@126.com
* History		: Spi works well,but it's so slow 
***************************************************************/

#include "stdio.h"
#include "reg51.h"

#define DATA_L P2
#define DATA_H P1

#define _SPI 0

//for SPI 
#ifdef _SPI
sbit  CS = P1^1;
sbit  DI = P1^4;
sbit  DO = P1^0;

sbit  CLK = P1^2;
sbit  RST = P1^3;
#else
//for 16bits
sbit  CS  = P3^3;
sbit  RST = P3^5;
sbit  RS  = P3^4;
#endif

/**************************************************************************
*
*   Just for SPI to write a byte to Lcd
*
***************************************************************************/
#ifdef _SPI
void LCD_WriteByte(unsigned char dat)
{
	unsigned char i;

    for(i=0;i<8;i++)
	{
		CLK = 0;
	    dat<<=1;
		DI = CY;
		CLK = 1;
    }
}
#endif

  #define START_BYTE  0x70 
  #define SET_INDEX   0x00 
  #define READ_STATUS 0x01 
  #define WRITE_REG   0x02 
  #define READ_REG    0x03 


void  LCD_WriteRegIndex(unsigned char index) /* Select GRAM Reg */ 

{
	CS = 0;
    LCD_WriteByte(START_BYTE|SET_INDEX);//for register index
    LCD_WriteByte(0);	 //lower byte first
    LCD_WriteByte(index);
	CS = 1;

}

/****************************************************************************
*
*	Write a byte to register index and the parameter to 
*	the register
*
****************************************************************************/
void LCD_WriteReg(unsigned char index,unsigned int val)
{

#ifdef _SPI
    LCD_WriteRegIndex(index);

	CS = 0;
    LCD_WriteByte(START_BYTE|WRITE_REG);
    LCD_WriteByte(val>>8);
    LCD_WriteByte(val&0xff);
    CS = 1;

#else
	CS = 0;
	RS = 0;
	DATA_L = index; //low
	DATA_H = 0;
	WR = 0;
	WR = 1;

    RS = 1;
	DATA_L = (unsigned char)val; //low
	DATA_H = (unsigned char )(val>>8);
    WR = 0;
	WR = 1;
	CS = 1;
#endif
}

/****************************************************************************
*
* command for Write data 
*
*****************************************************************************/
void Lcd_Write_Start()
{
#ifdef _SPI
      LCD_WriteRegIndex(0x22); /* Select GRAM Reg */ 
      CS = 0; 
      LCD_WriteByte(START_BYTE|WRITE_REG);
#else
    CS = 0;
	RS = 0;
	DATA_L = 0x22;
	DATA_H = 0x00;
    WR = 0;
	WR = 1;
	RS = 1;
#endif
}

/****************************************************************************
*
*  Write one word to Lcd 
*
*****************************************************************************/
void LCD_WriteData(unsigned int val)
{

#ifdef _SPI
//  Lcd_Write_Start();
    LCD_WriteByte(val>>8);
    LCD_WriteByte(val&0xff);
#else
	DATA_L = (unsigned char)val;
	DATA_H = (unsigned char)(val>>8);

	WR = 0;
	WR = 1;
#endif
}

void Lcd_Write_End()
{
	CS = 1;
}


void delay(unsigned int n)
{
	unsigned int i;

	for(i=0;i<n;i++);
}

void Lcd_Init()
{
	RST = 0;
	delay(2000);
    RST = 1;
	delay(2000);

	LCD_WriteReg(0xe5, 0x8000); /* Set the internal vcore voltage */ 
	LCD_WriteReg(0x00, 0x0001); /* Start internal OSC. */ 
	LCD_WriteReg(0x01, 0x0100); /* set SS and SM bit */ 
	LCD_WriteReg(0x02, 0x0700); /* set 1 line inversion */ 
	LCD_WriteReg(0x03, 0x1230);  /* set GRAM write direction */ 

	LCD_WriteReg(0x04, 0x0000); /* Resize register */ 
 	LCD_WriteReg(0x08, 0x0202); /* set the back porch and front porch */ 
  	LCD_WriteReg(0x09, 0x0000); /* set non-display area refresh cycle ISC[3:0] */ 
  	LCD_WriteReg(0x10, 0x0000); /* FMARK function */ 
  
	LCD_WriteReg(0x0c, 0x0000); /* RGB interface setting */ 
	LCD_WriteReg(0x0d, 0x0000); /* Frame marker Position */ 
	LCD_WriteReg(0x0f, 0x0000); /* RGB interface polarity */ 

/* Adjust the Gamma Curve ----------------------------------------------------*/ 
	LCD_WriteReg(0x50, 0x0000);
	LCD_WriteReg(0x51, 0x00ef);
	LCD_WriteReg(0x52, 0x0000);
	LCD_WriteReg(0x53, 0x013f);
	LCD_WriteReg(0x60, 0x2700);
	LCD_WriteReg(0x61, 0x0001);
	LCD_WriteReg(0x6a, 0x0000);

/* Set GRAM area -------------------------------------------------------------*/ 
	LCD_WriteReg(0x80, 0x0000);/* Horizontal GRAM Start Address */
	LCD_WriteReg(0x81, 0x0000);/* Horizontal GRAM End Address */ 
	LCD_WriteReg(0x82, 0x0000);/* Vertical GRAM Start Address */
	LCD_WriteReg(0x83, 0x0000);/* Vertical GRAM End Address */ 

/* Power On sequence ---------------------------------------------------------*/ 
	LCD_WriteReg(0x10, 0x17b0);
	LCD_WriteReg(0x11, 0x0004);
	delay(50);
	LCD_WriteReg(0x12, 0x013e);
	delay(50);
	LCD_WriteReg(0x13, 0x1f00);/* Frame marker Position */
	LCD_WriteReg(0x29, 0x000f);
	delay(50);

	LCD_WriteReg(0x32, 0x0000); /* GRAM horizontal Address */ 
    LCD_WriteReg(0x33, 0x0000); /* GRAM Vertical Address */ 

    LCD_WriteReg(0x07, 0x0173); /* 262K color and display ON */ 

}

void Lcd_test()
{
	unsigned int i,j;
	Lcd_Write_Start();
	for(i=0;i<320;i++)
		for(j=0;j<240;j++)
		{
			if(i>279)LCD_WriteData(0x0000);
			else if(i>239)LCD_WriteData(0x001f);
			else if(i>199)LCD_WriteData(0x07e0);
			else if(i>159)LCD_WriteData(0x07ff);
			else if(i>119)LCD_WriteData(0xf800);
			else if(i>79)LCD_WriteData(0xf81f);
			else if(i>39)LCD_WriteData(0xffe0);
			else LCD_WriteData(0xffff);
		}
	Lcd_Write_End();
}


void main()
{
	Lcd_Init();
	Lcd_test();
	while(1);
}

⌨️ 快捷键说明

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