神农鼎 发表于 2026-6-28 23:13:07

STC32G12K128如何做个HID鼠标

STC32G12K128如何做个HID鼠标


神农鼎 发表于 2026-6-28 23:13:43

<p>好,现在信息就非常完整了。基于 <strong>数据手册第28章</strong> 和 <strong>官方论坛范例</strong>,我来给你一个完整的指南。</p>
<hr />
<h2>🖱️ STC32G12K128 制作 HID 鼠标完整指南</h2>
<h3>一、所需软硬件</h3>
<table>
<thead>
<tr>
<th>项目</th>
<th>说明</th>
</tr>
</thead>
<tbody>
<tr>
<td><strong>芯片</strong></td>
<td>STC32G12K128(任意封装均可,需引出 USB-DP/P3.0、USB-DN/P3.1)</td>
</tr>
<tr>
<td><strong>下载/调试</strong></td>
<td>AiCube-ISP 软件,设置 IRC 频率为<strong>24MHz</strong></td>
</tr>
<tr>
<td><strong>开发环境</strong></td>
<td>Keil C251,Memory Model 推荐<strong>XSmall</strong> 模式</td>
</tr>
<tr>
<td><strong>USB 连接</strong></td>
<td>P3.0 (USB-DP)、P3.1 (USB-DN) 直接连到电脑 USB 口(或用 STC-USB Link1D)</td>
</tr>
<tr>
<td><strong>按键</strong></td>
<td>接普通 GPIO,作为鼠标左/右/中键和移动方向</td>
</tr>
</tbody>
</table>
<hr />
<h3>二、两种实现路线</h3>
<h4>🔵 路线 A:使用 STC 官方 USB 库(推荐,代码更简洁)</h4>
<p>这是论坛帖子(thread-24404)中官方给出的方式,使用 STC 封装好的 USB 库函数。</p>
<p><strong>关键文件:</strong></p>
<ul>
<li><code>stc.h</code> — STC 头文件</li>
<li><code>usb.h</code> — USB 底层驱动</li>
<li><code>usb_req_class.h</code> — HID 类请求处理</li>
<li><code>timer.h</code> — 定时器(1ms 按键扫描)</li>
</ul>
<p><strong>项目创建步骤:</strong></p>
<ol>
<li>在 Keil 中新建 C251 工程,芯片选 STC32G12K128</li>
<li>Memory Model 设为 <strong>XSmall</strong></li>
<li>添加 USB 库文件(通过 AiCube-ISP → 下载软件 → USB库 获取)</li>
<li>添加以上头文件和主程序</li>
</ol>
<p><strong>主程序骨架:</strong></p>
<pre><code class="language-c">#include &quot;stc.h&quot;
#include &quot;usb.h&quot;
#include &quot;usb_req_class.h&quot;
#include &quot;timer.h&quot;

void main()
{
    WTST = 0;      // 最快速度
    EAXFR = 1;   // 使能XFR
    CKCON = 0;   // 提高XRAM速度

    usb_init();
    timer_init();

    EA = 1;

    while (1)
    {
      usb_class_in();   // USB HID 数据上报
      
      if (f1ms)         // 每 1ms 扫描按键
      {
            f1ms = 0;
            scan_key();   // 读取按键值并更新鼠标状态
      }
    }
}
</code></pre>
<p><strong>按键映射关系(实验箱默认):</strong></p>
<table>
<thead>
<tr>
<th>按键</th>
<th>鼠标动作</th>
<th>按键</th>
<th>鼠标动作</th>
</tr>
</thead>
<tbody>
<tr>
<td>KEY0</td>
<td><strong>左键</strong></td>
<td>KEY4</td>
<td><strong>左移</strong></td>
</tr>
<tr>
<td>KEY1</td>
<td><strong>中键</strong></td>
<td>KEY5</td>
<td><strong>右移</strong></td>
</tr>
<tr>
<td>KEY2</td>
<td><strong>右键</strong></td>
<td>KEY6</td>
<td><strong>上移</strong></td>
</tr>
<tr>
<td>—</td>
<td>—</td>
<td>KEY7</td>
<td><strong>下移</strong></td>
</tr>
</tbody>
</table>
<blockquote>
<p>完整代码包请到官网下载 <strong>&quot;STC32G实验箱演示程序&quot;</strong>,找到 <strong>&quot;72-基于HID协议的USB鼠标范例&quot;</strong>。</p>
</blockquote>
<hr />
<h4>🔵 路线 B:纯寄存器操作(更底层,不需要库文件)</h4>
<p>数据手册 <strong>第28.5.1节《HID人机接口设备范例》</strong> 给出了完整的寄存器级代码,手写 USB 枚举和 HID 报告描述符。</p>
<p><strong>关键描述符(需修改的部分):</strong></p>
<p><strong>① 设备描述符 DEVICEDESC</strong></p>
<pre><code class="language-c">char code DEVICEDESC = {
    0x12, 0x01,         // bLength, bDescriptorType(Device)
    0x00, 0x02,         // bcdUSB(2.00)
    0x00, 0x00, 0x00,   // class, subclass, protocol
    0x40,               // bMaxPacketSize0(64)
    0xbf, 0x34,         // idVendor(STCUSB:34bf)
    0x80, 0x43,         // idProduct(4380)
    0x00, 0x01,         // bcdDevice(1.00)
    0x01, 0x02, 0x00,   // iManufacturer, iProduct, iSerialNumber
    0x01                // bNumConfigurations
};
</code></pre>
<p><strong>② HID Report Descriptor(鼠标协议)</strong></p>
<p>数据手册中的范例是 Consumer Control 设备,要做鼠标需改为 <strong>鼠标专用的 Report Descriptor</strong>:</p>
<pre><code class="language-c">char code HIDREPORTDESC = {
    0x05, 0x01,         // USAGE_PAGE (Generic Desktop)
    0x09, 0x02,         // USAGE (Mouse)
    0xa1, 0x01,         // COLLECTION (Application)
    0x09, 0x01,         //   USAGE (Pointer)
    0xa1, 0x00,         //   COLLECTION (Physical)
    0x05, 0x09,         //   USAGE_PAGE (Button)
    0x19, 0x01,         //   USAGE_MINIMUM (Button 1)
    0x29, 0x03,         //   USAGE_MAXIMUM (Button 3)
    0x15, 0x00,         //   LOGICAL_MINIMUM (0)
    0x25, 0x01,         //   LOGICAL_MAXIMUM (1)
    0x95, 0x03,         //   REPORT_COUNT (3)
    0x75, 0x01,         //   REPORT_SIZE (1)
    0x81, 0x02,         //   INPUT (Data,Var,Abs)
    0x95, 0x01,         //   REPORT_COUNT (1)
    0x75, 0x05,         //   REPORT_SIZE (5)
    0x81, 0x03,         //   INPUT (Cnst,Var,Abs)
    0x05, 0x01,         //   USAGE_PAGE (Generic Desktop)
    0x09, 0x30,         //   USAGE (X)
    0x09, 0x31,         //   USAGE (Y)
    0x09, 0x38,         //   USAGE (Wheel)
    0x15, 0x81,         //   LOGICAL_MINIMUM (-127)
    0x25, 0x7f,         //   LOGICAL_MAXIMUM (127)
    0x75, 0x08,         //   REPORT_SIZE (8)
    0x95, 0x03,         //   REPORT_COUNT (3)
    0x81, 0x06,         //   INPUT (Data,Var,Rel)
    0xc0,               //   END_COLLECTION
    0xc0                // END_COLLECTION
};
</code></pre>
<blockquote>
<p>这个描述符定义了标准的 <strong>3键 + X/Y/Wheel 相对位移</strong> 的鼠标协议。</p>
</blockquote>
<p><strong>③ 发送鼠标数据</strong></p>
<p>在端点1的 IN 中断中,向 FIFO1 写入 <strong>4字节鼠标数据</strong>:</p>
<pre><code>字节0: [按键状态] bit0=左键, bit1=右键, bit2=中键
字节1: X轴位移(有符号,-127~+127)
字节2: Y轴位移(有符号)
字节3: 滚轮(有符号)
</code></pre>
<p>示例代码段:</p>
<pre><code class="language-c">BYTE MouseData;

// 更新鼠标数据
MouseData = (按键左 ? 0x01 : 0) | (按键右 ? 0x02 : 0) | (按键中 ? 0x04 : 0);
MouseData = X移动量;   // 正数右移,负数左移
MouseData = Y移动量;   // 正数下移,负数上移
MouseData = 0;      // 滚轮

// 写入端点 FIFO 并通知 USB 发送
WriteReg(INDEX, 1);
WriteFifo(FIFO1, MouseData, 4);
WriteReg(INCSR1, INIPRDY);
</code></pre>
<hr />
<h3>三、下载设置</h3>
<p>在 AiCube-ISP 中:</p>
<ul>
<li><strong>芯片型号</strong>:STC32G12K128</li>
<li><strong>IRC 频率</strong>:<strong>24MHz</strong>(必须,USB 需要精确时钟)</li>
<li><strong>USB 下载</strong>:勾选&quot;USB 下载&quot;,按住 P3.2 再上电进入下载模式</li>
</ul>
<hr />
<h3>四、范例代码获取方式</h3>
<table>
<thead>
<tr>
<th>方式</th>
<th>路径</th>
</tr>
</thead>
<tbody>
<tr>
<td><strong>官方完整工程</strong></td>
<td>官网 → 资料下载 →<strong>STC32G实验箱演示程序包</strong> → <code>72-基于HID协议的USB鼠标范例</code></td>
</tr>
<tr>
<td><strong>数据手册源码</strong></td>
<td>STC32G数据手册<strong>第28.5.1节</strong>(HID基础框架)和 <strong>第28.5.5节</strong>(鼠标专用说明)</td>
</tr>
<tr>
<td><strong>论坛帖子</strong></td>
<td><a href="https://www.stcaimcu.com/thread-24404-1-1.html">thread-24404</a> 有完整主程序</td>
</tr>
</tbody>
</table>
<hr />
<p>如果你需要,我可以帮你进一步梳理:</p>
<ul>
<li>如何改 Pin 脚(不用实验箱矩阵键盘,改用普通独立按键)</li>
<li>如何改成 <strong>鼠标+键盘复合设备</strong></li>
<li>如何自定义 VID/PID</li>
</ul>
<p>欢迎继续提问!🎯</p>
<hr />
<p><a href="https://www.stcaimcu.com/plugin.php?id=twpx_xunsearch&amp;mod=forum&amp;q=STC32G12K128%E5%A6%82%E4%BD%95%E5%81%9A%E4%B8%AAhid%E9%BC%A0%E6%A0%87&amp;f=_all&amp;m=yes&amp;syn=yes&amp;s=relevance&amp;fid=0">🔍 搜索 &quot;STC32G12K128如何做个hid鼠标&quot;</a></p>
<p><strong>📌 相关论坛讨论</strong></p>
<ol>
<li><a href="https://www.stcaimcu.com/forum.php?mod=viewthread&amp;tid=24404&amp;highlight=STC32G12K128%E5%A6%82%E4%BD%95%E5%81%9A%E4%B8%AAhid%E9%BC%A0%E6%A0%87&amp;page=1&amp;extra=#pid221448">72-基于HID协议的USB鼠标范例,STC32G12K128实验箱演示程序,STC32G8K64,STC32CL8K64</a></li>
<li><a href="https://www.stcaimcu.com/forum.php?mod=viewthread&amp;tid=22751&amp;highlight=STC32G12K128%E5%A6%82%E4%BD%95%E5%81%9A%E4%B8%AAhid%E9%BC%A0%E6%A0%87&amp;page=1&amp;extra=#pid209887">STC32G12K128 HID出不来了?</a></li>
<li><a href="https://www.stcaimcu.com/forum.php?mod=viewthread&amp;tid=572&amp;highlight=STC32G12K128%E5%A6%82%E4%BD%95%E5%81%9A%E4%B8%AAhid%E9%BC%A0%E6%A0%87&amp;page=1&amp;extra=#pid3705">51 开源:【HID-鼠标+HID-键盘】的USB-HID复合设备 + 绝对坐标|手写笔</a></li>
</ol>
页: [1]
查看完整版本: STC32G12K128如何做个HID鼠标