2019-07-20 10:26发布
正点原子 发表于 2017-12-18 14:01 1,ADC和定时器没有必然联系,所以,他们不相干。 2,ADC采样频率,由mcu主频、ADC分频,采样周期共同决定 ...
最多设置5个标签!
ADC推荐使用DMA方式来进行.
给你一个初始化例程
void adcConfig(void)
{
ADC_InitTypeDef ADC_InitStructure;
DMA_InitTypeDef DMA_InitStructure;
ADC_DeInit(ADC1);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1,ENABLE);
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1,ENABLE);
DMA_DeInit(DMA1_Channel1);
DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t) &ADC1->DR;
DMA_InitStructure.DMA_MemoryBaseAddr = (ui)adcbuf;
DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC;
DMA_InitStructure.DMA_BufferSize = 8; //8级缓冲区,将来用来求取平均值
DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;
DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord;
DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord;
DMA_InitStructure.DMA_Mode = DMA_Mode_Circular;
DMA_InitStructure.DMA_Priority = DMA_Priority_High;
DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;
DMA_Init(DMA1_Channel1,&DMA_InitStructure);
DMA_Cmd(DMA1_Channel1,ENABLE);
ADC_DMARequestModeConfig(ADC1,ADC_DMAMode_Circular);
ADC_DMACmd(ADC1,ENABLE);
ADC_StructInit(&ADC_InitStructure);
ADC_InitStructure.ADC_Resolution = ADC_Resolution_12b; //12位精度
ADC_InitStructure.ADC_ContinuousConvMode = ENABLE; //ADC连续转换模式
ADC_InitStructure.ADC_ExternalTrigConvEdge = ADC_ExternalTrigConvEdge_None; //无外部触发模式
ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right; //右对齐方式
ADC_InitStructure.ADC_ScanDirection = ADC_ScanDirection_Upward; //扫描方向
ADC_Init(ADC1,&ADC_InitStructure);
ADC_ChannelConfig(ADC1,ADC_Channel_5,ADC_SampleTime_71_5Cycles); // 精度影响结果.低精度的不可靠.
ADC_GetCalibrationFactor(ADC1);
ADC_Cmd(ADC1,ENABLE);
while (!ADC_GetFlagStatus(ADC1,ADC_FLAG_ADEN));
ADC_StartOfConversion(ADC1);
}
//ADC_SampleTime_71_5Cycles 这个参数不宜太小..否则,精度不够.
主频经过使能时钟之后,变成84M,然后进行4分频,ADC时钟变成21M,然后ADC时钟/采样周期是不是等于我的采样频率呢?是的话,采样周期跟那几个参数(库函数版本)有关呢?
一周热门 更多>