芯片:STM32F1
蓝牙模块:HC-05
小车:两个驱动轮,差速转弯
手机APP:蓝牙SPP
单片机源程序如下:
- #include "key.h"
- #include "system.h"
- #include "SysTick.h"
- #include "led.h"
- #include "usart.h"
- #include "public.h"
- #include "string.h"
- /*******************************************************************************
- * 函 数 名 : main
- * 函数功能 : 主函数
- * 输 入 : 无
- * 输 出 : 无
- *******************************************************************************/
- int main()
- {
- int R=140,L=140;
- init();
- LCD_Clear(BACK_COLOR); //清屏
- POINT_COLOR=BLACK;
- while(1)
- {
- LCD_ShowString(80,0,200,16,16,"L ");
- LCD_ShowString(80,20,200,16,16,"R ");
- LCD_ShowxNum(100,0,L,3,16,0);
- LCD_ShowxNum(100,20,R,3,16,0);
- if(k_up==0)
- {
- delay_ms(5);
- if(k_up==0)
- {
- L+=10;
- if(L>500)
- L=500;
- while(!k_up);
- }
- }
- if(k_down==0)
- {
- delay_ms(5);
- if(k_down==0)
- {
- L-=10;
- if(L<0)
- L=0;
- while(!k_down);
- }
- }
- if(k_left==0)
- {
- delay_ms(5);
- if(k_left==0)
- {
- R-=10;
- if(R<0)
- R=0;
- while(!k_left);
- }
- }
- if(k_right==1)
- {
- delay_ms(5);
- if(k_right==1)
- {
- R+=10;
- if(R>500)
- R=500;
- while(k_right);
- }
- }
-
-
- if(USART3_RX_STA&0x8000)
- {
- if(strcmp((const char*)USART3_RX_BUF,"16604304260")==0)
- {
- beep=1;
- delay_ms(1000);
- beep=0;
- Debug_screen();
- }
-
- if(USART3_RX_BUF[0]=='8')//return
- {
-
- TIM_SetCompare1(TIM3, L);
- TIM_SetCompare2(TIM3, 0);
- TIM_SetCompare3(TIM3, R);
- TIM_SetCompare4(TIM3, 0);
- beep=~beep;
- }
- if(USART3_RX_BUF[0]=='5')
- {
- TIM_SetCompare1(TIM3, 0);
- TIM_SetCompare2(TIM3, 0);
- TIM_SetCompare3(TIM3, 0);
- TIM_SetCompare4(TIM3, 0);
- //beep=~beep;
- }
- if(USART3_RX_BUF[0]=='2')//前进
- {
-
- TIM_SetCompare1(TIM3, 0);
- TIM_SetCompare2(TIM3, L);
- TIM_SetCompare3(TIM3, 0);
- TIM_SetCompare4(TIM3, R);
- beep=~beep;
- }
- if(USART3_RX_BUF[0]=='4')//左
- {
-
- TIM_SetCompare1(TIM3, 0);
- TIM_SetCompare2(TIM3, L);//占空比2>占空比1左轮反转
- TIM_SetCompare3(TIM3, R);
- TIM_SetCompare4(TIM3, 0);
- beep=~beep;
- }
- if(USART3_RX_BUF[0]=='6')//右
- {
-
- TIM_SetCompare1(TIM3, L);
- TIM_SetCompare2(TIM3, 0);
- TIM_SetCompare3(TIM3, 0);
- TIM_SetCompare4(TIM3, R);//占空比4>占空比3右轮反转
- beep=~beep;
- }
- USART3_RX_STA=0;
- }
- }
- }
复制代码
所有资料51hei提供下载:
蓝牙小车.7z
(242.56 KB, 下载次数: 45)
|