官网连接:https://docs.rt-thread.org/#/rt-thread-version/rt-thread-standard/programming-manual/device/adc/adc
一、配置步骤:
1、用cubemx配置底层;
2、cubemx配置好的文件替换之前的配置文件;
3、修改Kconfig文件,添加adc配置选项;
4、用env配置工程,打开adc;
5、打开mdk工程,打开adc功能配置选项宏定义;
6、添加测试函数;
7、测试展示;
二、用cubemx配置底层:
三、cubemx配置好的文件替换之前的配置文件:
四、修改Kconfig文件,添加adc配置选项:
menu "On-chip Peripheral Drivers"
********** 其他 ***********
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
********** 其他 ***********
endmenu
五、用env工具配置:
六、打开mdk工程,打开adc功能配置选项宏定义:
七、添加测试代码,配置debug:
adc测试代码:
/
#define ADC_DEV_NAME "adc1"
#define ADC_DEV_CHANNEL 1
#define REFER_VOLTAGE 330
#define CONVERT_BITS (1 << 12)
static int test_adc(void)
{
rt_adc_device_t adc_dev;
rt_uint32_t value, vol;
rt_err_t ret = RT_EOK;
rt_uint8_t count =0;
adc_dev = (rt_adc_device_t)rt_device_find(ADC_DEV_NAME);
if(adc_dev == RT_NULL)
{
rt_kprintf("adc smaple run failed! can't find %s device!\n", ADC_DEV_NAME);
return RT_ERROR;
}
ret = rt_adc_enable(adc_dev, ADC_DEV_CHANNEL);
while(count <10)
{
count ++;
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);
rt_thread_mdelay(500);
}
}
MSH_CMD_EXPORT(test_adc, function test adc);
整个main.c文件:
#include <rtthread.h>
#include <rtdevice.h>
#include <board.h>
/
int main(void)
{
int count = 1;
while (count++)
{
rt_thread_mdelay(100);
}
return RT_EOK;
}
/
#define LED0_PIN_NUM GET_PIN(C, 13)
void test_led(void)
{
uint8_t count =0; //tset count
rt_pin_mode(LED0_PIN_NUM, PIN_MODE_OUTPUT);
while(count <5)
{
count++;
rt_kprintf("LED计数:%d \t", count);
rt_pin_write(LED0_PIN_NUM, PIN_HIGH); //输出高
rt_kprintf("引脚输出高电平 \t");
rt_thread_mdelay(500);
rt_pin_write(LED0_PIN_NUM, PIN_LOW); //输出低
rt_kprintf("引脚输出底电平\r\n");
rt_thread_mdelay(500);
}
}
MSH_CMD_EXPORT(test_led, function test led control 5 times );
/
#define ADC_DEV_NAME "adc1"
#define ADC_DEV_CHANNEL 1
#define REFER_VOLTAGE 330
#define CONVERT_BITS (1 << 12)
static int test_adc(void)
{
rt_adc_device_t adc_dev;
rt_uint32_t value, vol;
rt_err_t ret = RT_EOK;
rt_uint8_t count =0;
adc_dev = (rt_adc_device_t)rt_device_find(ADC_DEV_NAME);
if(adc_dev == RT_NULL)
{
rt_kprintf("adc smaple run failed! can't find %s device!\n", ADC_DEV_NAME);
return RT_ERROR;
}
ret = rt_adc_enable(adc_dev, ADC_DEV_CHANNEL);
while(count <10)
{
count ++;
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);
rt_thread_mdelay(500);
}
}
MSH_CMD_EXPORT(test_adc, function test adc);
八、展示效果: