当前位置: 首页 > 工具软件 > ili 一例 > 使用案例 >

基于STM32F407的ILI9341 软SPI驱动 Keil 篇(兼容正点原子)

皇甫高阳
2023-12-01

基于STM32F407的ILI9341 软SPI驱动 Keil 篇(兼容正点原子)

最近普中的老ILI9325显示屏光荣下岗,网淘到普中转原子哥系列并口屏无法点亮,查询原理图发现背光显示(BLK)引脚不兼容,比较郁闷只能参考资料自己写驱动代码,做了些小改动尽量兼容原子哥其他硬件。

首先,先计算刷新频率:软件模拟SPI速率100M,使用320240分辨率彩色显示屏,16位RGB565;理论最高刷新频率=100M/(320240*16)≈20HZ

核心IO驱动程序如下:

STM32_模拟SPI读写一个字节:

void  SPI_WriteData(u8 Data)
{
	unsigned char i=0;
	for(i=8;i>0;i--)
	{
		if(Data&0x80)	
	  LCD_SDA_SET; 
      else LCD_SDA_CLR;
      LCD_SCL_CLR;       
      LCD_SCL_SET;
      Data<<=1; 
	}
}

STM32_模拟SPI读写指令:

void Lcd_WriteIndex(u8 Index)
{
   LCD_CS_CLR;
   LCD_RS_CLR;  
   SPI_WriteData(Index);
   LCD_CS_SET;
}

STM32_模拟SPI读写数据:

void Lcd_WriteData(u8 Data)
{
   
   LCD_CS_CLR;
   LCD_RS_SET; 
   SPI_WriteData(Data);
   LCD_CS_SET; 
}

STM32_模拟SPI写一个16位数据(主要填充色点颜色):

void LCD_WriteData_16Bit(uint16_t Data)
{
   LCD_CS_CLR;
   LCD_RS_SET;
	 SPI_WriteData(Data>>8); 	
	 SPI_WriteData(Data&0xFF); 	
   LCD_CS_SET; 
}

下面以原子哥的汉子显示为例(Lcd_Driver.c及Lcd_Driver.h) Lcd_Driver函数如下:

#include "Lcd_Driver.h"
#include "font.h" 
#include "delay.h"

//±¾²âÊÔ³ÌÐòʹÓõÄÊÇÄ£ÄâSPI½Ó¿ÚÇý¶¯
//¿É×ÔÓɸü¸Ä½Ó¿ÚIOÅäÖã¬Ê¹ÓÃÈÎÒâ×îÉÙ4 IO¼´¿ÉÍê³É±¾¿îÒº¾§Çý¶¯ÏÔʾ
/******************************************************************************
½Ó¿Ú¶¨ÒåÔÚLcd_Driver.hÄÚ¶¨Ò壬Çë¸ù¾Ý½ÓÏßÐ޸IJ¢ÐÞ¸ÄÏàÓ¦IO³õʼ»¯LCD_GPIO_Init()


#define LCD_BLK         //PB1    	  //±³¹â¿ØÖÆ           //FSMC D9   PE12
#define LCD_RS         	//PC5       //ÃüÁîÊý¾ÝÇл»       //FSMC D12  PE15
#define LCD_CS        	//PB12      //Ƭѡ               //FSMC D14  PD9
#define LCD_SDO/RST     //PB14      //¸´Î»               //FSMC D13  PD8 
#define LCD_SCL        	//PB13      //ʱÖÓÏß             //FSMC D10  PE13
#define LCD_SDA        	//PB15      //Êý¾ÝÏß             //FSMC D11  PE14 

#define LCD_MISO                    //FSMC 8  PE11 

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

//LCDµÄ»­±ÊÑÕÉ«ºÍ±³¾°É«	   
u16 POINT_COLOR=0x0000;	//»­±ÊÑÕÉ«
u16 BACK_COLOR=0xFFFF;  //±³¾°É« 

//¹ÜÀíLCDÖØÒª²ÎÊý
//ĬÈÏΪÊúÆÁ
_lcd_dev lcddev;


//Òº¾§IO³õʼ»¯ÅäÖÃ
void LCD_GPIO_Init(void)
{

	GPIO_InitTypeDef  GPIO_InitStructure;
	      
	RCC_AHB1PeriphClockCmd( RCC_AHB1Periph_GPIOD | RCC_AHB1Periph_GPIOE ,ENABLE);
	
	
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8 | GPIO_Pin_9;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;//ÆÕͨÊä³öģʽ
  GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;//ÍÆÍìÊä³ö
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;//100MHz
  GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;//ÉÏÀ­
	GPIO_Init(GPIOD, &GPIO_InitStructure);

	GPIO_InitStructure.GPIO_Pin =   GPIO_Pin_12 | GPIO_Pin_13 | GPIO_Pin_14 | GPIO_Pin_15 ;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;//ÆÕͨÊä³öģʽ
  GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;//ÍÆÍìÊä³ö
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;//100MHz
  GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;//ÉÏÀ­
	GPIO_Init(GPIOE, &GPIO_InitStructure);
	
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN;//ÆÕͨÊäÈëģʽ
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;//100M
  GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;//ÉÏÀ­
  GPIO_Init(GPIOE, &GPIO_InitStructure);      //³õʼ»¯GPIOE11
	
	LCD_BLK_SET;//¿ªÆô±³¹â
      
}


/****************************************************************************
* Ãû    ³Æ£ºvoid  SPIv_WriteData(u8 Data)
* ¹¦    ÄÜ£ºSTM32_Ä£ÄâSPIдһ¸ö×Ö½ÚÊý¾Ýµ×²ãº¯Êý
* Èë¿Ú²ÎÊý£ºData
* ³ö¿Ú²ÎÊý£ºÎÞ
* ˵    Ã÷£ºSTM32_Ä£ÄâSPI¶Áдһ¸ö×Ö½ÚÊý¾Ýµ×²ãº¯Êý
****************************************************************************/
void  SPI_WriteData(u8 Data)
{
	unsigned char i=0;
	for(i=8;i>0;i--)
	{
		if(Data&0x80)	
	  LCD_SDA_SET; //Êä³öÊý¾Ý
      else LCD_SDA_CLR;
      LCD_SCL_CLR;       
      LCD_SCL_SET;
      Data<<=1; 
	}
}


/****************************************************************************
* ¶ÁLCDÊý¾Ý
* ·µ»ØÖµ:¶Áµ½µÄÖµ
* Èë¿Ú²ÎÊý£ºdat     ¼Ä´æÆ÷Êý¾Ý
* ³ö¿Ú²ÎÊý£ºÎÞ
* ˵    Ã÷£ºÏò¿ØÖÆÆ÷Ö¸¶¨µØַдÈëÊý¾Ý£¬µ÷ÓÃÇ°ÐèÏÈд¼Ä´æÆ÷µØÖ·£¬ÄÚ²¿º¯Êý
* µ÷Ó÷½·¨:LCD_RD_DATA()
****************************************************************************/

u16 LCD_RD_DATA(void)
{
	u16 Data;			     //u16
	unsigned char i=0;
	
	LCD_SCL_CLR; 
	if(LCD_MOSI==1)	Data=0x1;
  else Data=0x0;      
  LCD_SCL_SET;
	
	for(i=15;i>0;i--)
	{
		LCD_SCL_CLR;
		if(LCD_MOSI==1)	Data=Data<<1|0x1;
    else Data=Data<<1|0x0;      
    LCD_SCL_SET;
	}
	return Data;	 
}


/****************************************************************************
* Ãû    ³Æ£ºvoid Lcd_WriteIndex(u8 idx)
* ¹¦    ÄÜ£ºÐ´ ST7735S ¿ØÖÆÆ÷¼Ä´æÆ÷µØÖ·
* Èë¿Ú²ÎÊý£ºidx   ¼Ä´æÆ÷µØÖ·
* ³ö¿Ú²ÎÊý£ºÎÞ
* ˵    Ã÷£ºµ÷ÓÃÇ°ÐèÏÈÑ¡ÖпØÖÆÆ÷£¬ÄÚ²¿º¯Êý
* µ÷Ó÷½·¨£ºLcd_WriteIndex(0x00);
****************************************************************************/
void Lcd_WriteIndex(u8 Index)
{
   
   //SPI дÃüÁîʱÐò¿ªÊ¼
   LCD_CS_CLR;
   LCD_RS_CLR;  //дÈëÃüÁî
   SPI_WriteData(Index);
   LCD_CS_SET;
}

/****************************************************************************
* Ãû    ³Æ£ºvoid Lcd_WriteData(u8 dat)
* ¹¦    ÄÜ£ºÐ´ ST7735S ¼Ä´æÆ÷Êý¾Ý
* Èë¿Ú²ÎÊý£ºdat     ¼Ä´æÆ÷Êý¾Ý
* ³ö¿Ú²ÎÊý£ºÎÞ
* ˵    Ã÷£ºÏò¿ØÖÆÆ÷Ö¸¶¨µØַдÈëÊý¾Ý£¬µ÷ÓÃÇ°ÐèÏÈд¼Ä´æÆ÷µØÖ·£¬ÄÚ²¿º¯Êý
* µ÷Ó÷½·¨£ºLcd_WriteData(0x30)
****************************************************************************/
void Lcd_WriteData(u8 Data)
{
   
   LCD_CS_CLR;
   LCD_RS_SET;   //дÈëÊý¾Ý
   SPI_WriteData(Data);
   LCD_CS_SET; 
}

//д¼Ä´æÆ÷º¯Êý
void Lcd_WriteReg(u8 Index,u8 Data)
{
	Lcd_WriteIndex(Index);
  Lcd_WriteData(Data);
}
			   

//ÏòÒº¾§ÆÁдһ¸ö16λÊý¾Ý
void LCD_WriteData_16Bit(uint16_t Data)
{
   LCD_CS_CLR;
   LCD_RS_SET;
	 SPI_WriteData(Data>>8); 	//дÈë¸ß8λÊý¾Ý
	 SPI_WriteData(Data&0xFF); 			//дÈëµÍ8λÊý¾Ý
   LCD_CS_SET; 
}



//µ±mdk -O1ʱ¼äÓÅ»¯Ê±ÐèÒªÉèÖÃ
//ÑÓʱi
void opt_delay(u8 i)
{
	while(i--);
}

//Òº¾§ÆÁ¸´Î»º¯Êý

void Lcd_Reset(void)
{
	  LCD_RST_CLR;
	  delay_ms(20);
	  LCD_RST_SET;
	  delay_ms(20);
}

//ÉèÖÃLCD²ÎÊý
//·½±ã½øÐкáÊúÆÁģʽÇл»
void LCD_SetParam(void)
{ 	
	lcddev.wramcmd=0x2C;
#if USE_HORIZONTAL==1	//ʹÓúáÆÁ	  
	lcddev.dir=1;//ºáÆÁ
	lcddev.width=320;
	lcddev.height=240;
	lcddev.setxcmd=0x2A;
	lcddev.setycmd=0x2B;			
	Lcd_WriteReg(0x36,0xA0);

#else//ÊúÆÁ
	lcddev.dir=0;//ÊúÆÁ				 	 		
	lcddev.width=240;
	lcddev.height=320;
	lcddev.setxcmd=0x2A;
	lcddev.setycmd=0x2B;	
	Lcd_WriteReg(0x36,0xC0);
#endif
}


/*************************************************
º¯ÊýÃû£ºLCD_Set_Region
¹¦ÄÜ£ºÉèÖÃlcdÏÔʾÇøÓò£¬ÔÚ´ËÇøÓòдµãÊý¾Ý×Ô¶¯»»ÐÐ
Èë¿Ú²ÎÊý£ºxyÆðµãºÍÖÕµã,Y_IncMode±íʾÏÈ×ÔÔöyÔÙ×ÔÔöx
·µ»ØÖµ£ºÎÞ
*************************************************/
void Lcd_SetRegion(u16 x_start,u16 y_start,u16 x_end,u16 y_end)
{
#if USE_HORIZONTAL==1	//ʹÓúáÆÁ
	
	Lcd_WriteIndex(0x2a);
	Lcd_WriteData(x_start>>8);
	Lcd_WriteData(x_start&0xFF);
	Lcd_WriteData(x_end>>8);
	Lcd_WriteData(x_end&0xFF);

	Lcd_WriteIndex(0x2b);
	Lcd_WriteData(y_start>>8);
	Lcd_WriteData(y_start&0xFF);
	Lcd_WriteData(y_end>>8);
	Lcd_WriteData(y_end&0xFF);
	
	
#else	
	Lcd_WriteIndex(0x2a);
	Lcd_WriteData(x_start>>8);
	Lcd_WriteData(x_start&0xFF);
	Lcd_WriteData(x_end>>8);
	Lcd_WriteData(x_end&0xFF);

	Lcd_WriteIndex(0x2b);
	Lcd_WriteData(y_start>>8);
	Lcd_WriteData(y_start&0xFF);
	Lcd_WriteData(y_end>>8);
	Lcd_WriteData(y_end&0xFF);
#endif
	
	Lcd_WriteIndex(0x2c); //¿ªÊ¼Ð´ÈëGRAM

}

/*************************************************
º¯ÊýÃû£ºLCD_Set_XY
¹¦ÄÜ£ºÉèÖÃlcdÏÔʾÆðʼµã
Èë¿Ú²ÎÊý£ºxy×ø±ê
·µ»ØÖµ£ºÎÞ
*************************************************/
void LCD_SetCursor(u16 x,u16 y)
{
  	//Lcd_SetRegion(x,y,x,y);
 #if USE_HORIZONTAL==1	//ʹÓúáÆÁ	

	  Lcd_SetRegion(x+1,y+2,x+1,y+2);
	#else		
	  Lcd_SetRegion(x+2,y+1,x+2,y+1);
	#endif
}

//»­µã
//x,y:×ø±ê
//POINT_COLOR:´ËµãµÄÑÕÉ«
void LCD_DrawPoint(u16 x,u16 y)
{
	LCD_SetCursor(x,y);		//ÉèÖùâ±êλÖà 
  LCD_WriteData_16Bit(POINT_COLOR); 
}

	
/*************************************************
º¯ÊýÃû£ºLCD_DrawPoint
¹¦ÄÜ£º»­Ò»¸öµã
Èë¿Ú²ÎÊý£ºÎÞ
·µ»ØÖµ£ºÎÞ
*************************************************/
void LCD_Fast_DrawPoint(u16 x,u16 y,u16 Data)
{
	//Lcd_SetRegion(x,y,x+1,y+1);
 #if USE_HORIZONTAL==1	//ʹÓúáÆÁ	
	
  Lcd_SetRegion(x+1,y+2,x+2,y+3);
	
	#else	
	
	Lcd_SetRegion(x+2,y+1,x+3,y+2);

	#endif
  LCD_WriteData_16Bit(Data);

}    

/*****************************************
 º¯Êý¹¦ÄÜ£º¶ÁTFTijһµãµÄÑÕÉ«                          
 ³ö¿Ú²ÎÊý£ºcolor  µãÑÕÉ«Öµ                                 
******************************************/
//unsigned int Lcd_ReadPoint(u16 x,u16 y)
//{
//  unsigned int Data;
//  Lcd_SetXY(x,y);
//  Lcd_WriteData(Data);
//  return Data;
//}
/*************************************************
º¯ÊýÃû£ºLcd_Clear
¹¦ÄÜ£ºÈ«ÆÁÇåÆÁº¯Êý
Èë¿Ú²ÎÊý£ºÌî³äÑÕÉ«COLOR
·µ»ØÖµ£ºÎÞ
*************************************************/
void LCD_Clear(u16 Color)               
{	
   unsigned int i,m;

#if USE_HORIZONTAL==1	//ʹÓúáÆÁ	

   //Lcd_SetRegion(2,1,X_MAX_PIXEL+1,Y_MAX_PIXEL);
	 Lcd_SetRegion(1,2,X_MAX_PIXEL,Y_MAX_PIXEL+1);
	
   Lcd_WriteIndex(0x2C);
	
 	for(i=0;i<Y_MAX_PIXEL;i++)
    for(m=0;m<X_MAX_PIXEL;m++)
    {	
     LCD_WriteData_16Bit(Color);
    } 
		
	
#else	
   Lcd_SetRegion(0,0,X_MAX_PIXEL,Y_MAX_PIXEL);
   Lcd_WriteIndex(0x2C);
 	for(i=0;i<Y_MAX_PIXEL;i++)
    for(m=0;m<X_MAX_PIXEL;m++)
    {	
			LCD_WriteData_16Bit(Color);
    } 
		
#endif	
		
}

void LCD_Init(void)
{

	  LCD_GPIO_Init();     //ʹÓÃÄ£ÄâSPI  ³õʼ»¯IO½Ó¿Ú
	  Lcd_Reset();         //¸´Î»Òº¾§ÆÁ
		Lcd_WriteIndex(0x11);//Sleep exit 
		delay_ms(20);		
		Lcd_WriteIndex(0xCF);  
		Lcd_WriteData(0x00); 
		Lcd_WriteData(0xC1); 
		Lcd_WriteData(0X30); 
		Lcd_WriteIndex(0xED);  
		Lcd_WriteData(0x64); 
		Lcd_WriteData(0x03); 
		Lcd_WriteData(0X12); 
		Lcd_WriteData(0X81); 
		Lcd_WriteIndex(0xE8);  
		Lcd_WriteData(0x85); 
		Lcd_WriteData(0x10); 
		Lcd_WriteData(0x7A); 
		Lcd_WriteIndex(0xCB);  
		Lcd_WriteData(0x39); 
		Lcd_WriteData(0x2C); 
		Lcd_WriteData(0x00); 
		Lcd_WriteData(0x34); 
		Lcd_WriteData(0x02); 
		Lcd_WriteIndex(0xF7);  
		Lcd_WriteData(0x20); 
		Lcd_WriteIndex(0xEA);  
		Lcd_WriteData(0x00); 
		Lcd_WriteData(0x00); 
		Lcd_WriteIndex(0xC0);    //Power control 
		Lcd_WriteData(0x1B);   //VRH[5:0] 
		Lcd_WriteIndex(0xC1);    //Power control 
		Lcd_WriteData(0x01);   //SAP[2:0];BT[3:0] 
		Lcd_WriteIndex(0xC5);    //VCM control 
		Lcd_WriteData(0x30); 	 //3F
		Lcd_WriteData(0x30); 	 //3C
		Lcd_WriteIndex(0xC7);    //VCM control2 
		Lcd_WriteData(0XB7); 
		Lcd_WriteIndex(0x36);    // Memory Access Control 
		Lcd_WriteData(0x48); 
		Lcd_WriteIndex(0x3A);   
		Lcd_WriteData(0x55); 
		Lcd_WriteIndex(0xB1);   
		Lcd_WriteData(0x00);   
		Lcd_WriteData(0x1A); 
		Lcd_WriteIndex(0xB6);    // Display Function Control 
		Lcd_WriteData(0x0A); 
		Lcd_WriteData(0xA2); 
		Lcd_WriteIndex(0xF2);    // 3Gamma Function Disable 
		Lcd_WriteData(0x00); 
		Lcd_WriteIndex(0x26);    //Gamma curve selected 
		Lcd_WriteData(0x01); 
		Lcd_WriteIndex(0xE0);    //Set Gamma 
		Lcd_WriteData(0x0F); 
		Lcd_WriteData(0x2A); 
		Lcd_WriteData(0x28); 
		Lcd_WriteData(0x08); 
		Lcd_WriteData(0x0E); 
		Lcd_WriteData(0x08); 
		Lcd_WriteData(0x54); 
		Lcd_WriteData(0XA9); 
		Lcd_WriteData(0x43); 
		Lcd_WriteData(0x0A); 
		Lcd_WriteData(0x0F); 
		Lcd_WriteData(0x00); 
		Lcd_WriteData(0x00); 
		Lcd_WriteData(0x00); 
		Lcd_WriteData(0x00); 		 
		Lcd_WriteIndex(0XE1);    //Set Gamma 
		Lcd_WriteData(0x00); 
		Lcd_WriteData(0x15); 
		Lcd_WriteData(0x17); 
		Lcd_WriteData(0x07); 
		Lcd_WriteData(0x11); 
		Lcd_WriteData(0x06); 
		Lcd_WriteData(0x2B); 
		Lcd_WriteData(0x56); 
		Lcd_WriteData(0x3C); 
		Lcd_WriteData(0x05); 
		Lcd_WriteData(0x10); 
		Lcd_WriteData(0x0F); 
		Lcd_WriteData(0x3F); 
		Lcd_WriteData(0x3F); 
		Lcd_WriteData(0x0F); 
		Lcd_WriteIndex(0x2B); 
		Lcd_WriteData(0x00);
		Lcd_WriteData(0x00);
		Lcd_WriteData(0x01);
		Lcd_WriteData(0x3f);
		Lcd_WriteIndex(0x2A); 
		Lcd_WriteData(0x00);
		Lcd_WriteData(0x00);
		Lcd_WriteData(0x00);
		Lcd_WriteData(0xef);	 
		Lcd_WriteIndex(0x11); //Exit Sleep
		delay_ms(20);
		Lcd_WriteIndex(0x29); //display on	
        LCD_SetParam();//ÉèÖÃLCD²ÎÊý	 
}

//LCD¿ªÆôÏÔʾ
void LCD_DisplayOn(void)
{					   
	Lcd_WriteIndex(0X29);	//¿ªÆôÏÔʾ
}	 
//LCD¹Ø±ÕÏÔʾ
void LCD_DisplayOff(void)
{	   
  Lcd_WriteIndex(0X28);	//¹Ø±ÕÏÔʾ
}  
//ÔÚÖ¸¶¨ÇøÓòÄÚÌî³äµ¥¸öÑÕÉ«
//(sx,sy),(ex,ey):Ìî³ä¾ØÐζԽÇ×ø±ê,ÇøÓò´óСΪ:(ex-sx+1)*(ey-sy+1)   
//color:ÒªÌî³äµÄÑÕÉ«
void LCD_Fill(u16 sx,u16 sy,u16 ex,u16 ey,u16 color)
{          
	u16 i,j;
	u16 xlen=0;
	xlen=ex-sx+1;	 
	for(i=sy;i<=ey;i++)
	{
		 LCD_SetCursor(sx,i);      				//ÉèÖùâ±êλÖà 
		 for(j=0;j<xlen;j++) LCD_WriteData_16Bit(color);	//ÏÔʾÑÕÉ« 	    
		}	 
}  

//»­Ïß
//x1,y1:Æðµã×ø±ê
//x2,y2:ÖÕµã×ø±ê  
void LCD_DrawLine(u16 x1, u16 y1, u16 x2, u16 y2)
{
	u16 t; 
	int xerr=0,yerr=0,delta_x,delta_y,distance; 
	int incx,incy,uRow,uCol; 
	delta_x=x2-x1; //¼ÆËã×ø±êÔöÁ¿ 
	delta_y=y2-y1; 
	uRow=x1; 
	uCol=y1; 
	if(delta_x>0)incx=1; //ÉèÖõ¥²½·½Ïò 
	else if(delta_x==0)incx=0;//´¹Ö±Ïß 
	else {incx=-1;delta_x=-delta_x;} 
	if(delta_y>0)incy=1; 
	else if(delta_y==0)incy=0;//ˮƽÏß 
	else{incy=-1;delta_y=-delta_y;} 
	if( delta_x>delta_y)distance=delta_x; //Ñ¡È¡»ù±¾ÔöÁ¿×ø±êÖá 
	else distance=delta_y; 
	for(t=0;t<=distance+1;t++ )//»­ÏßÊä³ö 
	{  
		LCD_DrawPoint(uRow,uCol);//»­µã 
		xerr+=delta_x ; 
		yerr+=delta_y ; 
		if(xerr>distance) 
		{ 
			xerr-=distance; 
			uRow+=incx; 
		} 
		if(yerr>distance) 
		{ 
			yerr-=distance; 
			uCol+=incy; 
		} 
	}  
}    
//»­¾ØÐÎ	  
//(x1,y1),(x2,y2):¾ØÐεĶԽÇ×ø±ê
void LCD_DrawRectangle(u16 x1, u16 y1, u16 x2, u16 y2)
{
	LCD_DrawLine(x1,y1,x2,y1);
	LCD_DrawLine(x1,y1,x1,y2);
	LCD_DrawLine(x1,y2,x2,y2);
	LCD_DrawLine(x2,y1,x2,y2);
}
//ÔÚÖ¸¶¨Î»Öû­Ò»¸öÖ¸¶¨´óСµÄÔ²
//(x,y):ÖÐÐĵã
//r    :°ë¾¶
void LCD_Draw_Circle(u16 x0,u16 y0,u8 r)
{
	int a,b;
	int di;
	a=0;b=r;	  
	di=3-(r<<1);             //ÅжÏϸöµãλÖõıêÖ¾
	while(a<=b)
	{
		LCD_DrawPoint(x0+a,y0-b);             //5
 		LCD_DrawPoint(x0+b,y0-a);             //0           
		LCD_DrawPoint(x0+b,y0+a);             //4               
		LCD_DrawPoint(x0+a,y0+b);             //6 
		LCD_DrawPoint(x0-a,y0+b);             //1       
 		LCD_DrawPoint(x0-b,y0+a);             
		LCD_DrawPoint(x0-a,y0-b);             //2             
  		LCD_DrawPoint(x0-b,y0-a);             //7     	         
		a++;
		//ʹÓÃBresenhamËã·¨»­Ô²     
		if(di<0)di +=4*a+6;	  
		else
		{
			di+=10+4*(a-b);   
			b--;
		} 						    
	}
} 									  
//ÔÚÖ¸¶¨Î»ÖÃÏÔʾһ¸ö×Ö·û
//x,y:Æðʼ×ø±ê
//num:ÒªÏÔʾµÄ×Ö·û:" "--->"~"
//size:×ÖÌå´óС 12/16/24
//mode:µþ¼Ó·½Ê½(1)»¹ÊǷǵþ¼Ó·½Ê½(0)
void LCD_ShowChar(u16 x,u16 y,u8 num,u8 size,u8 mode)
{  							  
    u8 temp,t1,t;
	u16 y0=y;
	u8 csize=(size/8+((size%8)?1:0))*(size/2);		//µÃµ½×ÖÌåÒ»¸ö×Ö·û¶ÔÓ¦µãÕó¼¯ËùÕ¼µÄ×Ö½ÚÊý	
 	num=num-' ';//µÃµ½Æ«ÒƺóµÄÖµ£¨ASCII×Ö¿âÊÇ´Ó¿Õ¸ñ¿ªÊ¼È¡Ä££¬ËùÒÔ-' '¾ÍÊǶÔÓ¦×Ö·ûµÄ×ֿ⣩
	for(t=0;t<csize;t++)
	{   
		if(size==12)temp=asc2_1206[num][t]; 	 	//µ÷ÓÃ1206×ÖÌå
		else if(size==16)temp=asc2_1608[num][t];	//µ÷ÓÃ1608×ÖÌå
		else if(size==24)temp=asc2_2412[num][t];	//µ÷ÓÃ2412×ÖÌå
		else return;								//ûÓеÄ×Ö¿â
		for(t1=0;t1<8;t1++)
		{			    
			if(temp&0x80)LCD_Fast_DrawPoint(x,y,POINT_COLOR);
			else if(mode==0)LCD_Fast_DrawPoint(x,y,BACK_COLOR);
			temp<<=1;
			y++;
			if(y>=lcddev.height)return;		//³¬ÇøÓòÁË
			if((y-y0)==size)
			{
				y=y0;
				x++;
				if(x>=lcddev.width)return;	//³¬ÇøÓòÁË
				break;
			}
		}  	 
	}  	    	   	 	  
}   
//m^nº¯Êý
//·µ»ØÖµ:m^n´Î·½.
u32 LCD_Pow(u8 m,u8 n)
{
	u32 result=1;	 
	while(n--)result*=m;    
	return result;
}			 
//ÏÔʾÊý×Ö,¸ßλΪ0,Ôò²»ÏÔʾ
//x,y :Æðµã×ø±ê	 
//len :Êý×ÖµÄλÊý
//size:×ÖÌå´óС
//color:ÑÕÉ« 
//num:ÊýÖµ(0~4294967295);	 
void LCD_ShowNum(u16 x,u16 y,u32 num,u8 len,u8 size)
{         	
	u8 t,temp;
	u8 enshow=0;						   
	for(t=0;t<len;t++)
	{
		temp=(num/LCD_Pow(10,len-t-1))%10;
		if(enshow==0&&t<(len-1))
		{
			if(temp==0)
			{
				LCD_ShowChar(x+(size/2)*t,y,' ',size,0);
				continue;
			}else enshow=1; 
		 	 
		}
	 	LCD_ShowChar(x+(size/2)*t,y,temp+'0',size,0); 
	}
} 
//ÏÔʾÊý×Ö,¸ßλΪ0,»¹ÊÇÏÔʾ
//x,y:Æðµã×ø±ê
//num:ÊýÖµ(0~999999999);	 
//len:³¤¶È(¼´ÒªÏÔʾµÄλÊý)
//size:×ÖÌå´óС
//mode:
//[7]:0,²»Ìî³ä;1,Ìî³ä0.
//[6:1]:±£Áô
//[0]:0,·Çµþ¼ÓÏÔʾ;1,µþ¼ÓÏÔʾ.
void LCD_ShowxNum(u16 x,u16 y,u32 num,u8 len,u8 size,u8 mode)
{  
	u8 t,temp;
	u8 enshow=0;						   
	for(t=0;t<len;t++)
	{
		temp=(num/LCD_Pow(10,len-t-1))%10;
		if(enshow==0&&t<(len-1))
		{
			if(temp==0)
			{
				if(mode&0X80)LCD_ShowChar(x+(size/2)*t,y,'0',size,mode&0X01);  
				else LCD_ShowChar(x+(size/2)*t,y,' ',size,mode&0X01);  
 				continue;
			}else enshow=1; 
		 	 
		}
	 	LCD_ShowChar(x+(size/2)*t,y,temp+'0',size,mode&0X01); 
	}
} 
//ÏÔʾ×Ö·û´®
//x,y:Æðµã×ø±ê
//width,height:ÇøÓò´óС  
//size:×ÖÌå´óС
//*p:×Ö·û´®ÆðʼµØÖ·		  
void LCD_ShowString(u16 x,u16 y,u16 width,u16 height,u8 size,u8 *p)
{         
	u8 x0=x;
	width+=x;
	height+=y;
    while((*p<='~')&&(*p>=' '))//ÅжÏÊDz»ÊÇ·Ç·¨×Ö·û!
    {       
        if(x>=width){x=x0;y+=size;}
        if(y>=height)break;//Í˳ö
        LCD_ShowChar(x,y,*p,size,0);
        x+=size/2;
        p++;
    }  
}

//¶ÁÈ¡¸öijµãµÄÑÕÉ«Öµ	 
//x,y:×ø±ê
//·µ»ØÖµ:´ËµãµÄÑÕÉ«
u16 LCD_ReadPoint(u16 x,u16 y)
{
 	u16 r=0,g=0,b=0;
	if(x>=lcddev.width||y>=lcddev.height)return 0;	//³¬¹ýÁË·¶Î§,Ö±½Ó·µ»Ø		   
	LCD_SetCursor(x,y);	    
	Lcd_WriteIndex(0X2E);                //9341 ·¢ËͶÁGRAMÖ¸Áî 
  r=LCD_RD_DATA();								//dummy Read	
  opt_delay(2);	
 	r=LCD_RD_DATA();  		  						//ʵ¼Ê×ø±êÑÕÉ« 
	opt_delay(2);
	b=LCD_RD_DATA(); 
	g=r&0XFF;		             //¶ÔÓÚ9341/5310/5510,µÚÒ»´Î¶ÁÈ¡µÄÊÇRGµÄÖµ,RÔÚÇ°,GÔÚºó,¸÷Õ¼8λ
	g<<=8;
  return (((r>>11)<<11)|((g>>10)<<5)|(b>>11));    //ILI9341/NT35310/NT35510ÐèÒª¹«Ê½×ª»»Ò»ÏÂ
}	


//ÔÚÖ¸¶¨ÇøÓòÄÚÌî³äÖ¸¶¨ÑÕÉ«¿é			 
//(sx,sy),(ex,ey):Ìî³ä¾ØÐζԽÇ×ø±ê,ÇøÓò´óСΪ:(ex-sx+1)*(ey-sy+1)   
//color:ÒªÌî³äµÄÑÕÉ«
void LCD_Color_Fill(u16 sx,u16 sy,u16 ex,u16 ey,u16 *color)
{  
	u16 height,width;
	u16 i,j;
	width=ex-sx+1; 			//µÃµ½Ìî³äµÄ¿í¶È
	height=ey-sy+1;			//¸ß¶È
 	for(i=0;i<height;i++)
	{
 		LCD_SetCursor(sx,sy+i);   	//ÉèÖùâ±êλÖà 
		for(j=0;j<width;j++)
		{
			LCD_WriteData_16Bit(color[i*width+j]);
		}
	}		  
}

乱码请自觉忽略(因Keil版本问题),附件以原子哥汉子显示为例,感兴趣的朋友可进行下载。

https://download.csdn.net/download/leotian966/12888996

 类似资料: