stm32 ADC的規(guī)則通道和注入通道混合使用
1 利用外部觸發(fā)或通過設(shè)置ADC_CR2寄存器的ADON位,啟動一組規(guī)則通道的轉(zhuǎn)換。 2 如果在規(guī)則通道轉(zhuǎn)換期間產(chǎn)生一外部注入觸發(fā),當(dāng)前轉(zhuǎn)換被復(fù)位,注入通道序列被以單次掃描方式進行轉(zhuǎn)換。 3 然后,恢復(fù)上次被中斷的規(guī)則組通道轉(zhuǎn)換。如果在注入轉(zhuǎn)換期間產(chǎn)生一規(guī)則事件,注入轉(zhuǎn)換不會被中斷,但是規(guī)則序列將在注入序列結(jié)束后被執(zhí)行。
1 ADC_InjectedSequencerLengthConfig(ADC1, 1);\設(shè)置注入通道長度2 ADC_InjectedChannelConfig(ADC1,ADC_Channel_10,1,ADC_SampleTime_7Cycles5);\配置注入通道3 ADC_SoftwareStartInjectedConvCmd(ADC1, ENABLE);\開始注入通道數(shù)據(jù)采樣和轉(zhuǎn)換
ADC_ExternalTrigInjectedConvConfig(ADC1, ADC_ExternalTrigInjecConv_None);
如果設(shè)置了 JAUTO 位,在規(guī)則組通道之后,注入組通道被自動轉(zhuǎn)換。這可以用來轉(zhuǎn)換在 ADC_SQRx 和 ADC_JSQR 寄存器中設(shè)置的多至 20 個轉(zhuǎn)換序列。
查了下文檔,只有在規(guī)則通道的轉(zhuǎn)換結(jié)束時才產(chǎn)生 DMA 請求,并將轉(zhuǎn)換的數(shù)據(jù)從 ADC_DR 寄存器傳輸?shù)接脩糁付ǖ哪康牡刂?,還有注入方式轉(zhuǎn)換后數(shù)據(jù)存儲到 ADC_DRJx寄存器和規(guī)則方式轉(zhuǎn)換后數(shù)據(jù)存儲在ADC_DR寄存器中。
本文引用地址:http://www.ex-cimer.com/article/201611/317106.htm
- staticvoidProtect_AdcInit(void)
- {
- ADC_InitTypeDefADC_InitStructure;
- ADC_InitStructure.ADC_Mode=ADC_Mode_Independent;
- ADC_InitStructure.ADC_ScanConvMode=ENABLE;
- ADC_InitStructure.ADC_ContinuousConvMode=DISABLE;
- ADC_InitStructure.ADC_ExternalTrigConv=ADC_ExternalTrigConv_None;//軟件觸發(fā)
- ADC_InitStructure.ADC_DataAlign=ADC_DataAlign_Right;
- ADC_InitStructure.ADC_NbrOfChannel=2;//規(guī)則通道的數(shù)量
- ADC_Init(ADC1,&ADC_InitStructure);//這個大部分是初始化規(guī)則通道的
- ADC_TempSensorVrefintCmd(ENABLE);
- ADC_RegularChannelConfig(ADC1,ADC_Channel_TempSensor,1,ADC_SampleTime_239Cycles5);
- ADC_RegularChannelConfig(ADC1,ADC_Channel_Vrefint,2,ADC_SampleTime_239Cycles5);
- ADC_InjectedSequencerLengthConfig(ADC1,1);
- ADC_InjectedChannelConfig(ADC1,ADC_Channel_10,1,ADC_SampleTime_7Cycles5);
- ADC_ExternalTrigInjectedConvConfig(ADC1,ADC_ExternalTrigInjecConv_None);//設(shè)置規(guī)則通道軟件觸發(fā)
- /*Enableautomaticinjectedconversionstartafterregularone*/
- //ADC_AutoInjectedConvCmd(ADC1,ENABLE);
- ADC_DMACmd(ADC1,ENABLE);
- /*EnableADC1externaltrigger*/
- ADC_ExternalTrigConvCmd(ADC1,DISABLE);
- ADC_ExternalTrigInjectedConvCmd(ADC1,DISABLE);
- ADC_Cmd(ADC1,ENABLE);
- ADC_ResetCalibration(ADC1);
- while(ADC_GetResetCalibrationStatus(ADC1));
- ADC_StartCalibration(ADC1);
- while(ADC_GetCalibrationStatus(ADC1));
- }
======================沒完,繼續(xù)=================
讀取數(shù)據(jù)在這里:
/* 注入轉(zhuǎn)換中斷 */
void ADC1_2_IRQHandler(void)
{
s32 inj_v1,inj_v2,inj_v3,inj_v4;
if(ADC_GetITStatus(ADC1,ADC_IT_JEOC) == SET){
ADC_ClearITPendingBit(ADC1,ADC_IT_JEOC);
inj_v1 = ADC_GetInjectedConversionValue(ADC1,ADC_InjectedChannel_1);
inj_v1 += ADC_GetInjectedConversionValue(ADC2,ADC_InjectedChannel_4);
inj_v1 >>= 1;
inj_v2 = ADC_GetInjectedConversionValue(ADC1,ADC_InjectedChannel_2);
inj_v2 += ADC_GetInjectedConversionValue(ADC2,ADC_InjectedChannel_3);
inj_v2 >>= 1;
inj_v3 = ADC_GetInjectedConversionValue(ADC1,ADC_InjectedChannel_3);
inj_v3 += ADC_GetInjectedConversionValue(ADC2,ADC_InjectedChannel_2);
inj_v3 >>= 1;
inj_v4 = ADC_GetInjectedConversionValue(ADC1,ADC_InjectedChannel_4);
inj_v4 += ADC_GetInjectedConversionValue(ADC2,ADC_InjectedChannel_1);
inj_v4 >>= 1;
}
}
/* 規(guī)則轉(zhuǎn)換中斷 */
void DMA1_Channel1_IRQHandler(void)
{
if(DMA_GetFlagStatus(DMA1_FLAG_TC1) == SET){
DMA_ClearFlag(DMA1_FLAG_TC1);
DMA_ClearITPendingBit(DMA1_IT_GL1);
ADC_SoftwareStartConvCmd(ADC1,DISABLE);
DMA_Cmd(DMA1_Channel1,DISABLE);
/* ¼ÆËãת»»Öµ */
regular_convert_calc();
/* Æô¶¯ÏÂÒ»´Î´«Êä */
DMA1_Channel1->CNDTR = NUM_OF_REG_CHANNEL;
DMA_Cmd(DMA1_Channel1,ENABLE);
ADC_SoftwareStartConvCmd(ADC1,ENABLE);
}
}
還有這篇說的也不錯:用TIM1產(chǎn)生6路ADC,用CCR4觸發(fā)ADC1的注入通道采樣
評論