×
单片机 > 单片机程序设计 > 详情

STM32F429之LTDC驱动图解

发布时间:2020-08-24 发布时间:
|

本文基于ST官方demo板STM32F429 Discovery硬件平台,以看图说话的形式给大家讲解LTDC的主要参数配置。关于本文提到的代码部分均摘自本人另一片文章《STM32F429之LTDC代码模板》,LCD硬件为240x320,驱动IC为ili9341。本文目的意在让大家通过几张图就能掌握STM32F429 LTDC控制器的配置要领,而从干涩的文字中解脱出来,方便记忆。当然本文只是讲解了LTDC一些常用的设置,关于更多细节的操作还是得参照ST的官方datasheet。


一、关于LTDC外设的时钟配置,只需要记住下面这张图就可以了(图片来源于STM32CubeMX RCC时钟树):


该图所对应的代码如下:

  1. /* Configure PLLSAI prescalers for LCD */  

  2. /* PLLSAI_VCO Input = HSE_VALUE/PLL_M = 1 Mhz */  

  3. /* PLLSAI_VCO Output = PLLSAI_VCO Input * PLLSAI_N = 192 Mhz */  

  4. /* PLLLCDCLK = PLLSAI_VCO Output/PLLSAI_R = 192/3 = 64 Mhz */  

  5. /* LTDC clock frequency = PLLLCDCLK / RCC_PLLSAIDivR = 64/8 = 8 Mhz */  

  6. RCC_PLLSAIConfig(192, 7, 3);  

  7. RCC_LTDCCLKDivConfig(RCC_PLLSAIDivR_Div8);  

  8.   

  9. /* Enable PLLSAI Clock */  

  10. RCC_PLLSAICmd(ENABLE);  

  11. /* Wait for PLLSAI activation */  

  12. while(RCC_GetFlagStatus(RCC_FLAG_PLLSAIRDY) == RESET)  

  13. {  

  14. }  



二、关于LCD的信号时序配置参数,只需要记住下面这张图就可以了:


注意:这部分需要根据LCD Datasheet要求严格配置。

改图所对应的代码如下:

  1. /* Initialize the horizontal synchronization polarity as active low*/  

  2. LTDC_InitStruct.LTDC_HSPolarity = LTDC_HSPolarity_AL;       

  3. /* Initialize the vertical synchronization polarity as active low */    

  4. LTDC_InitStruct.LTDC_VSPolarity = LTDC_VSPolarity_AL;       

  5. /* Initialize the data enable polarity as active low */   

  6. LTDC_InitStruct.LTDC_DEPolarity = LTDC_DEPolarity_AL;       

  7. /* Initialize the pixel clock polarity as input pixel clock */   

  8. LTDC_InitStruct.LTDC_PCPolarity = LTDC_PCPolarity_IPC;  

  9.   

  10. /* Timing configuration */  

  11. /* Configure horizontal synchronization width */       

  12. LTDC_InitStruct.LTDC_HorizontalSync = 9;  

  13. /* Configure vertical synchronization height */  

  14. LTDC_InitStruct.LTDC_VerticalSync = 1;  

  15. /* Configure accumulated horizontal back porch */  

  16. LTDC_InitStruct.LTDC_AccumulatedHBP = 29;   

  17. /* Configure accumulated vertical back porch */  

  18. LTDC_InitStruct.LTDC_AccumulatedVBP = 3;    

  19. /* Configure accumulated active width */    

  20. LTDC_InitStruct.LTDC_AccumulatedActiveW = 269;  

  21. /* Configure accumulated active height */  

  22. LTDC_InitStruct.LTDC_AccumulatedActiveH = 323;  

  23. /* Configure total width */  

  24. LTDC_InitStruct.LTDC_TotalWidth = 279;   

  25. /* Configure total height */  

  26. LTDC_InitStruct.LTDC_TotalHeigh = 327;  



三、LTDC的层——Layer

关于层的概念有以下几点:

1. STM32F429共有3个层,分别为Background、Layer1、Layer2,任何时候LCD显示的图像都是由层叠加后的最终结果;

2. Background层任何时候都有效,而Layer1、Layer2可以通过软件配置使能或禁止;

3. 这3个层的空间顺序,从下往上依次为Background、Layer1、Layer2,所以叠加的顺序如下图:


每个Layer支持窗口(Window)操作,所谓Window,就是指该层的图像只有在Window区域内有效,而Window区域外则用该层的DefaultColor填充。如下图:



关于Layer Window显示位置及大小的配置,记住下面这张图就可以了:


该图所对应的代码如下:

  1. /* Windowing configuration */   

  2. /* In this case all the active display area is used to display a picture then: 

  3. Horizontal start = horizontal synchronization + Horizontal back porch = 30  

  4. Horizontal stop = Horizontal start + window width -1 = 30 + 240 -1 

  5. Vertical start   = vertical synchronization + vertical back porch     = 4 

  6. Vertical stop   = Vertical start + window height -1  = 4 + 320 -1      */   

  7. LTDC_Layer_InitStruct.LTDC_HorizontalStart = 30;  

  8. LTDC_Layer_InitStruct.LTDC_HorizontalStop = (240 + 30 - 1);   

  9. LTDC_Layer_InitStruct.LTDC_VerticalStart = 4;  

  10. LTDC_Layer_InitStruct.LTDC_VerticalStop = 320 + 4 -1;   



每个Layer关联了一个显示缓冲区FrameBuffer,由于Background只能使用单色填充,所以它没有FrameBuffer。关于FrameBuffer有几个参数:Pixel Format、Buffer Address、Line Length、Number of lines、Buffer Pitch,这几个参数都是针对于Layer的Window而言的,它们的含义如下:

Pixel Format:不多说,RGB565、ARGB8888、ARGB1555等等;

Buffer Address:显示缓冲区的起始地址;

Line Length:window所对应的缓冲区宽度+3,以字节为单位;

Number of Lines:window所对应的缓冲区高度,以字节为单位;

Buffer Pitch:从这一行起始到下一行起始所经过的字节数,说白了就是换行时的一个行指针增量(也可以理解为整幅图像的宽度,以字节为单位);


假设Layer1的FrameBuffer如下图(RGB565):

                                        

设置window要显示的图像为下图粉色部分:


结合之前设置的Window的位置,最终显示的图像如下图(不考虑Layer2及透明色):


由此可以看到:

Start Address、Buffer Pitch决定了window在显存中每行的起始位置;

Line Length、Line Number决定了window在显存中的大小。

该部分所对应的代码如下:


  1. /* Input Address configuration */      

  2. LTDC_Layer_InitStruct.LTDC_CFBStartAdress = (u32)FrameBuffer;  

  3.   

  4. /* the length of one line of pixels in bytes + 3 then : 

  5. Line Lenth = Active high width x number of bytes per pixel + 3  

  6. Active high width         = 240  

  7. number of bytes per pixel = 2    (pixel_format : RGB565)  

  8. */  

  9. LTDC_Layer_InitStruct.LTDC_CFBLineLength = ((240 * 2) + 3);  

  10.   

  11. /*  the pitch is the increment from the start of one line of pixels to the  

  12. start of the next line in bytes, then : 

  13. Pitch = Active high width x number of bytes per pixel      

  14. */  

  15. LTDC_Layer_InitStruct.LTDC_CFBPitch = (240 * 2);    

  16.   

  17. /* configure the number of lines */  

  18. LTDC_Layer_InitStruct.LTDC_CFBLineNumber = 320;  


需要注意的是,为了保证图像能正确显示,请务必确认显存中图像的大小与window显示的大小一致,即Line Length、Line Number要与(HStop-HStart)、(VStop-VStart)相一致,否则显示将不正确。

最后不要忘了设置完所有Layer的寄存器后,光调用LTDC_LayerCmd()还不够,还需要调用LTDC_ReloadConfig(ENABLE)函数才能使所有层的设置生效。


『本文转载自网络,版权归原作者所有,如有侵权请联系删除』

热门文章 更多
单片机中高阻态的实质及意义