21ic问答首页 - TIM3捕获配置时,只要一NVIC_Init使能中断就卡死,咨询下大家
TIM3捕获配置时,只要一NVIC_Init使能中断就卡死,咨询下大家
AProgrammer2023-12-01
STM32F103ZET6板子,想用TIM3_CH2来捕获TIM8 CH3输出的pwm,为此将pwm TIM8_CH3输出的管脚PC8 和 TIM3_CH2管脚PA7 连起来后,发现程序执行不下去了(不连起来就能执行)。卡死了好像。请帮看下可能原因。
做过的尝试:
1、自己把TIM3 CH2的优先级改成最高(抢占优先级为0,响应优先级也为0),也不行。
2、PC8 和 TIM3_CH2管脚PA7 连线去除,发现程序能执行下去
3、中断不使能,就是不执行Nvic_init也可以执行下去,说明肯定是和中断相关。
TIM3_CH2初始化的代码:
void InitFan3InletFG(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
TIM_ICInitTypeDef TIM_ICInitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
NVIC_InitTypeDef NVIC_InitStructure;
//初始化GPIO复用
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
//初始化定时器
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);
TIM_TimeBaseStructure.TIM_Period = 0xFFFF-1;
TIM_TimeBaseStructure.TIM_Prescaler = 72-1; //1us,总时间65ms
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure);
/* IC2,上升沿触发 TI2FP2 */
TIM_ICInitStructure.TIM_Channel = TIM_Channel_2;
TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Rising;
TIM_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI;
TIM_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1;
TIM_ICInitStructure.TIM_ICFilter = 0x0;
TIM_PWMIConfig(TIM3, &TIM_ICInitStructure);
#if 0
/* IC2, 下降沿触发,TI2FP1*/
TIM_ICInitStructure.TIM_Channel = TIM_Channel_1;
TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Falling;
TIM_ICInitStructure.TIM_ICSelection = TIM_ICSelection_IndirectTI;
TIM_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1;
TIM_ICInitStructure.TIM_ICFilter = 0x0;
TIM_PWMIConfig(TIM5, &TIM_ICInitStructure);
#endif
TIM_SelectInputTrigger(TIM3, TIM_TS_TI2FP2);
TIM_SelectSlaveMode(TIM3, TIM_SlaveMode_Reset);
TIM_SelectMasterSlaveMode(TIM3,TIM_MasterSlaveMode_Enable);
TIM_Cmd(TIM3, ENABLE);
//使能CC2捕获中断,主
//// TIM_ClearITPendingBit(TIM3, TIM_IT_CC2); //20231130 1101: try add this
TIM_ITConfig(TIM3, TIM_IT_CC2, ENABLE);
//使能中断
NVIC_InitStructure.NVIC_IRQChannel = TIM3_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; //3; //0; //1; // 3; //20231130pm5 try modified to 1
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
/////// NVIC_Init(&NVIC_InitStructure); ///////1130pm15: disable try
NVIC_Init(&NVIC_InitStructure);
}
中断处理代码:
void TIM3_IRQHandler(void)
{
Timer3ISR();
}
void Timer3ISR(void)
{
int ret;
g_tim3Count++;
if (g_tim3Count % 100 == 0) {
//printf("timer3 got\r\n");
}
TIM_ClearITPendingBit(TIM3, TIM_IT_CC2);
TIM_ClearITPendingBit(TIM3, TIM_IT_CC1);
TIM_ClearITPendingBit(TIM3, TIM_IT_CC3);
TIM_ClearITPendingBit(TIM3, TIM_IT_CC4);
//TIM3, CH2通道
ret = TIM_GetFlagStatus(TIM3, TIM_FLAG_CC2);
if (ret)
{
TIM_ClearITPendingBit(TIM3, TIM_IT_CC2);
Tim3Ch2Isr();
}
//TIM3, CH1通道
ret = TIM_GetFlagStatus(TIM3, TIM_FLAG_CC1);
if (ret)
{
TIM_ClearITPendingBit(TIM3, TIM_IT_CC1);
Tim3Ch1Isr();
}
//TIM3, CH3通道
ret = TIM_GetFlagStatus(TIM3, TIM_FLAG_CC3);
if (ret)
{
TIM_ClearITPendingBit(TIM3, TIM_IT_CC3);
Tim3Ch3Isr();
}
//TIM3, CH4通道
ret = TIM_GetFlagStatus(TIM3, TIM_FLAG_CC4);
if (ret)
{
TIM_ClearITPendingBit(TIM3, TIM_IT_CC4);
Tim3Ch4Isr();
}
}
做过的尝试:
1、自己把TIM3 CH2的优先级改成最高(抢占优先级为0,响应优先级也为0),也不行。
2、PC8 和 TIM3_CH2管脚PA7 连线去除,发现程序能执行下去
3、中断不使能,就是不执行Nvic_init也可以执行下去,说明肯定是和中断相关。
TIM3_CH2初始化的代码:
void InitFan3InletFG(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
TIM_ICInitTypeDef TIM_ICInitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
NVIC_InitTypeDef NVIC_InitStructure;
//初始化GPIO复用
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
//初始化定时器
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);
TIM_TimeBaseStructure.TIM_Period = 0xFFFF-1;
TIM_TimeBaseStructure.TIM_Prescaler = 72-1; //1us,总时间65ms
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure);
/* IC2,上升沿触发 TI2FP2 */
TIM_ICInitStructure.TIM_Channel = TIM_Channel_2;
TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Rising;
TIM_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI;
TIM_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1;
TIM_ICInitStructure.TIM_ICFilter = 0x0;
TIM_PWMIConfig(TIM3, &TIM_ICInitStructure);
#if 0
/* IC2, 下降沿触发,TI2FP1*/
TIM_ICInitStructure.TIM_Channel = TIM_Channel_1;
TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Falling;
TIM_ICInitStructure.TIM_ICSelection = TIM_ICSelection_IndirectTI;
TIM_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1;
TIM_ICInitStructure.TIM_ICFilter = 0x0;
TIM_PWMIConfig(TIM5, &TIM_ICInitStructure);
#endif
TIM_SelectInputTrigger(TIM3, TIM_TS_TI2FP2);
TIM_SelectSlaveMode(TIM3, TIM_SlaveMode_Reset);
TIM_SelectMasterSlaveMode(TIM3,TIM_MasterSlaveMode_Enable);
TIM_Cmd(TIM3, ENABLE);
//使能CC2捕获中断,主
//// TIM_ClearITPendingBit(TIM3, TIM_IT_CC2); //20231130 1101: try add this
TIM_ITConfig(TIM3, TIM_IT_CC2, ENABLE);
//使能中断
NVIC_InitStructure.NVIC_IRQChannel = TIM3_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; //3; //0; //1; // 3; //20231130pm5 try modified to 1
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
/////// NVIC_Init(&NVIC_InitStructure); ///////1130pm15: disable try
NVIC_Init(&NVIC_InitStructure);
}
中断处理代码:
void TIM3_IRQHandler(void)
{
Timer3ISR();
}
void Timer3ISR(void)
{
int ret;
g_tim3Count++;
if (g_tim3Count % 100 == 0) {
//printf("timer3 got\r\n");
}
TIM_ClearITPendingBit(TIM3, TIM_IT_CC2);
TIM_ClearITPendingBit(TIM3, TIM_IT_CC1);
TIM_ClearITPendingBit(TIM3, TIM_IT_CC3);
TIM_ClearITPendingBit(TIM3, TIM_IT_CC4);
//TIM3, CH2通道
ret = TIM_GetFlagStatus(TIM3, TIM_FLAG_CC2);
if (ret)
{
TIM_ClearITPendingBit(TIM3, TIM_IT_CC2);
Tim3Ch2Isr();
}
//TIM3, CH1通道
ret = TIM_GetFlagStatus(TIM3, TIM_FLAG_CC1);
if (ret)
{
TIM_ClearITPendingBit(TIM3, TIM_IT_CC1);
Tim3Ch1Isr();
}
//TIM3, CH3通道
ret = TIM_GetFlagStatus(TIM3, TIM_FLAG_CC3);
if (ret)
{
TIM_ClearITPendingBit(TIM3, TIM_IT_CC3);
Tim3Ch3Isr();
}
//TIM3, CH4通道
ret = TIM_GetFlagStatus(TIM3, TIM_FLAG_CC4);
if (ret)
{
TIM_ClearITPendingBit(TIM3, TIM_IT_CC4);
Tim3Ch4Isr();
}
}
赞1
评论
2023-12-04
@一叶倾城wwq :定位了,是自己的问题,把中断向量表地址设置错了。感谢您的热心支持!
@AProgrammer :能仿真吗?仿真看一下,不一定用信号发生器,用另一个板子的芯片发pwm就行了
@一叶倾城wwq :“pwm TIM8_CH3输出的管脚PC8 和 TIM3_CH2管脚PA7 连起来“,是的,我现在就是TIM8输出的,TIM3想测量的。而且示波器在PC8量了,有持续的方波,占空比是50%, 真是郁闷,不知道为啥卡。
@AProgrammer :还有你输出pwm的定时器和捕获的定时器不要用一个,比如说用timer3输出pwm,用timer1去捕获,尽量分开操作,互不干扰
@AProgrammer :限流,保护引脚,防止一个输出高一个输出低,造成短路
@一叶倾城wwq :为啥要穿个100欧姆的电阻?
@AProgrammer :理论上是可以的,你穿个100欧电阻
能把输出管脚和这个定时器的输入直接连起来吗?我现在没有信号发生器
您需要登录后才可以回复 登录 | 注册