CH32F103C8 ADC采样问题?

定时器触发3个规则通道连续转化带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内存基地址

DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC;  //数据传输方向,从外设读取发送到内存

DMA_InitStructure.DMA_BufferSize = cndtr;  //DMA通道的DMA缓存的大小

DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;  //外设地址寄存器不变

DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;  //内存地址寄存器递增

DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord;  //数据宽度为16位

DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord; //数据宽度为16位

DMA_InitStructure.DMA_Mode = DMA_Mode_Circular;  //工作在循环模式

DMA_InitStructure.DMA_Priority = DMA_Priority_VeryHigh; //DMA通道 x拥有最高优先级 

DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;  //DMA通道x禁用设置为内存到内存传输

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 );

}

这样设置可以吗?

您好,附件为相关例程,你可以对比参考一下。

icon_rar.gifCH32F103 ADC-DMA-定时器触发中断,多通道.zip



只有登录才能回复,可以选择微信账号登录