微波EDA网,见证研发工程师的成长!
首页 > 硬件设计 > 嵌入式设计 > STM32F10XXX 双通道ADC数据采集,TIM2触发采集

STM32F10XXX 双通道ADC数据采集,TIM2触发采集

时间:11-17 来源:互联网 点击:

aAlign_Right;//数据右对齐

ADC_InitStructure.ADC_NbrOfChannel = 1;

ADC_Init(ADC1 , &ADC_InitStructure);

/* ADC1 regular channel14 configuration */

ADC_RegularChannelConfig(ADC1, ADC_Channel_14, 1, ADC_SampleTime_1Cycles5);

/* Enable ADC1 DMA */

ADC_DMACmd(ADC1, ENABLE);

/* ADC2 configuration ---配置同ADC1--------------------------------------------*/

ADC_InitStructure.ADC_Mode = ADC_Mode_RegSimult;

ADC_InitStructure.ADC_ScanConvMode = DISABLE;

ADC_InitStructure.ADC_ContinuousConvMode = DISABLE;

ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_T2_CC2;

ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;

ADC_InitStructure.ADC_NbrOfChannel = 1;

ADC_Init(ADC2, &ADC_InitStructure);

/* ADC2 regular channels configuration */

ADC_RegularChannelConfig(ADC2, ADC_Channel_11, 1, ADC_SampleTime_1Cycles5);

/* Enable ADC2 external trigger conversion */

ADC_ExternalTrigConvCmd(ADC2, ENABLE);

/* Enable ADC1 */

ADC_Cmd(ADC1, ENABLE);

/* Enable ADC1 reset calibaration register 校准ADC1*/

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

/* Enable ADC2 */

ADC_Cmd(ADC2, ENABLE);

/* Enable ADC2 reset calibaration register 校准ADC2*/

ADC_ResetCalibration(ADC2);

/* Check the end of ADC2 reset calibration register */

while(ADC_GetResetCalibrationStatus(ADC2));

/* Start ADC2 calibaration */

ADC_StartCalibration(ADC2);

/* Check the end of ADC2 calibration */

while(ADC_GetCalibrationStatus(ADC2));

/* Test on channel1 transfer complete flag */

//while(!DMA_GetFlagStatus(DMA_FLAG_TC1));

/* Clear channel1 transfer complete flag */

//DMA_ClearFlag(DMA_FLAG_TC1);

ADC_ExternalTrigConvCmd( ADC1, ENABLE);

}

/*******************************************************************************

* Function Name : ADC_Sample_Frequency_Set

* Description : 根据输入的频率设置,TIM2_CC2产生相应的频率

* 用来控制ADC的采样,Frequency=1000000/(Prescaler+1)来产生

* 因此有些频率计算不准确,一般频率为2或5的倍数才准确

* 频率范围为16Hz~1000,000Hz

* Input : u16 Frequency:输入所需要的采样频率

*******************************************************************************/

void ADC_Sample_Frequency_Set(u32 Frequency)

{

TIM_OCInitTypeDef TIM_OCInitStructure;

TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;

RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE );

TIM_Cmd(TIM2, DISABLE);//先停止TIM2时钟,以准备下面的设置

/* -----------------------------------------------------------------------

TIM2 配置: 产生TIM2_CC2时钟控制信号用于控制ADC采样

TIM2CLK =72 MHz

TIM2 ARR Register = 35 => TIM3 Frequency = (TIM3 counter clock/(ARR + 1))/2

TIM2 Frequency = 1000 KHz.

----------------------------------------------------------------------- */

/* Time base configuration */

TIM_TimeBaseStructure.TIM_Period = 35; //APR寄存器

TIM_TimeBaseStructure.TIM_Prescaler = 1000000/Frequency-1; //预分频值,用来调整频率,分频系数=1000khz/(prescaler+1)

TIM_TimeBaseStructure.TIM_ClockDivision = 0;

TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;

TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);

/* TIM_OCMode_Toggle Mode configuration: Channel2 */

TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1 ;//TIM_OCMode_Toggle,TIM_OCMode_PWM1

TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;

TIM_OCInitStructure.TIM_Pulse = TIM_TimeBaseStructure.TIM_Period; //CCR2寄存器

TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low;

TIM_OC2Ini

Copyright © 2017-2020 微波EDA网 版权所有

网站地图

Top