只进一次中断函数,后面再次MYDMA_Enable(DMA_Channel_TypeDef*DMA_CHx) 也不会进中断了
ADC_InitTypeDef ADC_InitStructure;
DMA_InitTypeDef DMA_InitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_ADCCLKConfig(RCC_ADCCLK_HSI14); //24/2=12M的AD时钟
/* GPIOC Periph clock enable */
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE);
//设置AD模拟输入端口为输入 1路AD 规则通道
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_Init(GPIOA, &GPIO_InitStructure);
/* DMA channel1 configuration ----------------------------------------------*/
//使能DMA
DMA_DeInit(DMA1_Channel1);
DMA_InitStructure.DMA_PeripheralBaseAddr = ADC1_DR_Address; //DMA通道1的地址
DMA_InitStructure.DMA_MemoryBaseAddr = (u32)&ADC_ConvertedValue[0]; //DMA传送地址
DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC; //传送方向
DMA_InitStructure.DMA_BufferSize =ADC_SBUF_MAX; //传送内存大小,N个16位 最多65535个
DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable; //传送内存地址递增
DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord; //ADC1转换的数据是16位
DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord; //传送的目的地址是16位宽度
DMA_InitStructure.DMA_Mode = DMA_Mode_Normal;//DMA_Mode_Circular;//DMA_Mode_Normal; //单次
DMA_InitStructure.DMA_Priority = DMA_Priority_High;
DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;
DMA_Init(DMA1_Channel1, &DMA_InitStructure);
//使能DMA通道1
DMA_Cmd(DMA1_Channel1, ENABLE);
/* ADC DMA request in circular mode */
ADC_DMARequestModeConfig(ADC1, ADC_DMAMode_OneShot);//ADC_DMAMode_OneShot ADC_DMAMode_Circular
/* Enable ADC_DMA */
ADC_DMACmd(ADC1, ENABLE);
/* Initialize ADC structure */
ADC_StructInit(&ADC_InitStructure);
// //ADC配置
ADC_InitStructure.ADC_Resolution = ADC_Resolution_12b;
ADC_InitStructure.ADC_ContinuousConvMode = ENABLE; //模数转换工作在连续模式,还是单次模式
ADC_InitStructure.ADC_ExternalTrigConvEdge = ADC_ExternalTrigConvEdge_None;//转换由软件而不是外部触发启动
ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;//ADC数据右对齐
ADC_InitStructure.ADC_ScanDirection = ADC_ScanDirection_Upward;
ADC_Init(ADC1, &ADC_InitStructure);
//ADC1 规则通道配置
/* Convert the ADC1 Channel 1 with 55.5 Cycles as sampling time */
ADC_ChannelConfig(ADC1, ADC_Channel_0 , ADC_ST);
ADC_GetCalibrationFactor(ADC1);
//使能ADC1
ADC_Cmd(ADC1, ENABLE);
//开始校准ADC1
while(!ADC_GetFlagStatus(ADC1, ADC_FLAG_ADEN));
//ADC1转换启动
ADC_StartOfConversion(ADC1);
// NVIC_SetPriority(DMA1_Channel1_IRQn,0x03);//中断优先级
// NVIC_EnableIRQ(DMA1_Channel1_IRQn);
DMA_ITConfig(DMA1_Channel1, DMA_IT_TC, ENABLE);
/*选择DMA1通道中断*/
NVIC_InitStructure.NVIC_IRQChannel = DMA1_Channel1_IRQn;
/*中断使能*/
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
/*优先级设为0 */
NVIC_InitStructure.NVIC_IRQChannelPriority = 0;
/*使能 DMA 中断*/
NVIC_Init(&NVIC_InitStructure);
MYDMA_Enable(DMA1_Channel1);//启动一次采集
/** 中断服务函数***/
void DMA1_Channel1_IRQHandler(void)
{
if(DMA_GetITStatus(DMA1_IT_TC1)) //
{
DMA_ClearFlag(DMA1_FLAG_TC1);
g_Dma_Transfer_Flag = 1;//一次数据转换完成
}
}
//DMA使能
void MYDMA_Enable(DMA_Channel_TypeDef*DMA_CHx)
{
// DMA_CHx->CCR&=~(1<<0);
// DMA_CHx->CNDTR=ADC_SBUF_MAX;
// DMA_CHx->CCR|=1<<0;
DMA_Cmd(DMA_CHx, DISABLE );
DMA_SetCurrDataCounter(DMA1_Channel1,ADC_SBUF_MAX);
DMA_Cmd(DMA_CHx, ENABLE );
}
|