用stm32f051做过无路DMAADC采样,都没有问题,这次想采集一路不进DMA中断,在TIM1中断中控制DMA,可是每次启动DMA得到的数组的第一个值总是错的,确实是采到了0xFFF和0x000,因为初始化数组每个元素都是1,也会出现那两个值,各位大侠给个方向。
void ADC1_CH_DMA_Config(char collectroadnum)
{
ADC_InitTypeDef ADC_InitStructure;
DMA_InitTypeDef DMA_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
/* GPIOC Periph clock enable */
/* GPIOC 外设时钟使能 */
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOC, ENABLE);
/* ADC1 Periph clock enable */
/* ADC1 外设时钟使能 */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE);
/* DMA1 clock enable */
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1 , ENABLE);
/* Configures the ADC clock (ADCCLK) */
/* ADCs 默认值初始化 */
ADC_DeInit(ADC1);//选择频道
/* 复位HSI14位 */
RCC->CR2 &= (uint32_t)0xFFFFFFFE;
//一次时间=(41.5 + 12.5)ADC时钟周期
//1us 一次1.5+12.5
/* 配置ADC时钟(ADCCLK) *///采用自带的14MHZ
RCC_ADCCLKConfig(RCC_ADCCLK_HSI14);
/* 当ADC1时钟被PCLK以4分频时失能抖动(jitter) */
//ADC_JitterCmd(ADC1, ADC_JitterOff_PCLKDiv4, DISABLE);
/* Configure ADC Channel12 as analog input */
/* 配置ADC通道作为模拟输入 */
if(collectroadnum==1)
{
//collect 1 road
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1 ;
}
else if(collectroadnum==2)
{
//collect 3 road
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2|GPIO_Pin_3;
}
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL ;
GPIO_Init(GPIOC, &GPIO_InitStructure);
//初始 ADC 配置
ADC_StructInit(&ADC_InitStructure);
/* 配置ADC1为12位分辨率的连续模式 */
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_InitStructure.ADC_ScanDirection = ADC_ScanDirection_Upward;//ADC_ScanDirection_Backward,ADC_ScanDirection_Upward
ADC_Init(ADC1, &ADC_InitStructure);
//采样时间
if(collectroadnum==1)
{
// 用1.5个ADC时钟周期作为采样时间配置ADC1通道10
ADC_ChannelConfig(ADC1, ADC_Channel_11 , ADC_SampleTime_1_5Cycles);
}
else if(collectroadnum==2)
{
// 用41.5个ADC时钟周期作为采样时间配置ADC1通道10
ADC_ChannelConfig(ADC1, ADC_Channel_12 , ADC_SampleTime_1_5Cycles);
// 用41.5个ADC时钟周期作为采样时间配置ADC1通道11
ADC_ChannelConfig(ADC1, ADC_Channel_13 , ADC_SampleTime_1_5Cycles);
}
/* ADC 校准 */
ADC_GetCalibrationFactor(ADC1);
/* 使能ADC外设(PerIdx) */
ADC_Cmd(ADC1, ENABLE);
/* Wait the ADCEN falg */
/* 等待ADC结束标志 */
while(!ADC_GetFlagStatus(ADC1, ADC_FLAG_ADEN));
/* Enable ADC_DMA */
ADC_DMACmd(ADC1, ENABLE);
/* ADC1 regular Software Start Conv */
/* ADC1 定期软件启动转换 */
ADC_StartOfConversion(ADC1);
//DMA 配置
/* DMA1 Channel1 Config */
//选择通道
DMA_DeInit(DMA1_Channel1);
//设置外设地址
DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)ADC1_DR_Address;
DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC;
//缓冲为(数组长度)
if(collectroadnum==1)
{
//设置内存映射地址
DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)&RegularConvData_Tab1;
DMA_InitStructure.DMA_BufferSize =30; //开启通道数1 个
}
else if(collectroadnum==2)
{
//设置内存映射地址
DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)&RegularConvData_Tab2;
DMA_InitStructure.DMA_BufferSize = 2; //开启通道数2 个
}
//关外设地址计数地址增量
DMA_InitStructure.DMA_PeripheralInc =DMA_PeripheralInc_Disable;
//内存地址计数
DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;
DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord;
DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord;
//循环模式
DMA_InitStructure.DMA_Mode = DMA_Mode_Normal;//单次写满20就停了
//高优先级
DMA_InitStructure.DMA_Priority = DMA_Priority_High;
//关内存到内存
DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;
DMA_Init(DMA1_Channel1, &DMA_InitStructure);
DMA_ITConfig(DMA1_Channel1, DMA_IT_TC, ENABLE);//DMA中断
//配置DMA循环模式
ADC_DMARequestModeConfig(ADC1, ADC_DMAMode_Circular);
//dma DMA中断
// Enable the DMA Interrupt
NVIC_InitStructure.NVIC_IRQChannel = DMA1_Channel1_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPriority = 0x01;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
DMA_ClearFlag(DMA1_FLAG_TC1);//清除DMA TC1标志
// DMA1 Channel1 enable
DMA_Cmd(DMA1_Channel1, ENABLE);
}
|