请教大神,STM32F302CC芯片AD校准死循环的问题
时间:10-02
整理:3721RD
点击:
请教大神,本人现在在做一个AD信号转换,然后DMA存储到FLASH的开发。发现AD在初始化过程中,进入校准步骤,WHILE条件判断会进入死循环,请大神帮小弟看看问题出在哪里,谢谢!
系统时钟配置:
//Configures the different system clocks.
void SYS_RCC_Config(void)
{
/* RCC system reset(for debug purpose) */
RCC_DeInit();
//使用内部晶振
RCC_HSICmd(ENABLE);
/* Disable HSE */
RCC_HSEConfig(RCC_HSE_OFF);
/* Enable Prefetch Buffer */
// FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable);
FLASH_PrefetchBufferCmd(ENABLE);
/* Flash 2 wait state */
FLASH_SetLatency(FLASH_Latency_2);
/* HCLK = SYSCLK */
RCC_HCLKConfig(RCC_SYSCLK_Div1); //AHB
/* PCLK1 = HCLK/2 */
RCC_PCLK1Config(RCC_HCLK_Div2);//AHB1
/* PCLK2 = HCLK */
RCC_PCLK2Config(RCC_HCLK_Div1); //AHB2
/* ADCCLK = PCLK2/4 */
RCC_ADCCLKConfig(RCC_ADC12PLLCLK_Div4); //AD是12MHz主频
/* PLLCLK = (8/2)MHz * 12 = 48 MHz */
RCC_PLLConfig(RCC_PLLSource_HSI_Div2, RCC_PLLMul_12); //内部8M
/* Enable PLL */
RCC_PLLCmd(ENABLE);
/* Wait till PLL is ready */
while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET)
{ }
/* Select PLL as system clock source */
RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);
/* Wait till PLL is used as system clock source */
while(RCC_GetSYSCLKSource() != 0x08)
{ }
}//End of function;
AD初始化代码
void CAP_ADCDMAInit(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
ADC_InitTypeDef ADC_InitStructure;
ADC_CommonInitTypeDef ADC_CommonInitStructure;
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1 | RCC_AHBPeriph_ADC12, ENABLE);
NVIC_InitStructure.NVIC_IRQChannel = DMA1_Channel1_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 2;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_Init(GPIOA, &GPIO_InitStructure);
ADC_StructInit(&ADC_InitStructure);
// Calibration procedure
ADC_VoltageRegulatorCmd(ADC1, ENABLE);
// Insert delay equal to 10 us
Tim_usDelay(10);
ADC_SelectCalibrationMode(ADC1, ADC_CalibrationMode_Single);
ADC_StartCalibration(ADC1);
while (ADC_GetCalibrationStatus(ADC1) != RESET);(进入死循环的地方)
calibration_value = ADC_GetCalibrationValue(ADC1);
ADC_CommonInitStructure.ADC_Mode = ADC_Mode_Independent;
ADC_CommonInitStructure.ADC_Clock = ADC_Clock_SynClkModeDiv4;//AD是12MHZ主频
ADC_CommonInitStructure.ADC_DMAAccessMode = ADC_DMAAccessMode_Disabled;
ADC_CommonInitStructure.ADC_DMAMode = ADC_DMAMode_OneShot;
ADC_CommonInitStructure.ADC_TwoSamplingDelay = 0;
ADC_CommonInit(ADC1, &ADC_CommonInitStructure);
ADC_InitStructure.ADC_ContinuousConvMode = ADC_ContinuousConvMode_Enable;
ADC_InitStructure.ADC_Resolution = ADC_Resolution_12b;
ADC_InitStructure.ADC_ExternalTrigConvEvent = ADC_ExternalTrigConvEvent_0;
ADC_InitStructure.ADC_ExternalTrigEventEdge = ADC_ExternalTrigEventEdge_None;
ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;
ADC_InitStructure.ADC_OverrunMode = ADC_OverrunMode_Disable;
ADC_InitStructure.ADC_AutoInjMode = ADC_AutoInjec_Disable;
ADC_InitStructure.ADC_NbrOfRegChannel = 1;
ADC_Init(ADC1, &ADC_InitStructure);
}
AD启动代码:
void CAP_ADCDMAStart(u16* lpAddr, u32 nSignalCnt, INT_HANDLER lpfncHandler)
{
DMA_InitTypeDef DMA_InitStructure;
g_DMA1IRQHandler = lpfncHandler;
// DMA channel1 configuration
DMA_DeInit(DMA1_Channel1);
DMA_InitStructure.DMA_PeripheralBaseAddr = ADC_DR_Address;
DMA_InitStructure.DMA_MemoryBaseAddr = (u32)lpAddr;
DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC;
DMA_InitStructure.DMA_BufferSize = (u32)nSignalCnt;
DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;
DMA_InitStructure.DMA_Mode = DMA_Mode_Normal;
DMA_InitStructure.DMA_Priority = DMA_Priority_High;
DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;
DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord;
DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord;
DMA_Init(DMA1_Channel1, &DMA_InitStructure);
DMA_ITConfig(DMA1_Channel1, DMA_IT_TC, ENABLE);
DMA_Cmd(DMA1_Channel1, ENABLE);
ADC_RegularChannelConfig(ADC1, ADC_Channel_2, 1, ADC_SampleTime_1Cycles5);
ADC_Cmd(ADC1, ENABLE);
/* wait for ADRDY */
while (!ADC_GetFlagStatus(ADC1, ADC_FLAG_RDY));
/* ADC1 DMA Enable */
ADC_DMACmd(ADC1, ENABLE);
ADC_DMAConfig(ADC1, ADC_DMAMode_OneShot);
ADC_StartConversion(ADC1);
}//End of function;
系统时钟配置:
//Configures the different system clocks.
void SYS_RCC_Config(void)
{
/* RCC system reset(for debug purpose) */
RCC_DeInit();
//使用内部晶振
RCC_HSICmd(ENABLE);
/* Disable HSE */
RCC_HSEConfig(RCC_HSE_OFF);
/* Enable Prefetch Buffer */
// FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable);
FLASH_PrefetchBufferCmd(ENABLE);
/* Flash 2 wait state */
FLASH_SetLatency(FLASH_Latency_2);
/* HCLK = SYSCLK */
RCC_HCLKConfig(RCC_SYSCLK_Div1); //AHB
/* PCLK1 = HCLK/2 */
RCC_PCLK1Config(RCC_HCLK_Div2);//AHB1
/* PCLK2 = HCLK */
RCC_PCLK2Config(RCC_HCLK_Div1); //AHB2
/* ADCCLK = PCLK2/4 */
RCC_ADCCLKConfig(RCC_ADC12PLLCLK_Div4); //AD是12MHz主频
/* PLLCLK = (8/2)MHz * 12 = 48 MHz */
RCC_PLLConfig(RCC_PLLSource_HSI_Div2, RCC_PLLMul_12); //内部8M
/* Enable PLL */
RCC_PLLCmd(ENABLE);
/* Wait till PLL is ready */
while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET)
{ }
/* Select PLL as system clock source */
RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);
/* Wait till PLL is used as system clock source */
while(RCC_GetSYSCLKSource() != 0x08)
{ }
}//End of function;
AD初始化代码
void CAP_ADCDMAInit(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
ADC_InitTypeDef ADC_InitStructure;
ADC_CommonInitTypeDef ADC_CommonInitStructure;
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1 | RCC_AHBPeriph_ADC12, ENABLE);
NVIC_InitStructure.NVIC_IRQChannel = DMA1_Channel1_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 2;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_Init(GPIOA, &GPIO_InitStructure);
ADC_StructInit(&ADC_InitStructure);
// Calibration procedure
ADC_VoltageRegulatorCmd(ADC1, ENABLE);
// Insert delay equal to 10 us
Tim_usDelay(10);
ADC_SelectCalibrationMode(ADC1, ADC_CalibrationMode_Single);
ADC_StartCalibration(ADC1);
while (ADC_GetCalibrationStatus(ADC1) != RESET);(进入死循环的地方)
calibration_value = ADC_GetCalibrationValue(ADC1);
ADC_CommonInitStructure.ADC_Mode = ADC_Mode_Independent;
ADC_CommonInitStructure.ADC_Clock = ADC_Clock_SynClkModeDiv4;//AD是12MHZ主频
ADC_CommonInitStructure.ADC_DMAAccessMode = ADC_DMAAccessMode_Disabled;
ADC_CommonInitStructure.ADC_DMAMode = ADC_DMAMode_OneShot;
ADC_CommonInitStructure.ADC_TwoSamplingDelay = 0;
ADC_CommonInit(ADC1, &ADC_CommonInitStructure);
ADC_InitStructure.ADC_ContinuousConvMode = ADC_ContinuousConvMode_Enable;
ADC_InitStructure.ADC_Resolution = ADC_Resolution_12b;
ADC_InitStructure.ADC_ExternalTrigConvEvent = ADC_ExternalTrigConvEvent_0;
ADC_InitStructure.ADC_ExternalTrigEventEdge = ADC_ExternalTrigEventEdge_None;
ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;
ADC_InitStructure.ADC_OverrunMode = ADC_OverrunMode_Disable;
ADC_InitStructure.ADC_AutoInjMode = ADC_AutoInjec_Disable;
ADC_InitStructure.ADC_NbrOfRegChannel = 1;
ADC_Init(ADC1, &ADC_InitStructure);
}
AD启动代码:
void CAP_ADCDMAStart(u16* lpAddr, u32 nSignalCnt, INT_HANDLER lpfncHandler)
{
DMA_InitTypeDef DMA_InitStructure;
g_DMA1IRQHandler = lpfncHandler;
// DMA channel1 configuration
DMA_DeInit(DMA1_Channel1);
DMA_InitStructure.DMA_PeripheralBaseAddr = ADC_DR_Address;
DMA_InitStructure.DMA_MemoryBaseAddr = (u32)lpAddr;
DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC;
DMA_InitStructure.DMA_BufferSize = (u32)nSignalCnt;
DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;
DMA_InitStructure.DMA_Mode = DMA_Mode_Normal;
DMA_InitStructure.DMA_Priority = DMA_Priority_High;
DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;
DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord;
DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord;
DMA_Init(DMA1_Channel1, &DMA_InitStructure);
DMA_ITConfig(DMA1_Channel1, DMA_IT_TC, ENABLE);
DMA_Cmd(DMA1_Channel1, ENABLE);
ADC_RegularChannelConfig(ADC1, ADC_Channel_2, 1, ADC_SampleTime_1Cycles5);
ADC_Cmd(ADC1, ENABLE);
/* wait for ADRDY */
while (!ADC_GetFlagStatus(ADC1, ADC_FLAG_RDY));
/* ADC1 DMA Enable */
ADC_DMACmd(ADC1, ENABLE);
ADC_DMAConfig(ADC1, ADC_DMAMode_OneShot);
ADC_StartConversion(ADC1);
}//End of function;
你的意思是程序一直死在ADC校准那儿么?程序直接下载的还是debug模式