舵机和超声波冲突了,应该怎么设置呀,定时器2.3用于编码器测速了
单片机源程序如下:
- #include "moto.h"
- #include "usart.h"
- #include "delay.h"
- #include "pwm.h"
- /***************** *********************************************************
- 函数功能:舵机PWM以及定时中断初始化
- 入口参数:入口参数:arr:自动重装值 psc:时钟预分频数
- 返回 值:无
- **************************************************************************/
- uint16_t count;
- void TRIG_Init(void)
- {
-
- GPIO_InitTypeDef GPIO_InitStructure;
-
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE); //使能PB端口时钟
- /*TRIG 触发信号 PB14*/
- GPIO_InitStructure.GPIO_Pin=GPIO_Pin_12;//引脚14
- GPIO_InitStructure.GPIO_Mode=GPIO_Mode_Out_PP; //复用推挽输出模式,定时器功能为A0引脚复用功能
- GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz; //定义该引脚输出速度为50MHZ
- GPIO_Init(GPIOB, &GPIO_InitStructure); //初始化引脚GPIOA0
-
- GPIO_SetBits(GPIOB,GPIO_Pin_12);
- }
- void Motor_PWM_Init(u16 arr,u16 psc)
- {
- TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStruct;
- TIM_OCInitTypeDef TIM_OCInitStruct;
- GPIO_InitTypeDef GPIO_InitStruct;
-
- RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4,ENABLE); //使能定时器4时钟
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE); //使能GPIOB的时钟
-
- GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF_PP; //复用推挽输出
- GPIO_InitStruct.GPIO_Pin = GPIO_Pin_6|GPIO_Pin_7|GPIO_Pin_8|GPIO_Pin_9; //PB6 7 8 9
- GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_Init(GPIOB,&GPIO_InitStruct);
-
- TIM_TimeBaseInitStruct.TIM_Period = arr; //设定计数器自动重装值
- TIM_TimeBaseInitStruct.TIM_Prescaler = psc; //设定预分频器
- TIM_TimeBaseInitStruct.TIM_CounterMode = TIM_CounterMode_Up;//TIM向上计数模式
- TIM_TimeBaseInitStruct.TIM_ClockDivision = 0; //设置时钟分割
- TIM_TimeBaseInit(TIM4,&TIM_TimeBaseInitStruct); //初始化定时器
-
- TIM_OCInitStruct.TIM_OCMode = TIM_OCMode_PWM1; //选择PWM1模式
- TIM_OCInitStruct.TIM_OutputState = TIM_OutputState_Enable; //比较输出使能
- TIM_OCInitStruct.TIM_Pulse = 0; //设置待装入捕获比较寄存器的脉冲值
- TIM_OCInitStruct.TIM_OCPolarity = TIM_OCPolarity_High; //设置输出极性
- TIM_OC1Init(TIM4,&TIM_OCInitStruct); //初始化输出比较参数
-
- TIM_OCInitStruct.TIM_OCMode = TIM_OCMode_PWM1; //选择PWM1模式
- TIM_OCInitStruct.TIM_OutputState = TIM_OutputState_Enable; //比较输出使能
- TIM_OCInitStruct.TIM_Pulse = 0; //设置待装入捕获比较寄存器的脉冲值
- TIM_OCInitStruct.TIM_OCPolarity = TIM_OCPolarity_High; //设置输出极性
- TIM_OC2Init(TIM4,&TIM_OCInitStruct); //初始化输出比较参数
-
- TIM_OCInitStruct.TIM_OCMode = TIM_OCMode_PWM1; //选择PWM1模式
- TIM_OCInitStruct.TIM_OutputState = TIM_OutputState_Enable; //比较输出使能
- TIM_OCInitStruct.TIM_Pulse = 0; //设置待装入捕获比较寄存器的脉冲值
- TIM_OCInitStruct.TIM_OCPolarity = TIM_OCPolarity_High; //设置输出极性
- TIM_OC3Init(TIM4,&TIM_OCInitStruct); //初始化输出比较参数
-
- TIM_OCInitStruct.TIM_OCMode = TIM_OCMode_PWM1; //选择PWM1模式
- TIM_OCInitStruct.TIM_OutputState = TIM_OutputState_Enable; //比较输出使能
- TIM_OCInitStruct.TIM_Pulse = 0; //设置待装入捕获比较寄存器的脉冲值
- TIM_OCInitStruct.TIM_OCPolarity = TIM_OCPolarity_High; //设置输出极性
- TIM_OC4Init(TIM4,&TIM_OCInitStruct); //初始化输出比较参数
-
- TIM_OC1PreloadConfig(TIM4,TIM_OCPreload_Enable); //CH1使能预装载寄存器
- TIM_OC2PreloadConfig(TIM4,TIM_OCPreload_Enable); //CH2使能预装载寄存器
- TIM_OC3PreloadConfig(TIM4,TIM_OCPreload_Enable); //CH3使能预装载寄存器
- TIM_OC4PreloadConfig(TIM4,TIM_OCPreload_Enable); //CH4使能预装载寄存器
-
- TIM_ARRPreloadConfig(TIM4, ENABLE); //使能TIM4在ARR上的预装载寄存器
-
- TIM_Cmd(TIM4,ENABLE); //使能定时器4
- }
- /***************** *********************************************************
- 函数功能:舵机PWM以及定时中断初始化
- 入口参数:入口参数:arr:自动重装值 psc:时钟预分频数
- 返回 值:无
- **************************************************************************/
- void Servo_PWM_Init(u16 arr,u16 psc)
- {
- TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStruct;
- NVIC_InitTypeDef NVIC_InitStruct;
- TIM_OCInitTypeDef TIM_OCInitStruct;
- GPIO_InitTypeDef GPIO_InitStruct;
- // TIM_ICInitTypeDef TIM3_ICInitStructure; //定义一个定时器捕获输入初始化的结构体
- //
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1,ENABLE); //使能定时器1时钟
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_GPIOB,ENABLE); //使能GPIOA的时钟
-
- GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF_PP; //复用推挽输出
- GPIO_InitStruct.GPIO_Pin = GPIO_Pin_11; //PA11
- GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_Init(GPIOA,&GPIO_InitStruct);
-
- /*ECOH 回响信号 PB7*/
- GPIO_InitStruct.GPIO_Pin=GPIO_Pin_8;//引脚13
- GPIO_InitStruct.GPIO_Mode=GPIO_Mode_IN_FLOATING; //设置为浮空输入
- GPIO_InitStruct.GPIO_Speed=GPIO_Speed_50MHz; //定义该引脚输出速度为50MHZ
- GPIO_Init(GPIOA, &GPIO_InitStruct); //初始化引脚GPIOA0
-
-
- TIM_TimeBaseInitStruct.TIM_Period = arr; //设定计数器自动重装值
- TIM_TimeBaseInitStruct.TIM_Prescaler = psc; //设定预分频器
- TIM_TimeBaseInitStruct.TIM_CounterMode = TIM_CounterMode_Up;//TIM向上计数模式
- TIM_TimeBaseInitStruct.TIM_ClockDivision = 0; //设置时钟分割
- TIM_TimeBaseInitStruct.TIM_RepetitionCounter=0;
- TIM_TimeBaseInit(TIM1,&TIM_TimeBaseInitStruct); //初始化定时器
-
- TIM_ITConfig(TIM1,TIM_IT_Update,ENABLE); //使能定时器中断
-
-
- NVIC_InitStruct.NVIC_IRQChannel = TIM1_UP_IRQn; //使能外部中断通道
- NVIC_InitStruct.NVIC_IRQChannelCmd = ENABLE; //使能外部中断通道
- NVIC_InitStruct.NVIC_IRQChannelPreemptionPriority = 1; //抢占优先级1
- NVIC_InitStruct.NVIC_IRQChannelSubPriority = 1; //响应优先级1
- NVIC_Init(&NVIC_InitStruct);
-
- TIM_OCInitStruct.TIM_OCMode = TIM_OCMode_PWM1; //选择PWM1模式
- TIM_OCInitStruct.TIM_OutputState = TIM_OutputState_Enable; //比较输出使能
- TIM_OCInitStruct.TIM_Pulse = 0; //设置待装入捕获比较寄存器的脉冲值
- TIM_OCInitStruct.TIM_OCPolarity = TIM_OCPolarity_High; //设置输出极性
- TIM_OC4Init(TIM1,&TIM_OCInitStruct); //初始化输出比较参数
-
- TIM_OC4PreloadConfig(TIM1,TIM_OCPreload_Enable); //CH4使能预装载寄存器
- TIM_OC2PreloadConfig(TIM1,TIM_OCPreload_Enable); //CH4使能预装载寄存器
- TIM_OC1PreloadConfig(TIM1,TIM_OCPreload_Enable); //CH4使能预装载寄存器
- TIM_OC3PreloadConfig(TIM1,TIM_OCPreload_Enable); //CH4使能预装载寄存器
-
- TIM_CtrlPWMOutputs(TIM1,ENABLE); //高级定时器输出需要设置这句
-
- TIM_ARRPreloadConfig(TIM1, ENABLE); //使能TIM1在ARR上的预装载寄存器
- TIM_ClearFlag(TIM1, TIM_FLAG_Update); // Clears the TIMx's pending flags.
- // TIM1->CCR4=1400;
- //
- // TIM_ITConfig(TIM1,TIM_IT_Update|TIM_IT_CC1,ENABLE);//允许TIM1更新中断|允许TIM1通道1捕获中断
- TIM_Cmd(TIM1,ENABLE); //使能定时器1
- }
- void Count_Dist(void)
- {
- int distance = 0, sum = 0;
- uint16_t time;
- uint16_t i = 0;
- delay_init(); //在 delay.h 中
- TRIG_Init();
- while(i < 5){
- GPIO_SetBits(GPIOB, GPIO_Pin_12);
- delay_us(20);
- GPIO_ResetBits(GPIOB, GPIO_Pin_12);
- TIM_Cmd(TIM1,DISABLE); //关闭定时器
- TIM1->CCR1=0; //TIM3的计数器清零
- while(GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_8) == 0);
- TIM_Cmd(TIM1, ENABLE); //打开定时器
- i += 1;
- while(GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_8) == 1); //回响信号消失
- TIM_Cmd(TIM1,DISABLE); //关闭定时器
- time = TIM_GetCounter(TIM1); //获取 TIM2 计数值; //获取 TIM2 计数值
- distance = (time + count*1000)/58.0; //通过回响信号计算距离
- sum = distance + sum; //将五次结果相加
- TIM1->CCR1 = 0; //将TIM2计数寄存器的计数值清零
- count = 0; //中断溢出次数清零
- delay_ms(10);
- }
- distance = (sum/5); //取5次的平均值
- printf("%d \n",distance);
-
- }
- void TIM1_UP_IRQHandler(void) { //中断函数
- if(TIM_GetITStatus(TIM1,TIM_IT_Update) != RESET) {
- TIM_ClearITPendingBit(TIM1, TIM_IT_Update); //清除中断标志
- count++;
- }
- }
复制代码
|