检查过,ADC和DMA的配置了,没什么问题,但一旦调用定义的函数NVIC_Configuration(),就出现bug;
以下是我的配置:
void NVIC_Configuration(void)
{
NVIC_InitTypeDef NVIC_InitStructure;
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0);
NVIC_InitStructure.NVIC_IRQChannel = DMA1_Channel1_IRQn; //开启DMA1_Channel1中断
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
void ADC_Configuration(void)
{
ADC_InitTypeDef ADC_InitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
DMA_InitTypeDef DMA_InitStructure;
/* Enable DMA clock */
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE);
/* Enable ADC1 and GPIOC clock */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1 , ENABLE);
/* DMA channel1 configuration ----------------------------------------------*/
//使能DMA
DMA_DeInit(DMA1_Channel1);
DMA_InitStructure.DMA_PeripheralBaseAddr = ADC1_DR_Address; //DMA通道1的地址
DMA_InitStructure.DMA_MemoryBaseAddr = (u32)&ADC_ConvertedValue; //DMA传送地址
DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC; //传送方向
DMA_InitStructure.DMA_BufferSize = 100; //传送内存大小,100个16位
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_Circular; //循环
DMA_InitStructure.DMA_Priority = DMA_Priority_High;
DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;
DMA_Init(DMA1_Channel1, &DMA_InitStructure);
/* 允许DMA1通道1传输结束中断 */
DMA_ITConfig(DMA1_Channel1,DMA_IT_TC, ENABLE);
//使能DMA通道1
DMA_Cmd(DMA1_Channel1, ENABLE);
//设置AD模拟输入端口为输入 一共2路AD 规则通道
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;
GPIO_Init(GPIOC, &GPIO_InitStructure);
//ADC配置
/* ADC转换时间: ─ STM32F103xx增强型产品:时钟为56MHz时为1μs(时钟为72MHz为1.17μs)
ADC采样范围0-3.3V */
RCC_ADCCLKConfig(RCC_PCLK2_Div6); //设置ADC的时钟为72MHZ/6=12M
ADC_InitStructure.ADC_Mode = ADC_Mode_Independent; //ADC1工作在独立模式
ADC_InitStructure.ADC_ScanConvMode = ENABLE; //模数转换工作在扫描模式(多通道)还是单次(单通道)模式
ADC_InitStructure.ADC_ContinuousConvMode = ENABLE; //模数转换工作在扫描模式(多通道)还是单次(单通道)模式
ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None;//转换由软件而不是外部触发启动
ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;//ADC数据右对齐
ADC_InitStructure.ADC_NbrOfChannel = 2;//规定了顺序进行规则转换的ADC通道的数目。这个数目的取值范围是1到16
ADC_Init(ADC1, &ADC_InitStructure);
/* ADC1 regular channels configuration [规则模式通道配置]*/
//ADC1 规则通道配置
ADC_RegularChannelConfig(ADC1, ADC_Channel_10, 1, ADC_SampleTime_55Cycles5); //通道10采样时间 55.5周期
ADC_RegularChannelConfig(ADC1, ADC_Channel_11, 2, ADC_SampleTime_55Cycles5); //通道11采样时间 55.5周期
//使能ADC1 DMA
ADC_DMACmd(ADC1, ENABLE);
//使能ADC1
ADC_Cmd(ADC1, ENABLE);
// 初始化ADC1校准寄存器
ADC_ResetCalibration(ADC1);
//检测ADC1校准寄存器初始化是否完成
while(ADC_GetResetCalibrationStatus(ADC1));
//开始校准ADC1
ADC_StartCalibration(ADC1);
//检测是否完成校准
while(ADC_GetCalibrationStatus(ADC1));
//ADC1转换启动
ADC_SoftwareStartConvCmd(ADC1, ENABLE);
}
|