本人在使用STM32F103设置TIM1输出6路PWM时,配置刹车中断函数NVIC_Init(&NVIC_InitStructure)时,程序在里面出不来。不知为何原因。本人新手,望各位大侠指点。现贴出代码如下: ADC_InitTypeDef ADC_InitStructure; TIM_TimeBaseInitTypeDef TIM1_TimeBaseStructure; TIM_OCInitTypeDef TIM1_OCInitStructure; TIM_BDTRInitTypeDef TIM1_BDTRInitStructure; NVIC_InitTypeDef NVIC_InitStructure; GPIO_InitTypeDef GPIO_InitStructure; /* ADC1, ADC2, DMA, GPIO, TIM1 clocks enabling -----------------------------*/ /* ADCCLK = PCLK2/6 */ RCC_ADCCLKConfig(RCC_PCLK2_Div6); /* Enable DMA clock */ RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE); /* Enable GPIOA, GPIOB, AFIO clocks */ RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO | RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB, ENABLE); /* Enable ADC1 clock */ RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE); /* Enable ADC2 clock */ RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC2, ENABLE); /* Enable TIM1 clock */ RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1, ENABLE); /* ADC1, ADC2, PWM pins configurations -------------------------------------*/ GPIO_StructInit(&GPIO_InitStructure); /****** Configure PA.05,06,7(ADC Channels [5..7]) as analog input ****/ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5 | GPIO_Pin_6 | GPIO_Pin_7; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN; GPIO_Init(GPIOA, &GPIO_InitStructure); GPIO_StructInit(&GPIO_InitStructure); /****** Configure PA.03 (ADC Channels [3]) as analog input ******/ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0|GPIO_Pin_1; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN; GPIO_Init(GPIOB, &GPIO_InitStructure); /* TIM1 Peripheral Configuration -------------------------------------------*/ /* TIM1 Registers reset */ TIM_DeInit(TIM1); TIM_TimeBaseStructInit(&TIM1_TimeBaseStructure); /* Time Base configuration */ TIM1_TimeBaseStructure.TIM_Prescaler = 0x0; //预分频器不分频. TIM1_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_CenterAligned1; //计数模式为中央对称模式. TIM1_TimeBaseStructure.TIM_Period = PWM_PERIOD; //计数周期. TIM1_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV2; //?? 2分频.??? // Initial condition is REP=0 to set the UPDATE only on the underflow TIM1_TimeBaseStructure.TIM_RepetitionCounter = REP_RATE; //???? TIM_TimeBaseInit(TIM1, &TIM1_TimeBaseStructure); //初始化TIM1时间计数单元 . TIM_OCStructInit(&TIM1_OCInitStructure); /* Channel 1, 2,3 in PWM mode */ TIM1_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1; //定时器模式为PWM1. TIM1_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; //输出状态使能. TIM1_OCInitStructure.TIM_OutputNState = TIM_OutputNState_Enable; //互补输出比较使能. TIM1_OCInitStructure.TIM_Pulse = 0x505; //dummy value //脉宽(占空比). TIM1_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; //输出极性为高电平有效. TIM1_OCInitStructure.TIM_OCNPolarity = TIM_OCNPolarity_High; //互补输出也是高电平有效. TIM1_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Reset; //空闲时引脚状态. TIM1_OCInitStructure.TIM_OCNIdleState = LOW_SIDE_POLARITY; //空闲时互补引脚状态. TIM_OC1Init(TIM1, &TIM1_OCInitStructure); TIM_OC2Init(TIM1, &TIM1_OCInitStructure); TIM_OC3Init(TIM1, &TIM1_OCInitStructure); /*Timer1 alternate function full remapping*/ GPIO_PinRemapConfig(GPIO_FullRemap_TIM1,ENABLE); GPIO_StructInit(&GPIO_InitStructure); /* GPIOE Configuration: Channel 1, 1N, 2, 2N, 3, 3N and 4 Output */ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8 | GPIO_Pin_9 | GPIO_Pin_10; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOA, &GPIO_InitStructure); /* Lock GPIOE Pin9 and Pin11 Pin 13 (High sides) */ // GPIO_PinLockConfig(GPIOE, GPIO_Pin_9 | GPIO_Pin_11 | GPIO_Pin_13); GPIO_StructInit(&GPIO_InitStructure); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13 | GPIO_Pin_14 | GPIO_Pin_15; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOB, &GPIO_InitStructure); GPIO_StructInit(&GPIO_InitStructure); /* GPIOE Configuration: BKIN pin */ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; GPIO_Init(GPIOB, &GPIO_InitStructure); //刹车引脚默认初始化. TIM_OCStructInit(&TIM1_OCInitStructure); /* Channel 4 Configuration in OC */ TIM1_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM2; //定时器模式为PWM1. TIM1_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; //输出状态使能. TIM1_OCInitStructure.TIM_OutputNState = TIM_OutputNState_Disable; //互补输出禁止. TIM1_OCInitStructure.TIM_Pulse = PWM_PERIOD - 1; //计数脉宽(占空比). TIM1_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; //输出极性为高电平有效. TIM1_OCInitStructure.TIM_OCNPolarity = TIM_OCNPolarity_Low; //互补输出也是低电平有效. TIM1_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Reset; //空闲状态下输出低电平. TIM1_OCInitStructure.TIM_OCNIdleState = LOW_SIDE_POLARITY; //空闲装修下输出低电平. TIM_OC4Init(TIM1, &TIM1_OCInitStructure); /* Enables the TIM1 Preload on CC1 Register */ TIM_OC1PreloadConfig(TIM1, TIM_OCPreload_Enable); //使能通道1的预装载寄存器CCR1. /* Enables the TIM1 Preload on CC2 Register */ TIM_OC2PreloadConfig(TIM1, TIM_OCPreload_Enable); /* Enables the TIM1 Preload on CC3 Register */ TIM_OC3PreloadConfig(TIM1, TIM_OCPreload_Enable); /* Enables the TIM1 Preload on CC4 Register */ TIM_OC4PreloadConfig(TIM1, TIM_OCPreload_Enable); /* Automatic Output enable, Break, dead time and lock configuration*///配置刹车和死区时间. TIM1_BDTRInitStructure.TIM_OSSRState = TIM_OSSRState_Enable; //运行模式关闭状态使能. TIM1_BDTRInitStructure.TIM_OSSIState = TIM_OSSIState_Enable; //空闲模式关闭状态使能. TIM1_BDTRInitStructure.TIM_LOCKLevel = TIM_LOCKLevel_1; //锁定级别为1. TIM1_BDTRInitStructure.TIM_DeadTime = DEADTIME; TIM1_BDTRInitStructure.TIM_Break = TIM_Break_Enable; //刹车功能使能. TIM1_BDTRInitStructure.TIM_BreakPolarity = TIM_BreakPolarity_Low; //保护功能触发电平:high or low. TIM1_BDTRInitStructure.TIM_AutomaticOutput = TIM_AutomaticOutput_Disable; //自动输出禁止. TIM_BDTRConfig(TIM1, &TIM1_BDTRInitStructure); ///配置刹车和死区时间. TIM_SelectOutputTrigger(TIM1, TIM_TRGOSource_Update); //选择定时器TIM1输出触发模式. TIM_ClearITPendingBit(TIM1, TIM_IT_Break); //清除刹车标志位. TIM_ITConfig(TIM1, TIM_IT_Break,ENABLE); //使能刹车中断. /* TIM1 counter enable */ TIM_Cmd(TIM1, ENABLE); // Resynch to have the Update evend during Undeflow TIM_GenerateEvent(TIM1, TIM_EventSource_Update); // Clear Update Flag TIM_ClearFlag(TIM1, TIM_FLAG_Update); TIM_ITConfig(TIM1, TIM_IT_Update, DISABLE); //禁止定时器TIM1溢出中断. TIM_ITConfig(TIM1, TIM_IT_CC4,DISABLE); //禁止定时器TIM1通道4中断. /* ADC1 registers reset ----------------------------------------------------*/ ADC_DeInit(ADC1); /* ADC2 registers reset ----------------------------------------------------*/ ADC_DeInit(ADC2); /* Enable ADC1 */ ADC_Cmd(ADC1, ENABLE); /* Enable ADC2 */ ADC_Cmd(ADC2, ENABLE); /* ADC1 configuration ------------------------------------------------------*/ ADC_StructInit(&ADC_InitStructure); ADC_InitStructure.ADC_Mode = ADC_Mode_InjecSimult; //ADC选择注入同步模式. ADC_InitStructure.ADC_ScanConvMode = ENABLE; //扫描模式使能. ADC_InitStructure.ADC_ContinuousConvMode = DISABLE; //连续转换模式禁止. ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None; ///转换是由软件而不是由外部触发. ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Left; //数据对其方式为左对齐. ADC_InitStructure.ADC_NbrOfChannel = 1; //规则转换的ADC通道数目. ADC_Init(ADC1, &ADC_InitStructure); /* ADC2 Configuration ------------------------------------------------------*/ ADC_StructInit(&ADC_InitStructure); ADC_InitStructure.ADC_ScanConvMode = ENABLE; //扫描模式使能. ADC_InitStructure.ADC_ContinuousConvMode = DISABLE; //连续转换模式禁止. ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None; ///转换是由软件而不是由外部触发. ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Left; //数据对其方式为左对齐. ADC_InitStructure.ADC_NbrOfChannel = 1; //规则转换的ADC通道数目. ADC_Init(ADC2, &ADC_InitStructure); // Start calibration of ADC1 ADC_StartCalibration(ADC1); //上电校准ADC1. // Start calibration of ADC2 ADC_StartCalibration(ADC2); //上电校准ADC2. // Wait for the end of ADCs calibration //等待ADC校准完成. while (ADC_GetCalibrationStatus(ADC1) & ADC_GetCalibrationStatus(ADC2)) { } SVPWM_3ShuntCurrentReadingCalibration(); //3相电流读校准. /* ADC2 Injected conversions configuration */ ADC_InjectedSequencerLengthConfig(ADC2,2); ADC_InjectedChannelConfig(ADC2, PHASE_A_ADC_CHANNEL, 1, SAMPLING_TIME_CK); ADC_InjectedChannelConfig(ADC2, TEMP_FDBK_CHANNEL, 2, SAMPLING_TIME_CK); /* Configure one bit for preemption priority */ NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2); NVIC_StructInit(&NVIC_InitStructure); /* Enable the ADC Interrupt */ NVIC_InitStructure.NVIC_IRQChannel = ADC1_2_IRQChannel; //中断源. NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = ADC_PRE_EMPTION_PRIORITY; NVIC_InitStructure.NVIC_IRQChannelSubPriority = ADC_SUB_PRIORITY; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //中断使能 NVIC_Init(&NVIC_InitStructure); /* Enable the Update Interrupt */ NVIC_InitStructure.NVIC_IRQChannel = TIM1_UP_IRQChannel; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = TIM1_UP_PRE_EMPTION_PRIORITY; NVIC_InitStructure.NVIC_IRQChannelSubPriority = TIM1_UP_SUB_PRIORITY; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure); /* Enable the TIM1 BRK Interrupt */ NVIC_InitStructure.NVIC_IRQChannel = TIM1_BRK_IRQChannel; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = BRK_PRE_EMPTION_PRIORITY; NVIC_InitStructure.NVIC_IRQChannelSubPriority = BRK_SUB_PRIORITY; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure); 在执行到最后一个函数时,跳不出来! |
RE:NVIC_Init(&NVIC_InitStructure)函数执行出不来
RE:NVIC_Init(&NVIC_InitStructure)函数执行出不来