因系統要求需升級ADC的采樣方式(以前方式:掃描方式,TIMER2觸發ADC軟啟動,2通道規則序列,DMA傳完中斷),為了進一步實現兩路信號的同步性能,采樣STM32 雙ADC同步規則轉換。(timer2觸發ADC軟啟動,2個單通道規則序列,DMA完成中斷)
問題:
1:只能看到ADC1通道的數據。ADC2通道的數據貌似在ADC1_DR中的高16位中無法看到。
============================
//初始化DMA_ADC
void ADCWithDMAInit(void)
{
//DMA init;??Using DMA channel 1
DMA_InitTypeDef DMA_InitStructure;
/* 允許 DMA1 */
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE);
/* DMA1 channel1 configuration ----------------------------------------------*/
DMA_DeInit(DMA1_Channel1);
DMA_InitStructure.DMA_PeripheralBaseAddr = ADC1_DR_Address;
DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)&VOLT_SIGNAL;
DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC;
DMA_InitStructure.DMA_BufferSize =1;
DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;
DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Word;// 全字節
DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Word;? ? ? ???// 全字節
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 DMA1 channel1 */
//DMA_Cmd(DMA1_Channel1, ENABLE);
DMA_ITConfig(DMA1_Channel1, DMA_IT_TC, ENABLE); //DMA1傳輸完成中斷
DMA_Cmd(DMA1_Channel1, ENABLE); //正式允許DMA
}
void ADC_Config(void)
{
ADC_InitTypeDef ADC_InitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
TIM_TimeBaseInitTypeDef? ?TIM_TimeBaseStructure;
//? ? ? ? TIM_OCInitTypeDef? ?? ?? ?TIM_OCInitStructure;
//? ? ? ? DMA_InitTypeDef DMA_InitStructure;
ADCWithDMAInit();
//DMA 中斷設置
NVIC_InitStructure.NVIC_IRQChannel=DMA1_Channel1_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority=0;
NVIC_InitStructure.NVIC_IRQChannelCmd=ENABLE;
NVIC_Init(&NVIC_InitStructure);
/*******************TIM**************/
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_GPIOC,ENABLE);??//GPIOAC時鐘
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2,ENABLE); //timer2時鐘
TIM_TimeBaseStructure.TIM_Period =7031;? ?? ?? ?//1.408khz
TIM_TimeBaseStructure.TIM_Prescaler = 9;
TIM_TimeBaseStructure.TIM_ClockDivision = 0x0;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
//TIM_TimeBaseStructure.TIM_RepetitionCounter =0;
TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);
/* Enable the TIM2 global Interrupt */
NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 6;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 6;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
//使能Time2 更新中斷。即溢出中斷
//TIM_ITConfig(TIM1, TIM_IT_Update, ENABLE);
TIM_ITConfig(TIM2, TIM_IT_Update, ENABLE);
/* Configure and enable ADC interrupt */
//NVIC_InitStructure.NVIC_IRQChannel = ADC1_2_IRQn;
//NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
//NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
//NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
//NVIC_Init(&NVIC_InitStructure);
/*******************ADC**************/
RCC_ADCCLKConfig(RCC_CFGR_ADCPRE_DIV6);//ADC時鐘PLCK2的6分頻 12M。ADC最大時鐘不能超過14M!
RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1|RCC_APB2Periph_ADC2, ENABLE);
//A口有1個通道
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA,&GPIO_InitStructure);
ADC_InitStructure.ADC_Mode = ADC_Mode_RegSimult;? ?? ? //ADC1工作在獨立模式 同步規則模式
ADC_InitStructure.ADC_ScanConvMode =DISABLE;? ?? ?? ?? ? //使能模數轉換工作在掃描模式(多通道)
ADC_InitStructure.ADC_ContinuousConvMode = DISABLE;? ?? ? //模數轉換工作在正常模式
ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None;? ? //軟啟動觸發啟動
ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;? ?//ADC數據右對齊
ADC_InitStructure.ADC_NbrOfChannel = 1;? ?? ?? ?? ?? ?? ?//順序進行規則轉換的ADC通道的數目
ADC_Init(ADC1, &ADC_InitStructure);? ?? ?? ?? ?? ?? ?? ? //根據ADC_InitStructure中指定的參數初始化外設ADC1的寄存?
//ADC_Init(ADC2, &ADC_InitStructure);
/* Enable ADC1 EOC interrupt */
//ADC_ITConfig(ADC1, ADC_IT_EOC, ENABLE);
//設置指定ADC的規則組通道,設置它們的轉化順序和采樣時間(取最大值)//增加通道到規則隊列中
ADC_RegularChannelConfig(ADC1, ADC_Channel_1 , 1, ADC_SampleTime_239Cycles5); //PA1
//C口有1個通道
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOC,&GPIO_InitStructure);
ADC_InitStructure.ADC_Mode = ADC_Mode_RegSimult;? ?? ? //ADC1工作在獨立模式 同步規則模式
ADC_InitStructure.ADC_ScanConvMode =DISABLE;? ?? ?? ?? ? //使能模數轉換工作在掃描模式(多通道)
ADC_InitStructure.ADC_ContinuousConvMode = DISABLE;? ?? ? //模數轉換工作在正常模式
ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None;? ? //軟啟動觸發啟動
ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;? ?//ADC數據右對齊
ADC_InitStructure.ADC_NbrOfChannel = 1;? ?? ?? ?? ?? ?? ?//順序進行規則轉換的ADC通道的數目
\
ADC_Init(ADC2, &ADC_InitStructure);? ?? ?? ?? ?? ?? ?? ? //根據ADC_InitStructure中指定的參數初始化外設ADC1的寄存?
ADC_RegularChannelConfig(ADC2, ADC_Channel_12 , 1, ADC_SampleTime_239Cycles5);//PC2
/* Enable ADC2 external trigger conversion */
ADC_ExternalTrigConvCmd(ADC2, ENABLE);
/* Enable ADC1 DMA*/
ADC_DMACmd(ADC1, ENABLE);
/* Enable ADC1 external trigger */
//ADC_ExternalTrigConvCmd(ADC1, ENABLE);
/* Enable ADC1 */
ADC_Cmd(ADC1, ENABLE);
/* Enable ADC2 */
ADC_Cmd(ADC2, ENABLE);
/* Enable ADC1 reset calibaration register */
ADC_ResetCalibration(ADC1);
/* Check the end of ADC1 reset calibration register */
while(ADC_GetResetCalibrationStatus(ADC1));
/* Start ADC1 calibaration */
ADC_StartCalibration(ADC1);
/* Check the end of ADC1 calibration */
while(ADC_GetCalibrationStatus(ADC1));
/* TIM1 counter enable */
TIM_Cmd(TIM2, ENABLE);
/* TIM1 main Output Enable */
//TIM_CtrlPWMOutputs(TIM2, ENABLE);
//for()
}