使用stm32f103单片机AD双通道采集和DMA部分,DMA采集数据保存不完整,如果是采集40个数据,DMA只能采集20个数据。代码如下:求高手帮忙
void GPIO_MainInit()
{
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3 | GPIO_Pin_4;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;
GPIO_Init(GPIOC,&GPIO_InitStructure);
}
void ADC12_Init()
{
DMA_InitTypeDef DMA_InitStructure;
DMA_DeInit(DMA1_Channel1);
DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32)(&(ADC1->DR)); //外设地址
DMA_InitStructure.DMA_MemoryBaseAddr = (uint32)(&ADCValue); //内存地址
DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC;//dma传输方向单向
DMA_InitStructure.DMA_BufferSize = 20; //设置DMA在传输时缓冲区的长度
DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable; //Disable设置DMA的外设递增模式,一个外设
DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable; //DisableEnable设置DMA的内存递增模式
DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord; //Half外设数据字长
DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord; //HalfWord内存数据字长
DMA_InitStructure.DMA_Mode = DMA_Mode_Circular; //Normal设置DMA的传输模式:连续不断的循环模式
DMA_InitStructure.DMA_Priority = DMA_Priority_High; //设置DMA的优先级别
DMA_InitStructure.DMA_M2M = DMA_M2M_Disable; ////设置DMA的2个memory中的变量互相访问
DMA_Init(DMA1_Channel1, &DMA_InitStructure);
// DMA_Cmd(DMA1_Channel1, ENABLE);
DMA_ITConfig(DMA1_Channel1,DMA_IT_TC,ENABLE); //传输结束中断
ADC_InitTypeDef ADC_InitStruct;
ADC_DeInit(ADC1);
ADC_InitStruct.ADC_Mode = ADC_Mode_Independent ; //独立工作模式
ADC_InitStruct.ADC_ScanConvMode = ENABLE;//ENABLE多通道 DISABLE扫描单通道
ADC_InitStruct.ADC_ContinuousConvMode = ENABLE ;//ADC_InitStruct.ADC_ContinuousConvMode = DISABLE ; //转换工作在单次模式
ADC_InitStruct.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None;//转换由软件而不是外部触发启动
ADC_InitStruct.ADC_DataAlign = ADC_DataAlign_Right;//数据右对齐
ADC_InitStruct.ADC_NbrOfChannel = 2 ;//1个ADC通道
ADC_Init(ADC1,&ADC_InitStruct);
/*使能ADC1,ADC_13通道,采样顺序1,采样时间71.5周期ADC_SampleTime_71Cycles5)*/
//ADC_RegularChannelConfig(ADC1 , ADC_Channel_13 , 2 ,ADC_SampleTime_239Cycles5);
ADC_RegularChannelConfig(ADC1 , ADC_Channel_14 , 1 ,ADC_SampleTime_239Cycles5);
/*使能DMA*/
ADC_DMACmd(ADC1,ENABLE);
ADC_ResetCalibration(ADC1);//重置指定的ADC1的校准寄存器 初始化校准寄存器
while(ADC_GetResetCalibrationStatus(ADC1));//初始化校准寄存器已完成 获取ADC1重置校准寄存器状态,直到校准寄存器重设完成
ADC_StartCalibration(ADC1);//开始指定ADC的校准状态
while(ADC_GetResetCalibrationStatus(ADC1));//获取ADC1校准程序,直到校准完成
DMA_Cmd(DMA1_Channel1, ENABLE);
//ADC_SoftwareStartConvCmd(ADC1,ENABLE);//使能ADC1的软件转换启动功能
ADC_Cmd(ADC1,ENABLE);
}
void DMA_NVIC(void)
{
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
NVIC_InitTypeDef NVIC_InitStructure;
//Enable the dma Interrupts liang
// clear IT flag first
// DMA_ClearFlag(DMA1_FLAG_TC1);
NVIC_InitStructure.NVIC_IRQChannel = DMA1_Channel1_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
void DMA1_Channel1_IRQHandler()
{
int16 dma_adc13 ;
int16 dma_adc14 ;
DMA_ClearITPendingBit(DMA1_IT_TC1);
DMA_ClearFlag(DMA1_FLAG_TC1);
}
IAR查看变量ADCValue[],发现只有10个数据,求高手~~~
|