最近在使用5寸LCD做一个项目。显示功能简单,但要求字体足够醒目。所以采用了256*128超大字体。使用PCtoLCD2002软件取模显示后发现字体刷新速度很慢,甚至眼睛可以看到刷新过程。我用的STM32F103,主频可达到72MHz,LCD 分辨率为800*480,而且使用的FSMC方式显示,理论上刷新频率应该能达到150帧/s( 72M/800/480 ),现在只是显示几个256*128 数字,不应该这么慢。经过最近的研究和调试,终于把速度提上去了。方式如下:
1.修改字体显示驱动程序
此方式是提高速度最重要的方式。之前LCD厂家给的驱动底层循环操作繁琐,执行效率太低,其原始代码如下:
void LCD_ShowChar(u16 x,u16 y,u8 num,u8 size,u8 mode)
{
u8 temp,t1,t;
u16 z=0;
u16 x0=x;
u16 colortemp=POINT_COLOR;
u16 qyt=0;
num=num-' ';//µÃµ½Æ«ÒƺóµÄÖµ
if(size==16)
{
qyt=16;
}else
{
qyt=64; //·ÖΪÁ½ÁÐÏÔʾ£¬¹ÊΪ32*2
}
z=num;
z*=qyt;
//ÉèÖô°¿Ú
if(!mode) //·Çµþ¼Ó·½Ê½
{
for(t=0;t<qyt;t++)
{
if(size==16)
{
temp=asc1608[z]; //µ÷ÓÃ1608×ÖÌå
}else
{
temp=asc3216[z]; //µ÷ÓÃ3216×ÖÌå
}
z++;
for(t1=0;t1<8;t1++)
{
if(temp&0x01)POINT_COLOR=colortemp;
else POINT_COLOR=BACK_COLOR;
LCD_DrawPoint(x,y);
temp>>=1;
x++;
if(x>=lcddev.width){POINT_COLOR=colortemp;return;}//³¬ÇøÓòÁË
if((x-x0)==(size/2))
{
x=x0;
y++;
if(y>=lcddev.height){POINT_COLOR=colortemp;return;}//³¬ÇøÓòÁË
break;
}
}
}
}else//µþ¼Ó·½Ê½
{
for(t=0;t<qyt;t++)
{
if(size==16)
{
temp=asc1608[z]; //µ÷ÓÃ1608×ÖÌå
}else
{
temp=asc3216[z]; //µ÷ÓÃ3216×ÖÌå
}
z++;
for(t1=0;t1<8;t1++)
{
if(temp&0x01)LCD_DrawPoint(x,y);
temp>>=1;
x++;
if(x>=lcddev.width){POINT_COLOR=colortemp;return;}//³¬ÇøÓòÁË
if((x-x0)==(size/2))
{
x=x0;
y++;
if(y>=lcddev.height){POINT_COLOR=colortemp;return;}//³¬ÇøÓòÁË
break;
}
}
}
}
POINT_COLOR=colortemp;
}
此函数的大致思路是,先判断待显示字体的大小(1608或3216两种字体),根据字模宽度显示每一行像素点的信息,然后再显示第二行等等。同时根据字模信息得到每一个像素点处应该绘制背景颜色还是画笔颜色。函数中调用的绘点函数如下:
//»µã
//x,y:×ø±ê
//POINT_COLOR:´ËµãµÄÑÕÉ«
void LCD_DrawPoint(u16 x,u16 y)
{
LCD_SetCursor(x,y); //ÉèÖùâ±êλÖÃ
LCD->LCD_REG = lcddev.wramcmd;
// LCD_WriteRAM_Prepare(); //¿ªÊ¼Ð´ÈëGRAM
LCD->LCD_RAM = POINT_COLOR;
}
其中,光标设置函数如下:
void LCD_SetCursor(u16 Xpos, u16 Ypos)
{
LCD->LCD_REG = 0x002A;
// LCD->LCD_RAM = Xpos;
LCD->LCD_RAM = Xpos>>8; //д¸ß°Ëλ
LCD->LCD_RAM = Xpos&0x00ff; //дµÍ°Ëλ
LCD->LCD_RAM = 799>>8; //дÈë×î´óXÖµ
LCD->LCD_RAM = 799&0x00ff;
LCD->LCD_REG = 0x002b;
LCD->LCD_RAM = Ypos>>8;
LCD->LCD_RAM = Ypos&0x00ff;
LCD->LCD_RAM = 479>>8; //дÈë×î´óYÖµ
LCD->LCD_RAM = 479&0x00ff;
}
可以看到,整个程序在显示字体,每一个像素点的显示都进行了很复杂的操作,几乎没有发挥到SSD1963的性能。字体小的时候看不出性能,但是如果用256*128字体,效果差异就很大了。因为每个字体包含3.2万个像素点,仅限一个字母,设置光标、绘点等一系列操作就要循环3.2万次。显示效果肯定会很慢。通过阅读SSD1963手册可以发现,其实字母显示完全没有必要对每一个像素点进行重定位,只需设置左上角和右下角坐标,然后持续写入颜色数据即可。
修改后的驱动函数如下:
void LCD_ShowNumBig(u16 x,u16 y,u8 num)
{
u8 temp,t1;
u16 t = 0;
u16 z=0;
u16 qyt=BIG_H*(BIG_W>>3); //¼ÆËãÿ¸ö×ÖĸռÓõÄ×Ö½ÚÊý
z=num;
z*=qyt;
LCD_FastSetCursor(x,y,x+BIG_W-1,y+BIG_H-1); //ÉèÖÃÏÔʾÇøÓò
LCD->LCD_REG = LCD1963_WRITE_MEM; //Æô¶¯´æ´¢Æ÷дÃüÁî
for(t=0;t<qyt;t++)
{
temp = asc256128[z++];
for(t1=0;t1<8;t1++)
{
LCD->LCD_RAM = temp&0x80? POINT_COLOR : BACK_COLOR;
temp<<=1;
}
}
}
其中 BIG_H和BIG_W分别是字体的高和宽。采用此方法,速度可提高约20倍。
2.去掉字模中的无用信息
字体取模时我们可以看到如下图所示,字母的上下左右包含很多空白区域(无用信息),我们可以通过设置红框内的参数,将上下左右的无用信息裁剪掉,此方法可以提升约40%的速度,降低2/5字模存储容量。
3.降低FSMC的数据建立时间和保持时间
FSMC初始化代码如下,对参数进行设置即可。
void LCD_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
FSMC_NORSRAMInitTypeDef FSMC_NORSRAMInitStructure;
FSMC_NORSRAMTimingInitTypeDef readWriteTiming;
FSMC_NORSRAMTimingInitTypeDef writeTiming;
// Enable FSMC, GPIOD, GPIOE clocks
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_FSMC, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB | RCC_APB2Periph_GPIOD | RCC_APB2Periph_GPIOE,ENABLE);
// Set PD.00(D2), PD.01(D3), PD.04(NOE), PD.05(NWE), PD.07(NE1), PD.08(D13),
// PD.09(D14), PD.10(D15), PD.13(A18), PD.14(D0), PD.15(D1) as alternate
// function push pull
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_4 | GPIO_Pin_5 |
GPIO_Pin_7 | GPIO_Pin_8 | GPIO_Pin_9 | GPIO_Pin_10|
GPIO_Pin_13 | GPIO_Pin_14 | GPIO_Pin_15;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(GPIOD, &GPIO_InitStructure);
// Set PE.07(D4), PE.08(D5), PE.09(D6), PE.10(D7), PE.11(D8), PE.12(D9), PE.13(D10),
// PE.14(D11), PE.15(D12) as alternate function push pull */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7 | GPIO_Pin_8 | GPIO_Pin_9 | GPIO_Pin_10 |
GPIO_Pin_11 | GPIO_Pin_12 | GPIO_Pin_13 | GPIO_Pin_14 |
GPIO_Pin_15;
GPIO_Init(GPIOE, &GPIO_InitStructure);
//PB14 --> LCD_RST
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(GPIOB, &GPIO_InitStructure);
readWriteTiming.FSMC_AddressSetupTime = 1; //µØÖ·½¨Á¢Ê±¼ä£¨ADDSET£©Îª2¸öHCLK 1/36M=27ns
readWriteTiming.FSMC_AddressHoldTime = 1; //µØÖ·±£³Öʱ¼ä£¨ADDHLD£©Ä£Ê½AδÓõ½
readWriteTiming.FSMC_DataSetupTime = 1; // Êý¾Ý±£´æʱ¼äΪ16¸öHCLK,ÒòΪҺ¾§Çý¶¯ICµÄ¶ÁÊý¾ÝµÄʱºò£¬ËٶȲ»ÄÜÌ«¿ì£¬ÓÈÆä¶Ô1289Õâ¸öIC¡£
readWriteTiming.FSMC_BusTurnAroundDuration = 1;
readWriteTiming.FSMC_CLKDivision = 1;
readWriteTiming.FSMC_DataLatency = 1;
readWriteTiming.FSMC_AccessMode = FSMC_AccessMode_A; //ģʽA
writeTiming.FSMC_AddressSetupTime = 1; //µØÖ·½¨Á¢Ê±¼ä£¨ADDSET£©Îª1¸öHCLK
writeTiming.FSMC_AddressHoldTime = 1; //µØÖ·±£³Öʱ¼ä£¨A
writeTiming.FSMC_DataSetupTime = 1; ////Êý¾Ý±£´æʱ¼äΪ4¸öHCLK
writeTiming.FSMC_BusTurnAroundDuration =1;
writeTiming.FSMC_CLKDivision =1;
writeTiming.FSMC_DataLatency =1;
writeTiming.FSMC_AccessMode = FSMC_AccessMode_A; //ģʽA
FSMC_NORSRAMInitStructure.FSMC_Bank = FSMC_Bank1_NORSRAM1;// ÕâÀïÎÒÃÇʹÓÃNE4 £¬Ò²¾Í¶ÔÓ¦BTCR[6],[7]¡£
FSMC_NORSRAMInitStructure.FSMC_DataAddressMux = FSMC_DataAddressMux_Disable; // ²»¸´ÓÃÊý¾ÝµØÖ·
FSMC_NORSRAMInitStructure.FSMC_MemoryType =FSMC_MemoryType_SRAM;// FSMC_MemoryType_SRAM; //SRAM
FSMC_NORSRAMInitStructure.FSMC_MemoryDataWidth = FSMC_MemoryDataWidth_16b;//´æ´¢Æ÷Êý¾Ý¿í¶ÈΪ16bit
FSMC_NORSRAMInitStructure.FSMC_BurstAccessMode =FSMC_BurstAccessMode_Disable;// FSMC_BurstAccessMode_Disable;
FSMC_NORSRAMInitStructure.FSMC_WaitSignalPolarity = FSMC_WaitSignalPolarity_Low;
FSMC_NORSRAMInitStructure.FSMC_AsynchronousWait=FSMC_AsynchronousWait_Disable;
FSMC_NORSRAMInitStructure.FSMC_WrapMode = FSMC_WrapMode_Disable;
FSMC_NORSRAMInitStructure.FSMC_WaitSignalActive = FSMC_WaitSignalActive_BeforeWaitState;
FSMC_NORSRAMInitStructure.FSMC_WriteOperation = FSMC_WriteOperation_Enable; // ´æ´¢Æ÷дʹÄÜ
FSMC_NORSRAMInitStructure.FSMC_WaitSignal = FSMC_WaitSignal_Disable;
FSMC_NORSRAMInitStructure.FSMC_ExtendedMode = FSMC_ExtendedMode_Enable; // ¶ÁдʹÓò»Í¬µÄʱÐò
FSMC_NORSRAMInitStructure.FSMC_WriteBurst = FSMC_WriteBurst_Disable;
FSMC_NORSRAMInitStructure.FSMC_ReadWriteTimingStruct = &readWriteTiming; //¶ÁдʱÐò
FSMC_NORSRAMInitStructure.FSMC_WriteTimingStruct = &writeTiming; //дʱÐò
FSMC_NORSRAMInit(&FSMC_NORSRAMInitStructure); //³õʼ»¯FSMCÅäÖÃ
FSMC_NORSRAMCmd(FSMC_Bank1_NORSRAM1, ENABLE); // ʹÄÜBANK1
}
|