在显示器件中有多种类型,其中以TFT屏较适于做图片显示。
这里选用的是一款基于ILI9341的2.2寸TFT屏,其分辨率为240x320像素点,并以SPI方式工作。
为实现下图所示的显示效果,其引脚与开发板的连接如下:
BLK-------PA5
SCL-------PA0
SDA-------PA1
DC--------PA3
RESET-----PA2
CS--------PA4
使用GPIO口来模拟SPI方式工作,其输出高低电平的引脚为:
#define LCD_SCK_Low() GPIOA->clr = GPIO_PINS_0
#define LCD_SCK_High() GPIOA->scr = GPIO_PINS_0
#define LCD_SDI_Low() GPIOA->clr = GPIO_PINS_1
#define LCD_SDI_High() GPIOA->scr = GPIO_PINS_1
#define LCD_REST_Low() GPIOA->clr = GPIO_PINS_2
#define LCD_REST_High() GPIOA->scr = GPIO_PINS_2
#define LCD_DC_Low() GPIOA->clr = GPIO_PINS_3
#define LCD_DC_High() GPIOA->scr = GPIO_PINS_3
#define LCD_CS_Low() GPIOA->clr = GPIO_PINS_4
#define LCD_CS_High() GPIOA->scr = GPIO_PINS_4
#define LCD_LED_Low() GPIOA->clr = GPIO_PINS_5
#define LCD_LED_High() GPIOA->scr = GPIO_PINS_5
配置相关引脚的函数为:
void app_tft_init(void)
{
gpio_init_type gpio_init_struct;
crm_periph_clock_enable(CRM_GPIOA_PERIPH_CLOCK, TRUE);
gpio_default_para_init(&gpio_init_struct);
gpio_init_struct.gpio_drive_strength = GPIO_DRIVE_STRENGTH_STRONGER;
gpio_init_struct.gpio_out_type = GPIO_OUTPUT_PUSH_PULL;
gpio_init_struct.gpio_mode = GPIO_MODE_OUTPUT;
gpio_init_struct.gpio_pins = GPIO_PINS_0|GPIO_PINS_1|GPIO_PINS_2|GPIO_PINS_3|GPIO_PINS_4|GPIO_PINS_5;
gpio_init_struct.gpio_pull = GPIO_PULL_NONE;
gpio_init(GPIOA, &gpio_init_struct);
LCD_LED_High();
}
模拟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;
LCD_SCK_Low();
delay_us(8);
LCD_SCK_High();
delay_us(8);
}
}
实现以指定色彩擦除屏幕的函数为:
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);
}
}
}
以屏幕擦除函数为基础,实现50*50像素点图标显示的函数为:
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<<=8;
if(n==0) da|=gImage_tb[k*2+1];
POINT_COLOR=da;
LCD_DrawPoint(x-j,y+i);
k++;
}
}
}
在配合汉字显示功能的情况下,实现图示效果的主程序为:
int main(void)
{
system_clock_config();
at32_board_init();
app_tft_init();
tft_Init();
LCD_Clear(RED);
LCD_Fill(0,0,239,50,WHITE);
show_tb(60,0,0);
delay_ms(1000);
BACK_COLOR=WHITE;
POINT_COLOR=RED;
LCD_ShowChar(80,16,'M',1);
LCD_ShowChar(90,16,'P',1);
LCD_ShowChar(100,16,'3',1);
showhanzi16(115,16,32);
showhanzi16(135,16,33);
showhanzi16(155,16,34);
showhanzi16(175,16,35);
showhanzi16(195,16,36);
BACK_COLOR=RED;
POINT_COLOR=YELLOW;
showhanzi16(60,75,0);
showhanzi16(80,75,1);
showhanzi16(100,75,2);
showhanzi16(30,75,37);
showhanzi16(60,105,3);
showhanzi16(80,105,4);
showhanzi16(100,105,5);
showhanzi16(120,105,6);
showhanzi16(60,135,7);
showhanzi16(80,135,8);
showhanzi16(60,165,9);
showhanzi16(80,165,10);
showhanzi16(60,195,11);
showhanzi16(80,195,12);
showhanzi16(100,195,13);
showhanzi16(120,195,14);
showhanzi16(60,225,15);
showhanzi16(80,225,16);
showhanzi16(100,225,17);
showhanzi16(120,225,18);
showhanzi16(140,225,19);
showhanzi16(60,255,20);
showhanzi16(80,255,21);
showhanzi16(100,255,22);
showhanzi16(120,255,23);
showhanzi16(140,255,24);
showhanzi16(160,255,25);
showhanzi16(180,255,26);
LCD_Fill(0,287,239,319,WHITE);
BACK_COLOR=WHITE;
POINT_COLOR=RED;
LCD_ShowString(60,294,"BY: jinglixixi");
while(1)
{
at32_led_toggle(LED2);
delay_ms(200);
at32_led_toggle(LED3);
delay_ms(200);
at32_led_toggle(LED4);
delay_ms(200);
}
}
有了TFT屏显示图片的功能,就为后续的打下了一个基础。