//写寄存器函数
//cmd:寄存器值
void LCD_WriteCmd(u16 cmd)
{
#ifdef TFTLCD_ILI9327
TFTLCD->LCD_CMD=cmd;
#endif
}
//写数据
//data:要写入的值
void LCD_WriteData(u16 data)
{
#ifdef TFTLCD_ILI9327
TFTLCD->LCD_DATA=data;
#endif
}
void LCD_WriteCmdData(u16 cmd,u16 data)
{
LCD_WriteCmd(cmd);
LCD_WriteData(data);
}
void LCD_WriteData_Color(u16 color)
{
#ifdef TFTLCD_ILI9327
TFTLCD->LCD_DATA=color>>8;
TFTLCD->LCD_DATA=color&0xff;
#endif
}
//读数据
//返回值:读到的值
u16 LCD_ReadData(void)
{
#ifdef TFTLCD_ILI9327
u16 ram;
ram=TFTLCD->LCD_DATA;
ram=ram<<8;
ram|=TFTLCD->LCD_DATA;
return ram;
#endif
}
//设置LCD显示方向
//dir:0,竖屏;1,横屏
void LCD_Display_Dir(u8 dir)
{
if(dir==0) //默认竖屏方向
{
#ifdef TFTLCD_ILI9327
LCD_WriteCmd(0x36); //设置彩屏显示方向的寄存器
LCD_WriteData(0x48);
tftlcd_data.height=400;
tftlcd_data.width=240;
#endif
tftlcd_data.dir=0;
}
else
{
#ifdef TFTLCD_ILI9327
LCD_WriteCmd(0x36); //设置彩屏显示方向的寄存器
LCD_WriteData(0x28);
tftlcd_data.height=240;
tftlcd_data.width=400;
#endif
tftlcd_data.dir=1;
}
}
void TFTLCD_Init(void)
{
// u16 i;//2022 10 30
TFTLCD_GPIO_Init();
TFTLCD_FSMC_Init();
delay_ms(50);
#ifdef TFTLCD_ILI9327
LCD_WriteCmd(0XEF);
tftlcd_data.id=TFTLCD->LCD_DATA; //dummy read
tftlcd_data.id=TFTLCD->LCD_DATA; //02
tftlcd_data.id=TFTLCD->LCD_DATA; //04
tftlcd_data.id=TFTLCD->LCD_DATA; //93
tftlcd_data.id=(tftlcd_data.id<<8)|TFTLCD->LCD_DATA; //27
#endif
printf(" LCD ID:%x\r\n",tftlcd_data.id); //打印LCD ID
#ifdef TFTLCD_ILI9327
//************* Start Initial Sequence **********//
LCD_WriteCmd(0xE9);
LCD_WriteData(0x20);
LCD_WriteCmd(0x11); //Exit Sleep
delay_ms(100);
LCD_WriteCmd(0x3A);
LCD_WriteData(0x55);
LCD_WriteCmd(0xD1);
LCD_WriteData(0x00);
LCD_WriteData(0x65); //调试此值改善水纹
LCD_WriteData(0x1F);
LCD_WriteCmd(0xD0);
LCD_WriteData(0x07);
LCD_WriteData(0x07);
LCD_WriteData(0x80);
LCD_WriteCmd(0x36);
LCD_WriteData(0x48);
LCD_WriteCmd(0xC1);
LCD_WriteData(0x10);
LCD_WriteData(0x10);
LCD_WriteData(0x02);
LCD_WriteData(0x02);
LCD_WriteCmd(0xC0); //Set Default Gamma
LCD_WriteData(0x00);
LCD_WriteData(0x35);
LCD_WriteData(0x00);
LCD_WriteData(0x00);
LCD_WriteData(0x01);
LCD_WriteData(0x02);
LCD_WriteCmd(0xC5); //Set frame rate
LCD_WriteData(0x01);
LCD_WriteCmd(0xD2); //power setting
LCD_WriteData(0x01);
LCD_WriteData(0x22);
LCD_WriteCmd(0xC8); //Set Gamma
LCD_WriteData(0x01);
LCD_WriteData(0x52);
LCD_WriteData(0x37);
LCD_WriteData(0x10);
LCD_WriteData(0x0d);
LCD_WriteData(0x01);
LCD_WriteData(0x04);
LCD_WriteData(0x51);
LCD_WriteData(0x77);
LCD_WriteData(0x01);
LCD_WriteData(0x01);
LCD_WriteData(0x0d);
LCD_WriteData(0x08);
LCD_WriteData(0x80);
LCD_WriteData(0x00);
LCD_WriteCmd(0x29); //display on
#endif
LCD_Display_Dir(0); //0:竖屏 1:横屏 默认竖屏
LCD_Clear(WHITE);
}
//设置窗口,并自动设置画点坐标到窗口左上角(sx,sy).
//sx,sy:窗口起始坐标(左上角)
//width,height:窗口宽度和高度,必须大于0!!
//窗体大小:width*height.
void LCD_Set_Window(u16 sx,u16 sy,u16 width,u16 height)
{
#ifdef TFTLCD_ILI9327
LCD_WriteCmd(0x2A);
LCD_WriteData(sx/256);
LCD_WriteData(sx%256);
LCD_WriteData(width/256);
LCD_WriteData(width%256);
LCD_WriteCmd(0x2B);
LCD_WriteData(sy/256);
LCD_WriteData(sy%256);
LCD_WriteData(height/256);
LCD_WriteData(height%256);
LCD_WriteCmd(0x2C);
#endif
}
//清屏函数
//color:要清屏的填充色
void LCD_Clear(u16 color)
{
uint16_t i, j ;
LCD_Set_Window(0, 0, tftlcd_data.width-1, tftlcd_data.height-1); //作用区域
for(i=0; i
for (j=0; j
LCD_WriteData_Color(color);
}
}
}
//在指定区域内填充单个颜色
//(sx,sy),(ex,ey):填充矩形对角坐标,区域大小为:(ex-sx+1)*(ey-sy+1)
//color:要填充的颜色
void LCD_Fill(u16 xState,u16 yState,u16 xEnd,u16 yEnd,u16 color)
{
uint16_t temp;
if((xState > xEnd) || (yState > yEnd))
{
return;
}
LCD_Set_Window(xState, yState, xEnd, yEnd);
xState = xEnd - xState + 1;
yState = yEnd - yState + 1;
while(xState--)
{
temp = yState;
while (temp--)
{
LCD_WriteData_Color(color);
}
}
}
//画点
//x,y:坐标
//FRONT_COLOR:此点的颜色
void LCD_DrawPoint(u16 x,u16 y)
{
LCD_Set_Window(x, y, x, y); //设置点的位置
LCD_WriteData_Color(FRONT_COLOR);
}
//快速画点
//x,y:坐标
//color:颜色
void LCD_DrawFRONT_COLOR(u16 x,u16 y,u16 color)
{
LCD_Set_Window(x, y, x, y);
LCD_WriteData_Color(color);
}
//读取个某点的颜色值
//x,y:坐标
//返回值:此点的颜色
u16 LCD_ReadPoint(u16 x,u16 y)
{
//u16 r=0,g=0,b=0; //2022 10 31注释
u16 r=0; //2022 10 31注释
if(x>=tftlcd_data.width||y>=tftlcd_data.height)return 0; //超过了范围,直接返回
LCD_Set_Window(x, y, x, y);
#ifdef TFTLCD_ILI9327
LCD_WriteCmd(0X2e);
r=LCD_ReadData(); //dummy Read
r=LCD_ReadData(); //实际坐标颜色
r=(r<<8)|(r>>8);
return r;
#endif
return r;
}
//画线
//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;
}
}
}
void LCD_DrawLine_Color(u16 x1, u16 y1, u16 x2, u16 y2,u16 color)
{
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_DrawFRONT_COLOR(uRow,uCol,color);//画点
xerr+=delta_x ;
yerr+=delta_y ;
if(xerr>distance)
{
xerr-=distance;
uRow+=incx;
}
if(yerr>distance)
{
yerr-=distance;
uCol+=incy;
}
}
}
// 画一个十字的标记
// x:标记的X坐标
// y:标记的Y坐标
// color:标记的颜色
void LCD_DrowSign(uint16_t x, uint16_t y, uint16_t color)
{
uint8_t i;
/* 画点 */
LCD_Set_Window(x-1, y-1, x+1, y+1);
for(i=0; i<9; i++)
{
LCD_WriteData_Color(color);
}
/* 画竖 */
LCD_Set_Window(x-4, y, x+4, y);
for(i=0; i<9; i++)
{
LCD_WriteData_Color(color);
}
/* 画横 */
LCD_Set_Window(x, y-4, x, y+4);
for(i=0; i<9; i++)
{
LCD_WriteData_Color(color);
}
}
//画矩形
//(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
if(size==12)temp=ascii_1206[num][t]; //调用1206字体
else if(size==16)temp=ascii_1608[num][t]; //调用1608字体
else if(size==24)temp=ascii_2412[num][t]; //调用2412字体
else return; //没有的字库
for(t1=0;t1<8;t1++)
{
if(temp&0x80)LCD_DrawFRONT_COLOR(x,y,FRONT_COLOR);
else if(mode==0)LCD_DrawFRONT_COLOR(x,y,BACK_COLOR);
temp<<=1;
y++;
if(y>=tftlcd_data.height)return; //超区域了
if((y-y0)==size)
{
y=y0;
x++;
if(x>=tftlcd_data.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
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
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>=' '))//判断是不是非法字符!
{
if(x>=width){x=x0;y+=size;}
if(y>=height)break;//退出
LCD_ShowChar(x,y,*p,size,0);
x+=size/2;
p++;
}
}
/****************************************************************************
*函数名:LCD_ShowFontHZ
*输 入:x:汉字显示的X坐标
* * y:汉字显示的Y坐标
* * cn:要显示的汉字
* * wordColor:文字的颜色
* * backColor:背景颜色
*输 出:
*功 能:写二号楷体汉字
****************************************************************************/
#if 0
void LCD_ShowFontHZ(u16 x, u16 y, u8 *cn)
{
u8 i, j, wordNum;
u16 color;
while (*cn != '\0')
{
LCD_Set_Window(x, y, x+31, y+28);
for (wordNum=0; wordNum<20; wordNum++)
{ //wordNum扫描字库的字数
if ((CnChar32x29[wordNum].Index[0]==*cn)
&&(CnChar32x29[wordNum].Index[1]==*(cn+1)))
{
for(i=0; i<116; i++)
{ //MSK的位数
color=CnChar32x29[wordNum].Msk[i];
for(j=0;j<8;j++)
{
if((color&0x80)==0x80)
{
LCD_WriteData_Color(FRONT_COLOR);
}
else
{
LCD_WriteData_Color(BACK_COLOR);
}
color<<=1;
}//for(j=0;j<8;j++)结束
}
}
} //for (wordNum=0; wordNum<20; wordNum++)结束
cn += 2;
x += 32;
}
}
#endif
#if 1
void LCD_ShowFontHZ(u16 x, u16 y, u8 *cn)
{
u8 i, j, wordNum;
u16 color;
u16 x0=x;
u16 y0=y;
while (*cn != '\0')
{
for (wordNum=0; wordNum<20; wordNum++)
{ //wordNum扫描字库的字数
if ((CnChar32x29[wordNum].Index[0]==*cn)
&&(CnChar32x29[wordNum].Index[1]==*(cn+1)))
{
for(i=0; i<116; i++)
{ //MSK的位数
color=CnChar32x29[wordNum].Msk[i];
for(j=0;j<8;j++)
{
if((color&0x80)==0x80)
{
LCD_DrawFRONT_COLOR(x,y,FRONT_COLOR);
}
else
{
LCD_DrawFRONT_COLOR(x,y,BACK_COLOR);
}
color<<=1;
x++;
if((x-x0)==32)
{
x=x0;
y++;
if((y-y0)==29)
{
y=y0;
}
}
}//for(j=0;j<8;j++)结束
}
}
} //for (wordNum=0; wordNum<20; wordNum++)结束
cn += 2;
x += 32;
x0=x;
}
}
#endif
void LCD_ShowPicture(u16 x, u16 y, u16 wide, u16 high,u8 *pic)
{
u16 temp = 0;
long tmp=0,num=0;
LCD_Set_Window(x, y, x+wide-1, y+high-1);
num = wide * high*2 ;
do
{
temp = pic[tmp + 1];
temp = temp << 8;
temp = temp | pic[tmp];
LCD_WriteData_Color(temp);//逐点显示
tmp += 2;
}
while(tmp < num);
}