感谢沁恒开发们提供的三模demo,大家做键盘的时候一定要跟开发大大们要最新的demo,去年提供的demo链接蓝牙和2.4G有问题,联系了大大们提供的新demo测试蓝牙、2.4G都正常


顺带提一句,demo里的按键扫描部分写的不太适合移植,而做键盘这部分和Fn功能键部分是最需要调整的,这里建议优化一下


对于键盘修复或者改装的,需要用到现有键位扫描,把自己用的代码分享给大家

#ifdef ENABLE_KEY_DEFINE_SCAN

uint8_t getPinNum(uint32_t pin) {

    uint8_t pos=0;
    if(pin==0)
    return 99;
    while(pin>1) {
        pos++;
        pin=pin>>1;
    }
    return pos;
}
void keyPositionScan(void) {

    GPIO* keys[ROW_NUM + COL_NUM];
    uint8_t i, j, idx = ROW_NUM + COL_NUM;
    //合并行列集合,方便遍历
    for (i = 0; i < idx; i++)
    {
        keys[i] = i < ROW_NUM ? rowPins[i] : colPins[i - ROW_NUM];
        //全部初始化成推挽输出,高电平(应该设置为开漏,CH582不支持)
        GPIO_SetBits(keys[i]);
        GPIO_ModeCfg(keys[i],GPIO_ModeOut_PP_5mA);
    }

    while (1)
    {
        for (i = 0; i < idx; i++)
        {
            //i切换为上拉输入
            GPIO_ModeCfg(keys[i],GPIO_ModeIN_PU);
            for (j = 0; j < idx; j++)
            {
                if (i != j)
                {
                    //拉低j,检查i电平,为低表示i:j按键按下
                    GPIO_ResetBits(keys[j]);
//                       mDelaymS(1);
                    if (GPIO_ReadPortPin(keys[i])==0)
                    {
                        //切换i:j状态,再次检查电平状态,j也为低表示无无冲二极管,j为高表示有无冲二极管,方向i正 j负
                        GPIO_SetBits(keys[j]);
                        GPIO_ModeCfg(keys[i],GPIO_ModeOut_PP_5mA);
                        GPIO_ModeCfg(keys[j],GPIO_ModeIN_PU);
                        GPIO_ResetBits(keys[i]);
//                           mDelaymS(1);
                        //j也为低表示无二极管
                        if (GPIO_ReadPortPin(keys[j])==0)
                        {
                            LOG_INFO("press P%c%d--P%c%d\r\n", keys[i]->port==0?'A':'B', getPinNum(keys[i]->pin), keys[j]->port==0?'A':'B', getPinNum(keys[j]->pin));
                        }
                        else
                        {
                            LOG_INFO("press P%c%d->P%c%d\r\n", keys[i]->port==0?'A':'B', getPinNum(keys[i]->pin), keys[j]->port==0?'A':'B', getPinNum(keys[j]->pin));
                        }
                        GPIO_SetBits(keys[i]);

                        GPIO_ModeCfg(keys[i],GPIO_ModeIN_PU);
                        GPIO_ModeCfg(keys[j],GPIO_ModeOut_PP_5mA);
                    }
                    GPIO_SetBits(keys[j]);
                }
            }

            GPIO_ModeCfg(keys[i],GPIO_ModeOut_PP_5mA);
        }
    }
}
#endif

1720077584211953.png

1720077584164440.jpg