第一個為非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ù)轉(zhuǎn)換
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方式,這樣采樣得到的數(shù)據(jù)很容易丟失,因為規(guī)則通道公用一個數(shù)據(jù)寄存器,因此需要在一個轉(zhuǎn)換周期內(nèi)讀出數(shù)據(jù),緊接著讀取下一個通道的數(shù)據(jù)。*/
ADC_DelaySelectionConfig(ADC1, ADC_DelayLength_Freeze);//推遲到轉(zhuǎn)換后的數(shù)據(jù)被讀取
ADC_PowerDownCmd(ADC1, ADC_PowerDown_Idle_Delay, ENABLE);//在延遲和空閑階段ADC是關(guān)閉的
/* 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 )//獲取通道數(shù)據(jù)
{
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];//內(nèi)存緩存adc通道數(shù)據(jù)
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;//內(nèi)存地址
DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC;
DMA_InitStructure.DMA_BufferSize = 2;
DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;//外設(shè)地址固定
DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable; //內(nèi)存地址后移
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ù)轉(zhuǎn)換
ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None;//軟件觸發(fā)
ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;
ADC_InitStructure.ADC_NbrOfChannel = 2;//轉(zhuǎn)換通道數(shù)目
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);//設(shè)置偏置電壓值
ADC_ExternalTrigInjectedConvConfig(ADC1, ADC_ExternalTrigInjecConv_None);
ADC_ExternalTrigConvCmd(ADC1, DISABLE); //禁止外部觸發(fā)
ADC_ExternalTrigInjectedConvCmd(ADC1, DISABLE);
//ADC_AutoInjectedConvCmd(ADC1,ENABLE); //規(guī)則通道自動轉(zhuǎn)換到注入通道
/* 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));//等待轉(zhuǎn)換完成
value=ADC_GetInjectedConversionValue(ADC1,ADC_InjectedChannel_1);//得到第一組的值
終于把ADC的非DMA的多通道采集完成,也是參考的網(wǎng)上的例子,學(xué)會了很多。其中要根據(jù)自己需求注意ADC是掃描模式,ADC是單次轉(zhuǎn)換還是連續(xù)轉(zhuǎn)換,還有ADC的模式也要注意。覺得ADC很博大精深,自己還得好好研究一番。
上海意泓電子科技有限責(zé)任公司 版權(quán)所有 未經(jīng)授權(quán)禁止復(fù)制或鏡像
CopyRight 2020-2025 www.toastedesign.com All rights reserved 滬ICP備2021005866號