神农鼎 发表于 2026-7-8 09:56:22

帮网友问:有1.8 LCD的ST7735适配代码吗

帮网友问:有1.8 LCD的ST7735适配代码吗



神农鼎 发表于 2026-7-8 09:56:51

<p>好的,根据论坛资料,STC官方社区有非常成熟的ST7735驱动方案。下面我为您整理一份可直接使用的适配代码。</p>
<hr />
<h2>STC单片机驱动1.8寸ST7735 LCD适配代码</h2>
<p>以下代码基于 <strong>STC8H8K64U / STC32G12K128 / Ai8051U</strong> 系列,使用 <strong>SPI接口</strong> 驱动1.8寸ST7735彩屏(128×160分辨率)。</p>
<h3>一、硬件接线</h3>
<table>
<thead>
<tr>
<th>ST7735引脚</th>
<th>STC单片机引脚</th>
</tr>
</thead>
<tbody>
<tr>
<td>SCL (SPI时钟)</td>
<td>P1.7</td>
</tr>
<tr>
<td>SDA (MOSI)</td>
<td>P1.6 (SPI MOSI)</td>
</tr>
<tr>
<td>CS (片选)</td>
<td>P1.1</td>
</tr>
<tr>
<td>DC (数据/命令)</td>
<td>P1.0</td>
</tr>
<tr>
<td>RST (复位)</td>
<td>P0.0</td>
</tr>
<tr>
<td>BL (背光)</td>
<td>3.3V (或PWM控制)</td>
</tr>
<tr>
<td>VCC</td>
<td>3.3V</td>
</tr>
<tr>
<td>GND</td>
<td>GND</td>
</tr>
</tbody>
</table>
<blockquote>
<p><strong>注意</strong>:ST7735的IO电压最高为3.6V,<strong>不能直接接5V</strong>,需使用3.3V供电。如果使用5V单片机,需加电平转换或串联限流电阻。</p>
</blockquote>
<hr />
<h3>二、完整驱动代码</h3>
<h4>文件1:<code>lcd.h</code></h4>
<pre><code class="language-c">#ifndef __LCD_H__
#define __LCD_H__

//========================================================================
// 文件: lcd.h
// 描述: ST7735 1.8寸 TFT LCD 驱动头文件
// 适用: STC8H8K64U / STC32G12K128 / Ai8051U 系列
// 接口: 4线SPI
// 分辨率: 128x160
// 晶振: 建议使用内部IRC 40MHz或外部晶振
//========================================================================

// 颜色定义
#define BLACK   0x0000
#define BLUE      0x001F
#define RED       0xF800
#define GREEN   0x07E0
#define CYAN      0x07FF
#define MAGENTA   0xF81F
#define YELLOW    0xFFE0
#define WHITE   0xFFFF

// 屏幕方向定义
#define LANDSCAPE   0
#define PORTRAIT      1

// 屏幕参数
#define LCD_WIDTH   128
#define LCD_HEIGHT    160

//========================================================================
// 函数声明
//========================================================================
void LCD_Init(void);
void LCD_SetWindow(unsigned char x0, unsigned char y0, unsigned char x1, unsigned char y1);
void LCD_WriteData_16bit(unsigned int color);
void LCD_Fill(unsigned char x0, unsigned char y0, unsigned char x1, unsigned char y1, unsigned int color);
void LCD_Clear(unsigned int color);
void LCD_DrawPoint(unsigned char x, unsigned char y, unsigned int color);
void LCD_Display(void);

#endif
</code></pre>
<h4>文件2:<code>lcd.c</code></h4>
<pre><code class="language-c">//========================================================================
// 文件: lcd.c
// 描述: ST7735 1.8寸 TFT LCD 驱动实现
// 适用: STC8H8K64U / STC32G12K128 / Ai8051U 系列
// 接口: 4线SPI (软件SPI,方便移植)
// 分辨率: 128x160
// 晶振: 40MHz
//========================================================================

#include &quot;lcd.h&quot;
#include &quot;intrins.h&quot;

//========================================================================
// 引脚定义(可根据实际接线修改)
//========================================================================
sbit LCD_CS   = P1^1;    // 片选
sbit LCD_DC   = P1^0;    // 数据/命令 (1=数据, 0=命令)
sbit LCD_RST= P0^0;    // 复位
sbit LCD_SCL= P1^7;    // SPI时钟
sbit LCD_SDA= P1^6;    // SPI数据(MOSI)

//========================================================================
// 微秒级延时函数(基于软件延时,需根据主频调整)
// 以下为40MHz主频下的近似延时
//========================================================================
static void delay_us(unsigned int us)
{
    unsigned int i, j;
    for(i = 0; i &lt; us; i++)
    {
      for(j = 0; j &lt; 10; j++);    // 40MHz下约1us
    }
}

static void delay_ms(unsigned int ms)
{
    unsigned int i, j;
    for(i = 0; i &lt; ms; i++)
    {
      for(j = 0; j &lt; 4000; j++);// 40MHz下约1ms
    }
}

//========================================================================
// SPI底层操作(软件SPI,通用性强)
//========================================================================
static void SPI_WriteByte(unsigned char dat)
{
    unsigned char i;

    for(i = 0; i &lt; 8; i++)
    {
      LCD_SCL = 0;                  // 时钟拉低
      if(dat &amp; 0x80)
            LCD_SDA = 1;                // 发送高位
      else
            LCD_SDA = 0;
      dat &lt;&lt;= 1;                      // 左移一位
      LCD_SCL = 1;                  // 时钟拉高(上升沿采样)
    }
}

//========================================================================
// LCD写命令
//========================================================================
static void LCD_WriteCmd(unsigned char cmd)
{
    LCD_CS = 0;                         // 片选拉低
    LCD_DC = 0;                         // 命令模式
    SPI_WriteByte(cmd);
    LCD_CS = 1;                         // 片选拉高
}

//========================================================================
// LCD写数据(8位)
//========================================================================
static void LCD_WriteData(unsigned char dat)
{
    LCD_CS = 0;                         // 片选拉低
    LCD_DC = 1;                         // 数据模式
    SPI_WriteByte(dat);
    LCD_CS = 1;                         // 片选拉高
}

//========================================================================
// LCD写数据(16位)
//========================================================================
void LCD_WriteData_16bit(unsigned int color)
{
    LCD_CS = 0;                         // 片选拉低
    LCD_DC = 1;                         // 数据模式
    SPI_WriteByte((unsigned char)(color &gt;&gt; 8));   // 高8位
    SPI_WriteByte((unsigned char)(color));      // 低8位
    LCD_CS = 1;                         // 片选拉高
}

//========================================================================
// 设置显示窗口
//========================================================================
void LCD_SetWindow(unsigned char x0, unsigned char y0, unsigned char x1, unsigned char y1)
{
    LCD_WriteCmd(0x2A);               // 列地址设置
    LCD_WriteData(0x00);
    LCD_WriteData(x0 + 2);            // ST7735偏移修正
    LCD_WriteData(0x00);
    LCD_WriteData(x1 + 2);

    LCD_WriteCmd(0x2B);               // 行地址设置
    LCD_WriteData(0x00);
    LCD_WriteData(y0 + 1);            // ST7735偏移修正
    LCD_WriteData(0x00);
    LCD_WriteData(y1 + 1);

    LCD_WriteCmd(0x2C);               // 开始写内存
}

//========================================================================
// 填充矩形区域
//========================================================================
void LCD_Fill(unsigned char x0, unsigned char y0, unsigned char x1, unsigned char y1, unsigned int color)
{
    unsigned int i, total;

    total = (x1 - x0 + 1) * (y1 - y0 + 1);

    LCD_SetWindow(x0, y0, x1, y1);

    for(i = 0; i &lt; total; i++)
    {
      LCD_WriteData_16bit(color);
    }
}

//========================================================================
// 清屏
//========================================================================
void LCD_Clear(unsigned int color)
{
    LCD_Fill(0, 0, LCD_WIDTH - 1, LCD_HEIGHT - 1, color);
}

//========================================================================
// 画点
//========================================================================
void LCD_DrawPoint(unsigned char x, unsigned char y, unsigned int color)
{
    LCD_SetWindow(x, y, x, y);
    LCD_WriteData_16bit(color);
}

//========================================================================
// ST7735初始化序列
//========================================================================
void LCD_Init(void)
{
    // 硬件复位
    LCD_RST = 0;
    delay_ms(10);
    LCD_RST = 1;
    delay_ms(10);

    // 软件复位
    LCD_WriteCmd(0x01);               // Software Reset
    delay_ms(10);

    // 睡眠退出
    LCD_WriteCmd(0x11);               // Sleep Out
    delay_ms(10);

    // 帧率设置
    LCD_WriteCmd(0xB1);               // Frame Rate Control
    LCD_WriteData(0x05);
    LCD_WriteData(0x3C);
    LCD_WriteData(0x3C);

    LCD_WriteCmd(0xB2);               // Frame Rate Control (Inversion)
    LCD_WriteData(0x05);
    LCD_WriteData(0x3C);
    LCD_WriteData(0x3C);

    LCD_WriteCmd(0xB3);               // Frame Rate Control (Partial)
    LCD_WriteData(0x05);
    LCD_WriteData(0x3C);
    LCD_WriteData(0x3C);
    LCD_WriteData(0x05);
    LCD_WriteData(0x3C);
    LCD_WriteData(0x3C);

    // 显示反转控制
    LCD_WriteCmd(0xB4);               // Display Inversion Control
    LCD_WriteData(0x03);

    // 电源控制
    LCD_WriteCmd(0xC0);               // Power Control 1
    LCD_WriteData(0x62);
    LCD_WriteData(0x02);
    LCD_WriteData(0x04);

    LCD_WriteCmd(0xC1);               // Power Control 2
    LCD_WriteData(0xC0);

    LCD_WriteCmd(0xC2);               // Power Control 3 (Normal)
    LCD_WriteData(0x0D);
    LCD_WriteData(0x00);

    LCD_WriteCmd(0xC3);               // Power Control 4 (Idle)
    LCD_WriteData(0x8D);
    LCD_WriteData(0x6A);

    LCD_WriteCmd(0xC4);               // Power Control 5 (Partial)
    LCD_WriteData(0x8D);
    LCD_WriteData(0xEE);

    // VCOM控制
    LCD_WriteCmd(0xC5);               // VCOM Control 1
    LCD_WriteData(0x0E);

    LCD_WriteCmd(0x36);               // Memory Access Control
    LCD_WriteData(0xC8);                // 竖屏模式(可根据需要调整)

    // 像素格式
    LCD_WriteCmd(0x3A);               // Interface Pixel Format
    LCD_WriteData(0x05);                // 16位色 (RGB565)

    // 伽马校正
    LCD_WriteCmd(0xE0);               // Gamma Positive
    LCD_WriteData(0x02);
    LCD_WriteData(0x1C);
    LCD_WriteData(0x07);
    LCD_WriteData(0x12);
    LCD_WriteData(0x37);
    LCD_WriteData(0x32);
    LCD_WriteData(0x29);
    LCD_WriteData(0x2D);
    LCD_WriteData(0x29);
    LCD_WriteData(0x25);
    LCD_WriteData(0x2B);
    LCD_WriteData(0x39);
    LCD_WriteData(0x00);
    LCD_WriteData(0x01);
    LCD_WriteData(0x03);
    LCD_WriteData(0x10);

    LCD_WriteCmd(0xE1);               // Gamma Negative
    LCD_WriteData(0x03);
    LCD_WriteData(0x1D);
    LCD_WriteData(0x07);
    LCD_WriteData(0x06);
    LCD_WriteData(0x2E);
    LCD_WriteData(0x2C);
    LCD_WriteData(0x29);
    LCD_WriteData(0x2D);
    LCD_WriteData(0x2E);
    LCD_WriteData(0x2E);
    LCD_WriteData(0x37);
    LCD_WriteData(0x3F);
    LCD_WriteData(0x00);
    LCD_WriteData(0x00);
    LCD_WriteData(0x02);
    LCD_WriteData(0x10);

    // 显示开启
    LCD_WriteCmd(0x29);               // Display ON
    delay_ms(10);
}
</code></pre>
<h4>文件3:<code>main.c</code></h4>
<pre><code class="language-c">//========================================================================
// 文件: main.c
// 描述: ST7735 1.8寸 TFT LCD 驱动演示程序
// 适用: STC8H8K64U / STC32G12K128 / Ai8051U 系列
// 编译: Keil C51 (8位) 或 Keil C251 (32位)
// 晶振: 40MHz 内部IRC
// 接线:
//   LCD_CS-&gt; P1.1
//   LCD_DC-&gt; P1.0
//   LCD_RST -&gt; P0.0
//   LCD_SCL -&gt; P1.7
//   LCD_SDA -&gt; P1.6
//========================================================================

#include &quot;lcd.h&quot;

//========================================================================
// 主函数
//========================================================================
void main(void)
{
    unsigned int i;

    // 关闭看门狗(如果启用)
    // WDT_CONTR = 0x00;

    // 初始化LCD
    LCD_Init();

    // 清屏为黑色
    LCD_Clear(BLACK);
    delay_ms(100);

    // 演示:填充各种颜色
    LCD_Fill(0, 0, 63, 79, RED);
    delay_ms(200);
    LCD_Fill(64, 0, 127, 79, GREEN);
    delay_ms(200);
    LCD_Fill(0, 80, 63, 159, BLUE);
    delay_ms(200);
    LCD_Fill(64, 80, 127, 159, YELLOW);
    delay_ms(500);

    // 清屏为白色
    LCD_Clear(WHITE);
    delay_ms(100);

    // 画点阵图案(画一个十字)
    for(i = 0; i &lt; LCD_WIDTH; i++)
    {
      LCD_DrawPoint(i, LCD_HEIGHT / 2, RED);
    }
    for(i = 0; i &lt; LCD_HEIGHT; i++)
    {
      LCD_DrawPoint(LCD_WIDTH / 2, i, RED);
    }

    delay_ms(1000);

    // 循环演示:彩虹色渐变
    while(1)
    {
      unsigned char x, y;
      unsigned int color;

      for(y = 0; y &lt; LCD_HEIGHT; y++)
      {
            for(x = 0; x &lt; LCD_WIDTH; x++)
            {
                // 生成彩虹渐变效果
                color = ((unsigned int)(x * 32) &lt;&lt; 8) | (unsigned int)(y * 16);
                LCD_DrawPoint(x, y, color);
            }
      }
      delay_ms(2000);

      // 清屏
      LCD_Clear(BLACK);
      delay_ms(1000);
    }
}
</code></pre>
<hr />
<h3>三、使用说明</h3>
<ol>
<li>
<p><strong>编译环境</strong>:</p>
<ul>
<li>STC8系列(8位):使用 <strong>Keil C51</strong> 编译</li>
<li>STC32系列(32位):使用 <strong>Keil C251</strong> 编译</li>
<li>Ai8051U系列:使用 <strong>Keil C251</strong> 编译</li>
</ul>
</li>
<li>
<p><strong>引脚修改</strong>:如果您的接线不同,修改 <code>lcd.c</code> 开头的 <code>sbit</code> 引脚定义即可。</p>
</li>
<li>
<p><strong>主频调整</strong>:代码中的延时基于 <strong>40MHz</strong> 主频。如果使用其他频率,请调整 <code>delay_us</code> 和 <code>delay_ms</code> 函数中的循环参数。</p>
</li>
<li>
<p><strong>ST7735偏移修正</strong>:1.8寸ST7735屏通常有行列偏移(2,1),代码中 <code>LCD_SetWindow</code> 已做修正。如果显示位置不对,可调整偏移量。</p>
</li>
<li>
<p><strong>更快的SPI方式</strong>:上述代码使用软件SPI,方便移植。如果需要更高刷屏速度,建议使用STC单片机的 <strong>硬件SPI</strong> 或 <strong>高速SPI(HSSPI)</strong> + <strong>DMA</strong> 方式,论坛实测在5V/36.864MHz下刷一帧可做到约18ms。</p>
</li>
</ol>
<hr />
<h3>四、论坛参考资料</h3>
<p>据STC官方论坛帖子(<a href="https://www.stcaimcu.com/forum.php?mod=viewthread&amp;tid=19060">AiCube 图形化自动生成程序,TFT-彩屏@Ai8051U, 驱动 ST7735S</a>)反馈:</p>
<ul>
<li>使用 <strong>AiCube-ISP</strong> 工具可图形化自动生成ST7735S/ST7739/ST7789驱动代码</li>
<li>帖子附件中包含 <code>STC32G-ST7735S.zip</code> 和 <code>USART1SPI_P2P_SPI.zip</code> 等完整工程文件可供下载</li>
<li>帖子中还提供了 <strong>ST7735驱动IC数据手册.pdf</strong> 可供参考</li>
</ul>
<p>如果您需要更高级的功能(如汉字显示、图片显示、DMA刷屏等),建议下载论坛附件中的完整工程,或使用 <strong>AiCube-ISP</strong> 工具的图形化配置功能自动生成代码。</p>
<hr />
<p><a href="https://www.stcaimcu.com/plugin.php?id=twpx_xunsearch&amp;mod=forum&amp;q=%E6%9C%891.8lcd%E7%9A%84st7735%E9%80%82%E9%85%8D%E4%BB%A3%E7%A0%81%E5%90%97&amp;f=_all&amp;m=yes&amp;syn=yes&amp;s=relevance&amp;fid=0">🔍 搜索 &quot;有1.8lcd的st7735适配代码吗&quot;</a></p>
<p><strong>📌 相关论坛讨论</strong></p>
<ol>
<li><a href="https://www.stcaimcu.com/forum.php?mod=viewthread&amp;tid=19339&amp;highlight=%E6%9C%891.8lcd%E7%9A%84st7735%E9%80%82%E9%85%8D%E4%BB%A3%E7%A0%81%E5%90%97&amp;page=1&amp;extra=#pid178924">【求助】1.8寸TFT模块电压多少 ST7735</a></li>
<li><a href="https://www.stcaimcu.com/forum.php?mod=viewthread&amp;tid=10451&amp;highlight=%E6%9C%891.8lcd%E7%9A%84st7735%E9%80%82%E9%85%8D%E4%BB%A3%E7%A0%81%E5%90%97&amp;page=1&amp;extra=#pid103033">32G软硬件驱动【1.8inch RGB_TFT】ST7735S</a></li>
<li>我在用 AI8H8K64U搭配 BL0942读取交流电的电压电流,这个代码始终编译错误 (</li>
</ol>
<p>⏱️ 查找用时 25.0s | 总回答用时 61.2s</p>

神农鼎 发表于 2026-7-8 09:58:02




https://www.stcaimcu.com/thread-10451-1-1.html

神农鼎 发表于 2026-7-8 09:59:39



STC32G144K246 驱动 ST7735LCD - TFT彩屏,触摸屏,DMA-i8080/M6800并口自动刷屏,DMA-SPI刷屏,外设直接到外设 国芯人工智能技术交流网站 - AI32位8051交流社区
页: [1]
查看完整版本: 帮网友问:有1.8 LCD的ST7735适配代码吗