CH32V208的ADC_IN1在PA1~4引脚,读取开发板VCC引脚ADC值和GND引脚ADC值,具体程序如下:
adc.h文件
#ifndef USER_ADC_H_
#define USER_ADC_H_
#define ADC_CHANNEL_NUM 4
#define ADC_DATA_length 16
void ADC_Function_Init(void);
void DMA_Tx_Init(void);
void ADC_DMA_CONF(void);
#endif
adc.c文件
#include "adc.h"
#include "debug.h"
/* Global Variable */
volatile u16 ADC_ConvertedValue[ADC_CHANNEL_NUM]={0};//[ADC_DATA_length]
/*********************************************************************
* @fn ADC_Function_Init
*
* [url=home.php?mod=space&uid=2666770]@Brief[/url] Initializes ADC collection.
*
* [url=home.php?mod=space&uid=1141835]@Return[/url] 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_Div6);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1|GPIO_Pin_2|GPIO_Pin_3|GPIO_Pin_4;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;
GPIO_Init(GPIOA, &GPIO_InitStructure);
ADC_DeInit(ADC1);
ADC_InitStructure.ADC_Mode = ADC_Mode_Independent;//配置ADC为独立模式
ADC_InitStructure.ADC_ScanConvMode = ENABLE; //多通道模式下开启扫描模式
ADC_InitStructure.ADC_ContinuousConvMode = ENABLE;//设置开启连续转换模式
ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None;//设置转换不是由外部触发启动,软件触发启动
ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;//设置ADC数据右对齐
ADC_InitStructure.ADC_NbrOfChannel = ADC_CHANNEL_NUM;//规则转换的ADC通道的数目
ADC_Init(ADC1, &ADC_InitStructure);
ADC_Cmd(ADC1, ENABLE);
// ADC_BufferCmd(ADC1, DISABLE); //disable buffer
ADC_ResetCalibration(ADC1);
while(ADC_GetResetCalibrationStatus(ADC1));
ADC_StartCalibration(ADC1);
while(ADC_GetCalibrationStatus(ADC1));
// Calibrattion_Val = Get_CalibrationValue(ADC1);
// ADC_BufferCmd(ADC1, ENABLE); //enable buffer
}
/*********************************************************************
* @fn Get_ADC_Val
*
* @brief Returns ADCx conversion result data.
*
* [url=home.php?mod=space&uid=3142012]@param[/url] 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 DMA_Tx_Init
*
* @brief Initializes the DMAy Channelx configuration.
*
* @param DMA_CHx - x can be 1 to 7.
* ppadr - Peripheral base address.
* memadr - Memory base address.
* bufsize - DMA channel buffer size.
*
* @return none
*/
void DMA_Tx_Init(void)
{
DMA_InitTypeDef DMA_InitStructure = {0};
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE);//使能开启DMA时钟
DMA_DeInit(DMA1_Channel1);//复位DMA控制器
DMA_InitStructure.DMA_PeripheralBaseAddr = (u32)&ADC1->RDATAR;//配置外设地址为ADC数据寄存器地址
DMA_InitStructure.DMA_MemoryBaseAddr = (u32)ADC_ConvertedValue;//配置存储器地址为读取ADC值地址
DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC;//配置数据源为外设,即DMA传输方式为外设到存储器
DMA_InitStructure.DMA_BufferSize = ADC_CHANNEL_NUM;//ADC_DATA_length*ADC_CHANNEL_NUM;//设置DMA数据缓冲区大小
DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;//设置DMA外设递增模式关闭
DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;//设置DMA存储器递增模式开启
DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord;//设置外设数据大小为半字,即两个字节
DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord;//设置存储器数据大小为半字,即两个字节
DMA_InitStructure.DMA_Mode = DMA_Mode_Circular;//设置DMA模式为循环传输模式
DMA_InitStructure.DMA_Priority = DMA_Priority_High;//设置DMA传输通道优先级为高,当使用一 DMA通道时,优先级设置不影响
DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;//因为此DMA传输方式为外设到存储器,因此禁用存储器到存储器传输方式
DMA_Init(DMA1_Channel1, &DMA_InitStructure);
DMA_Cmd(DMA1_Channel1 , ENABLE); //使能DMA
}
/*********************************************************************
* @fn Get_ConversionVal
*
* @brief Get Conversion Value.
*
* @param val - Sampling value
*
* @return val+Calibrattion_Val - Conversion Value.
*/
void ADC_DMA_CONF(void)
{
ADC_Function_Init();
DMA_Tx_Init();
// 配置 ADC 通道转换顺序为1,第一个转换,采样时间为55.5个时钟周期
ADC_RegularChannelConfig(ADC1, ADC_Channel_1, 1, ADC_SampleTime_55Cycles5);
ADC_RegularChannelConfig(ADC1, ADC_Channel_2, 2, ADC_SampleTime_55Cycles5);
ADC_RegularChannelConfig(ADC1, ADC_Channel_3, 3, ADC_SampleTime_55Cycles5);
ADC_RegularChannelConfig(ADC1, ADC_Channel_4, 4, ADC_SampleTime_55Cycles5);
ADC_DMACmd(ADC1, ENABLE); // 使能ADC DMA 请求
ADC_SoftwareStartConvCmd(ADC1, ENABLE); // 由于没有采用外部触发,所以使用软件触发ADC转换
}
通过以上配置,即可对VCC引脚及GND引脚ADC值进行读取。
main.c文件
#include "debug.h"
#include "iic.h"
#include "oled.h"
#include "ADC.h"
/* Global Variable */
extern u16 ADC_ConvertedValue[ADC_CHANNEL_NUM];
/*********************************************************************
*/
int main(void)
{
IIC_Init(400000, 0x02);
OLED_Init();
OLED_Clear(0);
float ADC_val0;
float ADC_val1;
float ADC_val2;
float ADC_val3;
Delay_Init();
ADC_DMA_CONF();
while(1)
{ ADC_val0 = (float)ADC_ConvertedValue[0];
ADC_val1 = (float)ADC_ConvertedValue[1];
ADC_val2 = (float)ADC_ConvertedValue[2];
ADC_val3 = (float)ADC_ConvertedValue[3];
OLED_ShowFloat(20,0,ADC_val0,2,16);
OLED_ShowFloat(20,2,ADC_val1,2,16);
OLED_ShowFloat(20,4,ADC_val2,2,16);
OLED_ShowFloat(20,6,ADC_val3,2,16);
Delay_Ms(500);
}
}
main.c文件主要进行相关函数的初始化以及输出通过ADC_IN1读取到的ADC值。