定時器觸發(fā)3個規(guī)則通道連續(xù)轉化帶DMA傳輸代碼如下:
void ADC_Function_Init(void)
{
ADC_InitTypeDef ADC_InitStructure;?
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE );??
? RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE );?
RCC_ADCCLKConfig(RCC_PCLK2_Div6);??
? ? ? ? ? ? ? ? ? ? ? ?
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;
GPIO_Init(GPIOA, &GPIO_InitStructure);
? ? ? ? ? ? ? ? ? ??
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;
GPIO_Init(GPIOA, &GPIO_InitStructure);
? ? ? ? ? ? ? ? ? ? ? ??
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;
GPIO_Init(GPIOA, &GPIO_InitStructure);
ADC_DeInit(ADC1);
ADC_InitStructure.ADC_Mode = ADC_Mode_Independent;
ADC_InitStructure.ADC_ScanConvMode = ENABLE;
ADC_InitStructure.ADC_ContinuousConvMode = ENABLE;
ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_T1_CC1;
ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;
ADC_InitStructure.ADC_NbrOfChannel = 3;
ADC_Init(ADC1, &ADC_InitStructure);
? //ADC_InjectedSequencerLengthConfig(ADC1, 3);?
ADC_RegularChannelConfig(ADC1, ADC_Channel_0, 1, ADC_SampleTime_239Cycles5);?
ADC_RegularChannelConfig(ADC1, ADC_Channel_1, 2, ADC_SampleTime_239Cycles5);?
ADC_RegularChannelConfig(ADC1, ADC_Channel_2, 3, ADC_SampleTime_239Cycles5);??
? ADC_DiscModeChannelCountConfig( ADC1, 3);
? ADC_ExternalTrigConvCmd(ADC1, ENABLE);
?
ADC_Cmd(ADC1, ENABLE);
? ADC_DMACmd(ADC1 , ENABLE);
ADC_ResetCalibration(ADC1);?
while(ADC_GetResetCalibrationStatus(ADC1));
ADC_StartCalibration(ADC1);?
while(ADC_GetCalibrationStatus(ADC1));
}
void DMA_Config(DMA_Channel_TypeDef* DMA_CHx,u32 cpar,u32 cmar,u16 cndtr)
{
? NVIC_InitTypeDef NVIC_InitStructure;
DMA_InitTypeDef DMA_InitStructure;
?RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE);//使能DMA傳輸
??
??
? NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);
? 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);
? DMA_DeInit(DMA_CHx);? ?//將DMA的通道1寄存器重設為缺省值
??
//DMA1_MEM_LEN=cndtr;
DMA_InitStructure.DMA_PeripheralBaseAddr = cpar;? //DMA外設基地址
DMA_InitStructure.DMA_MemoryBaseAddr = cmar;? //DMA內(nèi)存基地址
DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC;? //數(shù)據(jù)傳輸方向,從外設讀取發(fā)送到內(nèi)存
DMA_InitStructure.DMA_BufferSize = cndtr;? //DMA通道的DMA緩存的大小
DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;? //外設地址寄存器不變
DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;? //內(nèi)存地址寄存器遞增
DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord;? //數(shù)據(jù)寬度為16位
DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord; //數(shù)據(jù)寬度為16位
DMA_InitStructure.DMA_Mode = DMA_Mode_Circular;? //工作在循環(huán)模式
DMA_InitStructure.DMA_Priority = DMA_Priority_VeryHigh; //DMA通道 x擁有最高優(yōu)先級?
DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;? //DMA通道x禁用設置為內(nèi)存到內(nèi)存?zhèn)鬏?/p>
DMA_Init(DMA_CHx, &DMA_InitStructure);
?
? DMA_Cmd(DMA_CHx , ENABLE);
? DMA_ITConfig(DMA_CHx, DMA_IT_TC, ENABLE);
}
/*******************************************************************************
* Function Name? : TIM1_PWM_In
* Description? ? : Initializes TIM1 PWM output.
* Input? ? ? ? ? : arr: the period value.
*? ? ? ? ? ? ? ? ? psc: the prescaler value.
* ccp: the pulse value.
* Return? ? ? ? ?: None
*******************************************************************************/
void TIM1_PWM_In( u16 arr, u16 psc, u16 ccp )
{
GPIO_InitTypeDef GPIO_InitStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStructure;
RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOA,? ENABLE );
? RCC_APB2PeriphClockCmd(? RCC_APB2Periph_TIM1, ENABLE );
//GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8;
//GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
//GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
//GPIO_Init( GPIOA, &GPIO_InitStructure);
TIM_TimeBaseInitStructure.TIM_Period = arr;
TIM_TimeBaseInitStructure.TIM_Prescaler = psc;
TIM_TimeBaseInitStructure.TIM_ClockDivision = TIM_CKD_DIV1;
TIM_TimeBaseInitStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit( TIM1, &TIM_TimeBaseInitStructure);
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_Pulse = ccp;
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low;??
TIM_OC1Init( TIM1, &TIM_OCInitStructure );?
TIM_CtrlPWMOutputs(TIM1, ENABLE );
TIM_OC1PreloadConfig( TIM1, TIM_OCPreload_Disable );
TIM_ARRPreloadConfig( TIM1, ENABLE );
TIM_SelectOutputTrigger( TIM1, TIM_TRGOSource_Update );
TIM_Cmd( TIM1, ENABLE );
}
這樣設置可以嗎?