android单片机蓝牙小车,手把手教你做蓝牙小车
#include "reg51.h"
sbit PWM1 = P2^0;//右后轮电机驱动使能
sbit PWM2 = P2^1;//左后轮电机驱动使能
sbit PWM3 = P2^2;//右前轮电机驱动使能
sbit PWM4 = P2^3;//左前轮电机驱动使能
sbit motor_control_1 = P0^0;//右后轮后退
sbit motor_control_2 = P0^1;//右后轮前进
sbit motor_control_3 = P0^2;//左后轮后退
sbit motor_control_4 = P0^3;//左后轮前进
sbit motor_control_5 = P0^4;//右前轮后退
sbit motor_control_6 = P0^5;//右前轮前进
sbit motor_control_7 = P0^6;//左前轮后退
sbit motor_control_8 = P0^7;//左前轮前进
unsigned char ucBluetoothData = 230;//设置初始速度最大值为255最小为125
unsigned char uclock = 0;//互斥量,俗称原子锁
unsigned int uiPWMCnt1 = 0;
unsigned int uiPWM1 = 230;
unsigned int uiPWMCnt2 = 0;
unsigned int uiPWM2 = 230;
unsigned int uiPWMCnt3 = 0;
unsigned int uiPWM3 = 230;
unsigned int uiPWMCnt4 = 0;
unsigned int uiPWM4 = 230;
unsigned char ucTempPWM;//设置中间变量
void initial_myself();
void initial_peripheral();
void T0_time();
void usart_service(void);
//void usart_send(unsigned char ucSendData);
void delay_long(unsigned int uiDelayLong);
void go_forward(void);//前进
void fall_back(void);//后退
void turn_left(void);//左转
void turn_right(void);//右转
void stop();//刹车
void main()
{
initial_myself();
delay_long(100);
initial_peripheral();
while(1)
{
usart_service();
// delay_long(500);
}
}
//串口服务函数
void usart_service()
{
switch(ucBluetoothData)
{
case 0x04://前进
ucBluetoothData = 0x02;//避免一直触发
go_forward();
ucLock = 1;
uiPWM1 = uiPWM2 = uiPWM3 = uiPWM4 = ucTempPWM;
ucLock = 0;
break;
case 0x05://左转
ucBluetoothData = 0x02;//避免一直触发
turn_left();
ucLock = 1;
uiPWM2 = uiPWM4= ucTempPWM / 4;
uiPWM3 =uiPWM1 = ucTempPWM;
ucLock = 0;
break;
case 0x06://右转
ucBluetoothData = 0x02;//避免一直触发
turn_right();
ucLock = 1;
uiPWM2 = uiPWM4 = ucTempPWM;
uiPWM3 =uiPWM1 = ucTempPWM / 4;
ucLock = 0;
break;
case 0x07://后退
ucBluetoothData = 0x02;//避免一直触发
fall_back();
ucLock = 1;
uiPWM1 = uiPWM2 = uiPWM3 = uiPWM4 = ucTempPWM;
ucLock = 0;
break;
case 0x01:
ucBluetoothData = 0x02;//避免一直触发
stop();
ucLock = 1;
uiPWM1 = uiPWM2 = uiPWM3 = uiPWM4 = ucTempPWM;
ucLock = 0;
break;
case 0x00:
ucBluetoothData = 0x02;//避免一直触发
stop();
ucLock = 1;
uiPWM1 = uiPWM2 = uiPWM3 = uiPWM4 = ucTempPWM;
ucLock = 0;
break;
default :
break;
}
delay_long(100);
// usart_send(ucBluetoothData);
//速度调节的数据
if(ucBluetoothData!=0x00&&ucBluetoothData!=0x01 && ucBluetoothData!=0x02 && ucBluetoothData!=0x04 && ucBluetoothData!=0x05 && ucBluetoothData!=0x06 && ucBluetoothData!=0x07)
{
ucLock = 1;
ucTempPWM = uiPWM1 = uiPWM2 = uiPWM3 = uiPWM4 = ucBluetoothData;
ucLock = 0;
}
}
void T0_time() interrupt 1
{
TF0 = 0;//清除中断标志
TR0 = 0;//关定时器
uiPWMCnt1 ++;
uiPWMCnt2 ++;
uiPWMCnt3 ++;
uiPWMCnt4 ++;
if(ucLock == 0)
{
if(uiPWMCnt1 > 255)
{
uiPWMCnt1 = 0;
}
if(uiPWMCnt1 < uiPWM1)
{
PWM1 = 1;
}
else
{
PWM1 = 0;
}
if(uiPWMCnt2 > 255)
{
uiPWMCnt2 = 0;
}
if(uiPWMCnt2 < uiPWM2)
{
PWM2 = 1;
}
else
{
PWM2 = 0;
}
if(uiPWMCnt3 > 255)
{
uiPWMCnt3 = 0;
}
if(uiPWMCnt3 < uiPWM3)
{
PWM3 = 1;
}
else
{
PWM3 = 0;
}
if(uiPWMCnt4 > 255)
{
uiPWMCnt4 = 0;
}
if(uiPWMCnt4 < uiPWM4)
{
PWM4 = 1;
}
else
{
PWM4 = 0;
}
}
TH0 = 0xff;
TL0 = 0x28;
TR0 = 1;///开定时器
}
void initial_myself()
{
TMOD = 0x01;//设置定时器0为工作方式1
TH0 = 0xff;
TL0 = 0x28;
//配置串口
SCON = 0x50;
TMOD = 0x21;
TH1 = TL1 = -(11095200L/12/32/9600);
IP = 0x10;
stop();
PWM1 = 1;
PWM2 = 1;
PWM3 = 1;
PWM4 = 1;
}
void initial_peripheral()
{
EA = 1;//开总中断
ES = 1;//允许串口中断
ET0 = 1;//允许定时器中断
TR0 = 1;//启动定时器
TR1 = 1;//
}
void usart_receive(void) interrupt 4
{
if(RI == 1)
{
RI = 0;
ucBluetoothData = SBUF;
// uiSendCnt = 0;
}
else
{
TI = 0;
}
}
//void usart_send(unsigned char ucSendData)
//{
// ES = 0;
// TI = 0;
// SBUF = ucSendData;
// TI = 0;
// ES = 1;
//
//}
void delay_long(unsigned int uiDelayLong)
{
unsigned int i;
unsigned int j;
for(i = 0 ; i < uiDelayLong ; i++)
{
for(j = 0; j < 500; j++);
}
}
void stop()//停止
{
motor_control_1 = 0;
motor_control_2 = 0;
motor_control_3 = 0;
motor_control_4 = 0;
motor_control_5 = 0;
motor_control_6 = 0;
motor_control_7 = 0;
motor_control_8 = 0;
}
void fall_back()
{
motor_control_1 = 1;
motor_control_2 = 0;
motor_control_3 = 1;
motor_control_4 = 0;
motor_control_5 = 1;
motor_control_6 = 0;
motor_control_7 = 1;
motor_control_8 = 0;
}
void go_forward()
{
motor_control_1 = 0;
motor_control_2 = 1;
motor_control_3 = 0;
motor_control_4 = 1;
motor_control_5 = 0;
motor_control_6 = 1;
motor_control_7 = 0;
motor_control_8 = 1;
}
void turn_left()//左转
{
motor_control_1 = 0;
motor_control_2 = 1;
motor_control_3 = 0;
motor_control_4 = 1;
motor_control_5 = 0;
motor_control_6 = 1;
motor_control_7 = 0;
motor_control_8 = 1;
}
void turn_right()//右转
{
motor_control_1 = 0;
motor_control_2 = 1;
motor_control_3 = 0;
motor_control_4 = 1;
motor_control_5 = 0;
motor_control_6 = 1;
motor_control_7 = 0;
motor_control_8 = 1;
}
android单片机蓝牙小车,手把手教你做蓝牙小车相关推荐
- 手把手教你做蓝牙小车(一)
第1节 选择Arduino开发板 1.1 Arduino是什么 对Arduino,官方有一堆解释. 作为一个软件程序猿,在我眼里,Arduino是学习"可怕硬件"的一个便捷通道.它 ...
- 蓝牙控制小车c语言程序,手把手教你做蓝牙小车(一)
第1节 选择Arduino开发板 1.1 Arduino是什么 对Arduino,官方有一堆解释. 作为一个软件程序猿,在我眼里,Arduino是学习"可怕硬件"的一个便捷通道.它 ...
- 手把手教你做蓝牙小车(二)
第5节 BTChat 本节开始介绍Arduino蓝牙模块,配合Android应用,实现一个蓝牙聊天应用. 5.1 什么是蓝牙 简单说就是一种不同设备之间点对点通讯的技术. 有大篇大篇的蓝牙各种协议,各 ...
- 手把手教你做蓝牙聊天应用(二)-设计方案
第2节 设计方案 功能确定后,就要开始围绕功能进行功能的验证.界面设计的规划.以及程序结构的规划了. 2.1 技术验证 选定了现阶段要完成的核心功能后,我们首先需要对它们做技术上的验证,看看用什么样的 ...
- 手把手教你做蓝牙聊天应用(三)-获取要连接的设备
第3节 获取要连接的设备 这一节我们开始设计蓝牙聊天应用的界面.根据之前的规划,连接管理将放在单独的ConnectionManager模块当中,所以每当要使用连接功能的时候,我们就暂时把它空着,等到C ...
- 立创开源|手把手教你做个WiFi/蓝牙网关
我知道你也想要一个属于自己的WiFi/蓝牙网关 开源地址:ESP32 WiFi/蓝牙网关 ESP32 GetWay 工程说明 官方说明:安信可 Wi-Fi 家庭智能网关(ESP32-G)由安信可科技设 ...
- Android反编译:手把手教你制作高德地图车机共存版
Android反编译:手把手教你制作高德地图车机共存版 前言 前期准备 反编译流程 反编译获取有效信息 修改关键信息 修改包名 修改其它配置 生成APK 重新打包成apk 重新签名 绕过校验机制 定位 ...
- 手把手教你做关键词匹配项目(搜索引擎)---- 第九天
第九天 回顾: 8. 手把手教你做关键词匹配项目(搜索引擎)---- 第八天 7. 手把手教你做关键词匹配项目(搜索引擎)---- 第七天 6. 手把手教你做关键词匹配项目(搜索引擎)---- 第六天 ...
- python手机版做小游戏代码大全-Python大牛手把手教你做一个小游戏,萌新福利!...
原标题:Python大牛手把手教你做一个小游戏,萌新福利! 引言 最近python语言大火,除了在科学计算领域python有用武之地之外,在游戏.后台等方面,python也大放异彩,本篇博文将按照正规 ...
最新文章
- sqlmap写文件为空之谜
- C语言 数组排序 – 冒泡法排序 - C语言零基础入门教程
- Ubuntu安装Python的相对简单方法
- 工厂模式(描述语言PHP)
- 打造一流云计算机房,【迈向“双一流”】为科研插上云计算的“翅膀”
- 【原创】.NET读写Excel工具Spire.Xls使用(5)重量级的Excel图表功能
- 【数据结构】图文讲解神奇的单链表与双链表
- 魅族消息推送服务器,内部员工透露魅族早已完成统一推送服务适配
- Paradise(天堂)勒索病毒解密工具
- 佳缘男的计算机软件出差,交友我在世纪佳缘认识了一个男人,今年32岁,通信接近一个月,觉得 爱问知识人...
- Java宠物商店源代码
- Android 根据应用内选择时间调整TextClock时区
- JPBC部分API说明
- 计算机B和D的转换,模数转换
- 我的世界java1.16.3村庄种子,我的世界2020年最新版村庄种子
- 云主机和物理机的区别
- UP Meta—Web3.0世界创新型元宇宙金融协议
- opensource项目_Opensource.com 2014年年度阅读清单
- VS2019/MFC编程入门——文档、视图和框架:分割窗口
- 分钟换算成时分秒格式
热门文章
- Linux压缩包和用户管理及开关机指令
- idea去掉无用import类_@Import注解的魅力
- linux 使用jstack_案例解析:线程池使用不当导致的系统崩溃
- 设计模式的理解: 代理模式(Proxy)
- ubuntu / 18.04 系统上配置 DHCP Server
- Linux / argv、environ 和 env 的联系
- windows server 2012 FTP 服务器 / 访问网络共享盘
- java http请求 乱码_怎么解决java中的http请求乱码
- python读取nc文件转成img_使用python的netCDF4库读取.nc文件 和 创建.nc文件[转]
- JavaScript设计模式(一)