Skip to content

由 bzhou830 贡献,Ai-Thinker 进行整理

【外设移植】Ai-WB2+双轴摇杆模块

外设介绍

双轴摇杆模块采用PS2游戏手柄上金属按键摇杆电位器、模块设置二路模拟输出和一路数字输出接口、输出值分别对应(x、y双轴偏移量,其类型为模拟量。按键表示用户是否在z轴上按下、其类型为数字开关量。

模块接口如下:

移植过程

根据上述的分析可知,我们需要:

  1. 两路ADC分别用来采集x轴和y轴的模拟值;
  2. 一路GPIO输入用来检测Z轴上的数字输出;

总结一下就是要使用到ADC和GPIO输入两个功能。

需要注意的是上述ADC的使用中这里面只使用到了1个通道,而我们这里需要同时使用两个通道,这个需要翻一下文档来拆看对应API的调用。翻阅文档可以找到:

📜 点击展开完整代码
html
int hosal_adc_add_channel(hosal_adc_dev_t *adc,uint32_t channel)

添加向ADC设备添加ADC通道。参数说明如下:

  • adc:ADC端口设备
  • channel:需要添加的ADC通道
  • 返回值:成功时返回0,否则返回EIO或其他值

知道了这个,那么我们就可以添加多个ADC采集通道了。

硬件接线

硬件的连接,左边为WB2, 右边为双轴遥感

IO11 ---> VRX

IO5 ---> VRY

IO3 ---> SW

3V3 ---> 5V

GND ---> GND

代码解析

📜 点击展开完整代码
c
#include <stdio.h>

#include <string.h>

#include <FreeRTOS.h>

#include <task.h>

#include <hosal_adc.h>

#include <hosal_gpio.h>

#include <bl_adc.h>

#include <bl_gpio.h>

#include <blog.h>

/**********   BL602 ADC ************
*    channel0   ----->     gpio12
*    channel1   ----->     gpio4
*    channel2   ----->     gpio14
*    channel3   ----->     gpio13
*    channel4   ----->     gpio5
*    channel5   ----->     gpio6
*    channel7   ----->     gpio9
*    channel9   ----->     gpio10
*    channel10  ----->     gpio11
*    channel11  ----->     gpio15
*/

#define ADC_X_PIN           11
#define ADC_Y_PIN           5
#define KEY_PIN             3

void main(void)
{
    static uint8_t key_state = 0;
    static hosal_gpio_dev_t key;
    key.port = KEY_PIN;
    key.config = INPUT_PULL_UP;
    hosal_gpio_init(&key);

    static hosal_adc_dev_t adc0 = {
        .cb = NULL,
        .config = {
            .mode = HOSAL_ADC_ONE_SHOT,
            .pin = ADC_X_PIN,
            .sampling_freq = 340,
        },
        .dma_chan = 0,
        .p_arg = NULL,
        .port = 0,
    };

    hosal_adc_init(&adc0);

    hosal_adc_add_channel(&adc0, bl_adc_get_channel_by_gpio(ADC_X_PIN));
    hosal_adc_add_channel(&adc0, bl_adc_get_channel_by_gpio(ADC_Y_PIN));

    for (;;)
    {
        int v_y = hosal_adc_value_get(&adc0, bl_adc_get_channel_by_gpio(ADC_Y_PIN), 100);
        int v_x = hosal_adc_value_get(&adc0, bl_adc_get_channel_by_gpio(ADC_X_PIN), 100);
        hosal_gpio_input_get(&key, &key_state);

        blog_info("x = %ld mV, y = %ld mV, btn = %d\r\n", v_y, v_x, key_state);

        vTaskDelay(pdMS_TO_TICKS(1000));
    }
}

结果验证

Released under the MIT License. Build Time 2026-09-11 14:52:23