官網連接: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測驗代碼:
//******************************* adc 應用示例 *******************************************
/******************************************************************************************
*** 函式名稱: test_adc
*** 輸入引數: 無
*** 返 回 值: 無
*** 調度周期:無
*** 說 明:adc電壓采集測驗函式
*** 鏈 接:https://docs.rt-thread.org/#/rt-thread-version/rt-thread-standard/programming-manual/device/adc/adc
*** 編者 時間 版本
*** wagnlu 2021/04/04 V0.1
******************************************************************************************/
#define ADC_DEV_NAME "adc1" /* ADC 設備名稱 */
#define ADC_DEV_CHANNEL 1 /* ADC 通道 */
#define REFER_VOLTAGE 330 /* 參考電壓 3.3V,資料精度乘以100保留2位小數*/
#define CONVERT_BITS (1 << 12) /* 轉換位數為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檔案:
/*
* Copyright (c) 2006-2018, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
* 2018-11-06 SummerGift first version
*/
#include <rtthread.h>
#include <rtdevice.h>
#include <board.h>
//******************************* main function *******************************************
/******************************************************************************************
*** 函式名稱: main
*** 輸入引數: 無
*** 返 回 值: 無
*** 調度周期:無
*** 說 明:main函式
*** 鏈 接:無
*** 編者 時間 版本
*** wagnlu 2021/04/02 V0.1
******************************************************************************************/
int main(void)
{
int count = 1;
while (count++)
{
rt_thread_mdelay(100);
}
return RT_EOK;
}
//******************************* GPIO 應用示例 *******************************************
/******************************************************************************************
*** 函式名稱: functest
*** 輸入引數: 無
*** 返 回 值: 無
*** 調度周期:無
*** 說 明:led測驗函式
*** 鏈 接:https://docs.rt-thread.org/#/rt-thread-version/rt-thread-standard/programming-manual/device/pin/pin
*** 編者 時間 版本
*** wagnlu 2021/04/02 V0.1
******************************************************************************************/
/* defined the LED0 pin: PC13 */
#define LED0_PIN_NUM GET_PIN(C, 13)
void test_led(void)
{
uint8_t count =0; //tset count
/* set LED0 pin mode to output */
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 );
//******************************* adc 應用示例 *******************************************
/******************************************************************************************
*** 函式名稱: test_adc
*** 輸入引數: 無
*** 返 回 值: 無
*** 調度周期:無
*** 說 明:adc電壓采集測驗函式
*** 鏈 接:https://docs.rt-thread.org/#/rt-thread-version/rt-thread-standard/programming-manual/device/adc/adc
*** 編者 時間 版本
*** wagnlu 2021/04/04 V0.1
******************************************************************************************/
#define ADC_DEV_NAME "adc1" /* ADC 設備名稱 */
#define ADC_DEV_CHANNEL 1 /* ADC 通道 */
#define REFER_VOLTAGE 330 /* 參考電壓 3.3V,資料精度乘以100保留2位小數*/
#define CONVERT_BITS (1 << 12) /* 轉換位數為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);


八、展示效果:

轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/272565.html
標籤:其他
下一篇:stm32震動感應燈
