RT-Thread RTOS
An open source embedded real-time operating system
+ Collaboration diagram for ADC:

Data Structures

struct  rt_adc_ops
 
struct  rt_adc_device
 

Functions

rt_err_t rt_hw_adc_register (rt_adc_device_t adc, const char *name, const struct rt_adc_ops *ops, const void *user_data)
 
rt_uint32_t rt_adc_read (rt_adc_device_t dev, rt_int8_t channel)
 
rt_err_t rt_adc_enable (rt_adc_device_t dev, rt_int8_t channel)
 
rt_err_t rt_adc_disable (rt_adc_device_t dev, rt_int8_t channel)
 
rt_int16_t rt_adc_voltage (rt_adc_device_t dev, rt_int8_t channel)
 

Detailed Description

ADC driver api.

Example

#define ADC_DEV_NAME "adc1"
#define ADC_DEV_CHANNEL 5
#define REFER_VOLTAGE 330
#define CONVERT_BITS (1 << 12)
static int adc_vol_sample(int argc, char *argv[])
{
rt_adc_device_t adc_dev;
rt_uint32_t value, vol;
rt_err_t ret = RT_EOK;
adc_dev = (rt_adc_device_t)rt_device_find(ADC_DEV_NAME);
if (adc_dev == RT_NULL)
{
rt_kprintf("adc sample run failed! can't find %s device!\n", ADC_DEV_NAME);
return -RT_ERROR;
}
ret = rt_adc_enable(adc_dev, ADC_DEV_CHANNEL);
value = rt_adc_read(adc_dev, ADC_DEV_CHANNEL);
rt_kprintf("the value is :%d \n", value);
vol = value * REFER_VOLTAGE / CONVERT_BITS;
rt_kprintf("the voltage is :%d.%02d \n", vol / 100, vol % 100);
ret = rt_adc_disable(adc_dev, ADC_DEV_CHANNEL);
return ret;
}
MSH_CMD_EXPORT(adc_vol_sample, adc voltage convert sample);
rt_err_t rt_adc_enable(rt_adc_device_t dev, rt_int8_t channel)
enable the adc channel
rt_uint32_t rt_adc_read(rt_adc_device_t dev, rt_int8_t channel)
read the adc value
rt_err_t rt_adc_disable(rt_adc_device_t dev, rt_int8_t channel)
disable the adc channel
#define MSH_CMD_EXPORT(...)
Definition: finsh.h:142
adc device
Definition: adc.h:87

Function Documentation

◆ rt_hw_adc_register()

rt_err_t rt_hw_adc_register ( rt_adc_device_t  adc,
const char *  name,
const struct rt_adc_ops ops,
const void *  user_data 
)

register the adc device

Parameters
adcadc device
namedevice name
opsdevice ops
user_datadevice private data
Returns
rt_err_t error code

◆ rt_adc_read()

rt_uint32_t rt_adc_read ( rt_adc_device_t  dev,
rt_int8_t  channel 
)

read the adc value

Parameters
devadc device
channeladc channel
Returns
rt_uint32_t adc value

◆ rt_adc_enable()

rt_err_t rt_adc_enable ( rt_adc_device_t  dev,
rt_int8_t  channel 
)

enable the adc channel

Parameters
devadc device
channeladc channel
Returns
rt_err_t error code

◆ rt_adc_disable()

rt_err_t rt_adc_disable ( rt_adc_device_t  dev,
rt_int8_t  channel 
)

disable the adc channel

Parameters
devadc device
channeladc channel
Returns
rt_err_t error code

◆ rt_adc_voltage()

rt_int16_t rt_adc_voltage ( rt_adc_device_t  dev,
rt_int8_t  channel 
)

get the adc resolution

Parameters
devadc device
channeladc channel
Returns
rt_int16_t adc resolution