完善资料让更多小伙伴认识你,还能领取20积分哦, 立即完善>
本人在使用STM32F103设置tiM1输出6路PWM时,配置刹车中断函数NVIC_Init( amp;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( amp;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, amp;GPIO_InitStructure); GPIO_StructInit( amp;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, amp;GPIO_InitStructure); /* TIM1 Peripheral Configuration -------------------------------------------*/ /* TIM1 Registers reset */ TIM_DeInit(TIM1); TIM_TimeBaseStructInit( amp;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, amp;TIM1_TimeBaseStructure); //初始化TIM1时间计数单元 . TIM_OCStructInit( amp;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, amp;TIM1_OCInitStructure); TIM_OC2Init(TIM1, amp;TIM1_OCInitStructure); TIM_OC3Init(TIM1, amp;TIM1_OCInitStructure); /*Timer1 alternate function full remapping*/ GPIO_PinRemapConfig(GPIO_FullRemap_TIM1,ENABLE); GPIO_StructInit( amp;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, amp;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( amp;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, amp;GPIO_InitStructure); GPIO_StructInit( amp;GPIO_InitStructure); /* GPIOE Configuration: BKIN pin */ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; GPIO_Init(GPIOB, amp;GPIO_InitStructure); //刹车引脚默认初始化. TIM_OCStructInit( amp;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, amp;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, amp;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( amp;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, amp;ADC_InitStructure); /* ADC2 Configuration ------------------------------------------------------*/ ADC_StructInit( amp;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, amp;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) amp; 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( amp;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( amp;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( amp;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( amp;NVIC_InitStructure); 在执行到最后一个函数时,跳不出来! |
|
相关推荐
1个回答
|
|
1. 可能没有使能NVIC中断。请确保在使用中断之前,已经调用了`NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;`。
2. 可能没有正确设置中断优先级。请确保在使用中断之前,将`NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority`和`NVIC_InitStructure.NVIC_IRQChannelSubPriority`设置为正确的数值。 3. 可能没有正确配置TIM1的中断。请确保使用正确的TIM1中断来初始化`NVIC_InitStructure.NVIC_IRQChannel`。例如,如果您使用的是TIM1的更新中断,应该设置为`NVIC_InitStructure.NVIC_IRQChannel = TIM1_UP_IRQn;`。 |
|
|
|
只有小组成员才能发言,加入小组>>
调试STM32H750的FMC总线读写PSRAM遇到的问题求解?
1621 浏览 1 评论
X-NUCLEO-IHM08M1板文档中输出电流为15Arms,15Arms是怎么得出来的呢?
1546 浏览 1 评论
980 浏览 2 评论
STM32F030F4 HSI时钟温度测试过不去是怎么回事?
686 浏览 2 评论
ST25R3916能否对ISO15693的标签芯片进行分区域写密码?
1599 浏览 2 评论
1865浏览 9评论
STM32仿真器是选择ST-LINK还是选择J-LINK?各有什么优势啊?
648浏览 4评论
STM32F0_TIM2输出pwm2后OLED变暗或者系统重启是怎么回事?
517浏览 3评论
534浏览 3评论
stm32cubemx生成mdk-arm v4项目文件无法打开是什么原因导致的?
506浏览 3评论
小黑屋| 手机版| Archiver| 电子发烧友 ( 湘ICP备2023018690号 )
GMT+8, 2024-11-23 11:05 , Processed in 0.677243 second(s), Total 49, Slave 43 queries .
Powered by 电子发烧友网
© 2015 bbs.elecfans.com
关注我们的微信
下载发烧友APP
电子发烧友观察
版权所有 © 湖南华秋数字科技有限公司
电子发烧友 (电路图) 湘公网安备 43011202000918 号 电信与信息服务业务经营许可证:合字B2-20210191 工商网监 湘ICP备2023018690号