中级会员
- 积分
- 421
- 金钱
- 421
- 注册时间
- 2013-8-18
- 在线时间
- 39 小时
|
楼主 |
发表于 2017-7-10 09:00:48
|
显示全部楼层
我找到原因了,我使用的ADC3的通道与我使用的SDRAM的通道冲突了,导致ADC的DMA异常。修改通道就可以了。
以下贴上我可以通过定时器ADC采样的代码:
void TIM2_IRQHandler(void)
{
static unsigned int ADC_Counter = 0;
/* if (TIM_GetITStatus(TIM2, TIM_IT_Update) != RESET) */
if (((TIM2->SR & TIM_IT_UPDATE) != (unsigned short int)RESET) && ((TIM2->DIER & TIM_IT_UPDATE) != (unsigned short int)RESET))
{
ADC_Counter++;
if(ADC_Counter >= 100)
{
ADC_Counter = 0;
HAL_ADC_Start_DMA(&ADC3_Handle, (uint32_t*)&AD_Value, AD_CHANNEL_MAX);
//ADC3->CR2 |= (uint32_t)ADC_CR2_SWSTART;
}
/* Clear TIM2 updateinterrupt */
TIM2->SR = (unsigned short int)~TIM_IT_UPDATE;
}
}
ADC_HandleTypeDef ADC3_Handle;
DMA_HandleTypeDef DMA_ADC_Handle;
void ADC_DMA_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
ADC_ChannelConfTypeDef Channel_Config;
/*##-1- Enable peripherals and GPIO Clocks --------------------------------*/
/* Enable GPIO clock */
__HAL_RCC_GPIOF_CLK_ENABLE();
/* ADC3 Periph clock enable */
__HAL_RCC_ADC3_CLK_ENABLE();
/* Enable DMA2 clock */
__HAL_RCC_DMA2_CLK_ENABLE();
/*##-2- Configure peripheral GPIO -----------------------------------------*/
/* ADC3 Channel8 GPIO pin configuration */
GPIO_InitStructure.Pin = GPIO_PIN_8 | GPIO_PIN_9;
GPIO_InitStructure.Mode = GPIO_MODE_ANALOG;
GPIO_InitStructure.Pull = GPIO_NOPULL;
HAL_GPIO_Init(GPIOF, &GPIO_InitStructure);
/*##-3- Configure the ADC peripheral --------------------------------------*/
ADC3_Handle.Instance = ADC3;
ADC3_Handle.Init.ClockPrescaler = ADC_CLOCKPRESCALER_PCLK_DIV8;
ADC3_Handle.Init.Resolution = ADC_RESOLUTION_12B;
ADC3_Handle.Init.ScanConvMode = ENABLE;
ADC3_Handle.Init.ContinuousConvMode = DISABLE;
ADC3_Handle.Init.NbrOfConversion = AD_CHANNEL_MAX;
ADC3_Handle.Init.DiscontinuousConvMode = DISABLE;
ADC3_Handle.Init.NbrOfDiscConversion = 0;
ADC3_Handle.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE;
ADC3_Handle.Init.ExternalTrigConv = ADC_EXTERNALTRIGCONV_T1_CC1;
ADC3_Handle.Init.DataAlign = ADC_DATAALIGN_RIGHT;
ADC3_Handle.Init.DMAContinuousRequests = ENABLE;
ADC3_Handle.Init.EOCSelection = ADC_EOC_SEQ_CONV;
HAL_ADC_Init(&ADC3_Handle);
/*##-4- Configure the DMA streams -----------------------------------------*/
/* Set the parameters to be configured */
DMA_ADC_Handle.Instance = DMA2_Stream1;
DMA_ADC_Handle.Init.Channel = DMA_CHANNEL_2;
DMA_ADC_Handle.Init.Direction = DMA_PERIPH_TO_MEMORY;
DMA_ADC_Handle.Init.PeriphInc = DMA_PINC_DISABLE;
DMA_ADC_Handle.Init.MemInc = DMA_MINC_ENABLE;
DMA_ADC_Handle.Init.PeriphDataAlignment = DMA_PDATAALIGN_HALFWORD;
DMA_ADC_Handle.Init.MemDataAlignment = DMA_MDATAALIGN_HALFWORD;
DMA_ADC_Handle.Init.Mode = DMA_CIRCULAR;
DMA_ADC_Handle.Init.Priority = DMA_PRIORITY_HIGH;
DMA_ADC_Handle.Init.FIFOMode = DMA_FIFOMODE_DISABLE;
DMA_ADC_Handle.Init.FIFOThreshold = DMA_FIFO_THRESHOLD_HALFFULL;
DMA_ADC_Handle.Init.MemBurst = DMA_MBURST_SINGLE;
DMA_ADC_Handle.Init.PeriphBurst = DMA_PBURST_SINGLE;
HAL_DMA_Init(&DMA_ADC_Handle);
/* Associate the initialized DMA handle to the the ADC handle */
__HAL_LINKDMA(&ADC3_Handle, DMA_Handle, DMA_ADC_Handle);
Channel_Config.Channel = ADC_CHANNEL_6;
Channel_Config.Rank = 1;
Channel_Config.SamplingTime = ADC_SAMPLETIME_480CYCLES;
HAL_ADC_ConfigChannel(&ADC3_Handle, &Channel_Config);
Channel_Config.Channel = ADC_CHANNEL_7;
Channel_Config.Rank = 2;
Channel_Config.SamplingTime = ADC_SAMPLETIME_480CYCLES;
HAL_ADC_ConfigChannel(&ADC3_Handle, &Channel_Config);
}
优先级部分就不贴出来了。
|
|