初级会员
- 积分
- 67
- 金钱
- 67
- 注册时间
- 2022-7-15
- 在线时间
- 11 小时
|
18金钱
各位大佬们,首先,我用TIM1的四个通道产生PWM波,然后用其他定时器进行输入捕获脉冲数,通过控制脉冲数来控制步进电机转动,当达到我设置的脉冲数后,用TIM_SetCompare1(TIM1,0);函数停止,现在的问题是pwm正常产生,但是到达指定脉冲后无法停止,启动调试,发现在while循环里的判别函数里跳不出来,代码如下,求大佬指教。- 主函数
- delay_init(); //延时函数初始化
- NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2); //设置NVIC中断分组2:2位抢占优先级,2位响应优先级
- uart_init(115200); //串口初始化为115200
- LED_Init(); //LED端口初始化
- KEY_Init(); //初始化与按键连接的硬件接口
- // PEout(4)=0; //使能
- GPIO_ResetBits(GPIOE,GPIO_Pin_4);
- PEout(5)=1; //上圆周电机方向
- PEout(6)=1; //报警
- TIM1_CH4_PWM_Init(1000, 71); //不要传奇数 50 周期是500us 100 周期是1ms
- TIM3_Init() ; //配置 TIM2_CH1_ETR 为外部脉冲计数 并且使能中断 来一个上升沿进一次中断
- TIM5_Init();
- TIM1_CH3_PWM_Init(1000, 71);
- TIM1_CH2_PWM_Init(1000, 71);
- TIM1_CH1_PWM_Init(1000, 71);
- //
- while(1)
- {
- if(TIM_GetCounter(TIM3) >= 3)
- {
- TIM_SetCompare4(TIM1,0);
- }
- if(TIM_GetCounter(TIM5) >= 3)
- {
- TIM_SetCompare1(TIM1,0);//在这个循环里无法进入停止程序,两种可能,计数问题或者停止函数问题
- }
- pwm波产生函数
- void TIM1_CH4_PWM_Init(u16 arr ,u16 psc)
- {
- GPIO_InitTypeDef GPIO_InitStructure;
- TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStructure;
- TIM_OCInitTypeDef TIM_OCInitStructure;
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1,ENABLE);
- GPIO_InitStructure.GPIO_Pin=GPIO_Pin_11 ; //|GPIO_Pin_8; //11是ch4 8 是ch1
- GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
- GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AF_PP; //复用推挽输出 //
- GPIO_Init(GPIOA,&GPIO_InitStructure);
- TIM_TimeBaseInitStructure.TIM_Period = arr; //设置自动重装载寄存器周期的值
- TIM_TimeBaseInitStructure.TIM_Prescaler = psc;//设置用来作为TIMx时钟频率预分频值 7199:10k
- TIM_TimeBaseInitStructure.TIM_ClockDivision = 0;//设置时钟分割:TDTS = Tck_tim
- TIM_TimeBaseInitStructure.TIM_CounterMode = TIM_CounterMode_Up;//TIM向上计数模式
- TIM_TimeBaseInit(TIM1, & TIM_TimeBaseInitStructure);
-
- TIM_OCInitStructure.TIM_OCMode=TIM_OCMode_PWM1;
- TIM_OCInitStructure.TIM_OutputState=TIM_OutputState_Enable;
- TIM_OCInitStructure.TIM_Pulse=arr/2;
- TIM_OCInitStructure.TIM_OCPolarity=TIM_OCPolarity_High;
- TIM_OCInitStructure.TIM_OCIdleState=TIM_OCIdleState_Set;
- TIM_OC4Init(TIM1,&TIM_OCInitStructure);
- TIM_OC4PreloadConfig(TIM1, TIM_OCPreload_Enable);
- TIM_Cmd(TIM1,ENABLE);//ê1?ü?ò??ê§?üTIMxíaéè
- //下面这句是高级定时器才有,输出PWM必须打开
- TIM_CtrlPWMOutputs(TIM1, ENABLE); //pwm输出
- }
- void TIM1_CH3_PWM_Init(u16 arr ,u16 psc)
- {
- GPIO_InitTypeDef GPIO_InitStructure;
- TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStructure;
- TIM_OCInitTypeDef TIM_OCInitStructure;
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1,ENABLE);
- GPIO_InitStructure.GPIO_Pin=GPIO_Pin_10 ; //|GPIO_Pin_8;
- GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
- GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AF_PP; //复用推挽输出 //
- GPIO_Init(GPIOA,&GPIO_InitStructure);
- TIM_TimeBaseInitStructure.TIM_Period = arr; //设置自动重装载寄存器周期的值
- TIM_TimeBaseInitStructure.TIM_Prescaler = psc;//设置用来作为TIMx时钟频率预分频值 7199:10k
- TIM_TimeBaseInitStructure.TIM_ClockDivision = 0;//设置时钟分割:TDTS = Tck_tim
- TIM_TimeBaseInitStructure.TIM_CounterMode = TIM_CounterMode_Up;//TIM向上计数模式
- TIM_TimeBaseInit(TIM1, & TIM_TimeBaseInitStructure);
-
- TIM_OCInitStructure.TIM_OCMode=TIM_OCMode_PWM1;
- TIM_OCInitStructure.TIM_OutputState=TIM_OutputState_Enable;
- TIM_OCInitStructure.TIM_Pulse=arr/2;
- TIM_OCInitStructure.TIM_OCPolarity=TIM_OCPolarity_High;
- TIM_OCInitStructure.TIM_OCIdleState=TIM_OCIdleState_Set;
- TIM_OC3Init(TIM1,&TIM_OCInitStructure);
- TIM_OC3PreloadConfig(TIM1, TIM_OCPreload_Enable);
- TIM_Cmd(TIM1,ENABLE);//ê1?ü?ò??ê§?üTIMxíaéè
- //下面这句是高级定时器才有,输出PWM必须打开
- TIM_CtrlPWMOutputs(TIM1, ENABLE); //pwm输出
- }
- void TIM1_CH2_PWM_Init(u16 arr ,u16 psc)
- {
- GPIO_InitTypeDef GPIO_InitStructure;
- TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStructure;
- TIM_OCInitTypeDef TIM_OCInitStructure;
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1,ENABLE);
- GPIO_InitStructure.GPIO_Pin=GPIO_Pin_9 ; //|GPIO_Pin_8; //11是ch4 8 是ch1
- GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
- GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AF_PP; //复用推挽输出 //
- GPIO_Init(GPIOA,&GPIO_InitStructure);
- TIM_TimeBaseInitStructure.TIM_Period = arr; //设置自动重装载寄存器周期的值
- TIM_TimeBaseInitStructure.TIM_Prescaler = psc;//设置用来作为TIMx时钟频率预分频值 7199:10k
- TIM_TimeBaseInitStructure.TIM_ClockDivision = 0;//设置时钟分割:TDTS = Tck_tim
- TIM_TimeBaseInitStructure.TIM_CounterMode = TIM_CounterMode_Up;//TIM向上计数模式
- TIM_TimeBaseInit(TIM1, & TIM_TimeBaseInitStructure);
-
- TIM_OCInitStructure.TIM_OCMode=TIM_OCMode_PWM1;
- TIM_OCInitStructure.TIM_OutputState=TIM_OutputState_Enable;
- TIM_OCInitStructure.TIM_Pulse=arr/2;
- TIM_OCInitStructure.TIM_OCPolarity=TIM_OCPolarity_High;
- TIM_OCInitStructure.TIM_OCIdleState=TIM_OCIdleState_Set;
- TIM_OC2Init(TIM1,&TIM_OCInitStructure);
- TIM_OC2PreloadConfig(TIM1, TIM_OCPreload_Enable);
- TIM_Cmd(TIM1,ENABLE);//ê1?ü?ò??ê§?üTIMxíaéè
- //下面这句是高级定时器才有,输出PWM必须打开
- TIM_CtrlPWMOutputs(TIM1, ENABLE); //pwm输出
- }
- void TIM1_CH1_PWM_Init(u16 arr ,u16 psc)
- {
- GPIO_InitTypeDef GPIO_InitStructure;
- TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStructure;
- TIM_OCInitTypeDef TIM_OCInitStructure;
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1,ENABLE);
- GPIO_InitStructure.GPIO_Pin=GPIO_Pin_8 ; //|GPIO_Pin_8; //11是ch4 8 是ch1
- GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
- GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AF_PP; //复用推挽输出 //
- GPIO_Init(GPIOA,&GPIO_InitStructure);
- TIM_TimeBaseInitStructure.TIM_Period = arr; //设置自动重装载寄存器周期的值
- TIM_TimeBaseInitStructure.TIM_Prescaler = psc;//设置用来作为TIMx时钟频率预分频值 7199:10k
- TIM_TimeBaseInitStructure.TIM_ClockDivision = 0;//设置时钟分割:TDTS = Tck_tim
- TIM_TimeBaseInitStructure.TIM_CounterMode = TIM_CounterMode_Up;//TIM向上计数模式
- TIM_TimeBaseInit(TIM1, & TIM_TimeBaseInitStructure);
-
- TIM_OCInitStructure.TIM_OCMode=TIM_OCMode_PWM1;
- TIM_OCInitStructure.TIM_OutputState=TIM_OutputState_Enable;
- TIM_OCInitStructure.TIM_Pulse=arr/2;
- TIM_OCInitStructure.TIM_OCPolarity=TIM_OCPolarity_High;
- TIM_OCInitStructure.TIM_OCIdleState=TIM_OCIdleState_Set;
- TIM_OC1Init(TIM1,&TIM_OCInitStructure);
- TIM_OC1PreloadConfig(TIM1, TIM_OCPreload_Enable);
- TIM_Cmd(TIM1,ENABLE);//ê1?ü?ò??ê§?üTIMxíaéè
- //下面这句是高级定时器才有,输出PWM必须打开
- TIM_CtrlPWMOutputs(TIM1, ENABLE); //pwm输出
- }
- 输入捕获函数
- void TIM3_Init(void)
- {
- GPIO_InitTypeDef GPIO_InitStructure;
- TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
- NVIC_InitTypeDef NVIC_InitStructure;
-
- RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE); //使能TIM3时钟
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOD, ENABLE); //使能GPIOA时钟
-
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2; //PA0 清除之前设置
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPD; //PA0 输入
- GPIO_Init(GPIOD, &GPIO_InitStructure);
- GPIO_ResetBits(GPIOD,GPIO_Pin_2); //PA0 下拉
-
- //初始化定时器2 TIM2
- TIM_TimeBaseStructure.TIM_Period = 0xFFFF; //设定计数器自动重装值
- TIM_TimeBaseStructure.TIM_Prescaler =0; //预分频器
- TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1; //设置时钟分割:TDTS = Tck_tim
- TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //TIM向上计数模式
- TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure); //根据TIM_TimeBaseInitStruct中指定的参数初始化TIMx的时间基数单位
- TIM_ITRxExternalClockConfig(TIM3,TIM_TS_ETRF); //配置外部触发,否则不会计数
- TIM_ETRClockMode2Config(TIM3, TIM_ExtTRGPSC_OFF, TIM_ExtTRGPolarity_NonInverted, 0); //TIM_ExtTRGPolarity_Inverted 下降沿
- TIM_SetCounter(TIM3, 0);
- TIM_Cmd(TIM3,ENABLE ); //使能定时器
-
- TIM_ITConfig( //使能或者失能指定的TIM中断
- TIM3, //TIM2
- TIM_IT_Trigger ,
- ENABLE //使能
- );
- NVIC_InitStructure.NVIC_IRQChannel = TIM3_IRQn; //TIM3中断
- NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1; //先占优先级0级
- NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1; //从优先级3级
- NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQ通道被使能
- NVIC_Init(&NVIC_InitStructure); //根据NVIC_InitStruct中指定的参数初始化外设NVIC寄存器
- //Cnt = TIM_GetCounter(TIM3); // TIM_SetCounter(TIM3,0);
- }
- void TIM3_IRQHandler(void) //TIM3中断
- {
- if (TIM_GetITStatus(TIM3, TIM_IT_Trigger) != RESET) //检查指定的TIM中断发生与否:TIM 中断源
- {
- //time_cnt_linshi ++;
- TIM_ClearITPendingBit(TIM3, TIM_IT_Trigger ); //清除TIMx的中断待处理位:TIM 中断源
-
- //if(time_cnt_linshi==4) //这里 4 就表示输出4 个上升沿就停止输出
-
- //{
- //TIM_Cmd(TIM1, DISABLE);
- //time_cnt_linshi = 0;
- //}
- }
- }
- /* 指定个脉冲停止输出用这个
- void TIM3_IRQHandler(void) //TIM3中断
- {
- if (TIM_GetITStatus(TIM3, TIM_IT_Trigger) != RESET) //检查指定的TIM中断发生与否:TIM 中断源
- {
- time_cnt_linshi ++;
- TIM_ClearITPendingBit(TIM3, TIM_IT_Trigger ); //清除TIMx的中断待处理位:TIM 中断源
-
- if(time_cnt_linshi==4) //这里 4 就表示输出4 个上升沿就停止输出
-
- {
- TIM_Cmd(TIM1, DISABLE); //使能TIMx外设
- time_cnt_linshi = 0;
- }
- }
- }
- */
- void TIM5_Init(void)
- {
- GPIO_InitTypeDef GPIO_InitStructure;
- TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
- NVIC_InitTypeDef NVIC_InitStructure;
-
- RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM5, ENABLE); //使能TIM4时钟
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE); //使能GPIOE时钟
-
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0; //PE0 清除之前设置
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPD; //PE0 输入
- GPIO_Init(GPIOA, &GPIO_InitStructure);
- GPIO_ResetBits(GPIOA,GPIO_Pin_0); //PE0 下拉
-
- //初始化定时器5 TIM5
- TIM_TimeBaseStructure.TIM_Period = 0xFFFF; //设定计数器自动重装值
- TIM_TimeBaseStructure.TIM_Prescaler =0; //预分频器
- TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1; //设置时钟分割:TDTS = Tck_tim
- TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //TIM向上计数模式
- TIM_TimeBaseInit(TIM5, &TIM_TimeBaseStructure); //根据TIM_TimeBaseInitStruct中指定的参数初始化TIMx的时间基数单位
- TIM_ITRxExternalClockConfig(TIM5,TIM_TS_ETRF); //配置外部触发,否则不会计数
- TIM_ETRClockMode2Config(TIM5, TIM_ExtTRGPSC_OFF, TIM_ExtTRGPolarity_NonInverted, 0); //TIM_ExtTRGPolarity_Inverted 下降沿
- TIM_SetCounter(TIM4, 0);
- TIM_Cmd(TIM5,ENABLE ); //使能定时器
-
- TIM_ITConfig( //使能或者失能指定的TIM中断
- TIM5, //TIM4
- TIM_IT_Trigger ,
- ENABLE //使能
- );
- NVIC_InitStructure.NVIC_IRQChannel = TIM5_IRQn; //TIM4中断
- NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1; //先占优先级0级
- NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1; //从优先级3级
- NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQ通道被使能
- NVIC_Init(&NVIC_InitStructure); //根据NVIC_InitStruct中指定的参数初始化外设NVIC寄存器
- //Cnt = TIM_GetCounter(TIM3); // TIM_SetCounter(TIM3,0);
- }
- void TIM5_IRQHandler(void) //TIM4中断
- {
- if (TIM_GetITStatus(TIM5, TIM_IT_Trigger) != RESET) //检查指定的TIM中断发生与否:TIM 中断源
- {
- TIM_ClearITPendingBit(TIM5, TIM_IT_Trigger ); //清除TIMx的中断待处理位:TIM 中断源
-
- }}
复制代码
|
|