PY32F030无法使用ADC同时触发DMA与中断
不知道为什么,触发DMA就无法触发ADC的中断,,,触发中断就不能触发DMA,莫名其妙程序如下,写法是DMA的,如果进入中断则C = 100;
TIM_HandleTypeDef timh1 = {0};
TIM_OC_InitTypeDef timocinitstruct = {0};
TIM_MasterConfigTypeDef sMasterConfig = {0};
GPIO_InitTypeDef gpioinitstruct = {0};
ADC_HandleTypeDef ADCh1 = {0};
ADC_ChannelConfTypeDef sConfig = {0};
DMA_HandleTypeDef FOCDMAinitstruct = {0};
uint16_t ADC_value[2] = {0};
uint8_t C = 50;
void FOR_RCC_init(void)
{
//TIM需要
__HAL_RCC_GPIOA_CLK_ENABLE();
__HAL_RCC_GPIOB_CLK_ENABLE();
__HAL_RCC_TIM1_CLK_ENABLE();
//ADC
__HAL_RCC_ADC_FORCE_RESET();
__HAL_RCC_ADC_RELEASE_RESET(); /* 复位ADC */
__HAL_RCC_ADC_CLK_ENABLE(); /* ADC时钟使能 */
__HAL_RCC_DMA_CLK_ENABLE();
}
void ADC_NVIC_init(void)
{
HAL_NVIC_SetPriority(ADC_COMP_IRQn, 0, 0); /* 中断优先级设置 */
HAL_NVIC_EnableIRQ(ADC_COMP_IRQn); /* ADC中断使能 */
}
void ADC_DMA_init(void)
{
HAL_SYSCFG_DMA_Req(0); //不知道什么东西
FOCDMAinitstruct.Instance = DMA1_Channel1;
FOCDMAinitstruct.Init.Direction = DMA_PERIPH_TO_MEMORY;
FOCDMAinitstruct.Init.MemDataAlignment = DMA_MDATAALIGN_HALFWORD;
FOCDMAinitstruct.Init.MemInc = DMA_MINC_ENABLE;
FOCDMAinitstruct.Init.Mode = DMA_CIRCULAR;
FOCDMAinitstruct.Init.PeriphDataAlignment = DMA_PDATAALIGN_HALFWORD;
FOCDMAinitstruct.Init.PeriphInc = DMA_PINC_DISABLE;
FOCDMAinitstruct.Init.Priority = DMA_PRIORITY_MEDIUM;
__HAL_LINKDMA(&ADCh1,DMA_Handle,FOCDMAinitstruct);
HAL_DMA_Init(&FOCDMAinitstruct);
}
void FOR_ADC_init(void)
{
//GPIO_init
gpioinitstruct.Mode = GPIO_MODE_ANALOG;
gpioinitstruct.Pin = GPIO_PIN_0 | GPIO_PIN_1;
gpioinitstruct.Pull = GPIO_NOPULL;
HAL_GPIO_Init(GPIOA,&gpioinitstruct);
ADCh1.Instance = ADC1;
HAL_ADCEx_Calibration_Start(&ADCh1); /* ADC校准 */
ADCh1.Instance = ADC1;
ADCh1.Init.ClockPrescaler = ADC_CLOCK_SYNC_PCLK_DIV1; /* 设置ADC时钟 */
ADCh1.Init.Resolution = ADC_RESOLUTION_12B; /* 转换分辨率12bit */
ADCh1.Init.DataAlign = ADC_DATAALIGN_RIGHT; /* 数据右对齐 */
ADCh1.Init.ScanConvMode = ADC_SCAN_DIRECTION_FORWARD; /* 扫描序列方向:向上 */
ADCh1.Init.EOCSelection = ADC_EOC_SEQ_CONV; /* 连续采样??? */
ADCh1.Init.LowPowerAutoWait = ENABLE; /* 等待转换模式 */
ADCh1.Init.ContinuousConvMode = ENABLE; /* 连续转换模式 */
ADCh1.Init.DiscontinuousConvMode = DISABLE; /* 不使能非连续模式 */
ADCh1.Init.ExternalTrigConv = ADC_EXTERNALTRIGCONV_T1_TRGO; /* TIM1TRGO触发 */
ADCh1.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_RISING;
ADCh1.Init.DMAContinuousRequests = ENABLE; /* 使能DMA */
ADCh1.Init.Overrun = ADC_OVR_DATA_OVERWRITTEN; /* 当过载发生时,覆盖上一个值 */
ADCh1.Init.SamplingTimeCommon=ADC_SAMPLETIME_3CYCLES_5; /* 设置采样周期 */
HAL_ADC_Init(&ADCh1);
sConfig.Channel = ADC_CHANNEL_0;
sConfig.Rank = ADC_RANK_CHANNEL_NUMBER;
HAL_ADC_ConfigChannel(&ADCh1, &sConfig);
sConfig.Channel = ADC_CHANNEL_1;
sConfig.Rank = ADC_RANK_CHANNEL_NUMBER;
HAL_ADC_ConfigChannel(&ADCh1, &sConfig);
//HAL_ADC_Start_IT(&ADCh1);
HAL_ADC_Start_DMA(&ADCh1,(uint32_t*)(ADC_value),2);
}
void TIM1_PWM_init(void)
{
//TIM_init
timh1.Instance = TIM1;
timh1.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
timh1.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
timh1.Init.CounterMode = TIM_COUNTERMODE_CENTERALIGNED1;
timh1.Init.Period = 1200-1; //20KHz 1200*2 10KHz
timh1.Init.Prescaler = 2-1;
timh1.Init.RepetitionCounter = 0;
HAL_TIM_Base_Init(&timh1);
/* 配置TIM1为主机模式 输出TRGO信号 */
sMasterConfig.MasterOutputTrigger = TIM_TRGO_UPDATE; /* 选择更新事件作为触发源 */
sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE; /* 主/从模式无作用 */
HAL_TIMEx_MasterConfigSynchronization(&timh1, &sMasterConfig); /* 配置TIM1 */
HAL_TIM_Base_Start(&timh1);
}
void ADC_COMP_IRQHandler(void) //这个都进不来
{
HAL_ADC_IRQHandler(&ADCh1);
}
void HAL_ADC_ConvCpltCallback(ADC_HandleTypeDef *hadc) //ADC中断
{
if(hadc->Instance == ADC1)
{
C=100;
}
}
|