目录
一、常见显示器介绍
1、显示器分类
2、显示器的基本参数
二、TFT-LCD控制原理
1、TFT-LCD结构
2、TFT-LCD控制框图
3、控制原理
LCD数据传输时序
LCD数据传输时序参数
三、SSD1963液晶控制器
1、SSD1963液晶控制器
2、SSD1963内部框图分析
3、8080写时序分析
四、LCD测试程序
1、硬件设计
2、软件设计
(1)首先需要实现一个us级的延时和ms级的延时。
(2)lcd.h
(3)lcd.c分析
五、效果展示
一、常见显示器介绍
1、显示器分类
显示器属于计算机的 I/O 设备,即输入输出设备。它是一种将特定电子信息输出到屏幕上再反射到人眼的显示工具。常见显示器有三类: CRT显示器、LCD液晶显示器、LED点阵显示器
- CRT显示器:CRT显示器是靠电子束激发屏幕内表面的荧光粉来显示图像的,由于荧光粉被点亮后很快会熄灭,所以电子枪必须循环地不断激发这些点。
- LCD显示器:液晶显示器,简称 LCD(Liquid Crystal Display),相对于上一代 CRT显示器,LCD 显示器具有功耗低、体积小、承载的信息量大及不伤眼的优点,因而它成为了现在的主流电子显示设备,其中包括电视、电脑显示器、手机屏幕及各种嵌入式设备的显示器。
- 液晶是一种介于固体和液体之间的特殊物质,它是一种有机化合物,常态下呈液态,但是它的分子排列却和固体晶体一样非常规则,因此取名液晶。如果给液晶施加电场,会改变它的分子排列,从而改变光线的传播方向,配合偏振光片,它就具有控制光线透过率的作用,再配合彩色滤光片,改变加给液晶电压大小,就能改变某一颜色透光量的多少 利用这种原理,做出可控红、绿、蓝光输出强度的显示结构,把三种显示结构组成一个显示单位,通过控制红绿蓝的强度,可以使该单位混合输出不同的色彩,这样的一个显示单位被称为像素
- 注意: 液晶本身是不发光的,所以需要有一个背光灯提供光源
- LED点阵彩色显示器的单个像素点内包含红绿蓝三色LED灯,通过控制红绿蓝颜色的强度进行混色,实现全彩颜色输出,多个像素点构成一个屏幕。由于每个像素点都是LED灯自发光的,所以在户外白天也显示得非常清晰,但由于LED灯体积较大,导致屏幕的像素密度低,所以它一般只适合用于广场上的巨型显示器。相对来说,单色的LED点阵显示器应用得更广泛,如公交车上的信息展示牌、店广告牌等。
- OLED显示器: 新一代的OLED显示器与LED点阵彩色显示器的原理类似,但由于它采用的像素单元是“有机发光二极管”(Organic Light Emitting Diode),所以像素密度比普通LED点阵显示器高得多 OLED显示器不需要背光源、对比度高、轻薄、视角广及响应速度快等优点。待到生产工艺更加成熟时,必将取代现在液晶显示器的地位。
2、显示器的基本参数
像素
像素是组成图像的最基本单元要素,显示器的像素指它成像最小的点,即前面讲解液晶原理中提到的一个显示单元。
分辨率
一些嵌入式设备的显示器常常以“行像素值x列像素值”表示屏幕的分辨率。如分辨率800x480表示该显示器的每一行有800个像素点,每一列有480个像素点,也可理解为有800列,480行。
色彩深度
色彩深度指显示器的每个像素点能表示多少种颜色,一般用“位”(bit)来表示。如单色屏的每个像素点能表示亮或灭两种状态(即实际上能显示2种颜色),用1个数据位就可以表示像素点的所有状态,所以它的色彩深度为1bit,其它常见的显示屏色深为16bit、24bit。
显示器尺寸
显示器的大小一般以英寸表示,如5英寸、21英寸、24英寸等,这个长度是指屏幕对角线的长度, 通过显示器的对角线长度及长宽比可确定显示器的实际长宽尺寸
显存
液晶屏中的每个像素点都是数据,在实际应用中需要把每个像素点的数据缓存起来,再传输给液晶屏,一般会使用 SRAM 或 SDRAM 性质的存储器,而这些专门用于存储显示数据的存储器,则被称为显存。显存一般至少要能存储液晶屏的一帧显示数据
如分辨率为 800x480 的 液 晶 屏 使 用 RGB888 格 式 显 示 , 它 的 一 帧 显 示 数 据 大 小 为 :3x800x480=1152000 字 节 ((8+8+8)*800*480/8);若 使 用 RGB565 格 式 显 示 , 一 帧 显 示 数 据 大 小 为 :2x800x480=768000 字节。 ((5+6+5)*800*480/8)
一般来说,外置的液晶控制器会自带显存,而像 STM32F429等集成液晶控制器的芯片可使用内部 SRAM或外扩 SDRAM用于显存空间
二、TFT-LCD控制原理
1、TFT-LCD结构
- 完整的显示屏由液晶显示面板、电容触摸面板以及 PCB底板构成
- 液晶显示面板:用于显示图像,文字的彩色显示设备液晶显示面板:用于显示图像,文字的彩色显示设备
- 触摸面板:触摸面板带有触摸控制芯片,该芯片处理触摸信号并通过引出的信号线与外部器件通讯,触摸面板中间是透明的,它贴在液晶面板上面,一起构成屏幕的主体
- PCB底板: PCB 底板上可能会带有“液晶控制器芯片”因为控制液晶面板需要比较多的资源,所以大部分低级微控制器都不能直接控制液晶面板,需要额外配套一个专用液晶控制器来处理显示过程,外部微控制器只要把它希望显示的数据直接交给液晶控制器即可。而不带液晶控制器的 PCB底板 ,只有小部分的电源管理电路,液晶面板的信号线与外部微控制器相连,直接控制。
2、TFT-LCD控制框图
STM32F429 系列的芯片不需要额外的液晶控制器,也就是说它把专用液晶控制器的功能集成到 STM32F429 芯片内部了,可以理解为电脑的 CPU集成显卡。而 STM32F407 系列的芯片由于没有集成液晶控制器到芯片内部,所以它只能驱动自带控制器的屏幕,可以理解为电脑的外置显卡。
3、控制原理
液晶面板的控制信号线(不带液晶控制器):
RGB信号线
RGB信号线各有8根,分别用于表示液晶屏一个像素点的红、绿、蓝颜色分量。使用红绿蓝颜色分量来表示颜色是一种通用的做法,打开Windows系统自带的画板调色工具,可看到颜色的红绿蓝分量值,常见的颜色表示会在“RGB”后面附带各个颜色分量值的数据位数
如RGB565表示红绿蓝的数据线数分别为5、6、5根,一共为16个数据位,可表示216种颜色;
如果液晶屏的种颜色分量的数据线有8根,那它表示RGB888格式,一共24位数据线,可表示的颜色为224种。
同步时钟信号CLK
液晶屏与外部使用同步通讯方式,以CLK信号作为同步时钟,在同步时钟的驱动下,每个时钟传输一个像素点数据。
水平同步信号HSYNC
水平同步信号HSYNC(Horizontal Sync)用于表示液晶屏一行像素数据的传输结束,每传输完成液晶屏的一行像素数据时,HSYNC会发生电平跳变,如分辨率为800x480的显示屏(800列,480行),传输一帧的图像HSYNC的电平会跳变480次。
垂直同步信号VSYNC
垂直同步信号VSYNC(Vertical Sync)用于表示液晶屏一帧像素数据的传输结束,每传输完成一帧像素数据时,VSYNC会发生电平跳变。其中“帧”是图像的单位,一幅图像称为一帧,在液晶屏中,一帧指一个完整屏液晶像素点。人们常常用“帧/秒”来表示液晶屏的刷新特性,即液晶屏每秒可以显示多少帧图像,如液晶屏以60帧/秒的速率运行时,VSYNC每秒钟电平会跳变60次。
数据使能信号DE
数据使能信号DE(Data Enable)用于表示数据的有效性,当DE信号线为高电平时,RGB信号线表示的数据有效。
LCD数据传输时序
注:液晶屏显示的图像可看作一个矩形,液晶屏有一个显示指针,它指向将要显示的像素。显示指针的扫描方向方向从左到右、从上到下,一个像素点一个像素点地描绘图形。这些像素点的数据通过RGB数据线传输至液晶屏,它们在同步时钟CLK的驱动下一个一个地传输到液晶屏中,交给显示指针,传输完成一行时,水平同步信号HSYNC电平跳变一次,而传输完一帧时VSYNC电平跳变一次。
LCD数据传输时序参数
液晶显示指针在行与行之间,帧与帧之间切换时需要延时,而且HSYNC及VSYNC信号本身也有宽度,这些时间参数说明见下表:
三、SSD1963液晶控制器
1、SSD1963液晶控制器
STM32F429 系列的芯片不需要额外的液晶控制器,也就是说它把专用液晶控制器的功能集成到 STM32F429 芯片内部了,可以理解为电脑的 CPU集成显卡。而 STM32F407 系列的芯片由于没有集成液晶控制器到芯片内部,所以它只能驱动自带控制器的屏幕,可以理解为电脑的外置显卡。
液晶控制器:
液晶驱动芯片或LCD驱动器,其内部有着较大的缓存空间可以存储文字、图像等数据,并能够将这些信息送入液晶模块进行显示,由于专用的芯片,因此速度往往比较快。
LCD驱动芯片的主要功能就是对主机发送过来的数据/命令,进行变换,变成每个像素的RGB数据,使之在屏幕上显示出来。常见的液晶驱动芯片有ILI932、ILI9328、SSD1963、HX8347、ILI9341、NT5510等
2、SSD1963内部框图分析
特性:内部包含1215KB frame buffer支持分辨率为800*480的显示屏支持像素位深为24bpp的显示模式(RGB888)
3、8080写时序分析
STM32与LCD的电气连接
查询芯片手册关注Tpwcsl TCS TPWLW的延时时间(可以取大,不要取小)
NE连接CS
NWE连接WR
D[15:0]连接D[17:0]
地址线A[x]里面找一根来连接D/C
NE管脚,只有访问地址时才会拉低输出低电平。地址最末位基数代表高电平,偶数代表低电平
我们传输数据是16位一次,并非1个个字节传输。FSMC访问NOR FLASH是使用HADDR,是8位字节地址,而外部存储器是按字16位寻址,那么我们就让HADDR右1位,最低位视同没有用来访问控制存储器。这样我们A0实际对应了A1,那么为了让A0能代表高低电平,所以我们先让HADDR左移,再右移,这样我们A0还是保持了高低电平代表命令或数据。
例:
0x6Cxx xxxx1,先左移变为0010 (0x6C000002)高电平,访问时自动会右移1位
0x6Cxx xxxx0,先左移变为0000(0x6C000000) 低电平
四、LCD测试程序
1、硬件设计
我们使用FSMC模拟8080时序,开启RCC时钟168Mhz,开启串口USART1
cubmx配置,我们使用FSMC的blank1,NE4,外部LCD存储器数据位一次16位,命令和数据标志硬件连接到了A6。
查看LCD芯片手册,查找响应延时时间
Tpwcsl = ADDSET + DATAST+1>30
TCS = ADDSET > 2 ns
TPWLW = DATAST +1 > 12ns
LCD背光
2、软件设计
(1)首先需要实现一个us级的延时和ms级的延时。
#include "delay.h"
uint32_t usctick = 0;
uint32_t time_delay = 0;
extern TIM_HandleTypeDef htim6;
//延时nus
//nus为要延时的us数.
//nus:0~190887435(最大值即2^32/fac_us@fac_us=168)
static uint8_t fac_us = 168; //这里主时钟为168M, 所以在1us内ticks会减168次
void delay_us(uint32_t nus)
{
uint32_t ticks;
uint32_t told,tnow,tcnt=0;
uint32_t reload=SysTick->LOAD; //LOAD的值
ticks=nus*fac_us; //1us需要的节拍数
told=SysTick->VAL; //刚进入时的计数器值
while(1)
{
tnow=SysTick->VAL;
if(tnow!=told)
{
if(tnow<told)tcnt+=told-tnow; //这里注意一下SYSTICK是一个递减的计数器就可以了.
else tcnt+=reload-tnow+told;
told=tnow;
if(tcnt>=ticks)break; //时间超过/等于要延迟的时间,则退出.
}
};
}
void delay_ms(uint16_t nms)
{
delay_us((uint32_t)(nms*1000)); //普通方式延时
}
(2)lcd.h
介绍一个管理LCD重要参数结构体
typedef struct
{
uint16_t width; //LCD 宽度
uint16_t height; //LCD 高度
uint16_t id; //LCD ID
uint8_t dir; //0,竖屏;1,横屏。
uint16_t wramcmd; //开始写gram指令
uint16_t setxcmd; //设置x坐标指令
uint16_t setycmd; //设置y坐标指令
}_lcd_dev; //LCD管理参数结构体
typedef struct
{
vu16 LCD_REG;
vu16 LCD_RAM;
} LCD_TypeDef; //LCD地址结构体
(3)lcd.c分析
我们操作5510控制器定义几个函数
写寄存器序号
写LCD数据
读LCD数据
写寄存器(传入寄存器序号、需要写的数据)
读寄存器(传入寄存器序号),返回数据
开始写GRAM(图像寄存器)
写GRAM(rgb数据)
//写LCD寄存器序号
//regval:寄存器值
void LCD_WR_REG(vu16 regval)
{
regval=regval; //使用-o2优化时候,必须要插入的延时
LCD->LCD_REG=regval;//写入要写的寄存器序号
}
//写LCD数据
//data:要写入的值
void LCD_WR_DATA(vu16 data)
{
data=data; //使用-o2优化时候,必须要插入的延时
LCD->LCD_RAM=data;
}
//读LCD数据
//返回读到的值
u16 LCD_RD_DATA(void)
{
vu16 ram; //防止被优化
ram=LCD->LCD_RAM;
return ram;
}
//写寄存器
//LCD_Reg:寄存器地址
//LCD_RegValue:要写入的数据
void LCD_WriteReg(u16 LCD_Reg,u16 LCD_RegValue)
{
LCD->LCD_REG = LCD_Reg; //写入要写的寄存器序号
LCD->LCD_RAM = LCD_RegValue;//写入数据
}
//读寄存器
//LCD_Reg:寄存器地址
//返回值:读到的数据
u16 LCD_ReadReg(u16 LCD_Reg)
{
LCD_WR_REG(LCD_Reg); //写入要读的寄存器序号
delay_us(5);
return LCD_RD_DATA(); //返回读到的值
}
//开始写GRAM
void LCD_WriteRAM_Prepare(void)
{
LCD->LCD_REG=lcddev.wramcmd;
}
//LCD写GRAM
//RGB_Code:颜色值
void LCD_WriteRAM(u16 RGB_Code)
{
LCD->LCD_RAM = RGB_Code;//写16位GRAM
}
//-o1时间优化需要设置,延时i
void opt_delay(u8 i)
{
while(i--);
}
LCD初始化
void LCD_Init(void)
{
delay_ms(50); // delay 50 ms
LCD_WR_REG(0XDA00);
lcddev.id=LCD_RD_DATA(); //读回0x00
LCD_WR_REG(0XDB00);
lcddev.id=LCD_RD_DATA(); //读回0x00
lcddev.id<<=8;
LCD_WR_REG(0XDC00);
lcddev.id|=LCD_RD_DATA(); //读回0x00
if(lcddev.id==0x8000)lcddev.id=0x5510;/
printf(" LCD ID:%x\r\n",lcddev.id); //NT35510读回的ID是8000H,我们设置为5510方便区分。
if(lcddev.id==0x5510)
{
LCD_WriteReg(0xF000,0x55);
LCD_WriteReg(0xF001,0xAA);
LCD_WriteReg(0xF002,0x52);
LCD_WriteReg(0xF003,0x08);
LCD_WriteReg(0xF004,0x01);
//AVDD Set AVDD 5.2V
LCD_WriteReg(0xB000,0x0D);
LCD_WriteReg(0xB001,0x0D);
LCD_WriteReg(0xB002,0x0D);
//AVDD ratio
LCD_WriteReg(0xB600,0x34);
LCD_WriteReg(0xB601,0x34);
LCD_WriteReg(0xB602,0x34);
//AVEE -5.2V
LCD_WriteReg(0xB100,0x0D);
LCD_WriteReg(0xB101,0x0D);
LCD_WriteReg(0xB102,0x0D);
//AVEE ratio
LCD_WriteReg(0xB700,0x34);
LCD_WriteReg(0xB701,0x34);
LCD_WriteReg(0xB702,0x34);
//VCL -2.5V
LCD_WriteReg(0xB200,0x00);
LCD_WriteReg(0xB201,0x00);
LCD_WriteReg(0xB202,0x00);
//VCL ratio
LCD_WriteReg(0xB800,0x24);
LCD_WriteReg(0xB801,0x24);
LCD_WriteReg(0xB802,0x24);
//VGH 15V (Free pump)
LCD_WriteReg(0xBF00,0x01);
LCD_WriteReg(0xB300,0x0F);
LCD_WriteReg(0xB301,0x0F);
LCD_WriteReg(0xB302,0x0F);
//VGH ratio
LCD_WriteReg(0xB900,0x34);
LCD_WriteReg(0xB901,0x34);
LCD_WriteReg(0xB902,0x34);
//VGL_REG -10V
LCD_WriteReg(0xB500,0x08);
LCD_WriteReg(0xB501,0x08);
LCD_WriteReg(0xB502,0x08);
LCD_WriteReg(0xC200,0x03);
//VGLX ratio
LCD_WriteReg(0xBA00,0x24);
LCD_WriteReg(0xBA01,0x24);
LCD_WriteReg(0xBA02,0x24);
//VGMP/VGSP 4.5V/0V
LCD_WriteReg(0xBC00,0x00);
LCD_WriteReg(0xBC01,0x78);
LCD_WriteReg(0xBC02,0x00);
//VGMN/VGSN -4.5V/0V
LCD_WriteReg(0xBD00,0x00);
LCD_WriteReg(0xBD01,0x78);
LCD_WriteReg(0xBD02,0x00);
//VCOM
LCD_WriteReg(0xBE00,0x00);
LCD_WriteReg(0xBE01,0x64);
//Gamma Setting
LCD_WriteReg(0xD100,0x00);
LCD_WriteReg(0xD101,0x33);
LCD_WriteReg(0xD102,0x00);
LCD_WriteReg(0xD103,0x34);
LCD_WriteReg(0xD104,0x00);
LCD_WriteReg(0xD105,0x3A);
LCD_WriteReg(0xD106,0x00);
LCD_WriteReg(0xD107,0x4A);
LCD_WriteReg(0xD108,0x00);
LCD_WriteReg(0xD109,0x5C);
LCD_WriteReg(0xD10A,0x00);
LCD_WriteReg(0xD10B,0x81);
LCD_WriteReg(0xD10C,0x00);
LCD_WriteReg(0xD10D,0xA6);
LCD_WriteReg(0xD10E,0x00);
LCD_WriteReg(0xD10F,0xE5);
LCD_WriteReg(0xD110,0x01);
LCD_WriteReg(0xD111,0x13);
LCD_WriteReg(0xD112,0x01);
LCD_WriteReg(0xD113,0x54);
LCD_WriteReg(0xD114,0x01);
LCD_WriteReg(0xD115,0x82);
LCD_WriteReg(0xD116,0x01);
LCD_WriteReg(0xD117,0xCA);
LCD_WriteReg(0xD118,0x02);
LCD_WriteReg(0xD119,0x00);
LCD_WriteReg(0xD11A,0x02);
LCD_WriteReg(0xD11B,0x01);
LCD_WriteReg(0xD11C,0x02);
LCD_WriteReg(0xD11D,0x34);
LCD_WriteReg(0xD11E,0x02);
LCD_WriteReg(0xD11F,0x67);
LCD_WriteReg(0xD120,0x02);
LCD_WriteReg(0xD121,0x84);
LCD_WriteReg(0xD122,0x02);
LCD_WriteReg(0xD123,0xA4);
LCD_WriteReg(0xD124,0x02);
LCD_WriteReg(0xD125,0xB7);
LCD_WriteReg(0xD126,0x02);
LCD_WriteReg(0xD127,0xCF);
LCD_WriteReg(0xD128,0x02);
LCD_WriteReg(0xD129,0xDE);
LCD_WriteReg(0xD12A,0x02);
LCD_WriteReg(0xD12B,0xF2);
LCD_WriteReg(0xD12C,0x02);
LCD_WriteReg(0xD12D,0xFE);
LCD_WriteReg(0xD12E,0x03);
LCD_WriteReg(0xD12F,0x10);
LCD_WriteReg(0xD130,0x03);
LCD_WriteReg(0xD131,0x33);
LCD_WriteReg(0xD132,0x03);
LCD_WriteReg(0xD133,0x6D);
LCD_WriteReg(0xD200,0x00);
LCD_WriteReg(0xD201,0x33);
LCD_WriteReg(0xD202,0x00);
LCD_WriteReg(0xD203,0x34);
LCD_WriteReg(0xD204,0x00);
LCD_WriteReg(0xD205,0x3A);
LCD_WriteReg(0xD206,0x00);
LCD_WriteReg(0xD207,0x4A);
LCD_WriteReg(0xD208,0x00);
LCD_WriteReg(0xD209,0x5C);
LCD_WriteReg(0xD20A,0x00);
LCD_WriteReg(0xD20B,0x81);
LCD_WriteReg(0xD20C,0x00);
LCD_WriteReg(0xD20D,0xA6);
LCD_WriteReg(0xD20E,0x00);
LCD_WriteReg(0xD20F,0xE5);
LCD_WriteReg(0xD210,0x01);
LCD_WriteReg(0xD211,0x13);
LCD_WriteReg(0xD212,0x01);
LCD_WriteReg(0xD213,0x54);
LCD_WriteReg(0xD214,0x01);
LCD_WriteReg(0xD215,0x82);
LCD_WriteReg(0xD216,0x01);
LCD_WriteReg(0xD217,0xCA);
LCD_WriteReg(0xD218,0x02);
LCD_WriteReg(0xD219,0x00);
LCD_WriteReg(0xD21A,0x02);
LCD_WriteReg(0xD21B,0x01);
LCD_WriteReg(0xD21C,0x02);
LCD_WriteReg(0xD21D,0x34);
LCD_WriteReg(0xD21E,0x02);
LCD_WriteReg(0xD21F,0x67);
LCD_WriteReg(0xD220,0x02);
LCD_WriteReg(0xD221,0x84);
LCD_WriteReg(0xD222,0x02);
LCD_WriteReg(0xD223,0xA4);
LCD_WriteReg(0xD224,0x02);
LCD_WriteReg(0xD225,0xB7);
LCD_WriteReg(0xD226,0x02);
LCD_WriteReg(0xD227,0xCF);
LCD_WriteReg(0xD228,0x02);
LCD_WriteReg(0xD229,0xDE);
LCD_WriteReg(0xD22A,0x02);
LCD_WriteReg(0xD22B,0xF2);
LCD_WriteReg(0xD22C,0x02);
LCD_WriteReg(0xD22D,0xFE);
LCD_WriteReg(0xD22E,0x03);
LCD_WriteReg(0xD22F,0x10);
LCD_WriteReg(0xD230,0x03);
LCD_WriteReg(0xD231,0x33);
LCD_WriteReg(0xD232,0x03);
LCD_WriteReg(0xD233,0x6D);
LCD_WriteReg(0xD300,0x00);
LCD_WriteReg(0xD301,0x33);
LCD_WriteReg(0xD302,0x00);
LCD_WriteReg(0xD303,0x34);
LCD_WriteReg(0xD304,0x00);
LCD_WriteReg(0xD305,0x3A);
LCD_WriteReg(0xD306,0x00);
LCD_WriteReg(0xD307,0x4A);
LCD_WriteReg(0xD308,0x00);
LCD_WriteReg(0xD309,0x5C);
LCD_WriteReg(0xD30A,0x00);
LCD_WriteReg(0xD30B,0x81);
LCD_WriteReg(0xD30C,0x00);
LCD_WriteReg(0xD30D,0xA6);
LCD_WriteReg(0xD30E,0x00);
LCD_WriteReg(0xD30F,0xE5);
LCD_WriteReg(0xD310,0x01);
LCD_WriteReg(0xD311,0x13);
LCD_WriteReg(0xD312,0x01);
LCD_WriteReg(0xD313,0x54);
LCD_WriteReg(0xD314,0x01);
LCD_WriteReg(0xD315,0x82);
LCD_WriteReg(0xD316,0x01);
LCD_WriteReg(0xD317,0xCA);
LCD_WriteReg(0xD318,0x02);
LCD_WriteReg(0xD319,0x00);
LCD_WriteReg(0xD31A,0x02);
LCD_WriteReg(0xD31B,0x01);
LCD_WriteReg(0xD31C,0x02);
LCD_WriteReg(0xD31D,0x34);
LCD_WriteReg(0xD31E,0x02);
LCD_WriteReg(0xD31F,0x67);
LCD_WriteReg(0xD320,0x02);
LCD_WriteReg(0xD321,0x84);
LCD_WriteReg(0xD322,0x02);
LCD_WriteReg(0xD323,0xA4);
LCD_WriteReg(0xD324,0x02);
LCD_WriteReg(0xD325,0xB7);
LCD_WriteReg(0xD326,0x02);
LCD_WriteReg(0xD327,0xCF);
LCD_WriteReg(0xD328,0x02);
LCD_WriteReg(0xD329,0xDE);
LCD_WriteReg(0xD32A,0x02);
LCD_WriteReg(0xD32B,0xF2);
LCD_WriteReg(0xD32C,0x02);
LCD_WriteReg(0xD32D,0xFE);
LCD_WriteReg(0xD32E,0x03);
LCD_WriteReg(0xD32F,0x10);
LCD_WriteReg(0xD330,0x03);
LCD_WriteReg(0xD331,0x33);
LCD_WriteReg(0xD332,0x03);
LCD_WriteReg(0xD333,0x6D);
LCD_WriteReg(0xD400,0x00);
LCD_WriteReg(0xD401,0x33);
LCD_WriteReg(0xD402,0x00);
LCD_WriteReg(0xD403,0x34);
LCD_WriteReg(0xD404,0x00);
LCD_WriteReg(0xD405,0x3A);
LCD_WriteReg(0xD406,0x00);
LCD_WriteReg(0xD407,0x4A);
LCD_WriteReg(0xD408,0x00);
LCD_WriteReg(0xD409,0x5C);
LCD_WriteReg(0xD40A,0x00);
LCD_WriteReg(0xD40B,0x81);
LCD_WriteReg(0xD40C,0x00);
LCD_WriteReg(0xD40D,0xA6);
LCD_WriteReg(0xD40E,0x00);
LCD_WriteReg(0xD40F,0xE5);
LCD_WriteReg(0xD410,0x01);
LCD_WriteReg(0xD411,0x13);
LCD_WriteReg(0xD412,0x01);
LCD_WriteReg(0xD413,0x54);
LCD_WriteReg(0xD414,0x01);
LCD_WriteReg(0xD415,0x82);
LCD_WriteReg(0xD416,0x01);
LCD_WriteReg(0xD417,0xCA);
LCD_WriteReg(0xD418,0x02);
LCD_WriteReg(0xD419,0x00);
LCD_WriteReg(0xD41A,0x02);
LCD_WriteReg(0xD41B,0x01);
LCD_WriteReg(0xD41C,0x02);
LCD_WriteReg(0xD41D,0x34);
LCD_WriteReg(0xD41E,0x02);
LCD_WriteReg(0xD41F,0x67);
LCD_WriteReg(0xD420,0x02);
LCD_WriteReg(0xD421,0x84);
LCD_WriteReg(0xD422,0x02);
LCD_WriteReg(0xD423,0xA4);
LCD_WriteReg(0xD424,0x02);
LCD_WriteReg(0xD425,0xB7);
LCD_WriteReg(0xD426,0x02);
LCD_WriteReg(0xD427,0xCF);
LCD_WriteReg(0xD428,0x02);
LCD_WriteReg(0xD429,0xDE);
LCD_WriteReg(0xD42A,0x02);
LCD_WriteReg(0xD42B,0xF2);
LCD_WriteReg(0xD42C,0x02);
LCD_WriteReg(0xD42D,0xFE);
LCD_WriteReg(0xD42E,0x03);
LCD_WriteReg(0xD42F,0x10);
LCD_WriteReg(0xD430,0x03);
LCD_WriteReg(0xD431,0x33);
LCD_WriteReg(0xD432,0x03);
LCD_WriteReg(0xD433,0x6D);
LCD_WriteReg(0xD500,0x00);
LCD_WriteReg(0xD501,0x33);
LCD_WriteReg(0xD502,0x00);
LCD_WriteReg(0xD503,0x34);
LCD_WriteReg(0xD504,0x00);
LCD_WriteReg(0xD505,0x3A);
LCD_WriteReg(0xD506,0x00);
LCD_WriteReg(0xD507,0x4A);
LCD_WriteReg(0xD508,0x00);
LCD_WriteReg(0xD509,0x5C);
LCD_WriteReg(0xD50A,0x00);
LCD_WriteReg(0xD50B,0x81);
LCD_WriteReg(0xD50C,0x00);
LCD_WriteReg(0xD50D,0xA6);
LCD_WriteReg(0xD50E,0x00);
LCD_WriteReg(0xD50F,0xE5);
LCD_WriteReg(0xD510,0x01);
LCD_WriteReg(0xD511,0x13);
LCD_WriteReg(0xD512,0x01);
LCD_WriteReg(0xD513,0x54);
LCD_WriteReg(0xD514,0x01);
LCD_WriteReg(0xD515,0x82);
LCD_WriteReg(0xD516,0x01);
LCD_WriteReg(0xD517,0xCA);
LCD_WriteReg(0xD518,0x02);
LCD_WriteReg(0xD519,0x00);
LCD_WriteReg(0xD51A,0x02);
LCD_WriteReg(0xD51B,0x01);
LCD_WriteReg(0xD51C,0x02);
LCD_WriteReg(0xD51D,0x34);
LCD_WriteReg(0xD51E,0x02);
LCD_WriteReg(0xD51F,0x67);
LCD_WriteReg(0xD520,0x02);
LCD_WriteReg(0xD521,0x84);
LCD_WriteReg(0xD522,0x02);
LCD_WriteReg(0xD523,0xA4);
LCD_WriteReg(0xD524,0x02);
LCD_WriteReg(0xD525,0xB7);
LCD_WriteReg(0xD526,0x02);
LCD_WriteReg(0xD527,0xCF);
LCD_WriteReg(0xD528,0x02);
LCD_WriteReg(0xD529,0xDE);
LCD_WriteReg(0xD52A,0x02);
LCD_WriteReg(0xD52B,0xF2);
LCD_WriteReg(0xD52C,0x02);
LCD_WriteReg(0xD52D,0xFE);
LCD_WriteReg(0xD52E,0x03);
LCD_WriteReg(0xD52F,0x10);
LCD_WriteReg(0xD530,0x03);
LCD_WriteReg(0xD531,0x33);
LCD_WriteReg(0xD532,0x03);
LCD_WriteReg(0xD533,0x6D);
LCD_WriteReg(0xD600,0x00);
LCD_WriteReg(0xD601,0x33);
LCD_WriteReg(0xD602,0x00);
LCD_WriteReg(0xD603,0x34);
LCD_WriteReg(0xD604,0x00);
LCD_WriteReg(0xD605,0x3A);
LCD_WriteReg(0xD606,0x00);
LCD_WriteReg(0xD607,0x4A);
LCD_WriteReg(0xD608,0x00);
LCD_WriteReg(0xD609,0x5C);
LCD_WriteReg(0xD60A,0x00);
LCD_WriteReg(0xD60B,0x81);
LCD_WriteReg(0xD60C,0x00);
LCD_WriteReg(0xD60D,0xA6);
LCD_WriteReg(0xD60E,0x00);
LCD_WriteReg(0xD60F,0xE5);
LCD_WriteReg(0xD610,0x01);
LCD_WriteReg(0xD611,0x13);
LCD_WriteReg(0xD612,0x01);
LCD_WriteReg(0xD613,0x54);
LCD_WriteReg(0xD614,0x01);
LCD_WriteReg(0xD615,0x82);
LCD_WriteReg(0xD616,0x01);
LCD_WriteReg(0xD617,0xCA);
LCD_WriteReg(0xD618,0x02);
LCD_WriteReg(0xD619,0x00);
LCD_WriteReg(0xD61A,0x02);
LCD_WriteReg(0xD61B,0x01);
LCD_WriteReg(0xD61C,0x02);
LCD_WriteReg(0xD61D,0x34);
LCD_WriteReg(0xD61E,0x02);
LCD_WriteReg(0xD61F,0x67);
LCD_WriteReg(0xD620,0x02);
LCD_WriteReg(0xD621,0x84);
LCD_WriteReg(0xD622,0x02);
LCD_WriteReg(0xD623,0xA4);
LCD_WriteReg(0xD624,0x02);
LCD_WriteReg(0xD625,0xB7);
LCD_WriteReg(0xD626,0x02);
LCD_WriteReg(0xD627,0xCF);
LCD_WriteReg(0xD628,0x02);
LCD_WriteReg(0xD629,0xDE);
LCD_WriteReg(0xD62A,0x02);
LCD_WriteReg(0xD62B,0xF2);
LCD_WriteReg(0xD62C,0x02);
LCD_WriteReg(0xD62D,0xFE);
LCD_WriteReg(0xD62E,0x03);
LCD_WriteReg(0xD62F,0x10);
LCD_WriteReg(0xD630,0x03);
LCD_WriteReg(0xD631,0x33);
LCD_WriteReg(0xD632,0x03);
LCD_WriteReg(0xD633,0x6D);
//LV2 Page 0 enable
LCD_WriteReg(0xF000,0x55);
LCD_WriteReg(0xF001,0xAA);
LCD_WriteReg(0xF002,0x52);
LCD_WriteReg(0xF003,0x08);
LCD_WriteReg(0xF004,0x00);
//Display control
LCD_WriteReg(0xB100, 0xCC);
LCD_WriteReg(0xB101, 0x00);
//Source hold time
LCD_WriteReg(0xB600,0x05);
//Gate EQ control
LCD_WriteReg(0xB700,0x70);
LCD_WriteReg(0xB701,0x70);
//Source EQ control (Mode 2)
LCD_WriteReg(0xB800,0x01);
LCD_WriteReg(0xB801,0x03);
LCD_WriteReg(0xB802,0x03);
LCD_WriteReg(0xB803,0x03);
//Inversion mode (2-dot)
LCD_WriteReg(0xBC00,0x02);
LCD_WriteReg(0xBC01,0x00);
LCD_WriteReg(0xBC02,0x00);
//Timing control 4H w/ 4-delay
LCD_WriteReg(0xC900,0xD0);
LCD_WriteReg(0xC901,0x02);
LCD_WriteReg(0xC902,0x50);
LCD_WriteReg(0xC903,0x50);
LCD_WriteReg(0xC904,0x50);
LCD_WriteReg(0x3500,0x00);
LCD_WriteReg(0x3A00,0x55); //16-bit/pixel
LCD_WR_REG(0x1100);
delay_us(120);
LCD_WR_REG(0x2900);
}
LCD_Display_Dir(0); //默认为竖屏 后面介绍
LCD_LED=1; //点亮背光 pb15
LCD_Clear(WHITE); //后面介绍
}
LCD开启显示0x2900、LCD关闭显示0x2800
//LCD开启显示
void LCD_DisplayOn(void)
{
if(lcddev.id==0X5510)LCD_WR_REG(0X2900);
}
//LCD关闭显示
void LCD_DisplayOff(void)
{
if(lcddev.id==0X5510)LCD_WR_REG(0X2800);
}
LCD自动扫描方向:0x3600 该命令定义了帧内存的读写扫描方向。该命令不会改变其他驱动程序的状态。
void LCD_Scan_Dir(u8 dir)
{
u16 regval=0;
u16 dirreg=0;
u16 temp;
if(lcddev.id==0X5510)
{
switch(dir)
{
case L2R_U2D://从左到右,从上到下
regval|=(0<<7)|(0<<6)|(0<<5);
break;
case L2R_D2U://从左到右,从下到上
regval|=(1<<7)|(0<<6)|(0<<5);
break;
case R2L_U2D://从右到左,从上到下
regval|=(0<<7)|(1<<6)|(0<<5);
break;
case R2L_D2U://从右到左,从下到上
regval|=(1<<7)|(1<<6)|(0<<5);
break;
case U2D_L2R://从上到下,从左到右
regval|=(0<<7)|(0<<6)|(1<<5);
break;
case U2D_R2L://从上到下,从右到左
regval|=(0<<7)|(1<<6)|(1<<5);
break;
case D2U_L2R://从下到上,从左到右
regval|=(1<<7)|(0<<6)|(1<<5);
break;
case D2U_R2L://从下到上,从右到左
regval|=(1<<7)|(1<<6)|(1<<5);
break;
}
dirreg=0X3600;
//5510不需要BGR
LCD_WriteReg(dirreg,regval);
LCD_WR_REG(lcddev.setxcmd);LCD_WR_DATA(0);
LCD_WR_REG(lcddev.setxcmd+1);LCD_WR_DATA(0);
LCD_WR_REG(lcddev.setxcmd+2);LCD_WR_DATA((lcddev.width-1)>>8);
LCD_WR_REG(lcddev.setxcmd+3);LCD_WR_DATA((lcddev.width-1)&0XFF);
LCD_WR_REG(lcddev.setycmd);LCD_WR_DATA(0);
LCD_WR_REG(lcddev.setycmd+1);LCD_WR_DATA(0);
LCD_WR_REG(lcddev.setycmd+2);LCD_WR_DATA((lcddev.height-1)>>8);
LCD_WR_REG(lcddev.setycmd+3);LCD_WR_DATA((lcddev.height-1)&0XFF);
}
}
设置LCD显示方向:竖屏宽度480,高度800,横屏宽度800,高度480。列寄存器地址0x2A00,行寄存器地址0x2B00。
//设置LCD显示方向
//dir:0,竖屏;1,横屏
void LCD_Display_Dir(u8 dir)
{
lcddev.dir=dir;
if(dir==0) //竖屏
{
if(lcddev.id==0x5510)
{
lcddev.wramcmd=0X2C00; //写命令
lcddev.setxcmd=0X2A00; //列地址
lcddev.setycmd=0X2B00; //行地址
lcddev.width=480;
lcddev.height=800;
}
}else //横屏
{
if(lcddev.id==0x5510)
{
lcddev.wramcmd=0X2C00;
lcddev.setxcmd=0X2A00;
lcddev.setycmd=0X2B00;
lcddev.width=800;
lcddev.height=480;
}
}
LCD_Scan_Dir(DFT_SCAN_DIR); //默认扫描方向
}
设置光标位置:对应行地址、列地址传入坐标值
void LCD_SetCursor(u16 Xpos, u16 Ypos)
{
if(lcddev.id==0X5510)
{
LCD_WR_REG(lcddev.setxcmd);LCD_WR_DATA(Xpos>>8); //x的高8位
LCD_WR_REG(lcddev.setxcmd+1);LCD_WR_DATA(Xpos&0XFF);//x的低8位
LCD_WR_REG(lcddev.setycmd);LCD_WR_DATA(Ypos>>8); //y的高8位
LCD_WR_REG(lcddev.setycmd+1);LCD_WR_DATA(Ypos&0XFF) //y的低8位
}
}
读点函数
读取内存(0x2E00)。定位到列与行的开始位置。然后从帧内存中读取D[23:0],列寄存器和行寄存器增量读取,可以通过发送任何其他命令来取消。
//读取某个点的颜色值
u32 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); //定位x、y对应行、列的寄存器地址
if(lcddev.id==0X5510)LCD_WR_REG(0X2E00); //5510发送GRAM指令,读取命令
r=LCD_RD_DATA(); //dummy Read
opt_delay(2);
r=LCD_RD_DATA(); //从这里开始,实际坐标颜色
opt_delay(2);
b=LCD_RD_DATA();
g=r&0XFF; //第一次读取的是RG的值,R在前,G在后,各占8位。
g<<=8;
return (((r>>11)<<11)|((g>>10)<<5)|(b>>11)); //r5位,g6位,b5位
}
设置窗口
//设置狂口,并自动设置画点坐标在窗口左上角(sx,sy)
//窗口宽度和高度,必须大于0
//窗体大小:width*height
void LCD_Set_Window(u16 sx,u16 sy,u16 width,u16 height)
{
u16 twidth,theight;
twidth=sx+width-1;
theight=sy+height-1;
if(lcddev.id==0X5510)
{
LCD_WR_REG(lcddev.setxcmd);LCD_WR_DATA(sx>>8);
LCD_WR_REG(lcddev.setxcmd+1);LCD_WR_DATA(sx&0XFF);
LCD_WR_REG(lcddev.setxcmd+2);LCD_WR_DATA(twidth>>8);
LCD_WR_REG(lcddev.setxcmd+3);LCD_WR_DATA(twidth&0XFF);
LCD_WR_REG(lcddev.setycmd);LCD_WR_DATA(sy>>8);
LCD_WR_REG(lcddev.setycmd+1);LCD_WR_DATA(sy&0XFF);
LCD_WR_REG(lcddev.setycmd+2);LCD_WR_DATA(theight>>8);
LCD_WR_REG(lcddev.setycmd+3);LCD_WR_DATA(theight&0XFF);
}
}
清屏
//清屏函数
//color:要清屏的填充色
void LCD_Clear(u32 color)
{
u32 index=0;
u32 totalpoint=lcddev.width;
totalpoint*=lcddev.height; //800*480
LCD_SetCursor(0x00,0x0000); //光标位置
LCD_WriteRAM_Prepare(); //开始写入GRAM
for(index=0;index<totalpoint;index++)
{
LCD->LCD_RAM=color;
}
}
在指定区域内填充单个颜色
//在指定区域内填充单个颜色
//区域大小为::(ex-sx+1)*(ey-sy+1)
//color:要填充的颜色
void LCD_Fill(u16 sx,u16 sy,u16 ex,u16 ey,u32 color)
{
u16 i,j;
u16 xlen=0;
xlen=ex-sx+1;
for(i=sy;i<=ey;i++)
{
LCD_SetCursor(sx,i); //设置光标位置
LCD_WriteRAM_Prepare(); //开始写入GRAM
for(j=0;j<xlen;j++)LCD->LCD_RAM=color; //显示颜色
}
}
画点
//画点
//x,y:坐标
//POINT_COLOR:此点的颜色
void LCD_DrawPoint(u16 x,u16 y)
{
LCD_SetCursor(x,y); //设置光标位置
LCD_WriteRAM_Prepare(); //开始写入GRAM
LCD->LCD_RAM=POINT_COLOR;
}
快速画点
//快速画点
//x,y:坐标
//color:颜色
void LCD_Fast_DrawPoint(u16 x,u16 y,u32 color)
{
if(lcddev.id==0X5510)
{
LCD_WR_REG(lcddev.setxcmd);LCD_WR_DATA(x>>8);
LCD_WR_REG(lcddev.setxcmd+1);LCD_WR_DATA(x&0XFF);
LCD_WR_REG(lcddev.setycmd);LCD_WR_DATA(y>>8);
LCD_WR_REG(lcddev.setycmd+1);LCD_WR_DATA(y&0XFF);
}
LCD->LCD_REG=lcddev.wramcmd;
LCD->LCD_RAM=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; //设置单步方向,+1
else if(delta_x==0)incx=0; //垂直线,不加
else {incx=-1;delta_x=-delta_x;} //反方向,-1
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) //确定下一点是画在x+1,还是x
{
xerr-=distance;
uRow+=incx;
}
if(yerr>distance) //确定下一点是画在y+1,还是y
{
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--;
}
}
}
五、效果展示
实验效果在主函数中使用填充、画线、画圆等函数。源码会在后面分享
int main(void)
{
/* USER CODE BEGIN 1 */
/* USER CODE END 1 */
/* MCU Configuration----------------------------------------------------------*/
/* Reset of all peripherals, Initializes the Flash interface and the Systick. */
HAL_Init();
/* USER CODE BEGIN Init */
/* USER CODE END Init */
/* Configure the system clock */
SystemClock_Config();
/* USER CODE BEGIN SysInit */
/* USER CODE END SysInit */
/* Initialize all configured peripherals */
MX_GPIO_Init();
MX_FSMC_Init();
MX_USART1_UART_Init();
/* USER CODE BEGIN 2 */
LCD_Init();
printf("lcd is ok\r\n");
LCD_Fill(100,100,200,200,RED);
LCD_DrawLine(210, 210, 300, 300);
LCD_DrawRectangle(310, 310, 350, 350); //»¾ØÕó
LCD_Draw_Circle(400,400,50);
/* USER CODE END 2 */
/* Infinite loop */
/* USER CODE BEGIN WHILE */
while (1)
{
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
}
/* USER CODE END 3 */
}