From 6a594d6cdc1549f12e48283729a5a3dad5a3003c Mon Sep 17 00:00:00 2001 From: dgjames <1943357252@qq.com> Date: Sat, 18 Jan 2025 12:41:57 +0800 Subject: [PATCH] =?UTF-8?q?ADC=20=E6=8E=A5=E5=85=A5=E6=9D=BF=E6=AD=A3?= =?UTF-8?q?=E7=A1=AE=E6=A3=80=E6=B5=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .config | 6 +++-- my_pro/indicator_led.c | 52 +++++++++++++++++++++++++++++++++++++++++- my_pro/myproject.c | 4 +--- rtconfig.h | 3 +++ 4 files changed, 59 insertions(+), 6 deletions(-) diff --git a/.config b/.config index 91d9b3a..99ce28f 100644 --- a/.config +++ b/.config @@ -201,7 +201,7 @@ CONFIG_RT_USING_I2C_BITOPS=y # CONFIG_RT_I2C_BITOPS_DEBUG is not set # CONFIG_RT_USING_SOFT_I2C is not set # CONFIG_RT_USING_PHY is not set -# CONFIG_RT_USING_ADC is not set +CONFIG_RT_USING_ADC=y # CONFIG_RT_USING_DAC is not set # CONFIG_RT_USING_NULL is not set # CONFIG_RT_USING_ZERO is not set @@ -1691,7 +1691,9 @@ CONFIG_BSP_USING_SPI=y CONFIG_BSP_USING_SPI2=y # CONFIG_BSP_SPI2_TX_USING_DMA is not set # CONFIG_BSP_SPI2_RX_USING_DMA is not set -# CONFIG_BSP_USING_ADC is not set +CONFIG_BSP_USING_ADC=y +CONFIG_BSP_USING_ADC1=y +# CONFIG_BSP_USING_ADC3 is not set CONFIG_BSP_USING_I2C=y # CONFIG_BSP_USING_I2C1 is not set CONFIG_BSP_USING_I2C2=y diff --git a/my_pro/indicator_led.c b/my_pro/indicator_led.c index ace31db..b26c508 100644 --- a/my_pro/indicator_led.c +++ b/my_pro/indicator_led.c @@ -1,4 +1,10 @@ #include "indicator_led.h" +#include "rtdevice.h" + +#define ADC_DEV_NAME "adc1" /* ADC 设备名称 */ +#define ADC_DEV_CHANNEL 5 /* ADC 通道 */ +#define REFER_VOLTAGE 330 /* 参考电压 3.3V,数据精度乘以100保留2位小数*/ +#define CONVERT_BITS (1 << 12) /* 转换位数为12位 */ #define LED_NUM 24 // LED灯珠个数 @@ -154,13 +160,57 @@ void led_breath_entry(void *parameter) } } -// MSH_CMD_EXPORT_ALIAS(led_breath_thread,BREATH, "BREATH LIGHT"); +#define BORAD_NOT_CORRECT 25 + +/** + * @brief 检测一下当前接入的是否是正确的板 +*/ +static int borad_check(void) +{ + 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); + + /* 转换为对应电压值 */ + vol = value * REFER_VOLTAGE / CONVERT_BITS; + if(vol>=0.25) + { + rt_kprintf("NOT correct borad!\n"); + rt_kprintf("the value is :%d \n", value); + rt_kprintf("the voltage is :%d.%02d \n", vol / 100, vol % 100); + return BORAD_NOT_CORRECT; + } + /* 关闭通道 */ + ret = rt_adc_disable(adc_dev, ADC_DEV_CHANNEL); + + return ret; +} +/* 导出到 msh 命令列表中 */ +MSH_CMD_EXPORT(borad_check, adc voltage convert sample); /** * @brief LED的初始化 */ int led_init(void) { + if (borad_check()==BORAD_NOT_CORRECT) + { + return 0; + } led_blink_thread = rt_thread_create("led blink control thread", led_blink_entry, RT_NULL, 1024, 20, 20); if (led_blink_thread == RT_NULL) { diff --git a/my_pro/myproject.c b/my_pro/myproject.c index 57eec14..3cee4fd 100644 --- a/my_pro/myproject.c +++ b/my_pro/myproject.c @@ -619,8 +619,6 @@ void my_project(void) serial_init(); - led_blink_init(); - - led_breath_init(); + led_init(); } MSH_CMD_EXPORT_ALIAS(my_project, myproject, run my project); \ No newline at end of file diff --git a/rtconfig.h b/rtconfig.h index cdf16f8..02d7f0c 100644 --- a/rtconfig.h +++ b/rtconfig.h @@ -133,6 +133,7 @@ #define RT_SERIAL_RB_BUFSZ 64 #define RT_USING_I2C #define RT_USING_I2C_BITOPS +#define RT_USING_ADC #define RT_USING_PWM #define RT_USING_RTC #define RT_USING_SOFT_RTC @@ -655,6 +656,8 @@ #define BSP_USING_ON_CHIP_FLASH #define BSP_USING_SPI #define BSP_USING_SPI2 +#define BSP_USING_ADC +#define BSP_USING_ADC1 #define BSP_USING_I2C #define BSP_USING_I2C2 #define BSP_I2C2_SCL_PIN 81