小会 · 2022年09月02日 · 河南

【MM32F5270开发板试用】+ TFTLCD FSMC方式驱动

上篇使用IO口方式驱动TFTLCD显示屏,这篇测试使用FSMC例程驱动TFTLCD显示屏。

一、FSMC手册资料

FSMC方式可以驱动多种类型的总线设备,这里显示屏使用的是8080控制方式。
20220902113106.png

二、代码

主要代码部分如下
2.1、lcd.c

/*
 * Copyright 2021 MindMotion Microelectronics Co., Ltd.
 * All rights reserved.
 *
 * SPDX-License-Identifier: BSD-3-Clause
 */

#include "lcd.h"
#include "board_init.h"
#include "delay.h"

extern void LCD_InitHardware(void);
extern void LCD_WriteCmd(uint16_t cmd);
extern void LCD_WriteData(uint16_t dat);
extern uint16_t LCD_ReadData(void);


static void lcd_rst(void)
{
    GPIO_SetBits(BOARD_LCD_RST_GPIO_PORT, BOARD_LCD_RST_GPIO_PIN);   
    delay_ms(1); 
    GPIO_ClearBits(BOARD_LCD_RST_GPIO_PORT, BOARD_LCD_RST_GPIO_PIN);  
    delay_ms(10);             // This delay time is necessary 
    GPIO_SetBits(BOARD_LCD_RST_GPIO_PORT, BOARD_LCD_RST_GPIO_PIN);
    delay_ms(120);
}


void LCD_Init(LCD_Init_Type * init)
{
        uint32_t id=0;
        LCD_InitHardware();
        lcd_rst();
            
        LCD_WriteCmd(0x0011);        //exit SLEEP mode
        delay_ms(120);

        LCD_WriteCmd(0x0036);
        LCD_WriteData(0x0060);            //MADCTL: memory data access control
        LCD_WriteCmd(0x003A);
        LCD_WriteData(0x0005);            //COLMOD: Interface Pixel format *** I use 262K-colors in 18bit/pixel format when using 8-bit interface to allow 3-bytes per pixel
    //    TFT_24_7789_Write_Command(0x003A);TFT_24_7789_Write_Data(0x0055);//COLMOD: Interface Pixel format  *** I use 65K-colors in 16bit/pixel (5-6-5) format when using 16-bit interface to allow 1-byte per pixel
        LCD_WriteCmd(0x00B2);
        LCD_WriteData(0x000C);
        LCD_WriteData(0x000C);
        LCD_WriteData(0x0000);
        LCD_WriteData(0x0033);
        LCD_WriteData(0x0033);            //PORCTRK: Porch setting
        LCD_WriteCmd(0x00B7);
        LCD_WriteData(0x0035);            //GCTRL: Gate Control
        LCD_WriteCmd(0x00BB);
        LCD_WriteData(0x001c);            //VCOMS: VCOM setting
        LCD_WriteCmd(0x00C0);
        LCD_WriteData(0x002C);            //LCMCTRL: LCM Control
        LCD_WriteCmd(0x00C2);
        LCD_WriteData(0x0001);
        LCD_WriteData(0x00FF);            //VDVVRHEN: VDV and VRH Command Enable
        LCD_WriteCmd(0x00C3);
        LCD_WriteData(0x000B);            //VRHS: VRH Set
        LCD_WriteCmd(0x00C4);
        LCD_WriteData(0x0020);            //VDVS: VDV Set
        LCD_WriteCmd(0x00C6);
        LCD_WriteData(0x000F);            //FRCTRL2: Frame Rate control in normal mode
        LCD_WriteCmd(0x00D0);
        LCD_WriteData(0x00A4);
        LCD_WriteData(0x00A1);            //PWCTRL1: Power Control 1
        LCD_WriteCmd(0x00E0);
        LCD_WriteData(0x00D0);
        LCD_WriteData(0x0000);
        LCD_WriteData(0x0003);
        LCD_WriteData(0x0009);
        LCD_WriteData(0x0013);
        LCD_WriteData(0x001c);
        LCD_WriteData(0x003a);
        LCD_WriteData(0x0055);
        LCD_WriteData(0x0048);
        LCD_WriteData(0x0018);
        LCD_WriteData(0x0012);
        LCD_WriteData(0x000e);
        LCD_WriteData(0x0019);
        LCD_WriteData(0x001e);            //PVGAMCTRL: Positive Voltage Gamma control
        LCD_WriteCmd(0x00E1);
        LCD_WriteData(0x00D0);
        LCD_WriteData(0x0000);
        LCD_WriteData(0x0003);
        LCD_WriteData(0x0009);
        LCD_WriteData(0x0005);
        LCD_WriteData(0x0025);
        LCD_WriteData(0x003a);
        LCD_WriteData(0x0055);
        LCD_WriteData(0x0050);
        LCD_WriteData(0x003d);
        LCD_WriteData(0x001c);
        LCD_WriteData(0x001d);
        LCD_WriteData(0x001d);
        LCD_WriteData(0x001e);            
        LCD_WriteCmd(0x0029);                //display ON
}

void LCD_SetWindow(uint16_t x0, uint16_t y0, uint16_t x1, uint16_t y1)
{
    LCD_WriteCmd(0x2a);
    LCD_WriteData(x0 >> 8);
    LCD_WriteData(x0 & 0xff);
    LCD_WriteData(x1 >> 8);
    LCD_WriteData(x1 & 0xff);
    LCD_WriteCmd(0x2b);
    LCD_WriteData(y0 >> 8);
    LCD_WriteData(y0 & 0xff);
    LCD_WriteData(y1 >> 8);
    LCD_WriteData(y1 & 0xff);
}

void LCD_ClearWindow(uint16_t x0, uint16_t y0, uint16_t x1, uint16_t y1, uint16_t color)
{
    LCD_SetWindow(x0, y0, x1, y1);
    LCD_WriteCmd(0x2c);
    for(uint32_t i = 0; i <= x1 - x0; i++)
    {
        for(uint32_t j = 0; j <= y1 - y0; j++)
        {
            LCD_WriteData(color);
        }
    }
}

void LCD_FillWindow(uint16_t x0, uint16_t y0, uint16_t x1, uint16_t y1, uint16_t * color)
{
    LCD_SetWindow(x0, y0, x1, y1);
    LCD_WriteCmd(0x2c);
    for(uint32_t i = 0; i <= x1 - x0; i++)
    {
        for(uint32_t j = 0; j <= y1 - y0; j++)
        {
            LCD_WriteData((* color++));
        }
    }
}

void LCD_DrawPixel(uint16_t x, uint16_t y, uint16_t color)
{
    LCD_ClearWindow(x, y, x, y, color);
}

2.2、lcd_port.c

/*
 * Copyright 2021 MindMotion Microelectronics Co., Ltd.
 * All rights reserved.
 *
 * SPDX-License-Identifier: BSD-3-Clause
 */

#include "lcd.h"
#include "board_init.h"


void LCD_InitHardware(void)
{
    FSMC_Init_Type fsmc_init =
    {
        .MemType = FSMC_MemType_SRAM,
        .MemSize = FSMC_MemSize_64MB
    };
    FSMC_Conf_Type fsmc_conf =
    {
        .ReadPeriod    = 0x1u,
        .AddrSetTime   = 0x3u,
        .WriteHoldTime = 0x1u, 
        .WritePeriod   = 0x2u, 
        .ReadySignal   = FSMC_ReadySignal_Internal,
        .SMReadPipe    = 0u,
        .BusWidth      = FSMC_BusWidth_16b
    };

    FSMC_Init(BOARD_LCD_FSMC_PORT, 3, &fsmc_init);
    FSMC_SetConf(BOARD_LCD_FSMC_PORT, BOARD_LCD_FSMC_SET_INDEX, &fsmc_conf);
    FSMC_EnableConf(BOARD_LCD_FSMC_PORT, 3, BOARD_LCD_FSMC_SET_INDEX);

    GPIO_WriteBit(BOARD_LCD_BL_GPIO_PORT, BOARD_LCD_BL_GPIO_PIN, 1u); /* enable back light. */
}

void LCD_WriteCmd(uint16_t cmd)
{
    FSMC_PutData16(BOARD_LCD_FSMC_PORT, 3, 0, cmd);
}

void LCD_WriteData(uint16_t dat)
{
    FSMC_PutData16(BOARD_LCD_FSMC_PORT, 3, (1u << BOARD_LCD_FSMC_RS_BIT_INDEX), dat);
}

uint16_t LCD_ReadData(void)
{
    return FSMC_GetData16(BOARD_LCD_FSMC_PORT, 3, (1 << BOARD_LCD_FSMC_RS_BIT_INDEX));
}

/* EOF. */

2.3、main.c

int main(void)
{
        BOARD_Init();
        delay_init();
        /* init the lcd. */
    LCD_Init_Type lcd_init =
    {
        .DisplayMode = LCD_DisplayMode_Landspace,
    };
    LCD_Init(&lcd_init);
        printf("\r\nmm32f5270 test.\r\n");
        while(1)
        {                
                GPIO_WriteBit(BOARD_LED0_GPIO_PORT, BOARD_LED0_GPIO_PIN, 1u);
                delay_ms(100);
                GPIO_WriteBit(BOARD_LED0_GPIO_PORT, BOARD_LED0_GPIO_PIN, 0u);
                delay_ms(100);
            
                LCD_ClearWindow(0u, 0u, BOARD_LCD_HEIGHT-1u, BOARD_LCD_WIDTH-1u, LCD_COLOR_RED);

        delay_ms(1000);
                LCD_ClearWindow(0u, 0u, BOARD_LCD_HEIGHT-1u, BOARD_LCD_WIDTH-1u, LCD_COLOR_GREEN);

        delay_ms(1000);
                LCD_ClearWindow(0u, 0u, BOARD_LCD_HEIGHT-1u, BOARD_LCD_WIDTH-1u, LCD_COLOR_BLUE);
                delay_ms(1000);
                
        }
}

三、程序运行
显示刷屏
20220902_112620.gif

推荐阅读
关注数
6151
内容数
276
灵动MM32 MCU相关技术知识,欢迎关注~
目录
极术微信服务号
关注极术微信号
实时接收点赞提醒和评论通知
安谋科技学堂公众号
关注安谋科技学堂
实时获取安谋科技及 Arm 教学资源
安谋科技招聘公众号
关注安谋科技招聘
实时获取安谋科技中国职位信息