stm32 ADC的規(guī)則通道和注入通道混合使用
1 利用外部觸發(fā)或通過設置ADC_CR2寄存器的ADON位,啟動一組規(guī)則通道的轉換。 2 如果在規(guī)則通道轉換期間產生一外部注入觸發(fā),當前轉換被復位,注入通道序列被以單次掃描方式進行轉換。 3 然后,恢復上次被中斷的規(guī)則組通道轉換。如果在注入轉換期間產生一規(guī)則事件,注入轉換不會被中斷,但是規(guī)則序列將在注入序列結束后被執(zhí)行。
1 ADC_InjectedSequencerLengthConfig(ADC1, 1);\設置注入通道長度2 ADC_InjectedChannelConfig(ADC1,ADC_Channel_10,1,ADC_SampleTime_7Cycles5);\配置注入通道3 ADC_SoftwareStartInjectedConvCmd(ADC1, ENABLE);\開始注入通道數(shù)據采樣和轉換
ADC_ExternalTrigInjectedConvConfig(ADC1, ADC_ExternalTrigInjecConv_None);
如果設置了 JAUTO 位,在規(guī)則組通道之后,注入組通道被自動轉換。這可以用來轉換在 ADC_SQRx 和 ADC_JSQR 寄存器中設置的多至 20 個轉換序列。
查了下文檔,只有在規(guī)則通道的轉換結束時才產生 DMA 請求,并將轉換的數(shù)據從 ADC_DR 寄存器傳輸?shù)接脩糁付ǖ哪康牡刂?,還有注入方式轉換后數(shù)據存儲到 ADC_DRJx寄存器和規(guī)則方式轉換后數(shù)據存儲在ADC_DR寄存器中。
本文引用地址:http://m.butianyuan.cn/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);//設置規(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ù)據在這里:
/* 注入轉換中斷 */
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ī)則轉換中斷 */
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產生6路ADC,用CCR4觸發(fā)ADC1的注入通道采樣
評論