Q群:
电话:
邮箱:
地址:
本开发指南给用户提供了ADC的基本配置,工程构建编译、操作API介绍以及使用示例展示。
以下配置均以万耦一代开发板(STML475)为例,STM32的ADC目前只支持单通道模式,即任意时刻只能访问获得一个通道的有效数据,其配置如下:
打开oneos\projects\xxxxx(project文件夹)\board\CubeMX_Config下的STM32CubeMX工程文件,按照具体的需求进行配置。
ADC单通道模式下,可以开启多个ADC,如打开了三个ADC,按照ADC的名称进行注册,每个ADC可以配置开启多个通道,通道顺序号和配置一致,但是任意时刻只能访问一个通道,配置实际上是配置好对应的引脚的参数。 配置ADC通道,并配置ADC通道参数:
Top) → Drivers→ MISC
OneOS Configuration
[*] Using generic GPIO device drivers
[*] Using push button device drivers
[*] Using led device drivers
[ ] Using buzzer device drivers
[*] Using ADC device drivers
[ ] Using DAC device drivers
[ ] Using PWM device drivers
[ ] Using Input Capture device drivers
[ ] Using Pulse Encoder device drivers
函数 | 说明 |
---|---|
os_device_find() | 根据ADC设备名称寻找设备并获取设备指针 |
os_device_open() | 打开设备 |
os_device_control() | 控制ADC设备 |
os_device_read_nonblock() | 以非阻塞方式读取ADC设备数据(电压值:mV) |
os_device_close() | 关闭设备 |
该函数根据ADC设备名查找对应的ADC设备,函数原型如下:
os_device_t *os_device_find(const char *name);
参数 | 说明 |
---|---|
name | ADC设备名称 |
返回 | 说明 |
设备指针 | 查找到对应设备将返回相应的设备指针 |
OS_NULL | 没有找到设备 |
该函数用于开启ADC设备,函数原型如下:
os_err_t os_device_open(os_device_t *dev);
参数 | 说明 |
---|---|
dev | 设备名称 |
返回 | 说明 |
OS_EOK | 设备打开成功 |
其他错误码 | 设备打开失败 |
该函数用于控制ADC设备通道开关,函数原型如下:
os_err_t os_device_control(os_device_t *dev, int cmd, void *arg);
参数 | 说明 |
---|---|
dev | ADC设备指针 |
cmd | 命令控制字,可取值:OS_ADC_CMD_ENABLE/OS_ADC_CMD_DISABLE |
arg | 控制的参数无效,直接给:OS_NULL |
返回 | 说明 |
OS_EOK | 函数执行成功 |
OS_ENOSYS | 执行失败,设备不支持 control 接口 |
其他错误码 | 执行失败 |
该函数用于以非阻塞方式读取ADC通道采样值(mV),函数原型如下:
os_size_t os_device_read_nonblock((os_device_t *dev, os_off_t pos, void *buffer, os_size_t size);
参数 | 说明 |
---|---|
dev | ADC设备指针 |
pos | ADC通道 |
buffer | ADC数据缓存指针 |
size | ADC数据大小,以字节为单位 |
返回 | 说明 |
读到数据的实际大小 | 返回数据大小,以字节为单位 |
<= 0 | 读取失败 |
注:使用时需要用户自己保证通道参数正确,驱动不能判断是否为有效通道;
该函数用于关闭设备,函数原型如下:
os_err_t os_device_close(os_device_t *dev);
参数 | 说明 |
---|---|
dev | 设备指针 |
返回 | 说明 |
OS_EOK | 关闭设备成功 |
OS_ERROR | 设备已经完全关闭,不能重复关闭设备 |
其他错误码 | 关闭设备失败 |
本例读取ADC的数据,并输出。
/**
***********************************************************************************************************************
* Copyright (c) 2020, China Mobile Communications Group Co.,Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*
* @file adc_test.c
*
* @brief The test file for adc.
*
* @revision
* Date Author Notes
* 2020-02-20 OneOS Team First Version
***********************************************************************************************************************
*/
#include <drv_cfg.h>
#include <shell.h>
#include <stdlib.h>
int adc_sample(int argc, char **argv)
{
os_uint32_t adc_channel;
os_int32_t adc_databuf;
os_err_t ret = OS_EOK;
os_device_t *adc_dev;
if (argc != 3)
{
os_kprintf("usage: adc_sample name channel\r\n");
os_kprintf(" adc_sample adc1 0\r\n");
return -1;
}
/* find device */
adc_dev = os_device_find(argv[1]);
if (adc_dev == OS_NULL)
{
os_kprintf("adc device not find! \r\n");
return -1;
}
adc_channel = strtol(argv[2], OS_NULL, 0);;
/* open adc */
os_device_open(adc_dev);
ret = os_device_control(adc_dev, OS_ADC_CMD_ENABLE, OS_NULL);
if (ret != OS_EOK)
{
os_kprintf("adc device cannot enable! \r\n");
os_device_close(adc_dev);
return -1;
}
for (int i = 0; i < 10;i++)
{
ret = os_device_read_nonblock(adc_dev, adc_channel, &adc_databuf, sizeof(adc_databuf));
if (ret == sizeof(adc_databuf))
{
os_kprintf("%s channel %s voltage value: %d\r\n", argv[1] ,argv[2], adc_databuf);
}
os_task_msleep(100);
}
os_device_control(adc_dev, OS_ADC_CMD_DISABLE, OS_NULL);
os_device_close(adc_dev);
return ret;
}
SH_CMD_EXPORT(adc_sample, adc_sample, "test set adc");
运行结果如下:
sh />adc_sample adc1 1
adc1 channle 1 voltage value: 514
adc1 channle 1 voltage value: 1010