- // PA0,PA1, 对应四个引脚对应测速的四个角,TIME2
- // PC4,PC5,电机A, PWMA--PB6
- // PC2,PC3,电机B PWMB--PB7
- // PB11--TX, PB10--RX 蓝牙
- // PB5 舵机
- /*
- OLED接线定义:
- VCC--3.3V/5V
- GND--GND
- SCL--PB8
- SDA--PB9
- */
- #include "delay.h"
- #include "sys.h"
- #include "usart.h"
- #include "CeSu.h" //测速文件
- #include "DianJi.h" //电机文件
- #include "Duoji.h" //舵机文件
- #include "LanYa.h" //蓝牙文件
- #include "oled_iic.h"
- #define High_speed 90
- #define Low_speed 50
- #define DuoJi_JiaoDu_Init 85
- //extern int16_t LunSuDu[4]; //实际值
- //extern int16_t COUNT[4]; //脉冲计数
- extern u8 receive_data[10];
- extern u8 ChuLi_data[10]; //蓝牙命令
- char Buf[128];
- int v=Low_speed,v0=0; //默认初始速度
- int angle=90; //舵机初始角度
- //float i; //测量电压
-
- //蓝牙命令处理
- void MingLing_ChuLi(u8 *MingLing)
- {
-
- if(MingLing[0]=='v') //检测电压
- {
- // i=analogRead(sensor)*5;i=i/1024*3; //analogRead(sensor),从指定的模拟引脚中读取值。
- MingLing[0]='t';
- }
-
- else if(MingLing[0]=='h') {v=High_speed;}//高速模式
-
- else if(MingLing[0]=='l') {v=Low_speed;}//中速模式
-
- else if(MingLing[0]=='1')
- {
- //v0=Low_speed;
- // DuoJi_KongZhi(110); //舵机110°
- motor(v,v);
-
- }
-
- else if(MingLing[0]=='2') {motor(v,-v);} //左转
-
- else if(MingLing[0]=='3') {motor(-v,v);} //右转
-
- else if(MingLing[0]=='4')
- {
- // DuoJi_KongZhi(70); //舵机110°
- motor(-v,-v);
- }
-
- else if(MingLing[0]=='5') //舵机角度增加
- {
- if(angle<120)
- {
- angle++;
- MingLing[0]='0';
- }
- else {
- angle=120;
- }
- DuoJi_KongZhi(angle);
- delay_ms(50);
- }
-
- else if(MingLing[0]=='6') //舵机角度减少
- {
- if(angle>60)
- {
- angle--;
- MingLing[0]='0';
- }
- else {
- angle=60;
- }
- DuoJi_KongZhi(angle);
- delay_ms(50);
- }
-
- else if(MingLing[0]=='7') {DuoJi_KongZhi(DuoJi_JiaoDu_Init);} //舵机回到90度
-
- else if(MingLing[0]=='8') {motor(v-40,v);}
-
- else if(MingLing[0]=='9') {motor(v,v-40);}
-
- else if(MingLing[0]=='t')
- {
- Dj_A_FangXiang(3);
- Dj_B_FangXiang(3);
- TIM_SetCompare1(TIM4,0);
- TIM_SetCompare2(TIM4,0);
- DuoJi_KongZhi(DuoJi_JiaoDu_Init);
- }
-
-
- }
- int main(void)
- {
-
- delay_init(); //延时函数初始化
- NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2); //设置NVIC中断分组2:2位抢占优先级,2位响应优先级
- uart_init(115200); //串口初始化为115200
- OLED_Init(); //OLED屏幕初始化
- MOTOR_Init(); //电机PWM控制初始化
- DianJi_PWM_Init();
- Uart3_Init(9600);
- TIM3_PWM_Init(200-1,7200-1); //PWM分频
- DuoJi_KongZhi(DuoJi_JiaoDu_Init); //舵机90度;
- OLED_Clear();
-
- TIM_SetCompare1(TIM4,0);
- TIM_SetCompare2(TIM4,0);
-
- // TIM2_CAP_Init(0XFFFF,72-1); //这里以1us频率捕获脉冲
- // TIM4_Int_Init(1000,72); //不分频。T=10MS,采集单位时间轮速
- while(1)
- {
- //超声波测距
-
- //红外循迹
-
- //接收蓝牙串口数据
- MingLing_ChuLi(ChuLi_data);
- //显示代码
- sprintf(Buf,"Receive: %s",receive_data);
- OLED_ShowCH(0,0*2,(u8 *)&Buf);
-
- sprintf(Buf,"Data: %s",ChuLi_data);
- OLED_ShowCH(0,1*2,(u8 *)&Buf);
- }
-
- }
复制代码
代码下载:
bb-8_stm32源码.7z
(201 KB, 下载次数: 7)
|