好记心不如烂笔头,为了方便以后快速查阅代码,提高开发效率,在这里特将LTDC驱动的初始化代码贴上来。本代码是基于ST官方STM32F429 Discovery Demo板的,学习LTDC驱动时参考了Demo板的官方例程,但是总觉得官方例程写得有点繁琐,不简洁明了,于是在此基础上重新整理代码得到如下初始化模板。
关于Discovery Demo板的原理图及例程,可以到这里去下载:http://www.st.com/web/catalog/tools/FM116/SC959/SS1532/PF259090
关于本工程源码,请直接点击附件下载。
说明:本测试例程只使用了Layer1,显示缓冲区为FrameBuffer,颜 {MOD}格式为RGB565,初始时缓冲区为0,所以显示Layer1时是全黑 {MOD},如果不使能Layer1只显示background,则屏幕显示为红 {MOD}。
LCD初始化函数:
[mw_shl_code=c,true]void LCD_Init(void)
{
LTDC_InitTypeDef LTDC_InitStruct;
/* Configure the LCD Control pins ------------------------------------------*/
LCD_CtrlLinesConfig();
/* Configure the LCD_SPI interface -----------------------------------------*/
LCD_SPI_Config();
/* Power on the LCD --------------------------------------------------------*/
LCD_PowerOn();
/* Enable the LTDC Clock */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_LTDC, ENABLE);
/* Configure the LCD Control pins */
LCD_GPIO_Config();
/* Enable Pixel Clock ------------------------------------------------------*/
/* Configure PLLSAI prescalers for LCD */
/* PLLSAI_VCO Input = HSE_VALUE/PLL_M = 1 Mhz */
/* PLLSAI_VCO Output = PLLSAI_VCO Input * PLLSAI_N = 192 Mhz */
/* PLLLCDCLK = PLLSAI_VCO Output/PLLSAI_R = 192/3 = 64 Mhz */
/* LTDC clock frequency = PLLLCDCLK / RCC_PLLSAIDivR = 64/8 = 8 Mhz */
RCC_PLLSAIConfig(192, 7, 3);
RCC_LTDCCLKDivConfig(RCC_PLLSAIDivR_Div8);
/* Enable PLLSAI Clock */
RCC_PLLSAICmd(ENABLE);
/* Wait for PLLSAI activation */
while(RCC_GetFlagStatus(RCC_FLAG_PLLSAIRDY) == RESET)
{
}
/* LTDC Initialization -----------------------------------------------------*/
/* Initialize the horizontal synchronization polarity as active low*/
LTDC_InitStruct.LTDC_HSPolarity = LTDC_HSPolarity_AL;
/* Initialize the vertical synchronization polarity as active low */
LTDC_InitStruct.LTDC_VSPolarity = LTDC_VSPolarity_AL;
/* Initialize the data enable polarity as active low */
LTDC_InitStruct.LTDC_DEPolarity = LTDC_DEPolarity_AL;
/* Initialize the pixel clock polarity as input pixel clock */
LTDC_InitStruct.LTDC_PCPolarity = LTDC_PCPolarity_IPC;
/* Timing configuration */
/* Configure horizontal synchronization width */
//LTDC_InitStruct.LTDC_HorizontalSync = 9;
LTDC_InitStruct.LTDC_HorizontalSync = 9;
/* Configure vertical synchronization height */
LTDC_InitStruct.LTDC_VerticalSync = 1;
/* Configure accumulated horizontal back porch */
LTDC_InitStruct.LTDC_AccumulatedHBP = 29;
/* Configure accumulated vertical back porch */
LTDC_InitStruct.LTDC_AccumulatedVBP = 3;
/* Configure accumulated active width */
LTDC_InitStruct.LTDC_AccumulatedActiveW = 269;
/* Configure accumulated active height */
LTDC_InitStruct.LTDC_AccumulatedActiveH = 323;
/* Configure total width */
LTDC_InitStruct.LTDC_TotalWidth = 279;
/* Configure total height */
LTDC_InitStruct.LTDC_TotalHeigh = 327;
/* Configure R,G,B component values for LCD background color */
LTDC_InitStruct.LTDC_BackgroundRedValue = 0xff;
LTDC_InitStruct.LTDC_BackgroundGreenValue = 0;
LTDC_InitStruct.LTDC_BackgroundBlueValue = 0;
LTDC_Init(<DC_InitStruct);
}
[/mw_shl_code]
LCD串行控制信号线配置:
[mw_shl_code=c,true]void LCD_CtrlLinesConfig(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
/* Enable GPIOs clock*/
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC | RCC_AHB1Periph_GPIOD, ENABLE);
/* Configure NCS in Output Push-Pull mode */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_Init(GPIOC, &GPIO_InitStructure);
/* Configure WRX in Output Push-Pull mode */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13;
GPIO_Init(GPIOD, &GPIO_InitStructure);
/* Set chip select pin high */
GPIO_SetBits(GPIOC, GPIO_Pin_2);
}
[/mw_shl_code]
[mw_shl_code=c,true]void LCD_SPI_Config(void)
{
SPI_InitTypeDef SPI_InitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
/* Enable LCD_SPI_SCK_GPIO_CLK, LCD_SPI_MISO_GPIO_CLK and LCD_SPI_MOSI_GPIO_CLK clock */
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOF, ENABLE);
/* Enable LCD_SPI and SYSCFG clock */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI5, ENABLE);
/* Configure LCD_SPI SCK pin */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_25MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_DOWN;
GPIO_Init(GPIOF, &GPIO_InitStructure);
/* Configure LCD_SPI MISO pin */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8;
GPIO_Init(GPIOF, &GPIO_InitStructure);
/* Configure LCD_SPI MOSI pin */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
GPIO_Init(GPIOF, &GPIO_InitStructure);
/* Connect SPI SCK */
GPIO_PinAFConfig(GPIOF, GPIO_PinSource7, GPIO_AF_SPI5);
/* Connect SPI MISO */
GPIO_PinAFConfig(GPIOF, GPIO_PinSource8, GPIO_AF_SPI5);
/* Connect SPI MOSI */
GPIO_PinAFConfig(GPIOF, GPIO_PinSource9, GPIO_AF_SPI5);
SPI_I2S_DeInit(SPI5);
/* SPI configuration -------------------------------------------------------*/
SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex;
SPI_InitStructure.SPI_Mode = SPI_Mode_Master;
SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b;
SPI_InitStructure.SPI_CPOL = SPI_CPOL_Low;
SPI_InitStructure.SPI_CPHA = SPI_CPHA_1Edge;
SPI_InitStructure.SPI_NSS = SPI_NSS_Soft;
/* SPI baudrate is set to 5.6 MHz (PCLK2/SPI_BaudRatePrescaler = 90/16 = 5.625 MHz)
to verify these constraints:
- ILI9341 LCD SPI interface max baudrate is 10MHz for write and 6.66MHz for read
- l3gd20 SPI interface max baudrate is 10MHz for write/read
- PCLK2 frequency is set to 90 MHz
*/
SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_16;
SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;
SPI_InitStructure.SPI_CRCPolynomial = 7;
SPI_Init(SPI5, &SPI_InitStructure);
/* Enable L3GD20_SPI */
SPI_Cmd(SPI5, ENABLE);
}
[/mw_shl_code]
LCD同步信号及数据信号GPIO配置:
[mw_shl_code=c,true]void LCD_GPIO_Config(void)
{
GPIO_InitTypeDef GPIO_InitStruct;
/* Enable GPIOI, GPIOJ, GPIOG, GPIOF, GPIOH AHB Clocks */
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA | RCC_AHB1Periph_GPIOB |
RCC_AHB1Periph_GPIOC | RCC_AHB1Periph_GPIOD |
RCC_AHB1Periph_GPIOF | RCC_AHB1Periph_GPIOG, ENABLE);
/* GPIOs Configuration */
/*
+------------------------+-----------------------+----------------------------+
+ LCD pins assignment +
+------------------------+-----------------------+----------------------------+
| LCD_TFT R2 <-> PC.12 | LCD_TFT G2 <-> PA.06 | LCD_TFT B2 <-> PD.06 |
| LCD_TFT R3 <-> PB.00 | LCD_TFT G3 <-> PG.10 | LCD_TFT B3 <-> PG.11 |
| LCD_TFT R4 <-> PA.11 | LCD_TFT G4 <-> PB.10 | LCD_TFT B4 <-> PG.12 |
| LCD_TFT R5 <-> PA.12 | LCD_TFT G5 <-> PB.11 | LCD_TFT B5 <-> PA.03 |
| LCD_TFT R6 <-> PB.01 | LCD_TFT G6 <-> PC.07 | LCD_TFT B6 <-> PB.08 |
| LCD_TFT R7 <-> PG.06 | LCD_TFT G7 <-> PD.03 | LCD_TFT B7 <-> PB.09 |
-------------------------------------------------------------------------------
| LCD_TFT HSYNC <-> PC.06 | LCDTFT VSYNC <-> PA.04 |
| LCD_TFT CLK <-> PG.07 | LCD_TFT DE <-> PF.10 |
-----------------------------------------------------
*/
/* GPIOA configuration */
GPIO_PinAFConfig(GPIOA, GPIO_PinSource3, GPIO_AF_LTDC);
GPIO_PinAFConfig(GPIOA, GPIO_PinSource4, GPIO_AF_LTDC);
GPIO_PinAFConfig(GPIOA, GPIO_PinSource6, GPIO_AF_LTDC);
GPIO_PinAFConfig(GPIOA, GPIO_PinSource11, GPIO_AF_LTDC);
GPIO_PinAFConfig(GPIOA, GPIO_PinSource12, GPIO_AF_LTDC);
GPIO_InitStruct.GPIO_Pin = GPIO_Pin_3 | GPIO_Pin_4 | GPIO_Pin_6 |
GPIO_Pin_11 | GPIO_Pin_12;
GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStruct.GPIO_OType = GPIO_OType_PP;
GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_Init(GPIOA, &GPIO_InitStruct);
/* GPIOB configuration */
GPIO_PinAFConfig(GPIOB, GPIO_PinSource0, GPIO_AF_LTDC);
GPIO_PinAFConfig(GPIOB, GPIO_PinSource1, GPIO_AF_LTDC);
GPIO_PinAFConfig(GPIOB, GPIO_PinSource8, GPIO_AF_LTDC);
GPIO_PinAFConfig(GPIOB, GPIO_PinSource9, GPIO_AF_LTDC);
GPIO_PinAFConfig(GPIOB, GPIO_PinSource10, GPIO_AF_LTDC);
GPIO_PinAFConfig(GPIOB, GPIO_PinSource11, GPIO_AF_LTDC);
GPIO_InitStruct.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_8 |
GPIO_Pin_9 | GPIO_Pin_10 | GPIO_Pin_11;
GPIO_Init(GPIOB, &GPIO_InitStruct);
/* GPIOC configuration */
GPIO_PinAFConfig(GPIOC, GPIO_PinSource6, GPIO_AF_LTDC);
GPIO_PinAFConfig(GPIOC, GPIO_PinSource7, GPIO_AF_LTDC);
GPIO_InitStruct.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7;// | GPIO_Pin_10;
GPIO_Init(GPIOC, &GPIO_InitStruct);
/* GPIOD configuration */
GPIO_PinAFConfig(GPIOD, GPIO_PinSource3, GPIO_AF_LTDC);
GPIO_PinAFConfig(GPIOD, GPIO_PinSource6, GPIO_AF_LTDC);
GPIO_InitStruct.GPIO_Pin = GPIO_Pin_3 | GPIO_Pin_6;
GPIO_Init(GPIOD, &GPIO_InitStruct);
/* GPIOF configuration */
GPIO_PinAFConfig(GPIOF, GPIO_PinSource10, GPIO_AF_LTDC);
GPIO_InitStruct.GPIO_Pin = GPIO_Pin_10;
GPIO_Init(GPIOF, &GPIO_InitStruct);
/* GPIOG configuration */
GPIO_PinAFConfig(GPIOG, GPIO_PinSource6, GPIO_AF_LTDC);
GPIO_PinAFConfig(GPIOG, GPIO_PinSource7, GPIO_AF_LTDC);
GPIO_PinAFConfig(GPIOG, GPIO_PinSource10, GPIO_AF_LTDC);
GPIO_PinAFConfig(GPIOG, GPIO_PinSource11, GPIO_AF_LTDC);
GPIO_PinAFConfig(GPIOG, GPIO_PinSource12, GPIO_AF_LTDC);
GPIO_InitStruct.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7 | GPIO_Pin_10 |
GPIO_Pin_11 | GPIO_Pin_12;
GPIO_Init(GPIOG, &GPIO_InitStruct);
}
[/mw_shl_code]
LCD上电初始化序列,请对照LCD datasheet要求进行配置,此处省略部分内容:
[mw_shl_code=c,true]/**
* @brief Configure the LCD controller (Power On sequence as described in ILI9341 Datasheet)
* @param None
* @retval None
*/
void LCD_PowerOn(void)
{
LCD_WriteCommand(0xCA);
LCD_WriteData(0xC3);
LCD_WriteData(0x08);
LCD_WriteData(0x50);
LCD_WriteCommand(LCD_POWERB);
LCD_WriteData(0x00);
LCD_WriteData(0xC1);
LCD_WriteData(0x30);
LCD_WriteCommand(LCD_POWER_SEQ);
LCD_WriteData(0x64);
LCD_WriteData(0x03);
LCD_WriteData(0x12);
LCD_WriteData(0x81);
...
LCD_WriteCommand(LCD_SLEEP_OUT);
delay(200);
LCD_WriteCommand(LCD_DISPLAY_ON);
/* GRAM start writing */
LCD_WriteCommand(LCD_GRAM);
}
[/mw_shl_code]
Layer初始化:
[mw_shl_code=c,true]void LCD_Layer1Init(void)
{
LTDC_Layer_InitTypeDef LTDC_Layer_InitStruct;
/* Windowing configuration */
/* In this case all the active display area is used to display a picture then:
Horizontal start = horizontal synchronization + Horizontal back porch = 30
Horizontal stop = Horizontal start + window width -1 = 30 + 240 -1
Vertical start = vertical synchronization + vertical back porch = 4
Vertical stop = Vertical start + window height -1 = 4 + 160 -1 */
LTDC_Layer_InitStruct.LTDC_HorizontalStart = 30;
LTDC_Layer_InitStruct.LTDC_HorizontalStop = (240 + 30 - 1);
LTDC_Layer_InitStruct.LTDC_VerticalStart = 4;
LTDC_Layer_InitStruct.LTDC_VerticalStop = 320 + 4 -1;
/* Pixel Format configuration*/
LTDC_Layer_InitStruct.LTDC_PixelFormat = LTDC_Pixelformat_RGB565;
/* Alpha constant (255 totally opaque) */
LTDC_Layer_InitStruct.LTDC_ConstantAlpha = 255;
/* Configure blending factors */
LTDC_Layer_InitStruct.LTDC_BlendingFactor_1 = LTDC_BlendingFactor1_PAxCA;
LTDC_Layer_InitStruct.LTDC_BlendingFactor_2 = LTDC_BlendingFactor2_PAxCA;
/* Default Color configuration (configure A,R,G,B component values) */
LTDC_Layer_InitStruct.LTDC_DefaultColorBlue = 0;
LTDC_Layer_InitStruct.LTDC_DefaultColorGreen = 0;
LTDC_Layer_InitStruct.LTDC_DefaultColorRed = 0;
LTDC_Layer_InitStruct.LTDC_DefaultColorAlpha = 0;
/* Input Address configuration */
LTDC_Layer_InitStruct.LTDC_CFBStartAdress = (u32)FrameBuffer;
/* the length of one line of pixels in bytes + 3 then :
Line Lenth = Active high width x number of bytes per pixel + 3
Active high width = 240
number of bytes per pixel = 2 (pixel_format : RGB565)
*/
LTDC_Layer_InitStruct.LTDC_CFBLineLength = ((240 * 2) + 3);
/* the pitch is the increment from the start of one line of pixels to the
start of the next line in bytes, then :
Pitch = Active high width x number of bytes per pixel
*/
LTDC_Layer_InitStruct.LTDC_CFBPitch = (240 * 2);
/* configure the number of lines */
LTDC_Layer_InitStruct.LTDC_CFBLineNumber = 320;
LTDC_LayerInit(LTDC_Layer1, <DC_Layer_InitStruct);
LTDC_LayerCmd(LTDC_Layer1, ENABLE);
LTDC_ReloadConfig(ENABLE);
}
[/mw_shl_code]
主程序:
[mw_shl_code=c,true]int main(void)
{
LCD_Init();
LCD_Layer1Init();
LTDC_Cmd(ENABLE);
while(1);
}
[/mw_shl_code]
本贴摘自于本人博客:http://blog.csdn.net/hexiaolong2009/article/details/43986205
一周热门 更多>