RTT添加AD驱动代码

1、在Kconfig文件中添加如下内容

RTT添加AD驱动代码_第1张图片

menuconfig BSP_USING_ADC
        bool "Enable ADC"
        default n
        select RT_USING_ADC
        if BSP_USING_ADC
            config BSP_USING_ADC1
                bool "Enable ADC1"
                default n
        endif

2、前往cubeMx中打开AD

RTT添加AD驱动代码_第2张图片

RTT添加AD驱动代码_第3张图片

 

3、从生成的main.c中复制时钟初始化代码到board.c

RTT添加AD驱动代码_第4张图片

 

测试代码:

 

#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);

 

你可能感兴趣的:(RTT,STM32)