初上STM32芯片,请教STM32F030芯片,系统时钟是内部48MHz,用TIM1_CC4事件触发ADC转换,DMA搬运转换数据和中断提示,TIM1是中间对齐模式,周期是62us。现在ADC转换完成DMA要4、5S才进一次中断,麻烦大家看看问题出在哪里?谢谢大家!!!
void main(void)
{
SystemInit();
if (SysTick_Config(SystemCoreClock / 1000))//设置为1 毫秒
{
while(1);
}
//LED
GPIO_InitTypeDef GPIO_InitStruct; //调用IO 定义的结构体
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);//时能IO 端口时钟
GPIO_InitStruct.GPIO_Pin = GPIO_Pin_11;//设置连接LED 等的IO 端口 11
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_OUT;//设置端口为输出模式
GPIO_InitStruct.GPIO_OType = GPIO_OType_PP;//设置输出推挽
GPIO_InitStruct.GPIO_Speed =GPIO_Speed_Level_3;//设置输出速度为第三级
GPIO_Init(GPIOA, &GPIO_InitStruct); //把设置参数运用到结构体中
// GPIO_SetBits(GPIOA, GPIO_Pin_11 );//熄灭LED 灯
GPIO_ResetBits(GPIOA,GPIO_Pin_11);
ADC_TIM_Config_2();
ADC1_DMA_Init();
while(1)
{
}
}
void ADC_TIM_Config_2(void)
{
ADC_InitTypeDef ADC_InitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
/* GPIOC Periph clock enable */
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);
/* ADC1 and TIM1 Periph clock enable */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1 | RCC_APB2Periph_TIM1 , ENABLE);
/* Configure ADC Channel11 as analog input */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4 ;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL ;
GPIO_Init(GPIOA, &GPIO_InitStructure);
/* TIM1 Configuration */
TIM_DeInit(TIM1);
TIM_TimeBaseStructInit(&TIM_TimeBaseStructure);
TIM_OCStructInit(&TIM_OCInitStructure);
/* Time base configuration */
TIM_TimeBaseStructure.TIM_Period = 3000/2u ;//
TIM_TimeBaseStructure.TIM_Prescaler = 0x0;
TIM_TimeBaseStructure.TIM_ClockDivision = 0x0;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_CenterAligned3;
TIM_TimeBaseInit(TIM1, &TIM_TimeBaseStructure);
/* Output Compare PWM Mode configuration */
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1; /* low edge by default */
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable ;//TIM_OutputState_Disable
TIM_OCInitStructure.TIM_Pulse = 400;
TIM_OC4Init(TIM1, &TIM_OCInitStructure);
/* TIM1 enable counter */
TIM_Cmd(TIM1, ENABLE);
/* Main Output Enable */
TIM_CtrlPWMOutputs(TIM1, ENABLE);
/* ADCs DeInit */
ADC_DeInit(ADC1);
//***********************************************************
/*设置ADC分频因子为4 ,ADC最大时间不能超过14M*/
RCC_ADCCLKConfig(RCC_ADCCLK_PCLK_Div4);
/* ADC1 and TIM1 Periph clock enable */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1 , ENABLE);
//**********************************************************
/* Initialize ADC structure */
ADC_StructInit(&ADC_InitStructure);
/* Configure the ADC1 in continous mode withe a resolutuion equal to 12 bits */
ADC_InitStructure.ADC_Resolution = ADC_Resolution_12b;
ADC_InitStructure.ADC_ContinuousConvMode = DISABLE ; //DISABLE ENABLE
ADC_InitStructure.ADC_ExternalTrigConvEdge = ADC_ExternalTrigConvEdge_RisingFalling;
ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_T1_CC4;
ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;
ADC_InitStructure.ADC_ScanDirection = ADC_ScanDirection_Upward;
ADC_Init(ADC1, &ADC_InitStructure);
/* Convert the ADC1 Channel 11 with 239.5 Cycles as sampling time */
ADC_ChannelConfig(ADC1, ADC_Channel_4 , ADC_SampleTime_1_5Cycles);
/* ADC Calibration */
ADC_GetCalibrationFactor(ADC1);
/* 循环模式下的 ADC DMA 请求 */
ADC_DMARequestModeConfig(ADC1, ADC_DMAMode_Circular);
/* 使能 ADC_DMA */
ADC_DMACmd(ADC1, ENABLE);
/* Enable ADCperipheral[PerIdx] */
ADC_Cmd(ADC1, ENABLE);
/* Wait the ADCEN falg */
while(!ADC_GetFlagStatus(ADC1, ADC_FLAG_ADEN));
/* ADC1 regular Software Start Conv */
ADC_StartOfConversion(ADC1);
}
void ADC1_DMA_Init(void)
{
DMA_InitTypeDef DMA_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
/* 使能DMA时钟 */
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1 , ENABLE);
/* 复位DMA1_Channel1 */
DMA_DeInit(DMA1_Channel1);
/*DMA外设ADC基地址*/
DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)ADC1_DR_Address;
/*DMA内存基地址*/
DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)&ADC_ConvertedValue;
/*外设作为数据传输源*/
DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC;
/*DMA通道的DMA缓存的大小*/
DMA_InitStructure.DMA_BufferSize = 2 * AD_Count;
/*外设地址寄存器不变*/
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循环模式,即完成后重新开始覆盖*/
DMA_InitStructure.DMA_Mode = DMA_Mode_Circular;
/*DMA优先级设置为高*/
DMA_InitStructure.DMA_Priority = DMA_Priority_High;
/*DMA通道x禁用内存到内存传输*/
DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;
/*DMA初始化*/
DMA_Init(DMA1_Channel1, &DMA_InitStructure);
/*清除一次DMA中断标志*/
DMA_ClearITPendingBit(DMA1_IT_TC1);
/*使能DMA传输完成中断*/
DMA_ITConfig(DMA1_Channel1, DMA1_IT_TC1, 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);
/* DMA1 Channel1 enable */
DMA_Cmd(DMA1_Channel1, ENABLE);
}
void DMA1_Channel1_IRQHandler()
{
static uint32_t data;
if(++data > 2000) //4、5才进一次中断
{
data =0;
if(GPIO_ReadOutputDataBit(GPIOA,GPIO_Pin_11))
GPIO_ResetBits(GPIOA,GPIO_Pin_11);
else
GPIO_SetBits(GPIOA,GPIO_Pin_11);
}
/*判断DMA传输完成中断*/
if(DMA_GetITStatus(DMA1_IT_TC1) != RESET)
{
flag_ADC = 1;
}
/*清除DMA中断标志位*/
DMA_ClearITPendingBit(DMA1_IT_TC1);
}
|