# ADC驱动框架
ADC(Analog-to-Digital Converter)
基本功能:按通道号获取原始值
# RT-Thread的ADC实现
1. 为省空间,所以的ADC通道合并为一个设备: RTDeviceClassMiscellaneous
2. 使用pos参数作为通道号
## 接口
`
struct rtadcops
{
rterrt (*convert)(struct rtdevice device, int channel, int value);
};
struct rtdeviceadc
{
struct rtdevice parent;
const struct rtadcops *ops;
};
```
ADC只需要实现转换接口,输入参数为通道号。
### 返回值的定义
暂定为原始值,数据类型为int,支持负数。
## read
```
static rtsizet adcread(rtdevicet dev, rtofft pos, void *buffer, rtsizet size)
{
rterrt result = RTEOK;
rtsizet i;
struct rtdeviceadc adc = (struct rtdeviceadc )dev;
int value = (int )buffer;
if(!adc->ops->convert)
{
return 0;
}
for(i=0; i
result = adc->ops->convert(dev, pos + i, value);
if(result != RT_EOK)
{
return 0;
}
value++;
}
return i;
}
```
### pos
pos参数为通道编号,每次可以读取多个通道。
### size
统一定义为byte,避免歧义。
每一个通道的大小是 sizeof(int)
注意: pos与size的定义并不相同。
占位: 驱动示例与测试
dev = rt_device_find("adc");
rt_device_open(dev, RT_DEVICE_FLAG_RDONLY);
int value;
rt_device_read(dev, channel, &value, sizeof(value));