硬汉嵌入式论坛

 找回密码
 立即注册
查看: 7496|回复: 7
收起左侧

STM32F429之LTDC代码模板

[复制链接]

3

主题

14

回帖

23

积分

新手上路

积分
23
发表于 2015-2-28 17:06:17 | 显示全部楼层 |阅读模式
好记心不如烂笔头,为了方便以后快速查阅代码,提高开发效率,在这里特将LTDC驱动的初始化代码贴上来。本代码是基于ST官方STM32F429 Discovery Demo板的,学习LTDC驱动时参考了Demo板的官方例程,但是总觉得官方例程写得有点繁琐,不简洁明了,于是在此基础上重新整理代码得到如下初始化模板。
关于Discovery Demo板的原理图及例程,可以到这里去下载:http://www.st.com/web/catalog/tools/FM116/SC959/SS1532/PF259090
关于本工程源码,请直接点击下载: LTDC_Test.zip (429 KB, 下载次数: 408)


说明:本测试例程只使用了Layer1,显示缓冲区为FrameBuffer,颜色格式为RGB565,初始时缓冲区为0,所以显示Layer1时是全黑色,如果不使能Layer1只显示background,则屏幕显示为红色。


LCD初始化函数:
  1. void LCD_Init(void)
  2. {
  3.   LTDC_InitTypeDef               LTDC_InitStruct;
  4.    
  5.   /* Configure the LCD Control pins ------------------------------------------*/
  6.   LCD_CtrlLinesConfig();
  7.   
  8.   /* Configure the LCD_SPI interface -----------------------------------------*/
  9.   LCD_SPI_Config();
  10.   
  11.   /* Power on the LCD --------------------------------------------------------*/
  12.   LCD_PowerOn();
  13.   
  14.   /* Enable the LTDC Clock */
  15.   RCC_APB2PeriphClockCmd(RCC_APB2Periph_LTDC, ENABLE);
  16.   
  17.   /* Configure the LCD Control pins */
  18.   LCD_GPIO_Config();  
  19.    
  20.   /* Enable Pixel Clock ------------------------------------------------------*/
  21.   
  22.   /* Configure PLLSAI prescalers for LCD */
  23.   /* PLLSAI_VCO Input = HSE_VALUE/PLL_M = 1 Mhz */
  24.   /* PLLSAI_VCO Output = PLLSAI_VCO Input * PLLSAI_N = 192 Mhz */
  25.   /* PLLLCDCLK = PLLSAI_VCO Output/PLLSAI_R = 192/3 = 64 Mhz */
  26.   /* LTDC clock frequency = PLLLCDCLK / RCC_PLLSAIDivR = 64/8 = 8 Mhz */
  27.   RCC_PLLSAIConfig(192, 7, 3);
  28.   RCC_LTDCCLKDivConfig(RCC_PLLSAIDivR_Div8);
  29.   
  30.   /* Enable PLLSAI Clock */
  31.   RCC_PLLSAICmd(ENABLE);
  32.   /* Wait for PLLSAI activation */
  33.   while(RCC_GetFlagStatus(RCC_FLAG_PLLSAIRDY) == RESET)
  34.   {
  35.   }
  36.   
  37.   /* LTDC Initialization -----------------------------------------------------*/
  38.   
  39.   /* Initialize the horizontal synchronization polarity as active low*/
  40.   LTDC_InitStruct.LTDC_HSPolarity = LTDC_HSPolarity_AL;     
  41.   /* Initialize the vertical synchronization polarity as active low */  
  42.   LTDC_InitStruct.LTDC_VSPolarity = LTDC_VSPolarity_AL;     
  43.   /* Initialize the data enable polarity as active low */
  44.   LTDC_InitStruct.LTDC_DEPolarity = LTDC_DEPolarity_AL;     
  45.   /* Initialize the pixel clock polarity as input pixel clock */
  46.   LTDC_InitStruct.LTDC_PCPolarity = LTDC_PCPolarity_IPC;
  47.   
  48.   /* Timing configuration */
  49.   /* Configure horizontal synchronization width */     
  50.   //LTDC_InitStruct.LTDC_HorizontalSync = 9;
  51.   LTDC_InitStruct.LTDC_HorizontalSync = 9;
  52.   /* Configure vertical synchronization height */
  53.   LTDC_InitStruct.LTDC_VerticalSync = 1;
  54.   /* Configure accumulated horizontal back porch */
  55.   LTDC_InitStruct.LTDC_AccumulatedHBP = 29;
  56.   /* Configure accumulated vertical back porch */
  57.   LTDC_InitStruct.LTDC_AccumulatedVBP = 3;  
  58.   /* Configure accumulated active width */  
  59.   LTDC_InitStruct.LTDC_AccumulatedActiveW = 269;
  60.   /* Configure accumulated active height */
  61.   LTDC_InitStruct.LTDC_AccumulatedActiveH = 323;
  62.   /* Configure total width */
  63.   LTDC_InitStruct.LTDC_TotalWidth = 279;
  64.   /* Configure total height */
  65.   LTDC_InitStruct.LTDC_TotalHeigh = 327;
  66.    
  67.   /* Configure R,G,B component values for LCD background color */                  
  68.   LTDC_InitStruct.LTDC_BackgroundRedValue = 0xff;            
  69.   LTDC_InitStruct.LTDC_BackgroundGreenValue = 0;         
  70.   LTDC_InitStruct.LTDC_BackgroundBlueValue = 0;
  71.   
  72.   LTDC_Init(<DC_InitStruct);
  73. }
复制代码
LCD串行控制信号线配置:
  1. void LCD_CtrlLinesConfig(void)
  2. {
  3.   GPIO_InitTypeDef GPIO_InitStructure;
  4.   /* Enable GPIOs clock*/
  5.   RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC | RCC_AHB1Periph_GPIOD, ENABLE);
  6.   /* Configure NCS in Output Push-Pull mode */
  7.   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
  8.   GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  9.   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
  10.   GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
  11.   GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
  12.   GPIO_Init(GPIOC, &GPIO_InitStructure);
  13.   
  14.   /* Configure WRX in Output Push-Pull mode */
  15.   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13;
  16.   GPIO_Init(GPIOD, &GPIO_InitStructure);
  17.   /* Set chip select pin high */
  18.   GPIO_SetBits(GPIOC, GPIO_Pin_2);
  19. }
复制代码
  1. void LCD_SPI_Config(void)
  2. {
  3.   SPI_InitTypeDef    SPI_InitStructure;
  4.   GPIO_InitTypeDef   GPIO_InitStructure;
  5.   /* Enable LCD_SPI_SCK_GPIO_CLK, LCD_SPI_MISO_GPIO_CLK and LCD_SPI_MOSI_GPIO_CLK clock */
  6.   RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOF, ENABLE);
  7.   /* Enable LCD_SPI and SYSCFG clock  */
  8.   RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI5, ENABLE);
  9.   
  10.   /* Configure LCD_SPI SCK pin */
  11.   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7;
  12.   GPIO_InitStructure.GPIO_Speed = GPIO_Speed_25MHz;
  13.   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
  14.   GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
  15.   GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_DOWN;
  16.   GPIO_Init(GPIOF, &GPIO_InitStructure);
  17.   /* Configure LCD_SPI MISO pin */
  18.   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8;
  19.   GPIO_Init(GPIOF, &GPIO_InitStructure);
  20.   /* Configure LCD_SPI MOSI pin */
  21.   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
  22.   GPIO_Init(GPIOF, &GPIO_InitStructure);
  23.   /* Connect SPI SCK */
  24.   GPIO_PinAFConfig(GPIOF, GPIO_PinSource7, GPIO_AF_SPI5);
  25.   /* Connect SPI MISO */
  26.   GPIO_PinAFConfig(GPIOF, GPIO_PinSource8, GPIO_AF_SPI5);
  27.   /* Connect SPI MOSI */
  28.   GPIO_PinAFConfig(GPIOF, GPIO_PinSource9, GPIO_AF_SPI5);
  29.   
  30.   SPI_I2S_DeInit(SPI5);
  31.   /* SPI configuration -------------------------------------------------------*/
  32.   SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex;
  33.   SPI_InitStructure.SPI_Mode = SPI_Mode_Master;
  34.   SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b;
  35.   SPI_InitStructure.SPI_CPOL = SPI_CPOL_Low;
  36.   SPI_InitStructure.SPI_CPHA = SPI_CPHA_1Edge;
  37.   SPI_InitStructure.SPI_NSS = SPI_NSS_Soft;
  38.   /* SPI baudrate is set to 5.6 MHz (PCLK2/SPI_BaudRatePrescaler = 90/16 = 5.625 MHz)
  39.        to verify these constraints:
  40.               - ILI9341 LCD SPI interface max baudrate is 10MHz for write and 6.66MHz for read
  41.               - l3gd20 SPI interface max baudrate is 10MHz for write/read
  42.               - PCLK2 frequency is set to 90 MHz
  43.        */
  44.   SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_16;
  45.   SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;
  46.   SPI_InitStructure.SPI_CRCPolynomial = 7;
  47.   SPI_Init(SPI5, &SPI_InitStructure);
  48.   /* Enable L3GD20_SPI  */
  49.   SPI_Cmd(SPI5, ENABLE);
  50. }
复制代码
LCD同步信号及数据信号GPIO配置:
  1. void LCD_GPIO_Config(void)
  2. {
  3.   GPIO_InitTypeDef GPIO_InitStruct;
  4.   
  5.   /* Enable GPIOI, GPIOJ, GPIOG, GPIOF, GPIOH AHB Clocks */
  6.   RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA | RCC_AHB1Periph_GPIOB | \
  7.     RCC_AHB1Periph_GPIOC | RCC_AHB1Periph_GPIOD | \
  8.       RCC_AHB1Periph_GPIOF | RCC_AHB1Periph_GPIOG, ENABLE);
  9.   
  10.   /* GPIOs Configuration */
  11.   /*
  12.   +------------------------+-----------------------+----------------------------+
  13.   +                       LCD pins assignment                                   +
  14.   +------------------------+-----------------------+----------------------------+
  15.   |  LCD_TFT R2 <-> PC.12  |  LCD_TFT G2 <-> PA.06 |  LCD_TFT B2 <-> PD.06      |
  16.   |  LCD_TFT R3 <-> PB.00  |  LCD_TFT G3 <-> PG.10 |  LCD_TFT B3 <-> PG.11      |
  17.   |  LCD_TFT R4 <-> PA.11  |  LCD_TFT G4 <-> PB.10 |  LCD_TFT B4 <-> PG.12      |
  18.   |  LCD_TFT R5 <-> PA.12  |  LCD_TFT G5 <-> PB.11 |  LCD_TFT B5 <-> PA.03      |
  19.   |  LCD_TFT R6 <-> PB.01  |  LCD_TFT G6 <-> PC.07 |  LCD_TFT B6 <-> PB.08      |
  20.   |  LCD_TFT R7 <-> PG.06  |  LCD_TFT G7 <-> PD.03 |  LCD_TFT B7 <-> PB.09      |
  21.   -------------------------------------------------------------------------------
  22.   |  LCD_TFT HSYNC <-> PC.06  | LCDTFT VSYNC <->  PA.04 |
  23.   |  LCD_TFT CLK   <-> PG.07  | LCD_TFT DE   <->  PF.10 |
  24.   -----------------------------------------------------
  25.   
  26.   */
  27.   
  28.   /* GPIOA configuration */
  29.   GPIO_PinAFConfig(GPIOA, GPIO_PinSource3, GPIO_AF_LTDC);
  30.   GPIO_PinAFConfig(GPIOA, GPIO_PinSource4, GPIO_AF_LTDC);
  31.   GPIO_PinAFConfig(GPIOA, GPIO_PinSource6, GPIO_AF_LTDC);
  32.   GPIO_PinAFConfig(GPIOA, GPIO_PinSource11, GPIO_AF_LTDC);
  33.   GPIO_PinAFConfig(GPIOA, GPIO_PinSource12, GPIO_AF_LTDC);
  34.   
  35.   GPIO_InitStruct.GPIO_Pin = GPIO_Pin_3 | GPIO_Pin_4 | GPIO_Pin_6 | \
  36.                              GPIO_Pin_11 | GPIO_Pin_12;
  37.   GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
  38.   GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF;
  39.   GPIO_InitStruct.GPIO_OType = GPIO_OType_PP;
  40.   GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_NOPULL;
  41.   GPIO_Init(GPIOA, &GPIO_InitStruct);
  42.   
  43.   /* GPIOB configuration */  
  44.   GPIO_PinAFConfig(GPIOB, GPIO_PinSource0, GPIO_AF_LTDC);
  45.   GPIO_PinAFConfig(GPIOB, GPIO_PinSource1, GPIO_AF_LTDC);
  46.   GPIO_PinAFConfig(GPIOB, GPIO_PinSource8, GPIO_AF_LTDC);
  47.   GPIO_PinAFConfig(GPIOB, GPIO_PinSource9, GPIO_AF_LTDC);
  48.   GPIO_PinAFConfig(GPIOB, GPIO_PinSource10, GPIO_AF_LTDC);
  49.   GPIO_PinAFConfig(GPIOB, GPIO_PinSource11, GPIO_AF_LTDC);
  50.   
  51.   GPIO_InitStruct.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_8 | \
  52.                              GPIO_Pin_9 | GPIO_Pin_10 | GPIO_Pin_11;  
  53.   GPIO_Init(GPIOB, &GPIO_InitStruct);
  54.   
  55.   /* GPIOC configuration */
  56.   GPIO_PinAFConfig(GPIOC, GPIO_PinSource6, GPIO_AF_LTDC);
  57.   GPIO_PinAFConfig(GPIOC, GPIO_PinSource7, GPIO_AF_LTDC);
  58.   
  59.   GPIO_InitStruct.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7;// | GPIO_Pin_10;
  60.   GPIO_Init(GPIOC, &GPIO_InitStruct);
  61.   
  62.   /* GPIOD configuration */
  63.   GPIO_PinAFConfig(GPIOD, GPIO_PinSource3, GPIO_AF_LTDC);
  64.   GPIO_PinAFConfig(GPIOD, GPIO_PinSource6, GPIO_AF_LTDC);
  65.   
  66.   GPIO_InitStruct.GPIO_Pin = GPIO_Pin_3 | GPIO_Pin_6;
  67.   GPIO_Init(GPIOD, &GPIO_InitStruct);
  68.   
  69.   /* GPIOF configuration */
  70.   GPIO_PinAFConfig(GPIOF, GPIO_PinSource10, GPIO_AF_LTDC);
  71.   
  72.   GPIO_InitStruct.GPIO_Pin = GPIO_Pin_10;
  73.   GPIO_Init(GPIOF, &GPIO_InitStruct);     
  74.   
  75.   /* GPIOG configuration */  
  76.   GPIO_PinAFConfig(GPIOG, GPIO_PinSource6, GPIO_AF_LTDC);
  77.   GPIO_PinAFConfig(GPIOG, GPIO_PinSource7, GPIO_AF_LTDC);
  78.   GPIO_PinAFConfig(GPIOG, GPIO_PinSource10, GPIO_AF_LTDC);
  79.   GPIO_PinAFConfig(GPIOG, GPIO_PinSource11, GPIO_AF_LTDC);
  80.   GPIO_PinAFConfig(GPIOG, GPIO_PinSource12, GPIO_AF_LTDC);
  81.   
  82.   GPIO_InitStruct.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7 | GPIO_Pin_10 | \
  83.                              GPIO_Pin_11 | GPIO_Pin_12;
  84.   GPIO_Init(GPIOG, &GPIO_InitStruct);
  85. }
复制代码
LCD上电初始化序列,请对照LCD datasheet要求进行配置,此处省略部分内容:
  1. /**
  2.   * @brief  Configure the LCD controller (Power On sequence as described in ILI9341 Datasheet)
  3.   * @param  None
  4.   * @retval None
  5.   */
  6. void LCD_PowerOn(void)
  7. {
  8.   LCD_WriteCommand(0xCA);
  9.   LCD_WriteData(0xC3);
  10.   LCD_WriteData(0x08);
  11.   LCD_WriteData(0x50);
  12.   LCD_WriteCommand(LCD_POWERB);
  13.   LCD_WriteData(0x00);
  14.   LCD_WriteData(0xC1);
  15.   LCD_WriteData(0x30);
  16.   LCD_WriteCommand(LCD_POWER_SEQ);
  17.   LCD_WriteData(0x64);
  18.   LCD_WriteData(0x03);
  19.   LCD_WriteData(0x12);
  20.   LCD_WriteData(0x81);
  21.   
  22.   ...
  23.   LCD_WriteCommand(LCD_SLEEP_OUT);
  24.   delay(200);
  25.   LCD_WriteCommand(LCD_DISPLAY_ON);
  26.   /* GRAM start writing */
  27.   LCD_WriteCommand(LCD_GRAM);
  28. }
复制代码
Layer初始化:
  1. void LCD_Layer1Init(void)
  2. {
  3.   LTDC_Layer_InitTypeDef         LTDC_Layer_InitStruct;
  4.   /* Windowing configuration */
  5.   /* In this case all the active display area is used to display a picture then:
  6.   Horizontal start = horizontal synchronization + Horizontal back porch = 30
  7.   Horizontal stop = Horizontal start + window width -1 = 30 + 240 -1
  8.   Vertical start   = vertical synchronization + vertical back porch     = 4
  9.   Vertical stop   = Vertical start + window height -1  = 4 + 160 -1      */
  10.   LTDC_Layer_InitStruct.LTDC_HorizontalStart = 30;
  11.   LTDC_Layer_InitStruct.LTDC_HorizontalStop = (240 + 30 - 1);
  12.   LTDC_Layer_InitStruct.LTDC_VerticalStart = 4;
  13.   LTDC_Layer_InitStruct.LTDC_VerticalStop = 320 + 4 -1;
  14.   
  15.   /* Pixel Format configuration*/           
  16.   LTDC_Layer_InitStruct.LTDC_PixelFormat = LTDC_Pixelformat_RGB565;
  17.   
  18.   /* Alpha constant (255 totally opaque) */
  19.   LTDC_Layer_InitStruct.LTDC_ConstantAlpha = 255;
  20.   
  21.   /* Configure blending factors */      
  22.   LTDC_Layer_InitStruct.LTDC_BlendingFactor_1 = LTDC_BlendingFactor1_PAxCA;   
  23.   LTDC_Layer_InitStruct.LTDC_BlendingFactor_2 = LTDC_BlendingFactor2_PAxCA;  
  24.   
  25.   /* Default Color configuration (configure A,R,G,B component values) */         
  26.   LTDC_Layer_InitStruct.LTDC_DefaultColorBlue = 0;        
  27.   LTDC_Layer_InitStruct.LTDC_DefaultColorGreen = 0;      
  28.   LTDC_Layer_InitStruct.LTDC_DefaultColorRed = 0;         
  29.   LTDC_Layer_InitStruct.LTDC_DefaultColorAlpha = 0;   
  30.   
  31.   /* Input Address configuration */   
  32.   LTDC_Layer_InitStruct.LTDC_CFBStartAdress = (u32)FrameBuffer;
  33.   
  34.   /* the length of one line of pixels in bytes + 3 then :
  35.   Line Lenth = Active high width x number of bytes per pixel + 3
  36.   Active high width         = 240
  37.   number of bytes per pixel = 2    (pixel_format : RGB565)
  38.   */
  39.   LTDC_Layer_InitStruct.LTDC_CFBLineLength = ((240 * 2) + 3);
  40.   
  41.   /*  the pitch is the increment from the start of one line of pixels to the
  42.   start of the next line in bytes, then :
  43.   Pitch = Active high width x number of bytes per pixel     
  44.   */
  45.   LTDC_Layer_InitStruct.LTDC_CFBPitch = (240 * 2);  
  46.   
  47.   /* configure the number of lines */
  48.   LTDC_Layer_InitStruct.LTDC_CFBLineNumber = 320;
  49.   
  50.   LTDC_LayerInit(LTDC_Layer1, <DC_Layer_InitStruct);
  51.   LTDC_LayerCmd(LTDC_Layer1, ENABLE);
  52.   LTDC_ReloadConfig(ENABLE);
  53. }
复制代码
主程序:
  1. int main(void)
  2. {
  3.   LCD_Init();
  4.   LCD_Layer1Init();
  5.   LTDC_Cmd(ENABLE);
  6.   while(1);
  7. }
复制代码

本贴来自于本人博客:http://blog.csdn.net/hexiaolong2009/article/details/43986205
回复

使用道具 举报

1万

主题

6万

回帖

10万

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
106840
QQ
发表于 2015-2-28 19:42:38 | 显示全部楼层
谢谢楼主分享,置cool[s:150]
回复

使用道具 举报

3

主题

14

回帖

23

积分

新手上路

积分
23
 楼主| 发表于 2015-2-28 22:27:03 | 显示全部楼层

回 eric2013 的帖子

eric2013:谢谢楼主分享,置cool[s:150]  (2015-02-28 19:42) 
多谢eric2013支持!
回复

使用道具 举报

36

主题

2040

回帖

2148

积分

至尊会员

积分
2148
发表于 2015-3-1 08:42:17 | 显示全部楼层
[s:149]  [s:149]  [s:149]
Ever tried. Ever failed. No matter. Try Again. Fail again. Fail better.
回复

使用道具 举报

0

主题

2

回帖

0

积分

新手上路

积分
0
发表于 2015-3-3 15:11:21 | 显示全部楼层
谢谢楼主分享
回复

使用道具 举报

2

主题

14

回帖

2

积分

新手上路

加油

积分
2
发表于 2015-3-10 10:04:47 | 显示全部楼层
为什么我下载到板子里面就没得显示呢?
回复

使用道具 举报

6

主题

390

回帖

408

积分

高级会员

积分
408
发表于 2015-5-4 15:36:15 | 显示全部楼层
刚开始研究429
回复

使用道具 举报

8

主题

33

回帖

7

积分

新手上路

积分
7
发表于 2015-6-11 21:05:28 | 显示全部楼层
这个是驱动 ILI9341,并不是裸屏的对吧
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|小黑屋|Archiver|手机版|硬汉嵌入式论坛

GMT+8, 2024-5-7 05:01 , Processed in 0.253688 second(s), 34 queries .

Powered by Discuz! X3.4 Licensed

Copyright © 2001-2023, Tencent Cloud.

快速回复 返回顶部 返回列表