您好,歡迎訪問上?,F易電子元器件有限公司網站!
第一個為非DMA模式的多通道采集
void ADC_DMA_Config(void)
{
ADC_InitTypeDef ADC_InitStructure;
RCC_HSICmd(ENABLE);
while(RCC_GetFlagStatus(RCC_FLAG_HSIRDY) == RESET);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE);
ADC_DeInit(ADC1);
ADC_StructInit(&ADC_InitStructure);
ADC_InitStructure.ADC_Resolution = ADC_Resolution_8b;
ADC_InitStructure.ADC_ScanConvMode = DISABLE; //禁止掃描模式
ADC_InitStructure.ADC_ContinuousConvMode = ENABLE; //連續(xù)轉換
ADC_InitStructure.ADC_ExternalTrigConvEdge = ADC_ExternalTrigConvEdge_None;
ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;
ADC_InitStructure.ADC_NbrOfConversion = 1;
ADC_Init(ADC1,&ADC_InitStructure);
/*沒有采用中斷和DMA方式,這樣采樣得到的數據很容易丟失,因為規(guī)則通道公用一個數據寄存器,因此需要在一個轉換周期內讀出數據,緊接著讀取下一個通道的數據。*/
ADC_DelaySelectionConfig(ADC1, ADC_DelayLength_Freeze);//推遲到轉換后的數據被讀取
ADC_PowerDownCmd(ADC1, ADC_PowerDown_Idle_Delay, ENABLE);//在延遲和空閑階段ADC是關閉的
/* Enable ADC1 */
ADC_Cmd(ADC1, ENABLE);
/* Wait until the ADC1 is ready */
while(ADC_GetFlagStatus(ADC1, ADC_FLAG_ADONS) == RESET)
{
}
}
uint16_t GetADCValue(uint32_t CH_Data )//獲取通道數據
{
unsigned char i;
for(i=0; i<3; i++)
{
AD_Value[i] = 0xFF;
switch (i)
{
case 0: ADC_RegularChannelConfig(ADC1, ADC_Channel_2, 1, ADC_SampleTime_9Cycles); break;
case 1: ADC_RegularChannelConfig(ADC1, ADC_Channel_3, 1, ADC_SampleTime_9Cycles); break;
default: ADC_RegularChannelConfig(ADC1, ADC_Channel_3, 1, ADC_SampleTime_9Cycles); break;
}
ADC_Cmd(ADC1, ENABLE);
// ADC_TempSensorVrefintCmd(ENABLE);
ADC_SoftwareStartConv(ADC1);
while (ADC_GetFlagStatus(ADC1, ADC_FLAG_EOC) == RESET)
AD_Value[i] = ADC_GetConversionValue(ADC1);
ADC_ClearFlag(ADC1, ADC_FLAG_EOC);
ADC_SoftwareStartConv(ADC1);
ADC_Cmd(ADC1, DISABLE);
}
return AD_Value[CH_Data];
}
第二個為dma多通道采樣與注入采樣
__IO uint16_t ADC_ConvertedValue[2];//內存緩存adc通道數據
void ADC1_Mode_Config(void)
{
DMA_InitTypeDef DMA_InitStructure;
ADC_InitTypeDef ADC_InitStructure;
/* DMA channel1 configuration */
DMA_DeInit(DMA1_Channel1);
DMA_InitStructure.DMA_PeripheralBaseAddr = ADC1_DR_Address;//ADC地址
DMA_InitStructure.DMA_MemoryBaseAddr = (u32)&ADC_ConvertedValue;//內存地址
DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC;
DMA_InitStructure.DMA_BufferSize = 2;
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);
/* Enable DMA channel1 */
DMA_Cmd(DMA1_Channel1, ENABLE);
/* ADC1 configuration */
ADC_InitStructure.ADC_Mode = ADC_Mode_Independent;//獨立ADC模式
ADC_InitStructure.ADC_ScanConvMode = ENABLE ; //開掃描模式
ADC_InitStructure.ADC_ContinuousConvMode = ENABLE;//連續(xù)轉換
ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None;//軟件觸發(fā)
ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;
ADC_InitStructure.ADC_NbrOfChannel = 2;//轉換通道數目
ADC_Init(ADC1, &ADC_InitStructure);
ADC_TempSensorVrefintCmd(ENABLE);
ADC_RegularChannelConfig(ADC1, ADC_Channel_TempSensor, 1, ADC_SampleTime_55Cycles5);
ADC_RegularChannelConfig(ADC1, ADC_Channel_11, 2, ADC_SampleTime_55Cycles5);
ADC_InjectedSequencerLengthConfig(ADC1, 1); //注入通道與其長度
ADC_InjectedChannelConfig(ADC1,ADC_Channel_11,1,ADC_SampleTime_7Cycles5);
// ADC_SetInjectedOffset(ADC1, ADC_InjectedChannel_1, 1000);//設置偏置電壓值
ADC_ExternalTrigInjectedConvConfig(ADC1, ADC_ExternalTrigInjecConv_None);
ADC_ExternalTrigConvCmd(ADC1, DISABLE); //禁止外部觸發(fā)
ADC_ExternalTrigInjectedConvCmd(ADC1, DISABLE);
//ADC_AutoInjectedConvCmd(ADC1,ENABLE); //規(guī)則通道自動轉換到注入通道
/* Enable ADC1 DMA */
ADC_DMACmd(ADC1, ENABLE);
/* Enable ADC1 */
ADC_Cmd(ADC1, ENABLE);
ADC_ResetCalibration(ADC1);
while(ADC_GetResetCalibrationStatus(ADC1));
ADC_StartCalibration(ADC1);
while(ADC_GetCalibrationStatus(ADC1));
ADC_SoftwareStartConvCmd(ADC1, ENABLE);
}
由下面代碼可以得到注入通道ad值
ADC_SoftwareStartInjectedConvCmd(ADC1, ENABLE);//啟動adc注入通道
while(RESET ==ADC_GetFlagStatus(ADC1,ADC_FLAG_JEOC));//等待轉換完成
value=ADC_GetInjectedConversionValue(ADC1,ADC_InjectedChannel_1);//得到第一組的值
終于把ADC的非DMA的多通道采集完成,也是參考的網上的例子,學會了很多。其中要根據自己需求注意ADC是掃描模式,ADC是單次轉換還是連續(xù)轉換,還有ADC的模式也要注意。覺得ADC很博大精深,自己還得好好研究一番。
上?,F易電子元器件有限公司 版權所有 未經授權禁止復制或鏡像
CopyRight 2020-2025 29711999.com.cn All rights reserved 滬ICP備2020031792號