- ADC 模块包含 2 个 12 位的逐次逼近型的模拟数字转换器,最高 14MHz 的输入时钟。支持 16 个
外部通道和 2 个内部信号源采样源。可完成通道的单次转换、连续转换,通道间自动扫描模式、间
断模式、外部触发模式、双重采样等功能。可以通过模拟看门狗功能监测通道电压是否在阈值范围
内,本次实验采用一路ADC间隔均值采样,然后打印输出采样值和温度值,熟悉STM32开发用易上手配置。✨✨✨- 比赛详情官网:https://www.wch.cn/RISC-V-MCU-competition/#/?indexFromSubmit=0
这是使用MounRiver Studio开发的项目,支持在RISC-V核心基础硬件CH32V307评估板上使用带有msh Shell的RTOS快速原型。
MCU:CH32V307VCT6,主频 144MHz,FLASH和RAM可配置
l 12 位分辨率
l 支持 16 个外部通道和 2 个内部信号源采样
l 多通道的多种采样转换方式:单次、连续、扫描、触发、间断等
l 数据对齐模式:左对齐、右对齐
l 采样时间可按通道分别编程
l 规则转换和注入转换均支持外部触发
l 模拟看门狗监测通道电压,自校准功能
l 双重模式
l ADC 通道输入范围:0≤VIN≤VDDA
l 输入增益可调,可实现小信号放大采样
首先,应安装 CH32V307 评估板的驱动程序,打开设备管理器查看USB 端口和外部接口已准备就绪。
环境搭建教程:https://blog.csdn.net/VOR234/article/details/128932474
评估板说明及参考例程:https://www.wch.cn/downloads/CH32V307EVT_ZIP.html
进入EXAM目录,就有对应的外设教程
进入DInternal_Temperature
文件下,双击Internal_Temperature.wvproj
,
打开项目工程如下,main.c
在user
文件夹下
main.c
,参考电压是3.3V
/********************************** (C) COPYRIGHT *******************************
* File Name : main.c
* Author : WCH
* Version : V1.0.0
* Date : 2021/06/06
* Description : Main program body.
*********************************************************************************
* Copyright (c) 2021 Nanjing Qinheng Microelectronics Co., Ltd.
* Attention: This software (modified or not) and binary are used for
* microcontroller manufactured by Nanjing Qinheng Microelectronics.
*******************************************************************************//**@NoteInternal temperature sensor routine:Through the ADC channel 16(PA2), the output voltage value and temperature value of the internaltemperature sensor are collected.*/#include "debug.h"/* Global Variable */
s16 Calibrattion_Val = 0;/********************************************************************** @fn ADC_Function_Init** @brief Initializes ADC collection.** @return none*/
void ADC_Function_Init(void)
{ADC_InitTypeDef ADC_InitStructure={0};GPIO_InitTypeDef GPIO_InitStructure={0};RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE );RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE );RCC_ADCCLKConfig(RCC_PCLK2_Div8);GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;GPIO_Init(GPIOA, &GPIO_InitStructure);ADC_DeInit(ADC1);ADC_InitStructure.ADC_Mode = ADC_Mode_Independent;ADC_InitStructure.ADC_ScanConvMode = DISABLE;ADC_InitStructure.ADC_ContinuousConvMode = DISABLE;ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None;ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;ADC_InitStructure.ADC_NbrOfChannel = 1;ADC_Init(ADC1, &ADC_InitStructure);ADC_Cmd(ADC1, ENABLE);ADC_BufferCmd(ADC1, DISABLE); //disable bufferADC_ResetCalibration(ADC1);while(ADC_GetResetCalibrationStatus(ADC1));ADC_StartCalibration(ADC1);while(ADC_GetCalibrationStatus(ADC1));Calibrattion_Val = Get_CalibrationValue(ADC1); ADC_BufferCmd(ADC1, ENABLE); //enable bufferADC_TempSensorVrefintCmd(ENABLE);
}/********************************************************************** @fn Get_ADC_Val** @brief Returns ADCx conversion result data.** @param ch - ADC channel.* ADC_Channel_0 - ADC Channel0 selected.* ADC_Channel_1 - ADC Channel1 selected.* ADC_Channel_2 - ADC Channel2 selected.* ADC_Channel_3 - ADC Channel3 selected.* ADC_Channel_4 - ADC Channel4 selected.* ADC_Channel_5 - ADC Channel5 selected.* ADC_Channel_6 - ADC Channel6 selected.* ADC_Channel_7 - ADC Channel7 selected.* ADC_Channel_8 - ADC Channel8 selected.* ADC_Channel_9 - ADC Channel9 selected.* ADC_Channel_10 - ADC Channel10 selected.* ADC_Channel_11 - ADC Channel11 selected.* ADC_Channel_12 - ADC Channel12 selected.* ADC_Channel_13 - ADC Channel13 selected.* ADC_Channel_14 - ADC Channel14 selected.* ADC_Channel_15 - ADC Channel15 selected.* ADC_Channel_16 - ADC Channel16 selected.* ADC_Channel_17 - ADC Channel17 selected.** @return none*/
u16 Get_ADC_Val(u8 ch)
{u16 val;ADC_RegularChannelConfig(ADC1, ch, 1, ADC_SampleTime_239Cycles5 );ADC_SoftwareStartConvCmd(ADC1, ENABLE);while(!ADC_GetFlagStatus(ADC1, ADC_FLAG_EOC ));val = ADC_GetConversionValue(ADC1);return val;
}/********************************************************************** @fn Get_ADC_Average** @brief Returns ADCx conversion result average data.** @param ch - ADC channel.* ADC_Channel_0 - ADC Channel0 selected.* ADC_Channel_1 - ADC Channel1 selected.* ADC_Channel_2 - ADC Channel2 selected.* ADC_Channel_3 - ADC Channel3 selected.* ADC_Channel_4 - ADC Channel4 selected.* ADC_Channel_5 - ADC Channel5 selected.* ADC_Channel_6 - ADC Channel6 selected.* ADC_Channel_7 - ADC Channel7 selected.* ADC_Channel_8 - ADC Channel8 selected.* ADC_Channel_9 - ADC Channel9 selected.* ADC_Channel_10 - ADC Channel10 selected.* ADC_Channel_11 - ADC Channel11 selected.* ADC_Channel_12 - ADC Channel12 selected.* ADC_Channel_13 - ADC Channel13 selected.* ADC_Channel_14 - ADC Channel14 selected.* ADC_Channel_15 - ADC Channel15 selected.* ADC_Channel_16 - ADC Channel16 selected.* ADC_Channel_17 - ADC Channel17 selected.** @return val - The Data conversion value.*/
u16 Get_ADC_Average(u8 ch,u8 times)
{u32 temp_val=0;u8 t;u16 val;for(t=0;ttemp_val+=Get_ADC_Val(ch);Delay_Ms(5);}val = temp_val/times;return val;
}/********************************************************************** @fn Get_ConversionVal** @brief Get Conversion Value.** @param val - Sampling value** @return val+Calibrattion_Val - Conversion Value.*/
u16 Get_ConversionVal(s16 val)
{if((val+Calibrattion_Val)<0) return 0;if((Calibrattion_Val+val)>4095||val==4095) return 4095;return (val+Calibrattion_Val);
}/********************************************************************** @fn main** @brief Main program.** @return none*/
int main(void)
{u16 ADC_val;s32 val_mv;SystemCoreClockUpdate();Delay_Init();USART_Printf_Init(115200); printf("SystemClk:%d\r\n",SystemCoreClock);printf( "ChipID:%08x\r\n", DBGMCU_GetCHIPID() );ADC_Function_Init();printf("CalibrattionValue:%d\n", Calibrattion_Val); while(1){ADC_val = Get_ADC_Average( ADC_Channel_TempSensor, 10 );Delay_Ms(500);ADC_val = Get_ConversionVal(ADC_val);printf( "ADC-Val:%04d\r\n", ADC_val);val_mv = (ADC_val*3300/4096);printf("mv-T-%d,%0d\n",val_mv ,TempSensor_Volt_To_Temper(val_mv));Delay_Ms(2);}
}
开发板数据线连接电脑就可以开始连接调试🛹🛹🛹,首先开始编译,编译成功如下
然后下载,下载成功如下
根据程序设计调试,可以用手指触摸芯片,即可输出相关温度变化
代码下载后验证,点击串口调试器,设置串口参数确认。
复位运行成功如下打印温度变化,从12度到15度
SystemClk:96000000
ChipID:30700518
CalibrattionValue:9ADC-Val:1786
mv-T-1438,12ADC-Val:1781
mv-T-1434,13ADC-Val:1777
mv-T-1431,13ADC-Val:1775
mv-T-1430,13ADC-Val:1773
mv-T-1428,14ADC-Val:1772
mv-T-1427,14ADC-Val:1771
mv-T-1426,14ADC-Val:1770
mv-T-1426,14ADC-Val:1768
mv-T-1424,15ADC-Val:1768
mv-T-1424,15ADC-Val:1769
mv-T-1425,15ADC-Val:1768
mv-T-1424,15ADC-Val:1767
mv-T-1423,15ADC-Val:1767
mv-T-1423,15ADC-Val:1767
🥳🥳🥳通过对这篇文章我们掌握了沁恒WCH CH32V307V-R1开发板读取板载温度实验,这样就可以设置适当工作环境防寒暑,确保设备正常接下来会有许多有趣的实验,尝试与Arduino通讯做更加好玩的实验,进而丰富我们的生活。🛹🛹🛹从而实现对外部世界进行感知,充分认识这个有机与无机的环境,🥳🥳🥳科学地合理地进行创作和发挥效益,然后为人类社会发展贡献一点微薄之力。🤣🤣🤣
参考文献: