STM32单片机源程序如下:
- #include "led.h"
- #include "delay.h"
- #include "key.h"
- #include "sys.h"
- #include "usart.h"
- #include "timer.h"
- #include "motor.h"
- #include "sg90.h"
- #include "HCSR.h"
- #include "oled.h"
- #include "beep.h"
- #include "motorpwm.h"
- #include "TCRT.h"
- #define motor1_PWM TIM1->CCR1
- #define motor3_PWM TIM1->CCR3
- #define motor2_PWM TIM1->CCR2
-
- #define motor4_PWM TIM1->CCR4
- // motor11=1; PA7
- // motor12=0; PA6
- //
- // motor21=1; PB7
- // motor21=0; PB6
- extern u8 res;
-
- int main(void)
- {
- int len;
-
- delay_init(); //延时函数初始化
- NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2); //设置NVIC中断分组2:2位抢占优先级,2位响应优先级
- uart_init(9600); //串口初始化为9600
- My_USART2_Init();
- OLED_Init();
- OLED_Clear();
- motor_pwm_init();
- Motor_Init();
- TIM3_PWM_Init(199,7199); //不分频。T为20ms,这样的配置就是为了让输出的PWM信号达到舵机要求的20ms周期。
- SG90_pwm_init();
- TCRT_config(); //循迹模块初始化
- CH_SR04_Init();
- motor1_PWM=0;motor3_PWM =0;motor21=0; motor22=0;
- // motor1_PWM=10;motor2_PWM=10;motor3_PWM =10;motor4_PWM =10;
- beep_Init();
- while(1)
- {
- len =Senor_Using();
- // printf("距离为:%dcm\n",length);
- OLED_ShowCHinese(0,0,0);//前
- OLED_ShowCHinese(18,0,1);//方
- OLED_ShowCHinese(36,0,2);//距
- OLED_ShowCHinese(54,0,3);//离
- OLED_ShowCHinese(72,0,4);//为
- OLED_Float(3,3,len,1);
- delay_ms(20);
- // if(res=='1'){motor11=1; motor12=0;motor21=1; motor22=0;TIM_SetCompare2(TIM3,175);}//前进右转90
- if(res=='1')//避障
- {
-
- float length_res[5]; //用来存放测距结果
- len =Senor_Using();
- // printf("距离为:%dcm\n",length);
- OLED_ShowCHinese(0,0,0);//前
- OLED_ShowCHinese(18,0,1);//方
- OLED_ShowCHinese(36,0,2);//距
- OLED_ShowCHinese(54,0,3);//离
- OLED_ShowCHinese(72,0,4);//为
- OLED_Float(3,3,len,1);
- delay_ms(20);
- SG90_Front; //舵机摆正
- delay_ms(100);
- length_res[0] =Senor_Using(); //测前方距离放在数组里
- delay_ms(100);
-
-
- if(length_res[0]>40.00) //如果前方距离大于30cm 前进
- {
- motor1_PWM=70;motor3_PWM =70; motor21=0; motor22=0;
- SG90_Front;
- TIM_SetCompare2(TIM3,185); //方向回中
-
- }
-
- if(length_res[0]<40.00) //如果前方距离小于30厘米 停车测左右距离
- {
- beep=1;
-
- motor1_PWM=0;motor3_PWM =0;motor21=0; motor22=0;
-
- SG90_Left_45; //舵机左转45度测距
- delay_ms(700);beep=0;
- length_res[1] =Senor_Using(); //把测量结果放进数组
- SG90_Right_45; //舵机右转45度测距
- delay_ms(700);
- length_res[4] =Senor_Using(); //把测量结果放进数组
-
- SG90_Front; //舵机摆正
- delay_ms(100);
- if(length_res[1]>length_res[4]) //如果左边的距离大于右边的距离
- {
- do //舵机摆正
- {
- SG90_Front;
- delay_ms(10);
- length_res[0] =Senor_Using(); //重复测前方的距离同时左转
- delay_ms(10);
- TIM_SetCompare2(TIM3,195);
- motor1_PWM=75;motor3_PWM =75; motor21=0; motor22=0;
- }
- while(length_res[0]<40.00); //一直转到前方距离大于30cm
- }
- if(length_res[1]<length_res[4]) //如果右边的距离大于左边的距离
- {
- do
- {
- SG90_Front;
- delay_ms(10);
- length_res[0] =Senor_Using();
- delay_ms(10);
- TIM_SetCompare2(TIM3,175);
- motor1_PWM=75;motor3_PWM =75; motor21=0; motor22=0;
- }
- while(length_res[0]<40.00); //一直转到前方距离大于30cm
- }
-
- }
-
-
- }
- if(res=='2')
- {
- if(Senor_Using()<30)
- { beep=1;
- delay_ms(500);
- beep=0;motor1_PWM=0;motor3_PWM =0;motor21=0; motor22=0;delay_ms(300);}motor1_PWM=75;motor3_PWM =65;motor21=0; motor22=0; TIM_SetCompare2(TIM3,180);}//前进右转45
- if(res=='3')
- {
- if(Senor_Using()<30)
- {beep=1;
- delay_ms(500);
- beep=0;motor1_PWM=0;motor3_PWM =0;motor21=0; motor22=0;delay_ms(600);
- }
- motor1_PWM=75;motor3_PWM =75;motor21=0; motor22=0; TIM_SetCompare2(TIM3,185);}//直行
- if(res=='4')
- {
- if(Senor_Using()<30)
- {beep=1;
- delay_ms(500);
- beep=0;motor1_PWM=0;motor3_PWM =0;motor21=0; motor22=0;delay_ms(300);}motor1_PWM=65;motor3_PWM =75;motor21=0; motor22=0; TIM_SetCompare2(TIM3,190);}//前进左转45
- // if(res=='5'){motor11=1; motor12=0;motor21=1; motor22=0;TIM_SetCompare2(TIM3,195);}//前进左转90
- if(res=='0'){TIM_SetCompare2(TIM3,185);motor1_PWM=0;motor3_PWM =0;;motor21=0; motor22=0;}
- if(res=='6'){motor1_PWM=30;motor3_PWM =30;motor21=1; motor22=1;TIM_SetCompare2(TIM3,185);}//后退
- if(res=='7'){motor1_PWM=30;motor3_PWM =30;motor21=1; motor22=1;TIM_SetCompare2(TIM3,180);}//后退右转45
- if(res=='8'){motor1_PWM=30;motor3_PWM =30;motor21=1; motor22=1;TIM_SetCompare2(TIM3,190);}//后退左转45
- if(res=='5')
- {
-
- Read_TCRT_Date(); //读循迹线值
- TIM_SetCompare2(TIM3,185); //回中
-
- if(L==1&&M==1&&R==1)//11
- {
- motor1_PWM=75;motor3_PWM =75;motor21=0; motor22=0; //如果都没有读取到黑线,直走
- TIM_SetCompare2(TIM3,0);
- delay_ms(10);
- }
- if(L==1&&M==0&&R==1)//101
- {
- motor1_PWM=75;motor3_PWM =75;motor21=0; motor22=0; //如果中间一个读取到黑线,直走
- TIM_SetCompare2(TIM3,185); //回中
- delay_ms(10);
- }
- if(L==1&&M==1&&R==0)//110
- {
- motor1_PWM=75;motor3_PWM =75;motor21=0; motor22=0; //直走
- TIM_SetCompare2(TIM3,179); //如果右边读取到黑线,右转
- delay_ms(10);
- }
- if(L==0&&M==1&&R==1)//011
- {
- motor1_PWM=75;motor3_PWM =75;motor21=0; motor22=0; //直走
- TIM_SetCompare2(TIM3,191); //如果左边读取到黑线,左转
- delay_ms(10);
- }
- if(L==0&&M==0&&R==0)//000
- {
- motor1_PWM=75;motor3_PWM =75;motor21=0; motor22=0;
- TIM_SetCompare2(TIM3,185);
-
- }
- }
- }
-
-
- //在周期20ms的PWM信号中,不同的脉宽对应舵机不同的转动角度,在0.5ms-2.5ms间有效
- // TIM_SetCompare2(TIM4,175);//总共计数200,前面设置的模式是计数在比较值之后才是高电平
- //也就是高电平的时间是[(200-175)/200]*20ms=2.5ms,即转过360度,180度舵机依次类推
- }
复制代码
所有程序51hei附件下载:
stm32利用蓝牙控制舵机1.3.7z
(215.34 KB, 下载次数: 31)
|