WCHF205RBT6使用双USB开发键盘鼠标

void EP1_IN_Callback(void)

{

    static uint8_t state = 0;

    uint8_t keyboard_report[8] = {0};  // 初始化键盘报告


    // 根据当前状态设置键盘报告中的按键码

    switch (state)

    {

        // 字母键 A-Z

        case 0: keyboard_report[2] = KEY_A; break;

        case 1: keyboard_report[2] = KEY_B; break;

        case 2: keyboard_report[2] = KEY_C; break;

        case 3: keyboard_report[2] = KEY_D; break;

        case 4: keyboard_report[2] = KEY_E; break;

        case 5: keyboard_report[2] = KEY_F; break;

        case 6: keyboard_report[2] = KEY_G; break;

        case 7: keyboard_report[2] = KEY_H; break;

        case 8: keyboard_report[2] = KEY_I; break;

        case 9: keyboard_report[2] = KEY_J; break;

        case 10: keyboard_report[2] = KEY_K; break;

        case 11: keyboard_report[2] = KEY_L; break;

        case 12: keyboard_report[2] = KEY_M; break;

        case 13: keyboard_report[2] = KEY_N; break;

        case 14: keyboard_report[2] = KEY_O; break;

        case 15: keyboard_report[2] = KEY_P; break;

        case 16: keyboard_report[2] = KEY_Q; break;

        case 17: keyboard_report[2] = KEY_R; break;

        case 18: keyboard_report[2] = KEY_S; break;

        case 19: keyboard_report[2] = KEY_T; break;

        case 20: keyboard_report[2] = KEY_U; break;

        case 21: keyboard_report[2] = KEY_V; break;

        case 22: keyboard_report[2] = KEY_W; break;

        case 23: keyboard_report[2] = KEY_X; break;

        case 24: keyboard_report[2] = KEY_Y; break;

        case 25: keyboard_report[2] = KEY_Z; break;


        // 数字键 0-9

        case 26: keyboard_report[2] = KEY_1; break;

        case 27: keyboard_report[2] = KEY_2; break;

        case 28: keyboard_report[2] = KEY_3; break;

        case 29: keyboard_report[2] = KEY_4; break;

        case 30: keyboard_report[2] = KEY_5; break;

        case 31: keyboard_report[2] = KEY_6; break;

        case 32: keyboard_report[2] = KEY_7; break;

        case 33: keyboard_report[2] = KEY_8; break;

        case 34: keyboard_report[2] = KEY_9; break;

        case 35: keyboard_report[2] = KEY_0; break;


        // 功能键 F1-F12

        case 36: keyboard_report[2] = KEY_F1; break;

        case 37: keyboard_report[2] = KEY_F2; break;

        case 38: keyboard_report[2] = KEY_F3; break;

        case 39: keyboard_report[2] = KEY_F4; break;

        case 40: keyboard_report[2] = KEY_F5; break;

        case 41: keyboard_report[2] = KEY_F6; break;

        case 42: keyboard_report[2] = KEY_F7; break;

        case 43: keyboard_report[2] = KEY_F8; break;

        case 44: keyboard_report[2] = KEY_F9; break;

        case 45: keyboard_report[2] = KEY_F10; break;

        case 46: keyboard_report[2] = KEY_F11; break;

        case 47: keyboard_report[2] = KEY_F12; break;


        // 符号键和特殊功能键

        case 48: keyboard_report[2] = KEY_ENTER; break;

        case 49: keyboard_report[2] = KEY_ESC; break;

        case 50: keyboard_report[2] = KEY_BACKSPACE; break;

        case 51: keyboard_report[2] = KEY_TAB; break;

        case 52: keyboard_report[2] = KEY_SPACE; break;

        case 53: keyboard_report[2] = KEY_MINUS; break;

        case 54: keyboard_report[2] = KEY_EQUAL; break;

        case 55: keyboard_report[2] = KEY_LEFTBRACE; break;

        case 56: keyboard_report[2] = KEY_RIGHTBRACE; break;

        case 57: keyboard_report[2] = KEY_BACKSLASH; break;

        case 58: keyboard_report[2] = KEY_SEMICOLON; break;

        case 59: keyboard_report[2] = KEY_APOSTROPHE; break;

        case 60: keyboard_report[2] = KEY_GRAVE; break;

        case 61: keyboard_report[2] = KEY_COMMA; break;

        case 62: keyboard_report[2] = KEY_DOT; break;

        case 63: keyboard_report[2] = KEY_SLASH; break;

        case 64: keyboard_report[2] = KEY_CAPSLOCK; break;


        // 方向键

        case 65: keyboard_report[2] = KEY_RIGHT; break;

        case 66: keyboard_report[2] = KEY_LEFT; break;

        case 67: keyboard_report[2] = KEY_DOWN; break;

        case 68: keyboard_report[2] = KEY_UP; break;


        // 控制键

        case 69: keyboard_report[2] = KEY_LEFTCTRL; break;

        case 70: keyboard_report[2] = KEY_LEFTSHIFT; break;

        case 71: keyboard_report[2] = KEY_LEFTALT; break;

        case 72: keyboard_report[2] = KEY_LEFTMETA; break;

        case 73: keyboard_report[2] = KEY_RIGHTCTRL; break;

        case 74: keyboard_report[2] = KEY_RIGHTSHIFT; break;

        case 75: keyboard_report[2] = KEY_RIGHTALT; break;

        case 76: keyboard_report[2] = KEY_RIGHTMETA; break;


        // 音量和媒体控制键

        case 77: keyboard_report[2] = KEY_MEDIA_PLAYPAUSE; break;

        case 78: keyboard_report[2] = KEY_MEDIA_STOPCD; break;

        case 79: keyboard_report[2] = KEY_MEDIA_PREVIOUSSONG; break;

        case 80: keyboard_report[2] = KEY_MEDIA_NEXTSONG; break;

        case 81: keyboard_report[2] = KEY_MEDIA_VOLUMEUP; break;

        case 82: keyboard_report[2] = KEY_MEDIA_VOLUMEDOWN; break;

        case 83: keyboard_report[2] = KEY_MEDIA_MUTE; break;


        default: state = 0; break;  // 重置状态

    }


    // 将键盘报告复制到 USB 缓冲区并设置数据包大小

    UserToPMABufferCopy(keyboard_report, GetEPTxAddr(ENDP1), 8);

    SetEPTxCount(ENDP1, 8);  // 设置传输的数据包大小

    SetEPTxValid(ENDP1);     // 使能端点传输


    state++;  // 切换到下一个键码

    if (state > 83)  // 总共84个按键,循环回到初始状态

    {

        state = 0;

    }

}



/*********************************************************************

 * @fn      EP2_IN_Callback

 *

 * @brief   Callback for Mouse IN endpoint (EP2)

 *

 * @return  none

 */

void EP2_IN_Callback(void)

{

    static uint8_t state = 0;

    uint8_t mouse_report[3] = {0};  // 初始化鼠标报告


    switch (state)

    {

        case 0:

            // 模拟鼠标左键按下

            mouse_report[0] = 0x01;  // 左键按下

            break;

        case 1:

            // 模拟鼠标右键按下

            mouse_report[0] = 0x02;  // 右键按下

            break;

        case 2:

            // 模拟鼠标左键和右键同时按下

            mouse_report[0] = 0x03;  // 左键 + 右键按下

            break;

        case 3:

            // 模拟鼠标向右移动 20 个单位

            mouse_report[0] = 0x00;  // 无按键

            mouse_report[1] = 20;    // X 轴移动 20

            mouse_report[2] = 0;     // Y 轴不移动

            break;

        case 4:

            // 模拟鼠标向左移动 20 个单位

            mouse_report[0] = 0x00;  // 无按键

            mouse_report[1] = -20;   // X 轴移动 -20

            mouse_report[2] = 0;     // Y 轴不移动

            break;

        case 5:

            // 模拟鼠标向上移动 20 个单位

            mouse_report[0] = 0x00;  // 无按键

            mouse_report[1] = 0;     // X 轴不移动

            mouse_report[2] = -20;   // Y 轴移动 -20

            break;

        case 6:

            // 模拟鼠标向下移动 20 个单位

            mouse_report[0] = 0x00;  // 无按键

            mouse_report[1] = 0;     // X 轴不移动

            mouse_report[2] = 20;    // Y 轴移动 20

            break;

        default:

            state = 0;

            break;

    }


    // 将鼠标报告复制到 USB 缓冲区

    UserToPMABufferCopy(mouse_report, GetEPTxAddr(ENDP2), 3);

    SetEPTxCount(ENDP2, 3);  // 设置传输的数据包大小

    SetEPTxValid(ENDP2);     // 使能端点传输


    state++;  // 切换到下一个鼠标操作状态

    if (state > 6)

    {

        state = 0;  // 循环回到初始状态

    }

}


USB键鼠通讯的两个Call  Back 设置是否正确 要想实现与上位机的通讯 用键鼠进行操作 还需要哪些设置
可否提供一下与上位机通信的例程 谢谢



您好,麻烦具体说一下你的需求,若是想要USB模拟键鼠或者USB做主机操作键鼠,在EVT都有对应的例程,可以参考一下。此外,关于CH32F205的USB例程,请参考EVT USB文件夹下USBFS文件夹下或者USBHS文件夹下例程,USBD例程不适用于F205。


只有登录才能回复,可以选择微信账号登录