jinglixixi 发表于 2022-2-19 09:59

【GD32L233C-START测评】TFT屏的显示驱动

<p class="MsoNoSpacing" style="text-align:justify"><span style="font-size:10.5pt"><span style="font-family:Calibri,sans-serif"><span style="font-size:9.5pt"><span style="font-family:&quot;微软雅黑&quot;,&quot;sans-serif&quot;"><span style="color:#626262">除了OLED显示屏之外,另一种大尺寸的彩色显示屏当数TFT显示屏,此类显示屏在接口方式上有2种方式,一是采用SPI接口,另一种则是采用并行接口方式。相对来讲还是SPI接口比较节省GPIO资源,故这里所介绍的是SPI接口的TFT显示屏。</span></span></span></span></span></p>

<p style="text-align:justify"><span style="font-size:10.5pt"><span style="font-family:Calibri,sans-serif"><span style="font-size:9.5pt"><span style="font-family:&quot;微软雅黑&quot;,&quot;sans-serif&quot;"><span style="color:#626262">由于它与前面介绍的基于SPI接口的彩色OLED屏比较相似,故在显示驱动方面有极大的借鉴意义,可较顺利地实现。</span></span></span></span></span></p>

<p style="text-align:justify"><span style="font-size:10.5pt"><span style="font-family:Calibri,sans-serif"><span style="font-size:9.5pt"><span style="font-family:&quot;微软雅黑&quot;,&quot;sans-serif&quot;"><span style="color:#626262">这里所用的是2.4寸的TFT屏,其分辨率为240*320。</span></span></span></span></span></p>

<p style="text-align:justify"><span style="font-size:10.5pt"><span style="font-family:Calibri,sans-serif"><span style="font-size:9.5pt"><span style="font-family:&quot;微软雅黑&quot;,&quot;sans-serif&quot;"><span style="color:#626262">该显示屏与开发板的连接关系为:</span></span></span></span></span></p>

<p style="text-align:justify"><span style="font-size:10.5pt"><span style="font-family:Calibri,sans-serif">CLK----PB10</span></span></p>

<p style="text-align:justify"><span style="font-size:10.5pt"><span style="font-family:Calibri,sans-serif">DIN ----PB11</span></span></p>

<p style="text-align:justify"><span style="font-size:10.5pt"><span style="font-family:Calibri,sans-serif">DC&nbsp; ----PB13</span></span></p>

<p style="text-align:justify"><span style="font-size:10.5pt"><span style="font-family:Calibri,sans-serif">RST ----PB14</span></span></p>

<p style="text-align:justify"><span style="font-size:10.5pt"><span style="font-family:Calibri,sans-serif">CS&nbsp; ----PB15</span></span></p>

<p style="text-align:justify"><span style="font-size:10.5pt"><span style="font-family:Calibri,sans-serif"><span style="font-size:9.5pt"><span style="font-family:&quot;微软雅黑&quot;,&quot;sans-serif&quot;"><span style="color:#626262">相关引脚输出高低电平的语句定义为:</span></span></span></span></span></p>

<p style="text-align:justify"><span style="font-size:10.5pt"><span style="font-family:Calibri,sans-serif">#define LCD_SCK_Low()&nbsp;&nbsp; gpio_bit_reset(GPIOB, GPIO_PIN_10)</span></span></p>

<p style="text-align:justify"><span style="font-size:10.5pt"><span style="font-family:Calibri,sans-serif">#define LCD_SCK_High()&nbsp; gpio_bit_set(GPIOB, GPIO_PIN_10)</span></span></p>

<p style="text-align:justify"><span style="font-size:10.5pt"><span style="font-family:Calibri,sans-serif">#define LCD_SDI_Low()&nbsp;&nbsp; gpio_bit_reset(GPIOB, GPIO_PIN_11)</span></span></p>

<p style="text-align:justify"><span style="font-size:10.5pt"><span style="font-family:Calibri,sans-serif">#define LCD_SDI_High() &nbsp;&nbsp;gpio_bit_set(GPIOB, GPIO_PIN_11)</span></span></p>

<p style="text-align:justify"><span style="font-size:10.5pt"><span style="font-family:Calibri,sans-serif">#define LCD_DC_Low()&nbsp; &nbsp;&nbsp;gpio_bit_reset(GPIOB, GPIO_PIN_13)</span></span></p>

<p style="text-align:justify"><span style="font-size:10.5pt"><span style="font-family:Calibri,sans-serif">#define LCD_DC_High()&nbsp;&nbsp; gpio_bit_set(GPIOB, GPIO_PIN_13)</span></span></p>

<p style="text-align:justify"><span style="font-size:10.5pt"><span style="font-family:Calibri,sans-serif">#define LCD_REST_Low()&nbsp; gpio_bit_reset(GPIOB, GPIO_PIN_14)</span></span></p>

<p style="text-align:justify"><span style="font-size:10.5pt"><span style="font-family:Calibri,sans-serif">#define LCD_REST_High() &nbsp;gpio_bit_set(GPIOB, GPIO_PIN_14)</span></span></p>

<p style="text-align:justify"><span style="font-size:10.5pt"><span style="font-family:Calibri,sans-serif">#define LCD_CS_Low()&nbsp; &nbsp;&nbsp;gpio_bit_reset(GPIOB, GPIO_PIN_15)</span></span></p>

<p style="text-align:justify"><span style="font-size:10.5pt"><span style="font-family:Calibri,sans-serif">#define LCD_CS_High()&nbsp; &nbsp;&nbsp;gpio_bit_set(GPIOB, GPIO_PIN_15)</span></span></p>

<p style="text-align:justify"><span style="font-size:10.5pt"><span style="font-family:Calibri,sans-serif"><span style="font-size:9.5pt"><span style="font-family:&quot;微软雅黑&quot;,&quot;sans-serif&quot;"><span style="color:#626262">以模拟发送进行数据发送的函数为:</span></span></span></span></span></p>

<pre>
<code class="language-cpp">void LCD_Writ_Bus(unsigned char com)
{
  unsigned char uci;
  for(uci=0;uci&lt;8;uci++)
  {
        if(com &amp; 0x80)
        {
            LCD_SDI_High();
        }
        else
        {
            LCD_SDI_Low();
        }
        com = com &lt;&lt; 1;
        LCD_SCK_Low();
        LCD_SCK_High();
  }
}</code></pre>

<p style="text-align:justify"><span style="font-size:10.5pt"><span style="font-family:Calibri,sans-serif"><span style="font-size:9.5pt"><span style="font-family:&quot;微软雅黑&quot;,&quot;sans-serif&quot;"><span style="color:#626262">该显示屏的清屏函数为:</span></span></span></span></span></p>

<pre>
<code class="language-cpp">void LCD_Clear(uint16_t Color)
{
    char VH,VL;
    uint16_t i,j;
    VH=Color&gt;&gt;8;
    VL=Color;
    Address_set(0,0,LCD_W-1,LCD_H-1);
    for(i=0;i&lt;LCD_W;i++)
    {
       for (j=0;j&lt;LCD_H;j++)
        {
            LCD_WR_DATA8(VH);
            LCD_WR_DATA8(VL);
        }
    }
}</code></pre>

<p style="text-align:justify">&nbsp;</p>

<p style="text-align:justify"><span style="font-size:10.5pt"><span style="font-family:Calibri,sans-serif"><span style="font-family:宋体">实现字符显示的函数为:</span></span></span></p>

<pre>
<code class="language-cpp">void LCD_ShowChar(uint16_t x,uint16_t y,char num,char size,char mode)
{
    char temp,t1,t;
    uint16_t y0=y;
    uint16_t colortemp=POINT_COLOR;
    num=num-' ';
    if(!mode)
    {
         for(t=0;t&lt;size;t++)
         {
              if(size==12)temp=asc2_1206;
              else temp=asc2_1608;
              for(t1=0;t1&lt;8;t1++)
              {
                  if(temp&amp;0x80) POINT_COLOR=colortemp;
                  else  POINT_COLOR=BACK_COLOR;
                  LCD_DrawPoint(x,y);
                  temp&lt;&lt;=1;
                  y++;
                  if(y&gt;=320)  { POINT_COLOR=colortemp; return; }
                  if((y-y0)==size)
                  {
                          y=y0;
                          x++;
                          if(x&gt;=240) {POINT_COLOR=colortemp; return;}
                          break;
                  }
             }
          }
    }
    else
    {
       for(t=0;t&lt;size;t++)
         {
              if(size==12)temp=asc2_1206;
              else temp=asc2_1608;
              for(t1=0;t1&lt;8;t1++)
             {
                   if(temp&amp;0x80) LCD_DrawPoint(x,y);
                    temp&lt;&lt;=1;
                    y++;
                    if(y&gt;=320){POINT_COLOR=colortemp;return;}
                    if((y-y0)==size)
                    {
                           y=y0;
                           x++;
                           if(x&gt;=240){POINT_COLOR=colortemp;return;}
                            break;
                    }
               }
          }
     }
     POINT_COLOR=colortemp;
}</code></pre>

<p style="text-align:justify"><span style="font-size:10.5pt"><span style="font-family:Calibri,sans-serif"><span style="font-family:宋体">实现字符串显示的主程序为:</span></span></span></p>

<pre>
<code class="language-cpp">int main(void)
{
   systick_config();
   rcu_periph_clock_enable(RCU_GPIOB);
   gpio_mode_set(GPIOB, GPIO_MODE_OUTPUT, GPIO_PUPD_NONE, GPIO_PIN_10 | GPIO_PIN_11| GPIO_PIN_13| GPIO_PIN_14| GPIO_PIN_15);
   gpio_output_options_set(GPIOB, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_10 | GPIO_PIN_11| GPIO_PIN_13| GPIO_PIN_14| GPIO_PIN_15);
   delay_1ms(500);
   LCD_Init();
   LCD_Clear(RED);
   xianshi();
   Delay32M_ms(2000);
   LCD_Clear(BLACK);
   show_image(110,110,1);
   while(1);
}</code></pre>

<p style="text-align:justify"><span style="font-size:10.5pt"><span style="font-family:Calibri,sans-serif"><span style="font-family:宋体">经程序的编译与下载,其显示效果如图</span>1<span style="font-family:宋体">所示。</span></span></span></p>

<p style="text-align:justify"><span style="font-size:10.5pt"><span style="font-family:Calibri,sans-serif"><span style="font-family:&quot;微软雅黑&quot;,&quot;sans-serif&quot;"><span style="color:#666666">图1 显示效果</span></span></span></span></p>

<p style="text-align:justify"><span style="font-size:10.5pt"><span style="font-family:Calibri,sans-serif"><span style="font-family:宋体">在配置正弦曲线数组的情况下,使用画线函数则可完成波形曲线的绘制。</span></span></span></p>

<p style="text-align:justify"><span style="font-size:10.5pt"><span style="font-family:Calibri,sans-serif"><span style="font-family:宋体">正弦曲线数组为:</span></span></span></p>

<p style="text-align:justify"><span style="font-size:10.5pt"><span style="font-family:Calibri,sans-serif">unsigned char values=</span></span></p>

<p style="text-align:justify"><span style="font-size:10.5pt"><span style="font-family:Calibri,sans-serif">{</span></span></p>

<p style="text-align:justify"><span style="font-size:10.5pt"><span style="font-family:Calibri,sans-serif">&nbsp;{0x07, 0xFF},{0x08, 0xC8},{0x09, 0x8E},{0x0A, 0x51},{0x0B, 0x0F},</span></span></p>

<p style="text-align:justify"><span style="font-size:10.5pt"><span style="font-family:Calibri,sans-serif">&nbsp;{0x0B, 0xC4},{0x0C, 0x71},{0x0D, 0x12},{0x0D, 0xA7},{0x0E, 0x2E},</span></span></p>

<p style="text-align:justify"><span style="font-size:10.5pt"><span style="font-family:Calibri,sans-serif">&nbsp;{0x0E, 0xA5},{0x0F, 0x0D},{0x0F, 0x63},{0x0F, 0xA6},{0x0F, 0xD7},</span></span></p>

<p style="text-align:justify"><font face="Calibri, sans-serif">&nbsp;。。。</font></p>

<p style="text-align:justify"><span style="font-size:10.5pt"><span style="font-family:Calibri,sans-serif">&nbsp;{0x00, 0x27},{0x00, 0x58},{0x00, 0x9B},{0x00, 0xF1},{0x01, 0x59},</span></span></p>

<p style="text-align:justify"><span style="font-size:10.5pt"><span style="font-family:Calibri,sans-serif">&nbsp;{0x01, 0xD0},{0x02, 0x57},{0x02, 0xEC},{0x03, 0x8D},{0x04, 0x3A},</span></span></p>

<p style="text-align:justify"><span style="font-size:10.5pt"><span style="font-family:Calibri,sans-serif">&nbsp;{0x04, 0xEF},{0x05, 0xAD},{0x06, 0x70},{0x07, 0x36}</span></span></p>

<p style="text-align:justify"><span style="font-size:10.5pt"><span style="font-family:Calibri,sans-serif">};</span></span></p>

<p style="text-align:justify"><span style="font-size:10.5pt"><span style="font-family:Calibri,sans-serif"><span style="font-family:宋体">实现正弦曲线绘制的函数为:</span></span></span></p>

<pre>
<code class="language-cpp">void LCD_sin(void)
{
      int i,sp,sj;
      LCD_Clear(BLACK);
     sp=95;
      POINT_COLOR=RED;
      LCD_DrawLine(10, 304, 230, 304);
      LCD_DrawLine(125, 10, 125, 304);  
    POINT_COLOR= YELLOW;;
     for ( i = 0 ; i &lt; 64; i++)
      {
           sj=values;  
           sj&lt;&lt;=8;
           sj|=values;
          sj=sj/20;
          LCD_DrawLine(220-sp, 300-4*(i-1), 220-sj, 300-4*(i));
           sp=sj;
      }
}</code></pre>

<p style="text-align:justify"><span style="font-size:10.5pt"><span style="font-family:Calibri,sans-serif"><span style="font-family:&quot;微软雅黑&quot;,&quot;sans-serif&quot;"><span style="color:#666666">若使用该函数进行绘制,则波形曲线如图2所示。此外,若在读取数据时添加一定的相位差,则可以绘制出三相交流电的波形曲线。</span></span></span></span></p>

<p style="text-align:justify"><span style="font-size:10.5pt"><span style="font-family:Calibri,sans-serif"><span style="font-family:&quot;微软雅黑&quot;,&quot;sans-serif&quot;"><span style="color:#666666">图2 绘制波形曲线</span></span></span></span></p>

<p style="text-align:justify"><span style="font-size:10.5pt"><span style="font-family:Calibri,sans-serif"><span style="font-family:&quot;微软雅黑&quot;,&quot;sans-serif&quot;"><span style="color:#666666">若使用图片绘制函数,则可显示图3所示效果。</span></span></span></span></p>

<p style="text-align:justify"><span style="font-size:10.5pt"><span style="font-family:Calibri,sans-serif"><span style="font-family:&quot;微软雅黑&quot;,&quot;sans-serif&quot;"><span style="color:#666666">图3 显示图片效果</span></span></span></span></p>

<p style="text-align:justify"><span style="font-size:10.5pt"><span style="font-family:Calibri,sans-serif"><span style="font-family:&quot;微软雅黑&quot;,&quot;sans-serif&quot;"><span style="color:#666666">该图片绘制函数为:</span></span></span></span></p>

<pre>
<code class="language-cpp">void show_image(unsigned int x,unsigned int y)
{
      unsigned int i,j,k;
      unsigned int da;
      k=0;
      for(i=0;i&lt;68;i++)
      {
           LCD_SetCursor(x,y+i);
           LCD_WriteRAM_Prepare();
           for(j=0;j&lt;56;j++)
           {
               da=gImage_jsj;
                 da&lt;&lt;=8;
                 da|=gImage_jsj;
               POINT_COLOR=da;
                 LCD_DrawPoint(x+j,y+i);
                 k++;
           }
      }
}</code></pre>

<p><span style="font-size:10.5pt"><span style="font-family:&quot;微软雅黑&quot;,&quot;sans-serif&quot;"><span style="color:#666666">这样在这款2.4寸TFT彩屏上就实现了文字、图形及图片的显示功能,也为后续的应用铺平了。</span></span></span></p>

csfc 发表于 2024-5-27 13:33

<p>大佬,可以发个完整的代码吗?</p>
页: [1]
查看完整版本: 【GD32L233C-START测评】TFT屏的显示驱动