本文主要是介绍stm32学习记录——舵机与超声波测距的感应垃圾桶(附工程码),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
首先是超声波测距的代码,原理就是定时器产生PWM波控制舵机旋转到指定角度。
void HC_SR04Gpio1Init(void)
{GPIO_InitTypeDef GPIO_InitStructure;RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOA|RCC_APB2Periph_GPIOB, ENABLE);GPIO_InitStructure.GPIO_Pin=GPIO_Pin_0; //ECHOGPIO_InitStructure.GPIO_Mode=GPIO_Mode_IPD;GPIO_Init(GPIOB,&GPIO_InitStructure);GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1; //接TRIG 触发控制信号输入GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOB, &GPIO_InitStructure);
}void Timer3_Init(uint16 psc,uint16 arr)
{RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3,ENABLE);TIM_DeInit(TIM3);TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStructure;TIM_TimeBaseInitStructure.TIM_Prescaler=psc;TIM_TimeBaseInitStructure.TIM_CounterMode=TIM_CounterMode_Up;TIM_TimeBaseInitStructure.TIM_Period=arr;TIM_TimeBaseInitStructure.TIM_ClockDivision=TIM_CKD_DIV1;TIM_TimeBaseInit(TIM3,&TIM_TimeBaseInitStructure);TIM_ICInitTypeDef TIM_ICInitStructure;TIM_ICInitStructure.TIM_Channel=TIM_Channel_3;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=0;TIM_ICInit(TIM3,&TIM_ICInitStructure);TIM_ClearITPendingBit(TIM3,TIM_IT_Update|TIM_IT_CC3);TIM_ITConfig(TIM3,TIM_IT_Update|TIM_IT_CC3,ENABLE);NVIC_InitTypeDef NVIC_InitStructure;NVIC_InitStructure.NVIC_IRQChannel=TIM3_IRQn;NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=0;NVIC_InitStructure.NVIC_IRQChannelSubPriority=2;NVIC_InitStructure.NVIC_IRQChannelCmd=ENABLE;NVIC_Init(&NVIC_InitStructure);TIM_Cmd(TIM3,ENABLE);}
InputCapture icCapture={RESET,0,0,0,0};
uint32 Distance=0;
void TIM3_IRQHandler(void)
{if(icCapture.icFinishFlag==0){if(TIM_GetITStatus(TIM3,TIM_IT_CC3)!= RESET){if(icCapture.Edge==RESET){icCapture.Edge=SET;TIM_SetCounter(TIM3,0);TIM_OC3PolarityConfig(TIM3,TIM_OCPolarity_Low);}else{icCapture.icFinishFlag=1;icCapture.icCaptureVal=TIM_GetCapture3(TIM3);Distance=(icCapture.icUpdateCnt*65535+icCapture.icCaptureVal)*340/20000; icCapture.icUpdateCnt=0;icCapture.Edge=RESET;TIM_OC3PolarityConfig(TIM3,TIM_OCPolarity_High);icCapture.icFinishFlag=0;}}if(TIM_GetITStatus(TIM3,TIM_IT_Update)!= RESET){if(icCapture.Edge==SET){icCapture.icUpdateCnt++;printf("The icUpdateCnt is:%d\r\n",icCapture.icUpdateCnt);}}}TIM_ClearITPendingBit(TIM3,TIM_IT_Update|TIM_IT_CC3);
}
再来就是舵机。
void Duoji_Tim4_Init(u16 arr,u16 psc)
{GPIO_InitTypeDef GPIO_InitStructure;TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;TIM_OCInitTypeDef TIM_OCInitStructure;RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4, ENABLE); //使能定时器4时钟RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE); //使能GPIO外设和AFIO复用功能模块时钟 326389//设置该引脚为复用输出功能,输出TIM4 CH2的PWM脉冲波形 GPIOB.6GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 ; //TIM_CH1-4GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //复用推挽输出GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;GPIO_Init(GPIOB, &GPIO_InitStructure); //初始化GPIO//**************初始化TIM4*****************//TIM_TimeBaseStructure.TIM_Period = arr; //设置在下一个更新事件装入活动的自动重装载寄存器周期的值TIM_TimeBaseStructure.TIM_Prescaler =psc; //设置用来作为TIMx时钟频率除数的预分频值 TIM_TimeBaseStructure.TIM_ClockDivision = 0; //设置时钟分割:TDTS = Tck_timTIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //TIM向上计数模式TIM_TimeBaseInit(TIM4, &TIM_TimeBaseStructure); //根据TIM_TimeBaseInitStruct中指定的参数初始化TIMx的时间基数单位//*******初始化TIM4 Channel2 PWM模式********// TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1; //选择定时器模式:TIM脉冲宽度调制模式2TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; //比较输出使能TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; //输出极性:TIM输出比较极性高TIM_OC1Init(TIM4, &TIM_OCInitStructure); //根据T指定的参数初始化外设TIM4 OC1TIM_OC1PreloadConfig(TIM4, TIM_OCPreload_Enable); //使能TIM4在CCR2上的预装载寄存器TIM_Cmd(TIM4, ENABLE); //使能TIM4
}
最后附上可以使用的工程代码
链接:https://pan.baidu.com/s/1ADlwZHGD430xtg6XPdg8VQ
提取码:9438
这篇关于stm32学习记录——舵机与超声波测距的感应垃圾桶(附工程码)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!