1) ADC多通道采集:(多通道采集必須用掃描模式,掃描模式時(shí)規(guī)則組的通道共用一個(gè)寄存器,so必須用DMA傳輸;為防止數(shù)據(jù)錯(cuò)位,不能用連續(xù)模式,而應(yīng)在查詢或中斷中先關(guān)閉ADC轉(zhuǎn)換,再開(kāi)啟ADC轉(zhuǎn)換)so應(yīng)采用掃描、非連續(xù)的方式、DMA正常模式,DMA的EOC中斷或查詢;
上述的配置下,ADC運(yùn)行流程如下:
每個(gè)規(guī)則通道采集之后,每個(gè)ADC_DR會(huì)更新,這個(gè)更新會(huì)啟動(dòng)一次DMA,同時(shí)會(huì)產(chǎn)生EOC,然后DMA會(huì)傳輸此數(shù)據(jù),DMA的傳輸會(huì)清除EOC標(biāo)志,然后DMA的傳輸量計(jì)數(shù)器--,so,每一組規(guī)則通道轉(zhuǎn)換完成后也不會(huì)產(chǎn)生EOC(詳情看英文版STM32手冊(cè)V15版本-Page220,而非V9版本),so利用這個(gè)EOC產(chǎn)生ADC中斷或查詢時(shí),標(biāo)志位已為RESET,so根本就辦不到,但可利用DMA正常模式,DMA_EOC標(biāo)志被置位在中斷或查詢中ADC轉(zhuǎn)換先關(guān)閉,對(duì)應(yīng)的DMACmd關(guān)閉,然后重新寫入DMA的傳輸數(shù)據(jù)量CNDTR,再打開(kāi)DMACmd,再清除DMA標(biāo)志位,再打開(kāi)ADC轉(zhuǎn)換,同時(shí),存儲(chǔ)模擬量的變量或數(shù)組需要用volatile修飾
DMA查詢的代碼如下
if(DMA_GetFlagStatus(DMA1_FLAG_TC1)!=RESET) //?D??í¨μà1′?ê?íê3é
{
ADC_SoftwareStartConvCmd(ADC1,DISABLE);
DMA_Cmd(DMA1_Channel1,DISABLE);
DMA_SetCurrDataCounter(ADC1_DMA_CHANNLE,ADC1_DMA_BUFFER_SIZE); DMA_Cmd(DMA1_Channel1,ENABLE);
DMA_ClearFlag(DMA1_FLAG_TC1);//
ADC_SoftwareStartConvCmd(ADC1,ENABLE);
}
同時(shí)也可以ADC1、ADC3同時(shí)使用,ADC3利用PF6-9,可復(fù)用為Channel4-7,頁(yè)要為掃描模式,非連續(xù)模式;代碼如下:
ADC_InitStructure.ADC_NbrOfChannel = 4;
ADC_Init(ADC3, &ADC_InitStructure);
ADC_RegularChannelConfig(ADC3,ADC_Channel_4,1,ADC_SampleTime_239Cycles5);
ADC_RegularChannelConfig(ADC3,ADC_Channel_5, 2, ADC_SampleTime_239Cycles5 ); ADC_RegularChannelConfig(ADC3,ADC_Channel_6, 3, ADC_SampleTime_239Cycles5 );
ADC_RegularChannelConfig(ADC3, ADC_Channel_7, 4,ADC_SampleTime_239Cycles5 );
ADC_DMACmd(ADC3, ENABLE);
ADC_Cmd(ADC3,ENABLE);
ADC_ResetCalibration(ADC3);
while(ADC_GetResetCalibrationStatus(ADC3));
ADC_StartCalibration(ADC3);
while(ADC_GetCalibrationStatus(ADC3));
ADC_SoftwareStartConvCmd(ADC3, ENABLE);