- #include "lcd.h"
- #include "stdlib.h"
- #include "font.h"
- #include "delay.h"
-
- //画笔颜 {MOD},背景颜 {MOD}
- u16 POINT_COLOR = 0x0000, BACK_COLOR = WHITE;//0xFFFF;
- //写寄存器函数
- void LCD_WR_REG(u8 data)
- {
- LCD_RS_CLR;//写地址
- LCD_CS_CLR;
- LCD_RD_SET;
- LCD_WR_CLR;
- DATAOUT(data);
- LCD_WR_SET;
- LCD_RS_SET;
- LCD_CS_SET;
- }
- //写寄存器
- void LCD_WriteReg(u8 LCD_Reg, u16 LCD_RegValue)
- {
- LCD_WR_REG(LCD_Reg);
- LCD_WR_DATA(LCD_RegValue);
- }
- //开始写GRAM
- void LCD_WriteRAM_Prepare(void)
- {
- LCD_WR_REG(0x2C);
- }
- //LCD写GRAM
- void LCD_WriteRAM(u16 RGB_Code)
- {
- LCD_WR_DATA(RGB_Code);//写十六位GRAM
- }
- //从ILI93xx读出的数据为GBR格式,而我们写入的时候为RGB格式。
- //通过该函数转换
- //c:GBR格式的颜 {MOD}值
- //返回值:RGB格式的颜 {MOD}值
- u16 LCD_BGR2RGB(u16 c)
- {
- u16 r,g,b,rgb;
- b=(c>>0)&0x1f;
- g=(c>>5)&0x3f;
- r=(c>>11)&0x1f;
- rgb=(b<<11)+(g<<5)+(r<<0);
- return(rgb);
- }
- //LCD开启显示 ILI9341
- void LCD_DisplayOn(void)
- {
- LCD_WR_REG(0X29); //26万 {MOD}显示开启
- }
- //LCD关闭显示 ILI9341
- void LCD_DisplayOff(void)
- {
- LCD_WR_REG(0X28);//关闭显示
- }
- //设置光标位置
- //Xpos:横坐标
- //Ypos:纵坐标
- void LCD_SetCursor(u16 Xpos, u16 Ypos)
- {
- LCD_WR_REG(0X2A);
- LCD_WR_DATA(Xpos>>8);
- LCD_WR_DATA(Xpos&0XFF);
- LCD_WR_REG(0X2B);
- LCD_WR_DATA(Ypos>>8);
- LCD_WR_DATA(Ypos&0XFF);
- }
- //画点
- //x:0~239
- //y:0~319
- //POINT_COLOR:此点的颜 {MOD}
- void LCD_DrawPoint(u16 x,u16 y)
- {
- LCD_SetCursor(x,y);//设置光标位置
- LCD_WR_REG(0X2C);//开始写入GRAM
- LCD_WR_DATA(POINT_COLOR);
- }
- void LCD_Fast_DrawPoint(u16 x,u16 y,u16 color)
- {
- LCD_WR_REG(0x2A);
- LCD_WR_DATA(x>>8);
- LCD_WR_DATA(x&0XFF);
- LCD_WR_REG(0x2B);
- LCD_WR_DATA(y>>8);
- LCD_WR_DATA(y&0XFF);
-
- LCD_WR_REG(0X2C);//开始写入GRAM
- LCD_WR_DATA(color);
- }
- //初始化lcd
- //该初始化函数可以初始化各种ILI93XX液晶,但是其他函数是基于ILI9320的!!!
- //在其他型号的驱动芯片上没有测试!
- void LCD_Init(void)
- {
- GPIO_InitTypeDef GPIO_InitStructure;
-
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_GPIOB|RCC_APB2Periph_GPIOD|RCC_APB2Periph_AFIO, ENABLE );
- GPIO_PinRemapConfig(GPIO_Remap_SWJ_JTAGDisable , ENABLE); //JTAG-DP 失能 + SW-DP使能
-
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_All;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; //推挽输出
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_Init(GPIOD, &GPIO_InitStructure);
- GPIO_Write(GPIOD,0XFF);
-
- //LCDBL,RD,WR,RS,CS
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3|GPIO_Pin_4|GPIO_Pin_5|GPIO_Pin_6|GPIO_Pin_7;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; //推挽输出
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_Init(GPIOB, &GPIO_InitStructure);
- GPIO_SetBits(GPIOB, GPIO_Pin_3|GPIO_Pin_4|GPIO_Pin_5|GPIO_Pin_6|GPIO_Pin_7); //CS RS WR RD
-
- //LCDRESET
- GPIO_InitStructure.GPIO_Pin =GPIO_Pin_15;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; //推挽输LCDRESET
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_Init(GPIOA, &GPIO_InitStructure);
- GPIO_SetBits(GPIOA, GPIO_Pin_15);
-
- LCDBKON;//点亮LCD背光
- //***************************RESET LCD Driver******************************
- LCD_RESET_SET; //reset-->1
- delay_ms(1); //Delay 1ms
- LCD_RESET_CLR; //reset-->0
- delay_ms(10); //Delay 10ms
- LCD_RESET_SET; //reset-->1
- delay_ms(120); //Delay 200ms
- //************************Start initial sequence***************************
- LCD_WR_REG(0xCF);
- LCD_WR_DATA(0x00);
- LCD_WR_DATA(0xDB);//83
- LCD_WR_DATA(0X30);
- LCD_WR_REG(0xB1);
- LCD_WR_DATA(0x00);
- LCD_WR_DATA(0x18);
- LCD_WR_REG(0xED);
- LCD_WR_DATA(0x64);
- LCD_WR_DATA(0x03);
- LCD_WR_DATA(0X12);
- LCD_WR_DATA(0x81);
- LCD_WR_REG(0xE8);
- LCD_WR_DATA(0x85);
- LCD_WR_DATA(0x00);
- LCD_WR_DATA(0x70);
- LCD_WR_REG(0xCB);
- LCD_WR_DATA(0x39);
- LCD_WR_DATA(0x2C);
- LCD_WR_DATA(0x00);
- LCD_WR_DATA(0x34);
- LCD_WR_DATA(0x02);
- LCD_WR_REG(0xF7);
- LCD_WR_DATA(0x20);
- LCD_WR_REG(0xEA);
- LCD_WR_DATA(0x00);
- LCD_WR_DATA(0x00);
- LCD_WR_REG(0xC0);
- LCD_WR_DATA(0x22);
- LCD_WR_REG(0xC1);
- LCD_WR_DATA(0x12);
- LCD_WR_REG(0xC5);
- LCD_WR_DATA(0x5C);
- LCD_WR_DATA(0x4C);
- LCD_WR_REG(0xC7);
- LCD_WR_DATA(0x8F);
- LCD_WR_REG(0x36);
- LCD_WR_DATA(0x48);
- LCD_WR_REG(0x3A);
- LCD_WR_DATA(0x55);
- LCD_WR_REG(0xF2);
- LCD_WR_DATA(0x02);
- LCD_WR_REG(0x26);
- LCD_WR_DATA(0x01);
- LCD_WR_REG(0xE0);
- LCD_WR_DATA(0x0F);
- LCD_WR_DATA(0x20);
- LCD_WR_DATA(0x19);
- LCD_WR_DATA(0x0F);
- LCD_WR_DATA(0x10);
- LCD_WR_DATA(0x08);
- LCD_WR_DATA(0x4A);
- LCD_WR_DATA(0xF6);
- LCD_WR_DATA(0x3A);
- LCD_WR_DATA(0x0F);
- LCD_WR_DATA(0x14);
- LCD_WR_DATA(0x09);
- LCD_WR_DATA(0x18);
- LCD_WR_DATA(0x0B);
- LCD_WR_DATA(0x08);
- LCD_WR_REG(0xE1);
- LCD_WR_DATA(0x00);
- LCD_WR_DATA(0x1E);
- LCD_WR_DATA(0x1E);
- LCD_WR_DATA(0x05);
- LCD_WR_DATA(0x0F);
- LCD_WR_DATA(0x04);
- LCD_WR_DATA(0x31);
- LCD_WR_DATA(0x33);
- LCD_WR_DATA(0x43);
- LCD_WR_DATA(0x04);
- LCD_WR_DATA(0x0B);
- LCD_WR_DATA(0x06);
- LCD_WR_DATA(0x27);
- LCD_WR_DATA(0x34);
- LCD_WR_DATA(0x0F);
- LCD_WR_REG(0x11);
-
- delay_ms(120);
- LCD_WR_REG(0x29);
- LCD_WR_REG(0x2A);
- LCD_WR_DATA(0x00);
- LCD_WR_DATA(0x00);
- LCD_WR_DATA(0x00);
- LCD_WR_DATA(0xEF);
- LCD_WR_REG(0x2B);
- LCD_WR_DATA(0x00);
- LCD_WR_DATA(0x00);
- LCD_WR_DATA(0x01);
- LCD_WR_DATA(0x3F);
- LCD_WR_REG(0x2C);
- delay_ms(10);
- LCD_Clear(WHITE);
- }
-
- //清屏函数
- //Color:要清屏的填充 {MOD}
- void LCD_Clear(u16 Color)
- {
- u32 index=0;
- LCD_SetCursor(0x00,0x0000);//设置光标位置
- LCD_WriteRAM_Prepare(); //开始写入GRAM
- for(index=0;index<76800;index++)
- {
- LCD_WR_DATA(Color);
- }
- }
- //在指定区域内填充指定颜 {MOD}
- //区域大小:
- // (xend-xsta)*(yend-ysta)
- void LCD_Fill(u16 xsta,u16 ysta,u16 xend,u16 yend,u16 color)
- {
- u16 i,j;
- u16 xlen=0;
- xlen=xend-xsta+1;
- for(i=ysta;i<=yend;i++)
- {
- LCD_SetCursor(xsta,i); //设置光标位置
- LCD_WriteRAM_Prepare(); //开始写入GRAM
- for(j=0;j<xlen;j++)LCD_WR_DATA(color);//设置光标位置
- }
-
- }
- //在指定区域内填充指定颜 {MOD}块
- //(sx,sy),(ex,ey):填充矩形对角坐标,区域大小为:(ex-sx+1)*(ey-sy+1)
- //color:要填充的颜 {MOD}
- 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); //设置光标位置
- LCD_WriteRAM_Prepare(); //开始写入GRAM
- for(j=0;j<width;j++)
- LCD_WR_DATA(color[i*height+j]);//写入数据
- }
- }
- //画线
- //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_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 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-b,y0-a); //3
- LCD_DrawPoint(x0+b,y0-a); //0
- LCD_DrawPoint(x0-a,y0+b); //1
- LCD_DrawPoint(x0-b,y0-a); //7
- LCD_DrawPoint(x0-a,y0-b); //2
- LCD_DrawPoint(x0+b,y0+a); //4
- LCD_DrawPoint(x0+a,y0-b); //5
- LCD_DrawPoint(x0+a,y0+b); //6
- LCD_DrawPoint(x0-b,y0+a);
- a++;
- //使用Bresenham算法画圆
- if(di<0)di +=4*a+6;
- else
- {
- di+=10+4*(a-b);
- b--;
- }
- LCD_DrawPoint(x0+a,y0+b);
- }
- }
- void Draw_Circle2(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-b,y0-a); //3
- LCD_DrawPoint(x0+b,y0-a); //0
- //LCD_DrawPoint(x0-a,y0+b); //1
- //LCD_DrawPoint(x0-b,y0-a); //7
- LCD_DrawPoint(x0-a,y0-b); //2
- LCD_DrawPoint(x0+b,y0+a); //4
- LCD_DrawPoint(x0+a,y0-b); //5
- //LCD_DrawPoint(x0+a,y0+b); //6
- //LCD_DrawPoint(x0-b,y0+a);
- a++;
- //使用Bresenham算法画圆
- if(di<0)di +=4*a+6;
- else
- {
- di+=10+4*(a-b);
- b--;
- }
- //LCD_DrawPoint(x0+a,y0+b);
- }
- }
- //在指定位置显示一个字符
- //x:0~234
- //y:0~308
- //num:要显示的字符:" "--->"~"
- //size:字体大小 12/16
- //mode:叠加方式(1)还是非叠加方式(0)
- //在指定位置显示一个字符
- //x:0~234
- //y:0~308
- //num:要显示的字符:" "--->"~"
- //size:字体大小 12/16
- //mode:叠加方式(1)还是非叠加方式(0)
- void LCD_ShowChar(u16 x,u16 y,u8 num,u8 size,u8 mode)
- {
- u8 temp;
- u8 pos,t;
- u16 x0=x;
- u16 colortemp=POINT_COLOR;
-
- if(x>(LCDWIDTH-size/2)||y>(LCDHEIGHT-size))return;
- //设置窗口
- num=num-' ';//得到偏移后的值
- if(!mode) //非叠加方式
- {
- for(pos=0;pos<size;pos++)
- {
- if(size==12)temp=asc2_1206[num][pos];//调用1206字体
- else temp=asc2_1608[num][pos]; //调用1608字体
- for(t=0;t<size/2;t++)
- {
- if(temp&0x01)POINT_COLOR=colortemp;
- else POINT_COLOR=WHITE;//BACK_COLOR;
- LCD_DrawPoint(x,y);
- temp>>=1;
- x++;
- }
- x=x0;
- y++;
- }
- }else//叠加方式
- {
- for(pos=0;pos<size;pos++)
- {
- if(size==12)temp=asc2_1206[num][pos];//调用1206字体
- else temp=asc2_1608[num][pos]; //调用1608字体
- for(t=0;t<size/2;t++)
- {
- if(temp&0x01)LCD_DrawPoint(x+t,y+pos);//画一个点
- temp>>=1;
- }
- }
- }
- POINT_COLOR=colortemp;
- }
- //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:颜 {MOD}
- //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>=' '))//判断是不是非法字符!
- {
- if(x>=width){x=x0;y+=size;}
- if(y>=height)break;//退出
- LCD_ShowChar(x,y,*p,size,0);
- x+=size/2;
- p++;
- }
- }
- void LCDOpenWindows(u16 x, u16 y, u16 len, u16 wid)
- {
- LCD_WR_REG(0X2A);
- LCD_WR_DATA(x>>8); //start
- LCD_WR_DATA(x-((x>>8)<<8));
- LCD_WR_DATA((x+len-1)>>8); //end
- LCD_WR_DATA((x+len-1)-(((x+len-1)>>8)<<8));
-
- LCD_WR_REG(0X2B);
- LCD_WR_DATA(y>>8); //start
- LCD_WR_DATA(y-((y>>8)<<8));
- LCD_WR_DATA((y+wid-1)>>8); //end
- LCD_WR_DATA((y+wid-1)-(((y+wid-1)>>8)<<8));
- LCD_WR_REG(0x2C);
- }
- /****************************************************************************
- * 名 称:void ili9341_DrawPicture(u16 StartX,u16 StartY,u16 EndX,u16 EndY,u16 *pic)
- * 功 能:在指定座标范围显示一副图片
- * 入口参数:StartX 行起始座标
- * StartY 列起始座标
- * EndX 行结束座标
- * EndY 列结束座标
- pic 图片头指针
- * 出口参数:无
- * 说 明:图片取模格式为水平扫描,16位颜 {MOD}模式
- * 调用方法:ili9320_DrawPicture(0,0,100,100,(u16*)demo);
- ****************************************************************************/
- void ili9341_DrawPicture(u16 StartX,u16 StartY,u16 Xend,u16 Yend,u8 *pic)
- {
- static u16 i=0,j=0;
-
- u16 *bitmap = (u16 *)pic;
-
- LCDOpenWindows(StartX,StartY,Xend,Yend);
-
- for(i=0;i<Yend;i++)
- {
- for(j=0;j<Xend;j++) LCD_WriteRAM(*bitmap++);
- }
-
- }
复制代码
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
刷全屏需要多久啊!我的600ms才能完整的刷一次屏。好慢。怎么回事。SPI12M
一周热门 更多>