本帖最后由 jinglixixi 于 2023-8-11 21:58 编辑
#申请原创# @21小跑堂
在色彩显示方面TFT屏有着较好地性能,这里使用的是一款2.2寸显示屏,其显示分辨率为240*320像素点,它采用SPI方式工作。 TFT显示屏与开发板的连接关系为: CS ---PE12 RST---PE3 DC ---PE4 SDA---PA12 SCL---PA11 BL ---PE5 TFT显示屏所用引脚的配置函数为: void tft_gpio(void)
{
stc_gpio_cfg_t stcGpioCfg;
Sysctrl_SetPeripheralGate(SysctrlPeripheralGpio, TRUE);
stcGpioCfg.enDir = GpioDirOut;
stcGpioCfg.enPu = GpioPuDisable;
stcGpioCfg.enPd = GpioPdEnable;
Gpio_Init(GpioPortA, GpioPin11, &stcGpioCfg);
Gpio_Init(GpioPortA, GpioPin12, &stcGpioCfg);
Gpio_Init(GpioPortE, GpioPin3, &stcGpioCfg);
Gpio_Init(GpioPortE, GpioPin4, &stcGpioCfg);
Gpio_Init(GpioPortE, GpioPin5, &stcGpioCfg);
Gpio_Init(GpioPortE, GpioPin12, &stcGpioCfg);
}
各引脚输出高低电平的引脚定义为: #define LCD_CS_High() Gpio_SetIO(GpioPortE,GpioPin12) //CS #define LCD_CS_Low() Gpio_ClrIO(GpioPortE, GpioPin12) #define LCD_REST_High() Gpio_SetIO(GpioPortE,GpioPin3) //RES #define LCD_REST_Low() Gpio_ClrIO(GpioPortE,GpioPin3) #define LCD_DC_High() Gpio_SetIO(GpioPortE,GpioPin4) //DC #define LCD_DC_Low() Gpio_ClrIO(GpioPortE, GpioPin4) #define LCD_SDI_High() Gpio_SetIO(GpioPortA,GpioPin12) //DIN #define LCD_SDI_Low() Gpio_ClrIO(GpioPortA, GpioPin12) #define LCD_SCK_High() Gpio_SetIO(GpioPortA, GpioPin11) //CLK #define LCD_SCK_Low() Gpio_ClrIO(GpioPortA,GpioPin11) #define LCD_LED_High() Gpio_SetIO(GpioPortE,GpioPin5) //BLK #define LCD_LED_Low() Gpio_ClrIO(GpioPortE,GpioPin5)
TFT显示屏模拟SPI方式字节数据的函数为: void LCD_Writ_Bus(unsigned char com)
{
unsigned char uci;
for(uci=0;uci<8;uci++)
{
if(com & 0x80)
{
LCD_SDI_High();
}
else
{
LCD_SDI_Low();
}
com = com << 1;
delay_1us(1);
LCD_SCK_Low();
delay_1us(1);
LCD_SCK_High();
delay_1us(1);
}
}
实现以色彩清除屏幕的函数为: void LCD_Clear(unsigned int Color)
{
char VH,VL;
unsigned int i,j;
VH=Color>>8;
VL=Color;
Address_set(0,0,LCD_W-1,LCD_H-1);
for(i=0;i<LCD_W;i++)
{
for (j=0;j<LCD_H;j++)
{
LCD_WR_DATA8(VH);
LCD_WR_DATA8(VL);
}
}
}
实现图标显示的函数为: void show_tb(unsigned int x,unsigned int y,unsigned int n)
{
unsigned int i,j,k;
unsigned int da;
k=0;
for(i=0;i<50;i++)
{
for(j=0;j<50;j++)
{
if(n==0) da=gImage_tb[k*2];
...
da=da<<8;
if(n==0) da|=gImage_tb[k*2+1];
...
POINT_COLOR=da;
LCD_DrawPoint(x-j,y+i);
k++;
}
}
}
实现满屏图像显示的函数为: void show_pic(const unsigned char *p)
{
int i,j;
unsigned char picH,picL;
Address_set(0,0,LCD_W-1,LCD_H-1);
for(i=0;i<240;i++)
{
for(j=0;j<320;j++)
{
picH=*p++;
picL=*p++;
LCD_WR_DATA8(picH);
LCD_WR_DATA8(picL);
}
}
}
实现字符串显示的函数为: void LCD_ShowString(unsigned int x,unsigned int y,const char *p)
{
while(*p!='\0')
{
if(x>LCD_W-16){x=0;y+=16;}
if(y>LCD_H-16){y=x=0;}
LCD_ShowChar(x,y,*p,0);
x+=8;
p++;
}
}
进行TFT显示屏驱动测试的主程序为: int32_t main(void)
{
App_LedInit();
tft_gpio();
tft_Init();
BACK_COLOR=YELLOW;
POINT_COLOR=RED;
LCD_Clear(YELLOW);
LCD_ShowString(80,30,"HC32L196");
show_tb(60,20,0);
delay1ms(2000);
show_pic(gImage_123);
while(1)
{
Gpio_SetIO(STK_LED_PORT, STK_LED_PIN);
delay1ms(1000);
Gpio_ClrIO(STK_LED_PORT, STK_LED_PIN);
delay1ms(1000);
}
}
经程序的编译与下载,其显示效果如图1和图2所示。 图1 图像显示效果
图2 图标及字符串显示效果
在添加汉字显示函数的情况下,可实现汉字的输出显示,其函数内容为: void showhanzi16(unsigned int x,unsigned int y,unsigned char index)
{
unsigned char i,j,k;
unsigned char *temp=hanzi16;
temp+=index*32;
for(j=0;j<16;j++)
{
Lcd_SetXYP(x,y+j); //LCD_SetCursor(x,y+j);
for(k=0;k<2;k++)
{
for(i=0;i<8;i++)
{
if((*temp&(1<<i))!=0)
{
LCD_WR_DATA(POINT_COLOR);
}
else
{
LCD_WR_DATA(BACK_COLOR);
}
}
temp++;
}
}
}
在添加按键处理的情况下,可实现选择处理功能。 对按键使用引脚的配置函数为: void App_keyInit(void)
{
stc_gpio_cfg_t stcGpioCfg;
Sysctrl_SetPeripheralGate(SysctrlPeripheralGpio, TRUE);
stcGpioCfg.enDir = GpioDirIn;
stcGpioCfg.enPu = GpioPuEnable;
stcGpioCfg.enPd = GpioPdDisable;
Gpio_ClrIO(GpioPortA, GpioPin7);
Gpio_Init(GpioPortA, GpioPin7, &stcGpioCfg);
}
实现汉字歌单选取功能的主程序为: int32_t main(void)
{
int8_t i=0;
App_LedInit();
tft_gpio();
tft_Init();
BACK_COLOR=BLACK;
POINT_COLOR=RED;
LCD_Clear(BLACK);
show_tb(50,1,0);
LCD_DrawLine(0, 1, 239, 1);
LCD_DrawLine(50, 1, 50, 51);
LCD_DrawLine(0, 51, 239, 51);
LCD_DrawLine(0, 295, 239, 295);
POINT_COLOR=YELLOW;
showhanzi16(40,70,20);
showhanzi16(60,70,21);
showhanzi16(40,95,22);
showhanzi16(60,95,23);
showhanzi16(40,115,30);
showhanzi16(60,115,31);
showhanzi16(80,115,32);
showhanzi16(100,115,33);
showhanzi16(40,135,34);
showhanzi16(60,135,35);
showhanzi16(40,155,40);
showhanzi16(60,155,41);
showhanzi16(80,155,42);
showhanzi16(40,175,5);
showhanzi16(60,175,6);
showhanzi16(80,175,7);
showhanzi16(40,195,0);
showhanzi16(60,195,1);
showhanzi16(80,195,2);
showhanzi16(100,195,3);
showhanzi16(120,195,4);
showhanzi16(40,215,8);
showhanzi16(60,215,9);
showhanzi16(40,235,10);
showhanzi16(60,235,11);
POINT_COLOR=RED;
showhanzi16(18,71,28);
LCD_ShowString(80,20,"MP3 Player");
LCD_ShowString(50,301,"BY: jinglixixi");
App_keyInit();
while(1)
{
if(Gpio_GetInputIO(GpioPortA, GpioPin7)==0)
经程序的编译与下载,其显示效果如图3和图4所示。 图3 中文显示效果
图4 歌单选取效果
|