使用stm32cubeMX生成模板,配置stm32f103c8t6键鼠一体化

2021-05-21 14:33:56 浏览数 (2)

cubeMX配置流程

pinout&configuration

  1. 打开cubeMX
  2. 芯片选择stm32f103c8te
  3. pa1引脚设置为GPIO_EXTI1
  4. NVIC中使能EXTI line1 interrupt
  5. RCC中HSE选择Crystal/ceramic Resonator
  6. SYS中Debug选择serial wire
  7. USB中勾选Device(FS)
  8. USB_DEVICE中Class选择HID
  9. 设备描述符修改vid和pid的值(避免和连接到电脑上其他usb设备冲突而引起错误)
  10. GPIO中pa1配置成Pull-down(按键接3.3v和pa1中间,若配置成Pull-up,则按键接gnd和pa1中间)

clock Configuration

  1. PLL选择HSE
  2. PLLMul选择x9
  3. USB Prescaler选择/1.5
  4. SYSCLK选择PLLCLK
  5. 最终配置结果usb48MHz,SYSCLK72MHz,检测无错

project manager

  1. 填写project名称

2.设置project Location

  1. ToolChain/IDE选择MDK-ARM v5.27
  2. code Generator 分别选择 copy only the necessary library files 和 Generate peripheral initialization as a pair of '.c/.h' files per peripheral
  3. 生成代码并打开项目

打开MDK-KEIL5

  1. 打开usbd_hid.c
  2. 修改USBD_HID_CfgFSDesc(HID设备全速配置描述符)

*USB_HID_CONFIG_DESC_SIZ 大小改为41U

*Descriptor of Joystick Mouse interface 中端点数量改为0x02,接口协议改为0x01

*配置描述符末尾新增输出端点描述符

3. 修改报告描述符HID_MOUSE_ReportDesc

*替换报告描述符内容为

代码语言:javascript复制
//将整个报告描述符复制替换过去, 其中键盘REPORT_ID是1,鼠标REPORT_ID是2
      0x05, 0x01,                    // USAGE_PAGE (Generic Desktop)
    0x09, 0x06,                    // USAGE (Keyboard)
    0xa1, 0x01,                    // COLLECTION (Application)
    0x85, 0x01,                    //   REPORT_ID (1)
    0x05, 0x07,                    //   USAGE_PAGE (Keyboard)
    0x19, 0xe0,                    //   USAGE_MINIMUM (Keyboard LeftControl)
    0x29, 0xe7,                    //   USAGE_MAXIMUM (Keyboard Right GUI)
    0x15, 0x00,                    //   LOGICAL_MINIMUM (0)
    0x25, 0x01,                    //   LOGICAL_MAXIMUM (1)
    0x75, 0x01,                    //   REPORT_SIZE (1)
    0x95, 0x08,                    //   REPORT_COUNT (8)
    0x81, 0x02,                    //   INPUT (Data,Var,Abs)
    0x95, 0x01,                    //   REPORT_COUNT (1)
    0x75, 0x08,                    //   REPORT_SIZE (8)
    0x81, 0x03,                    //   INPUT (Cnst,Var,Abs)
    0x95, 0x05,                    //   REPORT_COUNT (5)
    0x75, 0x01,                    //   REPORT_SIZE (1)
    0x05, 0x08,                    //   USAGE_PAGE (LEDs)
    0x19, 0x01,                    //   USAGE_MINIMUM (Num Lock)
    0x29, 0x05,                    //   USAGE_MAXIMUM (Kana)
    0x91, 0x02,                    //   OUTPUT (Data,Var,Abs)
    0x95, 0x01,                    //   REPORT_COUNT (1)
    0x75, 0x03,                    //   REPORT_SIZE (3)
    0x91, 0x03,                    //   OUTPUT (Cnst,Var,Abs)
    0x95, 0x06,                    //   REPORT_COUNT (6)
    0x75, 0x08,                    //   REPORT_SIZE (8)
    0x15, 0x00,                    //   LOGICAL_MINIMUM (0)
    0x25, 0x65,                    //   LOGICAL_MAXIMUM (101)
    0x05, 0x07,                    //   USAGE_PAGE (Keyboard)
    0x19, 0x00,                    //   USAGE_MINIMUM (Reserved (no event indicated))
    0x29, 0x65,                    //   USAGE_MAXIMUM (Keyboard Application)
    0x81, 0x00,                    //   INPUT (Data,Ary,Abs)
    0xc0,                           // END_COLLECTION
    
    
    0x05, 0x01,                    // USAGE_PAGE (Generic Desktop)
    0x09, 0x02,                    // USAGE (Mouse)
    0xa1, 0x01,                    // COLLECTION (Application)
    0x85, 0x02,                    //   REPORT_ID (2)
    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)
    0x15, 0x81,                    //     LOGICAL_MINIMUM (-127)
    0x25, 0x7f,                    //     LOGICAL_MAXIMUM (127)
    0x75, 0x08,                    //     REPORT_SIZE (8)
    0x95, 0x02,                    //     REPORT_COUNT (2)
    0x81, 0x06,                    //     INPUT (Data,Var,Rel)
    0xc0,                          //   END_COLLECTION
    0xc0                           // END_COLLECTION

*修改HID_MOUSE_REPORT_DESC_SIZE为117

3. 打开main.c

*设置全局变量

代码语言:javascript复制
uint8_t KeyBoard[9] = {1,0,0,0,0,0,0,0,0};
uint8_t KeyBoard01[9] = {1,0,0,0,0,0,0,0,0};
uint8_t Mouse[9] = {2,0,0,0,0,0,0,0,0};

*引入外部变量

代码语言:javascript复制
extern USBD_HandleTypeDef hUsbDeviceFS;

*声明并建立函数void key_test_tunc(void)

代码语言:javascript复制
void key_test_func(void)
{
    KeyBoard[3] = 8;//e,修改并发送KeyBoard[3]的值触发不同按键
    USBD_HID_SendReport(&hUsbDeviceFS,(uint8_t*)&KeyBoard,sizeof(KeyBoard));
    HAL_Delay(15);
    USBD_HID_SendReport(&hUsbDeviceFS,(uint8_t*)&KeyBoard01,sizeof(KeyBoard01));
    HAL_Delay(100);
}

*声明并建立函数void mouse_test_func(void)

代码语言:javascript复制
void mouse_test_func(void)
{
    Mouse[3] = -2; //up
    for(int count = 0;count<0x40000;count  )
    {
        USBD_HID_SendReport(&hUsbDeviceFS,(uint8_t*)&Mouse,sizeof(Mouse));
    }
}

0 人点赞