STM32 study notes - TFT2.4 color screen display pictures

Publisher:DreamySunsetLatest update time:2015-08-18 Source: eefocusKeywords:STM32 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere
To display images on a color screen, you need to first complete the color screen driver, and then write the application based on the driver.

If the color screen driver is written well, it can be used all the time, and the focus can be on writing the application, but if it is ported, the driver needs to be ported. In fact, porting only changes the very low-level code close to the processor.

 

Because the STM32F103C8 has only 64K of FLASH on the chip, it cannot store too much image data, and cannot display too large images.

The following program is annotated in detail, and you can basically use it once you understand it.

 

The header file lcd.h of the color screen driver is as follows:

#define  uchar  unsigned  char 

#define  uint  unsigned  int

 

 

#define  Bus_16         //16-bit data mode. If you use 8-bit mode, please comment this statement. If you use 16-bit mode, please open this statement.

#define   LCD_DataPortH  P1      //High 8-bit data port, only the high 8 bits are used in 8-bit mode 

#define   LCD_DataPortL  P0      //low 8-bit data port, in 8-bit mode, the low 8 bits can be left unconnected

 

 

 

#define  LCD_WR     GPIO_Pin_2   //WR   pin definition  P2^5

#define  LCD_RS     GPIO_Pin_1   //RS   pin definition  P2^6

#define  LCD_CS     GPIO_Pin_0   //CS   pin definition  P2^7

#define  LCD_RST    GPIO_Pin_11      //RST  pin definition  P3^3

#define  LCD_RD     GPIO_Pin_8   //RD   pin definition  P3^2

 

 

#define   LCD_SIZE_X 240

#define   LCD_SIZE_Y 320

 

 

 

extern   uint  colors[];

 

extern   void   pic_play(uint  xStart,  uint  xEnd,  uint  yStart,  uint  yEnd); //Function to display the picture

 

 

 

void  delayms(int  count)  ;

//void  LCD_Writ_Bus(char  VH,char  VL);

void  LCD_Init(void);

void  LCD_Writ_Bus(  uint  VH,  uint  VL);

void  LCD_Write_COM(char  VH,char  VL);

void  LCD_Write_DATA(char  VH,char  VL);

void  Pant(char  VH,char  VL);

void  Address_set(unsigned  int  x1, unsigned  int  y1, unsigned  int  x2, unsigned  int  y2);

 

 

 

The driver lcd.c file is as follows:

 

  

 

 

#include  "stm32f10x_lib.h"

#include  "Lcd.h"

 

//Header file storing image data

//#include  "picture.h"

#include  "picture_sara.h"

#include  "xiaoqian.h"

#include  "yang.h"

 

 

#define     LCD_rest(x)  GPIO_SetBits(GPIOA,  LCD_RST):    GPIO_ResetBits(GPIOA,  LCD_RST)  //Define bit operation function yourself

#define     LCD_rd(x)    GPIO_SetBits(GPIOA,  LCD_RD)    GPIO_ResetBits(GPIOA,  LCD_RD)

#define     LCD_rs(x)    GPIO_SetBits(GPIOB,  LCD_RS)    GPIO_ResetBits(GPIOB,  LCD_RS)

#define     LCD_wr(x)    GPIO_SetBits(GPIOB,  LCD_WR)    GPIO_ResetBits(GPIOB,  LCD_WR)

#define     LCD_cs(x)    GPIO_SetBits(GPIOB,  LCD_CS)    GPIO_ResetBits(GPIOB,  LCD_CS)

 

uint  colors[]=

{

  0xf800,0x07e0,0x001f,0xffe0,0x0000,0x07ff,0xf81f,0xffff   //color data

};

 

 

 

void  LCD_Init(void) //Initialize LCD

{

 

    LCD_rest(1);

    delayms(5);

LCD_rest(0);

delayms(5);

LCD_rest(1);

delayms(5);

 

LCD_cs(0);   //Open chip select

//

LCD_Write_COM(0x00,0xE5);  LCD_Write_DATA(0x78,0xF0);  //  set  SRAM  internal  timing

LCD_Write_COM(0x00,0x01);  LCD_Write_DATA(0x01,0x00);  //  set  SS  and  SM  bit

LCD_Write_COM(0x00,0x02);  LCD_Write_DATA(0x07,0x00);  //  set  line  inversion

LCD_Write_COM(0x00,0x03);  LCD_Write_DATA(0x10,0x30);  //  set  GRAM  write  direction  and  BGR=1.

LCD_Write_COM(0x00,0x04);  LCD_Write_DATA(0x00,0x00);  //  Resize  register

LCD_Write_COM(0x00,0x08);  LCD_Write_DATA(0x02,0x07);  //  set  the  back  porch  and  front  porch

LCD_Write_COM(0x00,0x09);  LCD_Write_DATA(0x00,0x00);  //  set  non-display  area  refresh  cycle  ISC[3:0]

LCD_Write_COM(0x00,0x0A);  LCD_Write_DATA(0x00,0x00);  //  FMARK  function

LCD_Write_COM(0x00,0x0C);  LCD_Write_DATA(0x00,0x00);  //  RGB  interface  setting

LCD_Write_COM(0x00,0x0D);  LCD_Write_DATA(0x00,0x00);  //  Frame  marker  Position

LCD_Write_COM(0x00,0x0F);  LCD_Write_DATA(0x00,0x00);  //  RGB  interface  polarity

//

LCD_Write_COM(0x00,0x10);  LCD_Write_DATA(0x00,0x00);  //  SAP,  BT[3:0],  AP,  DSTB,  SLP,  STB

LCD_Write_COM(0x00,0x11);  LCD_Write_DATA(0x00,0x07);  //  DC1[2:0],  DC0[2:0],  VC[2:0]

LCD_Write_COM(0x00,0x12);  LCD_Write_DATA(0x00,0x00);  //  VREG1OUT  voltage

LCD_Write_COM(0x00,0x13);  LCD_Write_DATA(0x00,0x00);  //  VDV[4:0]  for  VCOM  amplitude

LCD_Write_COM(0x00,0x07);  LCD_Write_DATA(0x00,0x01);

delayms(50);  //  Dis-charge  capacitor  power  voltage

LCD_Write_COM(0x00,0x10);  LCD_Write_DATA(0x10,0x90);  //  1490//SAP,  BT[3:0],  AP,  DSTB,  SLP,  STB

LCD_Write_COM(0x00,0x11);  LCD_Write_DATA(0x02,0x27);  //  DC1[2:0],  DC0[2:0],  VC[2:0]

delayms(50);  //  Delay  50ms

LCD_Write_COM(0x00,0x12);  LCD_Write_DATA(0x00,0x1F);  //001C//  Internal  reference  voltage=  Vci;

delayms(50);  //  Delay  50ms

LCD_Write_COM(0x00,0x13);  LCD_Write_DATA(0x15,0x00);  //0x1000//1400    Set  VDV[4:0]  for  VCOM  amplitude   1A00

LCD_Write_COM(0x00,0x29);  LCD_Write_DATA(0x00,0x27);  //0x0012  //001a   Set  VCM[5:0]  for  VCOMH   //0x0025   0034

LCD_Write_COM(0x00,0x2B);  LCD_Write_DATA(0x00,0x0D);  //  Set  Frame  Rate    000C

delayms(50);  //  Delay  50ms

LCD_Write_COM(0x00,0x20);  LCD_Write_DATA(0x00,0x00);  //  GRAM  horizontal  Address

LCD_Write_COM(0x00,0x21);  LCD_Write_DATA(0x00,0x00);  //  GRAM  Vertical  Address

//  -----------  Adjust  the  Gamma  Curve  ----------//

LCD_Write_COM(0x00,0x30);  LCD_Write_DATA(0x00,0x00);

LCD_Write_COM(0x00,0x31);  LCD_Write_DATA(0x07,0x07);

LCD_Write_COM(0x00,0x32);  LCD_Write_DATA(0x03,0x07);

LCD_Write_COM(0x00,0x35);  LCD_Write_DATA(0x02,0x00);

LCD_Write_COM(0x00,0x36);  LCD_Write_DATA(0x00,0x08);//0207

LCD_Write_COM(0x00,0x37);  LCD_Write_DATA(0x00,0x04);//0306

LCD_Write_COM(0x00,0x38);  LCD_Write_DATA(0x00,0x00);//0102

LCD_Write_COM(0x00,0x39);  LCD_Write_DATA(0x07,0x07);//0707

LCD_Write_COM(0x00,0x3C);  LCD_Write_DATA(0x00,0x02);//0702

LCD_Write_COM(0x00,0x3D);  LCD_Write_DATA(0x1D,0x04);//1604

 

//------------------  Set  GRAM  area  ---------------//

LCD_Write_COM(0x00,0x50);  LCD_Write_DATA(0x00,0x00);  //  Horizontal  GRAM  Start  Address

LCD_Write_COM(0x00,0x51);  LCD_Write_DATA(0x00,0xEF);  //  Horizontal  GRAM  End  Address

LCD_Write_COM(0x00,0x52);  LCD_Write_DATA(0x00,0x00);  //  Vertical  GRAM  Start  Address

LCD_Write_COM(0x00,0x53);  LCD_Write_DATA(0x01,0x3F);  //  Vertical  GRAM  Start  Address

LCD_Write_COM(0x00,0x60);  LCD_Write_DATA(0xA7,0x00);  //  Gate  Scan  Line

LCD_Write_COM(0x00,0x61);  LCD_Write_DATA(0x00,0x01);  //  NDL,VLE,  REV

LCD_Write_COM(0x00,0x6A);  LCD_Write_DATA(0x00,0x00);  //  set  scrolling  line

//--------------  Partial  Display  Control  ---------//

LCD_Write_COM(0x00,0x80);  LCD_Write_DATA(0x00,0x00);

LCD_Write_COM(0x00,0x81);  LCD_Write_DATA(0x00,0x00);

LCD_Write_COM(0x00,0x82);  LCD_Write_DATA(0x00,0x00);

LCD_Write_COM(0x00,0x83);  LCD_Write_DATA(0x00,0x00);

LCD_Write_COM(0x00,0x84);  LCD_Write_DATA(0x00,0x00);

LCD_Write_COM(0x00,0x85);  LCD_Write_DATA(0x00,0x00);

//--------------  Panel  Control  -------------------//

LCD_Write_COM(0x00,0x90);  LCD_Write_DATA(0x00,0x10);

LCD_Write_COM(0x00,0x92);  LCD_Write_DATA(0x06,0x00);

LCD_Write_COM(0x00,0x07);  LCD_Write_DATA(0x01,0x33);  //  262K  color  and  display  ON

 [page]

LCD_cs(1);   //Disable chip select

 

 

 

void  delayms(int  count)   // 

{

        int  i,j;

        for(i=0;i

                for(j=0;j<100;j++);

}

 

#ifdef   Bus_16     //Conditional compilation - 16-bit data mode   

void  LCD_Writ_Bus(uint  VH  uint  VL)    //parallel data write function

{

  //LCD_DataPortH=VH;    //High bit P1 port

//Shield the upper 8 bits   and send data to PA0-PA7

GPIOA->BSRR  VH  0x00ff;   

    GPIOA->BRR   (~VH)  0x00ff;

   //GPIO_SetBits(GPIOA,   VH  0x00ff);

   //GPIO_ResetBits(GPIOA,  (~  VH  0x00ff));

 

    //LCD_DataPortL=VL;    //Low bit P0 port

//Shield the lower 8 bits   and send data to PB8-PB15

GPIOB->BSRR  (VL)<<8  0xff00;   

    GPIOB->BRR   ((~VL)<<8)  0xff00;

 

LCD_wr(0);

LCD_wr(1);

}

#else //Conditional compilation - 8-bit data mode 

void  LCD_Writ_Bus(char  VH,char  VL)    //parallel data write function

{

     //LCD_DataPortH=VH;   //Eight-bit mode uses port P0 to send data

     //LCD_wr(0);

//LCD_wr(1);

//LCD_DataPortH=VL;  //Eight-bit mode uses P0 port to send data

//LCD_wr(0);

//LCD_wr(1);

}

#endif

 

 

void  LCD_Write_COM(char  VH,char  VL)   //Send command

{

    LCD_rs(0);

LCD_Writ_Bus(VH,VL);

}

 

 

void  LCD_Write_DATA(char  VH,char  VL) //Send data

{

    LCD_rs(1);

LCD_Writ_Bus(VH,VL);

}

 

 

void  Pant(char  VH,char  VL)     //fill the full screen function

{

int  i,j;

LCD_cs(0);   //Open chip select

Address_set(0,0,239,319);

    for(i=0;i<320;i++)

 {

  for  (j=0;j<240;j++)

    {

          LCD_Write_DATA(VH,VL);

    }

 

  }

     LCD_cs(1);   //Disable chip select

}

 

 

void  Address_set(unsigned  int  x1,unsigned  int  y1,unsigned  int  x2,unsigned  int  y2) //Set address range function

{

LCD_Write_COM(0x00,0x20);LCD_Write_DATA(x1>>8,x1);    //Set the X coordinate position

    LCD_Write_COM(0x00,0x21);LCD_Write_DATA(y1>>8,y1);    //Set Y coordinate position

    LCD_Write_COM(0x00,0x50);LCD_Write_DATA(x1>>8,x1);    //Start X

LCD_Write_COM(0x00,0x52);LCD_Write_DATA(y1>>8,y1);    //Start Y

    LCD_Write_COM(0x00,0x51);LCD_Write_DATA(x2>>8,x2);    //End X

LCD_Write_COM(0x00,0x53);LCD_Write_DATA(y2>>8,y2);    //End Y

    LCD_Write_COM(0x00,0x22);  

}

 

 

 

 

void   pic_play(uint  Start_X,  uint  End_X,uint  Start_Y,uint  End_Y)

{

   uint  m;

   //LCD_CS  0;

   GPIO_ResetBits(GPIOB,LCD_CS);   //Open chip select

   Address_set(Start_X,Start_Y,End_X,End_Y)  ; //Specify the range of image display

  //  LCD_RS   =  1; 

   GPIO_SetBits(GPIOB,  LCD_RS);    //Disable chip select

 

   //Loop and write all the data of the picture in sequence

 

 

  

 

   Address_set(0,0,141,79);     //When displaying pictures on a color screen, be sure to pay attention to the aspect ratio of the picture. If the aspect ratio is incorrect, the displayed picture will be distorted

   //For example, the length and width of the image output by the modulus software are (177, 100), so the display range of the image here must be set to (0, 0, 176, 99), that is, the x range is 0~176, and the y range is

   //0~99. Only in this way can the picture be displayed normally, otherwise the picture will be deformed.

   for(m=0;m<22728/2;m++)    //The number 35400 here specifies the range of the displayed image. Since there are only 35400 data in the image, it is specified here

   //The whole picture is displayed. If this number is less than 35400, only part of the picture will be displayed.

   {

       LCD_Write_DATA(gImage_sara[m*2+1],gImage_sara[m*2]);

   }

 

 

Address_set(0,90,65,189);   //Be sure to keep the proportion

for(m=0;m<13200/2;m++)

{

    LCD_Write_DATA(gImage_xiaoqian[m*2+1],gImage_xiaoqian[m*2]);

}

 

 

    Address_set(75,90,137,189); //Be sure to keep the proportion

for(m=0;m<12600/2;m++)

{

    LCD_Write_DATA(gImage_yang[m*2+1],gImage_yang[m*2]);

}

  

   //LCD_CS  1;

   GPIO_SetBits(GPIOB,  LCD_CS);   //Disable chip select

 

}

 

 

 

 

Main program main.c

In the main program, complete the initialization configuration of the system, and use the color screen driver to complete the display of pictures and other functions. The program is as follows:

  

 

 

#include  "stm32f10x_lib.h"

#include  "lcd.h"

 

 

GPIO_InitTypeDef  GPIO_InitStructure;    //Define the structure variable for GPIO initialization

ErrorStatus  HSEStartUpStatus; //Define the error status variable, which is an enumeration type

 

 

void  RCC_Configuration(void);

void  NVIC_Configuration(void);

void  Delay(vu32  nCount);

 

 

 

int  main(void)

{

#ifdef  DEBUG

  debug();   //Online debugging

#endif

 [page]

 

   RCC_Configuration();       //System clock configuration function   

 

   NVIC_Configuration();      //NVIC configuration function

 

    //Start the GPIO module clock

  RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA  RCC_APB2Periph_GPIOB  RCC_APB2Periph_AFIO,  ENABLE);

 

    //Set debug to normal IO port and disable SWJ

  GPIO_PinRemapConfig(GPIO_Remap_SWJ_Disable,ENABLE);  

                            

  GPIO_InitStructure.GPIO_Pin  GPIO_Pin_All;   //All GPIOs are ports of the same type

  GPIO_InitStructure.GPIO_Mode  GPIO_Mode_Out_PP;   //Push-pull output

  GPIO_InitStructure.GPIO_Speed  ​​=  GPIO_Speed_50MHz;   //The maximum output frequency is 50HZ

  GPIO_Init(GPIOA,  &GPIO_InitStructure);    //Initialize GPIOA port

  GPIO_Init(GPIOB,  &GPIO_InitStructure);    //Initialize GPIOB port

 

   GPIO_Write(GPIOA,0xffff);  // Set all 16  GPIOA  ports to high level

   GPIO_Write(GPIOB,0xffff);  // Set all 16  GPIOB  ports to high level

 

   LCD_Init();     //Initialize LCD

 

   //LCD_clear(6);    //Clear screen to purple

   Pant(colors[6]>>8,colors[6]);    //Paint the entire screen purple

   

 

   pic_play(0,173,0,173); //Display the picture

 

   //pic_play(7,61,8,74); //Display the picture

  //  pic_play(68,122,8,74);

  //  pic_play(7,61,82,148);

  //  pic_play(68,122,82,148);

 

   while(1);

 

}

 

 

void  RCC_Configuration(void)

  

 //Reset RCC external device registers to default values

  RCC_DeInit();

 

  // Turn on the external high-speed crystal oscillator

  RCC_HSEConfig(RCC_HSE_ON);

 

   //Wait for external high-speed clock to be ready

  HSEStartUpStatus  RCC_WaitForHSEStartUp();

 

  if(HSEStartUpStatus  ==  SUCCESS)    //The external high-speed clock is ready

  {     

    // Enable FLASH prefetch function

    FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable);

 

    //FLASH delay 2 cycles

    FLASH_SetLatency(FLASH_Latency_2);

 

  //Configure AHB (HCLK) clock = SYSCLK

    RCC_HCLKConfig(RCC_SYSCLK_Div1);  

  

   //Configure APB2 (PCLK2) clock = AHB clock

    RCC_PCLK2Config(RCC_HCLK_Div1); 

 

    //Configure APB1 (PCLK1) clock = AHB  1/2 clock

    RCC_PCLK1Config(RCC_HCLK_Div2);

 

     //Configure PLL clock  ==  external high-speed crystal clock * 9   PLLCLK  8MHz  72  MHz 

    RCC_PLLConfig(RCC_PLLSource_HSE_Div1,  RCC_PLLMul_9);

 

    // Enable PLL clock

    RCC_PLLCmd(ENABLE);

 

   //Wait for PLL clock to be ready

    while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY)  ==  RESET)

    {

    }

 

  //Configure system clock  PLL clock

    RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);

 

   // Check if the PLL clock is used as the system clock

    while(RCC_GetSYSCLKSource()  !=  0x08)

    {

    }

  }

}

 

 

void  NVIC_Configuration(void)

{

#ifdef   VECT_TAB_RAM  

   

  NVIC_SetVectorTable(NVIC_VectTab_RAM,  0x0); 

#else  

   

  NVIC_SetVectorTable(NVIC_VectTab_FLASH,  0x0);   

#endif

}

 

 

void  Delay(vu32  nCount)

{

  for(;  nCount  !=  0;  nCount--);

}

 

 

 

 

 

#ifdef   DEBUG

 

void  assert_failed(u8*  file,  u32  line)

  

 

  

  while  (1)

  {

  }

}

#endif

Keywords:STM32 Reference address:STM32 study notes - TFT2.4 color screen display pictures

Previous article:STM32 study notes - TFT2.4 color screen displays characters and Chinese characters
Next article:ucos-ii study notes - s12 lcd led two-task semaphore communication

Latest Microcontroller Articles
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号