From 79fe76276c7f827ac5a8b6930a052032ad426301 Mon Sep 17 00:00:00 2001
From: stark1898y <1658608470@qq.com>
Date: Mon, 9 Dec 2024 09:22:59 +0800
Subject: [PATCH] =?UTF-8?q?ADC=20IO=E5=86=99=E9=94=99=EF=BC=8C=E5=88=86?=
=?UTF-8?q?=E5=8E=8B=E7=94=B5=E5=8E=8B=E6=A3=80=E6=B5=8B=E5=8F=AF=E4=BB=A5?=
=?UTF-8?q?=E4=BA=86?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
.cproject | 1 +
APP/peripheral_main.c | 9 +++++++--
BSP/src/bsp_adc.c | 23 +++++++++++++++++------
3 files changed, 25 insertions(+), 8 deletions(-)
diff --git a/.cproject b/.cproject
index 26f5b24..bb0cd81 100644
--- a/.cproject
+++ b/.cproject
@@ -100,6 +100,7 @@
+
diff --git a/APP/peripheral_main.c b/APP/peripheral_main.c
index 6a38d46..8c8ebc8 100644
--- a/APP/peripheral_main.c
+++ b/APP/peripheral_main.c
@@ -163,13 +163,18 @@ int main(void)
// R32_PIN_CONFIG2
// GPIOA_ResetBits(GPIO_Pin_1);
- GPIOA_ModeCfg(GPIO_Pin_14, GPIO_ModeIN_Floating);
+ // GPIOA_ModeCfg(GPIO_Pin_14, GPIO_ModeIN_Floating);
- DelayMs(1000 * 5);
+ DelayMs(1000 * 3);
BSP_ADC_Init();
PRINT("%s\n", VER_LIB);
+ // while (1)
+ // {
+ // DelayMs(1000);
+ // }
+
CH59x_BLEInit();
HAL_Init();
GAPRole_PeripheralInit();
diff --git a/BSP/src/bsp_adc.c b/BSP/src/bsp_adc.c
index 526d48c..76d74de 100644
--- a/BSP/src/bsp_adc.c
+++ b/BSP/src/bsp_adc.c
@@ -20,6 +20,8 @@ void BSP_ADC_Init(void)
{
uint8_t i;
+#if 0
+ // ±10C,没啥参考价值
/* 温度采样并输出 */
PRINT("\n1.Temperature sampling...\n");
ADC_InterTSSampInit();
@@ -33,7 +35,9 @@ void BSP_ADC_Init(void)
C25 = (*((PUINT32)ROM_CFG_TMP_25C));
PRINT("%d %d %d \n", adc_to_temperature_celsius(adcBuff[i]), adcBuff[i], C25);
}
+#endif
+#if 1
// VBAT
uint32_t CountBat = 0;
ADC_InterBATSampInit();
@@ -55,21 +59,27 @@ void BSP_ADC_Init(void)
printf("min_number = %d, max_number = %d\n", min_number, max_number);
CountBat = (CountBat - min_number - max_number) / 18; // 删除最小与最大值
printf("AverageCountBat = %d\n", CountBat);
+#endif
/* 单通道采样:选择adc通道3做采样,对应 PA13引脚, 带数据校准功能 */
- GPIOA_ModeCfg(GPIO_Pin_13, GPIO_ModeIN_Floating);
+ GPIOA_ModeCfg(GPIO_Pin_14, GPIO_ModeIN_Floating);
+ DelayMs(300);
+
// 6dB(2倍) (ADC/4096+0.5)*Vref 1.5*Vref 0.525V~1.575V 0.6V~1.5V
- ADC_ExtSingleChSampInit(SampleFreq_3_2, ADC_PGA_2);
+ ADC_ExtSingleChSampInit(SampleFreq_3_2, ADC_PGA_0);
RoughCalib_Value = ADC_DataCalib_Rough(); // 用于计算ADC内部偏差,记录到全局变量 RoughCalib_Value中
PRINT("RoughCalib_Value =%d \n", RoughCalib_Value);
- printf("PA13:::::\n");
- ADC_ChannelCfg(3);
+ DelayMs(300);
+
+ printf("PA14:::::\n");
+ ADC_ChannelCfg(4);
for (i = 0; i < 20; i++)
{
adcBuff[i] = ADC_ExcutSingleConver() + RoughCalib_Value; // 连续采样20次
+ // DelayMs(5);
}
printf("original: ");
for (i = 0; i < 20; i++)
@@ -92,6 +102,7 @@ void BSP_ADC_Init(void)
printf("min=%d, max=%d, diff=%d\n", min_number, max_number, (max_number - min_number));
countadc = (countadc - min_number - max_number) / 18; // 删除最小与最大值
printf("countaveradc = %d\n", countadc);
- voltage = (double)countadc / 2048 * 1.05;
- printf("voltage=%1.3lfv\n", voltage);
+ voltage = (double)(countadc) / 2048 * 1.05;
+ printf("voltage=%1.3lf V\n", voltage);
+ PRINT("VIN: %1.3lf V\n", voltage * 2);
}