STM32F1 FSMC 控制8位彩 {MOD}液晶屏方法及帮助贴

2019-08-17 01:55发布

最近有一些同学都问我怎样用FSMC控制8位液晶屏!现在都陷入了一个误区基本上都挑不出来!在这我总结一下方法! 1.STM32速度比较高一般的都是45兆以上,这就跟51和430控制TFT的复位时所需要的延时要长一些!一般为(0xff00)级别! 2.有一些事从16位的程序上修改的,这个地方一般都忘了去修改了#define Bank1_LCD_D    ((uint32_t)0x60020000)    //disp Data ADDR
#define Bank1_LCD_C    ((uint32_t)0x60000000)  //disp Reg ADDR、、这 个地方时最容易出错的8位的话不是0x60020000是0x60010000!详细的算法在网上能找到!我用的是STM32VET6! 3.就是你的液晶屏是8位数据控制还是16位数据控制的这个要搞清楚! 总之在这不多说了吧源程序附上!希望对你们有帮助! #define Bank1_LCD_D    ((uint32_t)0x60010000)    //disp Data ADDR
#define Bank1_LCD_C    ((uint32_t)0x60000000)  //disp Reg ADDR uint16_t  a1;
void Delay1(__IO uint32_t nCount)
{
  for(; nCount != 0; nCount--);
}
//************************************写寄存器地址函数************************************//
void LCD_WR_REG(uint8_t index)
{
 *(__IO uint8_t *) (Bank1_LCD_C)= index; }
//***********************************写寄存器数据函数************************************//
void LCD_WR_DATA(uint8_t val)
{
 *(__IO uint8_t *) (Bank1_LCD_D)= val;
 
}
//***********************************读寄存器数据函数************************************//
uint16_t LCD_RD_DATA(uint16_t val)
{
 uint16_t  a;
 LCD_WR_REG(0x2e);
 a=*(__IO uint8_t *) (Bank1_LCD_D);
 a<<=8;
 a|=*(__IO uint8_t *) (Bank1_LCD_D);
 return a;

//***********************************液晶屏管教初始化程序************************************//
void GPIO_TFTConfiguration(void)
{
   GPIO_InitTypeDef GPIO_InitStructure;
 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1;         //LED1控制--PB5
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;    //推挽输出
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  GPIO_Init(GPIOC, &GPIO_InitStructure);       GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13 ;   //LED2, LED3
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  GPIO_Init(GPIOD, &GPIO_InitStructure);
 
 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1 ;   //LED2, LED3;
  GPIO_Init(GPIOE, &GPIO_InitStructure);
 
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_4 | GPIO_Pin_5 |
                                GPIO_Pin_14 | GPIO_Pin_15;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
  GPIO_Init(GPIOD, &GPIO_InitStructure);   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7 | GPIO_Pin_8 | GPIO_Pin_9 | GPIO_Pin_10 ;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
  GPIO_Init(GPIOE, &GPIO_InitStructure);   /* NE1 configuration */
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7;
  GPIO_Init(GPIOD, &GPIO_InitStructure);
 
  /* RS */
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11 ;
  GPIO_Init(GPIOD, &GPIO_InitStructure);
 }
//**************************FSMCINIT*********************************************//
void FSMC_LCD_Init(void)
{
  FSMC_NORSRAMInitTypeDef  FSMC_NORSRAMInitStructure;
  FSMC_NORSRAMTimingInitTypeDef  p;   p.FSMC_AddressSetupTime = 0X01;
  p.FSMC_AddressHoldTime = 0X00;
  p.FSMC_DataSetupTime =0X05;
  p.FSMC_BusTurnAroundDuration = 0X00;
  p.FSMC_CLKDivision = 0X00;
  p.FSMC_DataLatency = 0X00;
  p.FSMC_AccessMode = FSMC_AccessMode_B;             
    FSMC_NORSRAMInitStructure.FSMC_Bank = FSMC_Bank1_NORSRAM1;
    FSMC_NORSRAMInitStructure.FSMC_DataAddressMux = FSMC_DataAddressMux_Disable;
    FSMC_NORSRAMInitStructure.FSMC_MemoryType = FSMC_MemoryType_NOR;
    FSMC_NORSRAMInitStructure.FSMC_MemoryDataWidth = FSMC_MemoryDataWidth_8b;
    FSMC_NORSRAMInitStructure.FSMC_BurstAccessMode = FSMC_BurstAccessMode_Disable;
    FSMC_NORSRAMInitStructure.FSMC_WaitSignalPolarity = FSMC_WaitSignalPolarity_Low;
    FSMC_NORSRAMInitStructure.FSMC_WrapMode = FSMC_WrapMode_Disable;
    FSMC_NORSRAMInitStructure.FSMC_WaitSignalActive = FSMC_WaitSignalActive_BeforeWaitState;
    FSMC_NORSRAMInitStructure.FSMC_WriteOperation = FSMC_WriteOperation_Enable;
    FSMC_NORSRAMInitStructure.FSMC_WaitSignal = FSMC_WaitSignal_Disable;
    FSMC_NORSRAMInitStructure.FSMC_ExtendedMode = FSMC_ExtendedMode_Disable;
    FSMC_NORSRAMInitStructure.FSMC_WriteBurst = FSMC_WriteBurst_Disable;
    FSMC_NORSRAMInitStructure.FSMC_ReadWriteTimingStruct = &p;
    FSMC_NORSRAMInitStructure.FSMC_WriteTimingStruct = &p;
    FSMC_NORSRAMInit(&FSMC_NORSRAMInitStructure);
    // 使能 FSMC Bank1_SRAM Bank
    FSMC_NORSRAMCmd(FSMC_Bank1_NORSRAM1, ENABLE);
} //***********************************TFT 复位操作***************************************//
void lcd_rst(void)
{
 GPIO_ResetBits(GPIOE, GPIO_Pin_1);   
  Delay1(0xAF0000);         
  GPIO_SetBits(GPIOE, GPIO_Pin_1 );   
 Delay1(0xAF0000);
}
//**********************************初始化函数*******************************************//
void LCD_Init(void)
{
   lcd_rst();
//-----------------------------------------------------------------------
   LCD_WR_REG(0xcf); LCD_WR_DATA(0x00); LCD_WR_DATA(0xc1); LCD_WR_DATA(0x30);
   LCD_WR_REG(0xed); LCD_WR_DATA(0x64); LCD_WR_DATA(0x03); LCD_WR_DATA(0x12); LCD_WR_DATA(0x81);
   LCD_WR_REG(0xcb); LCD_WR_DATA(0x39); LCD_WR_DATA(0x2c); LCD_WR_DATA(0x00); LCD_WR_DATA(0x34); LCD_WR_DATA(0x02);
   LCD_WR_REG(0xea); LCD_WR_DATA(0x00); LCD_WR_DATA(0x00);
   LCD_WR_REG(0xe8); LCD_WR_DATA(0x85); LCD_WR_DATA(0x00); LCD_WR_DATA(0x7a);
//-----------------------------------------------------------------------
   LCD_WR_REG(0xc0); LCD_WR_DATA(0x21);           //电源控制
   LCD_WR_REG(0xc1); LCD_WR_DATA(0x12);           //倍压设置
//---------------------------------------------------------------------
   LCD_WR_REG(0xc5); LCD_WR_DATA(0x18); LCD_WR_DATA(0x54);  // VCOM设置1
   LCD_WR_REG(0xc7); LCD_WR_DATA(0x20|0x80);          // VCOM设置2
   LCD_WR_REG(0x3a); LCD_WR_DATA(0x55);               // RGB=16bit/pixel, MCU=16bit/pixel
   LCD_WR_REG(0x36); LCD_WR_DATA(0x08);               // 横屏显示 BGR=1
  
   LCD_WR_REG(0xb1); LCD_WR_DATA(0x00); LCD_WR_DATA(0x18); // 刷屏设置  18
   LCD_WR_REG(0xb6); LCD_WR_DATA(0x0a); LCD_WR_DATA(0xa2); // 功能设定
  LCD_WR_REG(0x2A);
//-----------------------------------------------------------------------
   LCD_WR_REG(0x11);  //唤醒
   Delay1(150);
   LCD_WR_REG(0x29);  //开显示 
}
//**********************************刷屏函数*******************************************//
void fullfull(uint16_t color)
{
   uint32_t i;
    uint8_t color1;
   uint8_t color2;
    color1=(color>>8)&0x00ff;
    color2=color&0x00ff;  
   LCD_WR_REG(0x2A);
  LCD_WR_DATA(0x00);
  LCD_WR_DATA(0x00);
  LCD_WR_DATA(0x00);
  LCD_WR_DATA(0xef);
 
   LCD_WR_REG(0x2B);
  LCD_WR_DATA(0x00);
  LCD_WR_DATA(0x00);
  LCD_WR_DATA(0x01);
  LCD_WR_DATA(0xef); 
   LCD_WR_REG(0x2c);//开始写数据
 for(i=0;i<76800;i++)
        {
            LCD_WR_DATA(color1);
       LCD_WR_DATA(color2);
        }
}
//**********************************显示图片函数*******************************************//
void FullBMP(const uint8_t *APK,uint16_t x1,uint16_t y1,uint16_t w,uint16_t h,uint8_t j)
// 起始列x1,起始行y1,宽w,高h
{
   uint32_t i,k;
    uint16_t x2,y2;
    uint8_t temp1,temp2;
    x2=x1+w-1;
    y2=y1+h-1;
    k=w*h;     LCD_WR_REG(0x2a);
   LCD_WR_DATA((x1>>8)&0x00ff); //列起始高8位
   LCD_WR_DATA(x1&0x00ff);      //列起始低8位
   LCD_WR_DATA((x2>>8)&0x00ff); //列结束高8位
   LCD_WR_DATA(x2&0x00ff);      //列结束低8位
  
   LCD_WR_REG(0x2b);
   LCD_WR_DATA((y1>>8)&0x00ff); //行起始高8位
   LCD_WR_DATA(y1&0x00ff);      //行起始低8位
   LCD_WR_DATA((y2>>8)&0x00ff); //行结束高8位 
   LCD_WR_DATA(y2&0x00ff);      //行结束低8位     LCD_WR_REG(0x2c);//开始写数据
  if(j==1)
  {
    for(i=0;i<k;i++)
     {
      temp1=*APK++;
      temp2=*APK++;
      LCD_WR_DATA(temp1);
      LCD_WR_DATA(temp2);
     }
  }
  else
  {
     for(i=0;i<k;i++)
     {
      temp1=0xff-*APK++;
      temp2=0xff-*APK++;
      LCD_WR_DATA(temp1);
      LCD_WR_DATA(temp2);
     }
   }
}
//======================================================
//  填充块
//======================================================
void FullBOX(uint16_t color,uint16_t x1,uint16_t y1,uint16_t w,uint16_t h)
// 起始列x1,起始行y1,宽w,高h
{
   uint32_t i,k;
    uint16_t x2,y2;
    uint8_t color1,color2;
    color1=(color>>8)&0x00ff;
    color2=color&0x00ff; 
   x2=x1+w-1;
    y2=y1+h-1;
    k=w*h;    LCD_WR_REG(0x2a);
   LCD_WR_DATA((x1>>8)&0x00ff); //列起始高8位
   LCD_WR_DATA(x1&0x00ff);      //列起始低8位
   LCD_WR_DATA((x2>>8)&0x00ff); //列结束高8位
   LCD_WR_DATA(x2&0x00ff);      //列结束低8位
  
   LCD_WR_REG(0x2b);
   LCD_WR_DATA((y1>>8)&0x00ff); //行起始高8位
   LCD_WR_DATA(y1&0x00ff);      //行起始低8位
   LCD_WR_DATA((y2>>8)&0x00ff); //行结束高8位 
   LCD_WR_DATA(y2&0x00ff);      //行结束低8位     LCD_WR_REG(0x2c);//开始写数据
   for(i=0;i<k;i++)
    {
        LCD_WR_DATA(color1);
    LCD_WR_DATA(color2);
    }
}
//======================================================
//  画矩形
//======================================================
void LCD_DrawRectangle(uint16_t color,uint16_t x1,uint16_t y1,uint16_t x,uint16_t y,uint16_t w1,uint16_t h1,uint16_t w2,uint16_t h2)
{      //x=x1+w2-1;y=y1+h1;
     FullBOX(color,x1,y1,w1,h1);  //heng
    FullBOX(color,x1,y1,w2,h2);  // shu
    FullBOX(color,x,y1,w1,h1);  // xiaheng
    FullBOX(color,x1,y,w2,h2);  // zushu
}
//======================================================
//  画点
//======================================================
void LCD_SetPoint(uint16_t color,uint16_t x1,uint16_t y1)
// 起始列x1,起始行y1,宽w,高h
{
    uint16_t x2,y2;
    uint8_t color1,color2;
    color1=(color>>8)&0x00ff;
    color2=color&0x00ff; 
   x2=x1;
    y2=y1;    LCD_WR_REG(0x2a);
   LCD_WR_DATA((x1>>8)&0x00ff); //列起始高8位
   LCD_WR_DATA(x1&0x00ff);      //列起始低8位
   LCD_WR_DATA((x2>>8)&0x00ff); //列结束高8位
   LCD_WR_DATA(x2&0x00ff);      //列结束低8位
  
   LCD_WR_REG(0x2b);
   LCD_WR_DATA((y1>>8)&0x00ff); //行起始高8位
   LCD_WR_DATA(y1&0x00ff);      //行起始低8位
   LCD_WR_DATA((y2>>8)&0x00ff); //行结束高8位 
   LCD_WR_DATA(y2&0x00ff);      //行结束低8位     LCD_WR_REG(0x2c);//开始写数据
   LCD_WR_DATA(color1);
  LCD_WR_DATA(color2);
}
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。