一、手册阅读
初始化时钟源采用6分频
ADC_CLKConfig(ADC1, ADC_CLK_Div6);
一个ADCCLK为 1/(48m/6) s = 1/8 us
设置的采样周期为11
ADC_RegularChannelConfig(ADC1, ch, 1, ADC_SampleTime_11Cycles);
TCONV = 采样时间 + 13TADCCLK
一个通道总的采样时间: 1/8us * (11+13) = 3us.
二、代码
#include "main.h"
/*********************************************************************
* @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);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;
GPIO_Init(GPIOA, &GPIO_InitStructure);
ADC_DeInit(ADC1);
ADC_CLKConfig(ADC1, ADC_CLK_Div6); // 采样时钟
ADC_InitStructure.ADC_Mode = ADC_Mode_Independent;
ADC_InitStructure.ADC_ScanConvMode = DISABLE; // 非扫描模式
ADC_InitStructure.ADC_ContinuousConvMode = DISABLE; // ENABLE; // 非连续采样
ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None; // 无外部触发
ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right; // 采样数据右对齐
ADC_InitStructure.ADC_NbrOfChannel = 2; // adc 通道数量
ADC_Init(ADC1, &ADC_InitStructure);
//ADC_DMACmd(ADC1, ENABLE); // 使能 DMA
ADC_Cmd(ADC1, 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.
*
* @return none
*/
u16 Get_ADC_Val(u8 ch)
{
u16 val;
ADC_RegularChannelConfig(ADC1, ch, 1, ADC_SampleTime_11Cycles);
ADC_SoftwareStartConvCmd(ADC1, ENABLE);
while(!ADC_GetFlagStatus(ADC1, ADC_FLAG_EOC));
val = ADC_GetConversionValue(ADC1);
return val;
}
// 验证采集第0个通道和第1个通道 两个通道的电压值
void test_ad_fun(void)
{
while(1){
u16 ad_0_val = 0;
u16 ad_1_val = 0;
Delay_Ms(100);
ad_0_val = Get_ADC_Val(0);
ad_1_val = Get_ADC_Val(1);
// Get_ADC_Val(3);
// Get_ADC_Val(4);
PRINT("ad 0 = %d,ad 1 = %d.\n",ad_0_val,ad_1_val);
}
}
三、调试
四、验证
给通道0的引脚接入0.4v 电压 验证打印出来ad值为330 左右
ADC是12bit精度 参考电压就是供电电压 我用usb供电所以参考电压就是5v
五、沁恒微的一个坑,,
部分批次的IC 不支持ADC通道3,5,11等还有不支持 IIC
————————————————
版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
原文链接:https://blog.csdn.net/qq_28851611/article/details/136349734
|