添加bsp_adc,测试内部vbat、temp、外部PA13电压
This commit is contained in:
parent
f119dc7d0d
commit
cc26414609
|
@ -56,6 +56,7 @@
|
||||||
<tool id="ilg.gnumcueclipse.managedbuild.cross.riscv.tool.c.compiler.1731377187" name="GNU RISC-V Cross C Compiler" superClass="ilg.gnumcueclipse.managedbuild.cross.riscv.tool.c.compiler">
|
<tool id="ilg.gnumcueclipse.managedbuild.cross.riscv.tool.c.compiler.1731377187" name="GNU RISC-V Cross C Compiler" superClass="ilg.gnumcueclipse.managedbuild.cross.riscv.tool.c.compiler">
|
||||||
<option IS_BUILTIN_EMPTY="false" IS_VALUE_EMPTY="false" id="ilg.gnumcueclipse.managedbuild.cross.riscv.option.c.compiler.include.paths.1567947810" name="Include paths (-I)" superClass="ilg.gnumcueclipse.managedbuild.cross.riscv.option.c.compiler.include.paths" useByScannerDiscovery="true" valueType="includePath">
|
<option IS_BUILTIN_EMPTY="false" IS_VALUE_EMPTY="false" id="ilg.gnumcueclipse.managedbuild.cross.riscv.option.c.compiler.include.paths.1567947810" name="Include paths (-I)" superClass="ilg.gnumcueclipse.managedbuild.cross.riscv.option.c.compiler.include.paths" useByScannerDiscovery="true" valueType="includePath">
|
||||||
<listOptionValue builtIn="false" value=""${workspace_loc:/${ProjName}/Startup}""/>
|
<listOptionValue builtIn="false" value=""${workspace_loc:/${ProjName}/Startup}""/>
|
||||||
|
<listOptionValue builtIn="false" value=""${workspace_loc:/${ProjName}/BSP/inc}""/>
|
||||||
<listOptionValue builtIn="false" value=""${workspace_loc:/${ProjName}/Profile/include}""/>
|
<listOptionValue builtIn="false" value=""${workspace_loc:/${ProjName}/Profile/include}""/>
|
||||||
<listOptionValue builtIn="false" value=""${workspace_loc:/${ProjName}/APP/include}""/>
|
<listOptionValue builtIn="false" value=""${workspace_loc:/${ProjName}/APP/include}""/>
|
||||||
<listOptionValue builtIn="false" value=""${workspace_loc:/${ProjName}/StdPeriphDriver/inc}""/>
|
<listOptionValue builtIn="false" value=""${workspace_loc:/${ProjName}/StdPeriphDriver/inc}""/>
|
||||||
|
@ -136,6 +137,7 @@
|
||||||
</folderInfo>
|
</folderInfo>
|
||||||
<sourceEntries>
|
<sourceEntries>
|
||||||
<entry flags="VALUE_WORKSPACE_PATH|RESOLVED" kind="sourcePath" name="APP"/>
|
<entry flags="VALUE_WORKSPACE_PATH|RESOLVED" kind="sourcePath" name="APP"/>
|
||||||
|
<entry excluding="src/bsp_uart.c|src/bsp_tim.c|inc/bsp_uart.h|inc/bsp_tim.h" flags="VALUE_WORKSPACE_PATH|RESOLVED" kind="sourcePath" name="BSP"/>
|
||||||
<entry excluding="Profile|KEY.c|LED.c" flags="VALUE_WORKSPACE_PATH|RESOLVED" kind="sourcePath" name="HAL"/>
|
<entry excluding="Profile|KEY.c|LED.c" flags="VALUE_WORKSPACE_PATH|RESOLVED" kind="sourcePath" name="HAL"/>
|
||||||
<entry flags="VALUE_WORKSPACE_PATH|RESOLVED" kind="sourcePath" name="LIB"/>
|
<entry flags="VALUE_WORKSPACE_PATH|RESOLVED" kind="sourcePath" name="LIB"/>
|
||||||
<entry flags="VALUE_WORKSPACE_PATH|RESOLVED" kind="sourcePath" name="Ld"/>
|
<entry flags="VALUE_WORKSPACE_PATH|RESOLVED" kind="sourcePath" name="Ld"/>
|
||||||
|
@ -164,5 +166,5 @@
|
||||||
</storageModule>
|
</storageModule>
|
||||||
<storageModule moduleId="org.eclipse.cdt.core.LanguageSettingsProviders"/>
|
<storageModule moduleId="org.eclipse.cdt.core.LanguageSettingsProviders"/>
|
||||||
<storageModule moduleId="org.eclipse.cdt.make.core.buildtargets"/>
|
<storageModule moduleId="org.eclipse.cdt.make.core.buildtargets"/>
|
||||||
|
<storageModule moduleId="refreshScope"/>
|
||||||
</cproject>
|
</cproject>
|
||||||
|
|
|
@ -5,7 +5,7 @@
|
||||||
<provider copy-of="extension" id="org.eclipse.cdt.ui.UserLanguageSettingsProvider"/>
|
<provider copy-of="extension" id="org.eclipse.cdt.ui.UserLanguageSettingsProvider"/>
|
||||||
<provider-reference id="org.eclipse.cdt.core.ReferencedProjectsLanguageSettingsProvider" ref="shared-provider"/>
|
<provider-reference id="org.eclipse.cdt.core.ReferencedProjectsLanguageSettingsProvider" ref="shared-provider"/>
|
||||||
<provider-reference id="org.eclipse.cdt.managedbuilder.core.MBSLanguageSettingsProvider" ref="shared-provider"/>
|
<provider-reference id="org.eclipse.cdt.managedbuilder.core.MBSLanguageSettingsProvider" ref="shared-provider"/>
|
||||||
<provider class="org.eclipse.cdt.managedbuilder.language.settings.providers.GCCBuiltinSpecsDetector" console="false" env-hash="-714121590444630656" id="ilg.gnumcueclipse.managedbuild.cross.riscv.GCCBuiltinSpecsDetector" keep-relative-paths="false" name="CDT RISC-V Cross GCC Built-in Compiler Settings" parameter="${COMMAND} ${FLAGS} ${cross_toolchain_flags} -E -P -v -dD "${INPUTS}"" prefer-non-shared="true">
|
<provider class="org.eclipse.cdt.managedbuilder.language.settings.providers.GCCBuiltinSpecsDetector" console="false" env-hash="-794068338182449592" id="ilg.gnumcueclipse.managedbuild.cross.riscv.GCCBuiltinSpecsDetector" keep-relative-paths="false" name="CDT RISC-V Cross GCC Built-in Compiler Settings" parameter="${COMMAND} ${FLAGS} ${cross_toolchain_flags} -E -P -v -dD "${INPUTS}"" prefer-non-shared="true">
|
||||||
<language-scope id="org.eclipse.cdt.core.gcc"/>
|
<language-scope id="org.eclipse.cdt.core.gcc"/>
|
||||||
<language-scope id="org.eclipse.cdt.core.g++"/>
|
<language-scope id="org.eclipse.cdt.core.g++"/>
|
||||||
</provider>
|
</provider>
|
||||||
|
|
|
@ -0,0 +1,5 @@
|
||||||
|
{
|
||||||
|
"files.associations": {
|
||||||
|
"bsp_adc.h": "c"
|
||||||
|
}
|
||||||
|
}
|
|
@ -17,6 +17,9 @@
|
||||||
#include "gattprofile.h"
|
#include "gattprofile.h"
|
||||||
#include "peripheral.h"
|
#include "peripheral.h"
|
||||||
|
|
||||||
|
|
||||||
|
#include "bsp_adc.h"
|
||||||
|
|
||||||
/*********************************************************************
|
/*********************************************************************
|
||||||
* GLOBAL TYPEDEFS
|
* GLOBAL TYPEDEFS
|
||||||
*/
|
*/
|
||||||
|
@ -123,6 +126,7 @@ int main(void)
|
||||||
GPIOB_SetBits(bTXD0);
|
GPIOB_SetBits(bTXD0);
|
||||||
GPIOB_ModeCfg(bTXD0, GPIO_ModeOut_PP_5mA);
|
GPIOB_ModeCfg(bTXD0, GPIO_ModeOut_PP_5mA);
|
||||||
UART0_DefInit();
|
UART0_DefInit();
|
||||||
|
PRINT("Start @ChipID=%02X\n", R8_CHIP_ID);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
// 12V_EN
|
// 12V_EN
|
||||||
|
@ -163,6 +167,8 @@ int main(void)
|
||||||
|
|
||||||
DelayMs(1000 * 5);
|
DelayMs(1000 * 5);
|
||||||
|
|
||||||
|
BSP_ADC_Init();
|
||||||
|
|
||||||
PRINT("%s\n", VER_LIB);
|
PRINT("%s\n", VER_LIB);
|
||||||
CH59x_BLEInit();
|
CH59x_BLEInit();
|
||||||
HAL_Init();
|
HAL_Init();
|
||||||
|
|
|
@ -0,0 +1,28 @@
|
||||||
|
/*
|
||||||
|
* @Author : stark1898y 1658608470@qq.com
|
||||||
|
* @Date : 2024-12-08 12:20:47
|
||||||
|
* @LastEditors : stark1898y 1658608470@qq.com
|
||||||
|
* @LastEditTime : 2024-12-08 12:20:47
|
||||||
|
* @FilePath : \BLE_TYQ_CH592F\BSP\inc\bsp_adc.h
|
||||||
|
* @Description :
|
||||||
|
*
|
||||||
|
* Copyright (c) 2024 by yzy, All Rights Reserved.
|
||||||
|
*/
|
||||||
|
#ifndef __BSP_ADC_H__
|
||||||
|
#define __BSP_ADC_H__
|
||||||
|
|
||||||
|
|
||||||
|
#include "CH59x_common.h"
|
||||||
|
|
||||||
|
|
||||||
|
void BSP_ADC_Init(void);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
#endif //!@__BSP_ADC_H__
|
||||||
|
|
|
@ -0,0 +1,97 @@
|
||||||
|
#include "bsp_adc.h"
|
||||||
|
|
||||||
|
// https://www.cnblogs.com/gscw/p/17682385.html
|
||||||
|
// 计算公式参考手册,建议使用 “建议实际可用测量电压范围” 作为 ADC 采集的增益选择判断标准。计算公式参考手册。
|
||||||
|
// 仅使用 ADC 功能,同时降低功耗,可以将引脚的数字输入功能关闭,配置 R16_PIN_ANALOG_IE。 注:如果引脚用于模拟功能(ADC/TouchKey),建议将该引脚的数字输入功能关闭,即设置数字输入禁用,从而可以降低功耗,并有利于减少干扰。
|
||||||
|
// PA9 引脚进行 ADC 采集,在置为浮空后,过 300ms 左右再进行 ADC 的采集。因为初次烧录的时候 boot 是串口功能开启的,PA9 是输出高电平。切换到浮空会需要等待时间。
|
||||||
|
|
||||||
|
uint16_t adcBuff[40];
|
||||||
|
|
||||||
|
volatile uint8_t adclen;
|
||||||
|
volatile uint8_t DMA_end = 0;
|
||||||
|
|
||||||
|
signed short RoughCalib_Value = 0; // ADC粗调偏差值
|
||||||
|
uint32_t countadc = 0;
|
||||||
|
uint16_t min_number = 0;
|
||||||
|
uint16_t max_number = 0;
|
||||||
|
double voltage = 0;
|
||||||
|
|
||||||
|
void BSP_ADC_Init(void)
|
||||||
|
{
|
||||||
|
uint8_t i;
|
||||||
|
|
||||||
|
/* 温度采样并输出 */
|
||||||
|
PRINT("\n1.Temperature sampling...\n");
|
||||||
|
ADC_InterTSSampInit();
|
||||||
|
for (i = 0; i < 20; i++)
|
||||||
|
{
|
||||||
|
adcBuff[i] = ADC_ExcutSingleConver(); // 连续采样20次
|
||||||
|
}
|
||||||
|
for (i = 0; i < 20; i++)
|
||||||
|
{
|
||||||
|
uint32_t C25 = 0;
|
||||||
|
C25 = (*((PUINT32)ROM_CFG_TMP_25C));
|
||||||
|
PRINT("%d %d %d \n", adc_to_temperature_celsius(adcBuff[i]), adcBuff[i], C25);
|
||||||
|
}
|
||||||
|
|
||||||
|
// VBAT
|
||||||
|
uint32_t CountBat = 0;
|
||||||
|
ADC_InterBATSampInit();
|
||||||
|
for (i = 0; i < 20; i++)
|
||||||
|
{
|
||||||
|
adcBuff[i] = ADC_ExcutSingleConver(); // 连续采样20次
|
||||||
|
}
|
||||||
|
for (i = 0; i < 20; i++)
|
||||||
|
{
|
||||||
|
CountBat += adcBuff[i];
|
||||||
|
if (i == 0)
|
||||||
|
{
|
||||||
|
min_number = adcBuff[i];
|
||||||
|
max_number = adcBuff[i];
|
||||||
|
}
|
||||||
|
min_number = ((min_number > adcBuff[i]) ? adcBuff[i] : min_number); // 软件滤波
|
||||||
|
max_number = ((max_number < adcBuff[i]) ? adcBuff[i] : max_number);
|
||||||
|
}
|
||||||
|
printf("min_number = %d, max_number = %d\n", min_number, max_number);
|
||||||
|
CountBat = (CountBat - min_number - max_number) / 18; // 删除最小与最大值
|
||||||
|
printf("AverageCountBat = %d\n", CountBat);
|
||||||
|
|
||||||
|
/* 单通道采样:选择adc通道3做采样,对应 PA13引脚, 带数据校准功能 */
|
||||||
|
GPIOA_ModeCfg(GPIO_Pin_13, GPIO_ModeIN_Floating);
|
||||||
|
// 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);
|
||||||
|
|
||||||
|
RoughCalib_Value = ADC_DataCalib_Rough(); // 用于计算ADC内部偏差,记录到全局变量 RoughCalib_Value中
|
||||||
|
PRINT("RoughCalib_Value =%d \n", RoughCalib_Value);
|
||||||
|
|
||||||
|
printf("PA13:::::\n");
|
||||||
|
ADC_ChannelCfg(3);
|
||||||
|
|
||||||
|
for (i = 0; i < 20; i++)
|
||||||
|
{
|
||||||
|
adcBuff[i] = ADC_ExcutSingleConver() + RoughCalib_Value; // 连续采样20次
|
||||||
|
}
|
||||||
|
printf("original: ");
|
||||||
|
for (i = 0; i < 20; i++)
|
||||||
|
{
|
||||||
|
PRINT("%d ", adcBuff[i]); // 注意:由于ADC内部偏差的存在,当采样电压在所选增益范围极限附近的时候,可能会出现数据溢出的现象
|
||||||
|
}
|
||||||
|
printf("\n");
|
||||||
|
|
||||||
|
for (i = 0; i < 20; i++)
|
||||||
|
{
|
||||||
|
countadc += adcBuff[i];
|
||||||
|
if (i == 0)
|
||||||
|
{
|
||||||
|
min_number = adcBuff[i];
|
||||||
|
max_number = adcBuff[i];
|
||||||
|
}
|
||||||
|
min_number = ((min_number > adcBuff[i]) ? adcBuff[i] : min_number); // 软件滤波
|
||||||
|
max_number = ((max_number < adcBuff[i]) ? adcBuff[i] : max_number);
|
||||||
|
}
|
||||||
|
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);
|
||||||
|
}
|
Loading…
Reference in New Issue