ILI9225笔记

Posted on Mon, Feb 6, 2023 单片机

ILI9225笔记

LCD_ 函数

LCD_setAddrWindow

/*
      函数说明:设置读写起始和结束地址
      入口数据:x1,x1 设置列的起始和结束地址
                y0,y1 设置行的起始和结束地址
       返回值:  无
*/

void LCD_setAddrWindow(UINT16 x0,UINT16 y0,UINT16 x1,UINT16 y1)
{
		LCD_writeCmd(0x2a);//列地址设置
		LCD_writeHalfword(x0);
		LCD_writeHalfword(x1);
		LCD_writeCmd(0x2b);//行地址设置
		LCD_writeHalfword(y0);
		LCD_writeHalfword(y1);
		LCD_writeCmd(0x2c);//储存器写
}

LCD_init [初始化]

LCD_setDir

void LCD_setDir(UINT8 dir)
{
    LCD_parameter.dir = dir & 3;  
// just perform the operation ourselves on the protected variables
    LCD_parameter.width = (LCD_parameter.dir & 1) ? LCD_H : LCD_W;
    LCD_parameter.height = (LCD_parameter.dir & 1) ? LCD_W : LCD_H;
	UINT16 val;
	switch(LCD_parameter.dir) 
	{
		case 0: 
			val = 0x1030; //0 degree 
			break;
		case 1: 
			val = 0x1028; //90 degree 
			break;
		case 2: 
			val = 0x1000; //180 degree 
			break;
		case 3: 
			val = 0x1018; //270 degree 
			break;
	}
	
	LCD_writeCmd(0x03);
	LCD_writeHalfword(val);
	//Vert_Scroll(0, HEIGHT, 0);
}

LCD_setColor —LCD_setFont

void LCD_setColor(UINT16 color){
	LCD_parameter.color = color;
}
void LCD_setBackground(UINT16 color){
	LCD_parameter.Background = color;
}

void LCD_setColorRGB(UINT8 r,UINT8 g,UINT8 b){
	LCD_parameter.color = RGB888_565(r,g,b); 
}
void LCD_setBackgroundRGB(UINT8 r,UINT8 g,UINT8 b){
	LCD_parameter.Background = RGB888_565(r,g,b);
}
void LCD_setFont(UINT8 font){
	LCD_parameter.font = font;
}

LCD_showChar

/******************************************************************************
      函数说明:显示单个字符
      入口数据:x,y显示坐标
                num 要显示的字符
                overlay:  0非叠加模式  1叠加模式
      返回值:  无
******************************************************************************/
void LCD_showChar(UINT16 x,UINT16 y,UINT8 num,UINT8 overlay)
{
	UINT8 temp,fontWidth,t,m=0;
	UINT16 i,TypefaceNum;//一个字符所占字节大小
	UINT16 x0=x;
	fontWidth=LCD_parameter.font/2;
	TypefaceNum=(fontWidth/8+((fontWidth%8)?1:0))*LCD_parameter.font;
	num=num-' ';    //得到偏移后的值
	
	LCD_setAddrWindow(x,y,x+fontWidth-1,y+LCD_parameter.font-1);  //设置光标位置 
	for(i=0;i<TypefaceNum;i++)
	{ 
		switch (LCD_parameter.font)
		{
		case 12:temp=ascii_1206[num][i];break;//调用1206字体
		case 16:temp=ascii_1608[num][i];break;//调用1608字体
		case 24:temp=ascii_2412[num][i];break;
		case 32:temp=ascii_3216[num][i];break;
		default:return;
		}
		
		for(t=0;t<8;t++)
		{
			if(!overlay)//非叠加模式
			{
				if(temp&(0x01<<t))LCD_writeHalfword(LCD_parameter.color);
				else LCD_writeHalfword(LCD_parameter.Background);
				m++;
				if(m%fontWidth==0)
				{
					m=0;
					break;
				}
			}
			else//叠加模式
			{
				if(temp&(0x01<<t))LCD_drawPixel(x,y);//画一个点
				x++;
				if((x-x0)==fontWidth)
				{
					x=x0;
					y++;
					break;
				}
			}
		}
	}   	 	  
}

LCD_showString

/******************************************************************************
      函数说明:显示字符串
      入口数据:x,y显示坐标
                *p 要显示的字符串
                overlay:  0非叠加模式  1叠加模式
      返回值:  无
******************************************************************************/
void LCD_showString(UINT16 x,UINT16 y,char *p,UINT8 overlay)
{         
	while(*p!='\0')
	{       
		LCD_showChar(x,y,*p,overlay);
		x+=LCD_parameter.font/2;
		p++;
	}  
}

LCD_showPicture

/******************************************************************************
      函数说明:显示图片
      入口数据:x,y起点坐标
                length 图片长度
                width  图片宽度
                pic[]  图片数组    
      返回值:  无
******************************************************************************/
void LCD_showPicture(UINT16 x,UINT16 y,UINT16 length,UINT16 width,UINT8 pic[])
{
	UINT16 i,j;
	UINT32 k=0;
	LCD_setAddrWindow(x,y,x+length-1,y+width-1);
	for(i=0;i<length;i++)
	{
		for(j=0;j<width;j++)
		{
			LCD_writeByte(pic[k*2]);
			LCD_writeByte(pic[k*2+1]);
			k++;
		}
	}
}

LCD_clear

/******************************************************************************
      函数说明:在指定区域填充背景颜色
      入口数据:x0,y0   起始坐标
                x1,y1   终止坐标
      返回值:  无
******************************************************************************/
void LCD_clear(UINT16 x0,UINT16 y0,UINT16 x1,UINT16 y1)
{          
	UINT32 cnt;
	if (x0 > x1)SWAP_HW(x0,x1);
	if (y0 > y1)SWAP_HW(y0,y1);

	LCD_setAddrWindow(x0,y0,x1,y1);
	cnt = (x1-x0+1)*(y1-y0+1);								   	 	
	for(;cnt != 0;cnt--)
	{
		LCD_writeHalfword(LCD_parameter.Background);
	} 					  	    
}

LCD_fillCircle—LCD_fillSemiCircle

//fill a circle,thx for LCDWIKI
void LCD_fillCircle(int x, int y, int radius)
{
	LCD_fill(x, y-radius,x, y+radius+1);
	LCD_fillSemiCircle(x, y, radius, 3, 0);
}

//fill a semi-circle,thx for LCDWIKI
void LCD_fillSemiCircle(int x0, int y0, int r, UINT8 cornername,int delta)
{
	int f     = 1 - r;
	int ddF_x = 1;
	int ddF_y = -2 * r;
	int x     = 0;
	int y     = r;

	while (x<y) 
	{
    	if (f >= 0) 
		{
      		y--;
      		ddF_y += 2;
      		f += ddF_y;
    	}
    	x++;
    	ddF_x += 2;
    	f += ddF_x;

    	if (cornername & 0x1) 
		{
      		LCD_fill(x0+x, y0-y,x0+x, y0+y+1+delta);
      		LCD_fill(x0+y, y0-x,x0+y, y0+x+1+delta);
    	}
    	if (cornername & 0x2) 
		{
      		LCD_fill(x0-x, y0-y, x0-x, y0+y+1+delta);
      		LCD_fill(x0-y, y0-x, x0-y, y0+x+1+delta);
    	}
  	}
}

LCD_writeByte—LCD_writeHalfword

/******************************************************************************
      函数说明:LCD 写入1字节
      入口数据:data 写入的数据
      返回值:  无
******************************************************************************/
void LCD_writeByte(UINT8 data)
{
	LCD_CS_SEL();
	CH549SPIMasterWrite(data);       //使用CH549的SPI写入1字节数据
	LCD_CS_DSEL();
}

/******************************************************************************
      函数说明:LCD 写入半字
      入口数据:data 写入的数据
      返回值:  无
******************************************************************************/
void LCD_writeHalfword(UINT16 data)
{
	LCD_writeByte(data>>8);
	LCD_writeByte(data);
}

LCD_writeCmd

/******************************************************************************
      函数说明:LCD写入命令
      入口数据:cmd 写入的命令
      返回值:  无
******************************************************************************/
void LCD_writeCmd(UINT8 cmd)
{
	LCD_DC_CMD();//写命令
	LCD_writeByte(cmd);
	LCD_DC_DATA();//写数据
}

LCD_fill

/******************************************************************************
      函数说明:在指定区域填充前景颜色
      入口数据:x0,y0   起始坐标
                x1,y1   终止坐标
      返回值:  无
******************************************************************************/
void LCD_fill(UINT16 x0,UINT16 y0,UINT16 x1,UINT16 y1)
{   
	UINT32 cnt;
	if (x0 > x1)SWAP_HW(x0,x1);
	if (y0 > y1)SWAP_HW(y0,y1);

	LCD_setAddrWindow(x0,y0,x1,y1);
	cnt = (x1-x0+1)*(y1-y0+1);								   	 	
		for(;cnt != 0;cnt--)
		{
			LCD_writeHalfword(LCD_parameter.color);
		} 
}

LCD_drawPixel

/******************************************************************************
      函数说明:在指定位置画点
      入口数据:x,y 画点坐标
      返回值:  无
******************************************************************************/
void LCD_drawPixel(UINT16 x,UINT16 y)
{
	LCD_setAddrWindow(x,y,x,y);//设置光标位置 
	LCD_writeHalfword(LCD_parameter.color);
}

LCD_drawLine

/******************************************************************************
      函数说明:画线
      入口数据:x0,y0   起始坐标
                x1,y1   终止坐标
      返回值:  无
******************************************************************************/
void LCD_drawLine(UINT16 x0,UINT16 y0,UINT16 x1,UINT16 y1)
{
	UINT16 t; 
	int xErr=0,yErr=0,dx,dy,dist;
	int incX,incY,x,y;
	dx=x1-x0; //计算坐标增量 
	dy=y1-y0;
	x=x0;//画线起点坐标
	y=y0;
	if(dx>0)incX=1; //设置单步方向 
	else if (dx==0)incX=0;//垂直线 
	else {incX=-1;dx=-dx;}
	if(dy>0)incY=1;
	else if (dy==0)incY=0;//水平线 
	else {incY=-1;dy=-dy;}
	if(dx>dy)dist=dx; //选取基本增量坐标轴 
	else dist=dy;
	for(t=0;t<dist+1;t++)
	{
		LCD_drawPixel(x,y);//画点
		xErr+=dx;
		yErr+=dy;
		if(xErr>dist)
		{
			xErr-=dist;
			x+=incX;
		}
		if(yErr>dist)
		{
			yErr-=dist;
			y+=incY;
		}
	}
}

LCD_drawRectangle

/******************************************************************************
      函数说明:画矩形
      入口数据:x1,y0   起始坐标
                x1,y1   终止坐标
      返回值:  无
******************************************************************************/
void LCD_drawRectangle(UINT16 x0, UINT16 y0, UINT16 x1, UINT16 y1)
{
	LCD_drawLine(x0,y0,x1,y0);
	LCD_drawLine(x0,y0,x0,y1);
	LCD_drawLine(x0,y1,x1,y1);
	LCD_drawLine(x1,y0,x1,y1);
}

LCD_drawCircle

/******************************************************************************
      函数说明:画圆
      入口数据:x0,y0   圆心坐标
                r       半径
      返回值:  无
******************************************************************************/
void LCD_drawCircle(UINT16 x0,UINT16 y0,UINT8 r)
{
	int a,b;
	a=0;b=r;	  
	while(a<=b)
	{
		LCD_drawPixel(x0-b,y0-a);             //3           
		LCD_drawPixel(x0+b,y0-a);             //0           
		LCD_drawPixel(x0-a,y0+b);             //1                
		LCD_drawPixel(x0-a,y0-b);             //2             
		LCD_drawPixel(x0+b,y0+a);             //4               
		LCD_drawPixel(x0+a,y0-b);             //5
		LCD_drawPixel(x0+a,y0+b);             //6 
		LCD_drawPixel(x0-b,y0+a);             //7
		a++;
		if((a*a+b*b)>(r*r))//判断要画的点是否过远
		{
			b--;
		}
	}
}