在做
STM32L151 多路AD采集时。
AD的初始化:
void ADC_Config(void)
{
/* Enable The HSI (16Mhz) */
RCC_HSICmd(ENABLE);
/* Check that HSI oscillator is ready */
while(RCC_GetFlagStatus(RCC_FLAG_HSIRDY) == RESET);
/* Configure RV3 input voltage */
/* Enable the GPIOF Clock */
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);
/* Configure PF.11 (ADC channel 1b) or PA.05 (ADC channel 5) in analog mode */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0|GPIO_Pin_7;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_Init(GPIOA, GPIO_InitStructure);
/* ADC1 Configura
tion ------------------------------------------------------*/
RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE);
/* ADC1 DeInit */
ADC_DeInit(ADC1);
#ifdef USE_STM32L152D_EVAL
/* Select ADC Bank channel */
ADC_BankSelection(ADC1, ADC_Bank_B);
#endif
/* ADC1 Configuration of channel18/31 and channel5/1b : continuous mode, external trigger (TIM2) */
ADC_InitStructure.ADC_Resolution = ADC_Resolution_12b;
ADC_InitStructure.ADC_ScanConvMode = ENABLE;
ADC_InitStructure.ADC_ContinuousConvMode = ENABLE;
ADC_InitStructure.ADC_ExternalTrigConvEdge = ADC_ExternalTrigConvEdge_Falling;
ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_T2_TRGO;
ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;
ADC_InitStructure.ADC_NbrOfConversion = 2;
ADC_Init(ADC1, ADC_InitStructure);
/* ADC1 Regular Channel Config */
ADC_RegularChannelConfig(ADC1, ADC_Channel_0, 1, ADC_SampleTime_192Cycles);
ADC_RegularChannelConfig(ADC1, ADC_Channel_7, 2, ADC_SampleTime_192Cycles);
/* Enables the ADC1 Power Down during Delay */
ADC_PowerDownCmd(ADC1, ADC_PowerDown_Idle_Delay, ENABLE);
/* Delay until the converted data has been read */
ADC_DelaySelectionConfig(ADC1, ADC_DelayLength_Freeze);
/* Enable ADC1 */
ADC_Cmd(ADC1, ENABLE);
/* Wait until the ADC1 is ready */
while(ADC_GetFlagStatus(ADC1, ADC_FLAG_ADONS) == RESET)
{
}
}
T2的初始化:
void TIM2_Config(void)
{
/* Enable TIM2 clock */
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);
TIM_DeInit(TIM2);
TIM_TimeBaseStructInit( TIM_TimeBaseStructure);
/* Time base configuration */
TIM_TimeBaseStructure.TIM_Period = 0xFF;
TIM_TimeBaseStructure.TIM_Prescaler = 0x1;
TIM_TimeBaseStructure.TIM_ClockDivision = 0x0;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM2, TIM_TimeBaseStructure);
/* TIM2 TRGO selection */
TIM_SelectOutputTrigger(TIM2, TIM_TRGOSource_Update);
/* Enable TIM2 Update Interrupt*/
TIM_ITConfig(TIM2, TIM_IT_Update, ENABLE);
NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQn;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init( NVIC_InitStructure);
/* Enable TIM2 */
TIM_Cmd(TIM2,ENABLE);
}
中断获取数据:
void TIM2_IRQHandler(void)
{
if(TIM_GetITStatus(TIM2, TIM_IT_Update) == SET)
{
iddtmp = ADC_GetConversionValue(ADC1);
rv3voltagetmp = ADC_GetConversionValue(ADC1);
/* Clear the TIM2 Update flag */
TIM_ClearITPendingBit(TIM2, TIM_IT_Update);
}
}
程序运行后发现iddtmp 和rv3voltagetmp 值是一样的。
发现使用下面的时钟初始化就没有问题:(使用了内部时钟MSI)
使用多路AD采集时,使用内部时钟MSI正常,使用外部时钟HSE时,2路AD采集值一直相同。