博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
51单片机之蓝牙遥控小车_效果展示+单片机知识+完整蓝牙电车代码
阅读量:5301 次
发布时间:2019-06-14

本文共 2687 字,大约阅读时间需要 8 分钟。

首先可能是你想实现的蓝牙小车效果(点击下面跳转B站)

关于51单片机基础

关于51单片机蓝牙电车驱动程序

L298N电机驱动

步进电机驱动

舵机驱动

蓝牙模块串口通信

PWM调速

#include "reg52.h"			 //此文件中定义了单片机的一些特殊功能寄存器typedef unsigned int u16;	  //对数据类型进行声明定义typedef unsigned char u8;//是否是前进bit isFoward = 0;//是否后退bit isBack = 0;//控制行驶电机sbit EN_A = P0^0;sbit IN_A1 = P0^1;sbit IN_A2 = P0^2;sbit IN_B1 = P0^3;sbit IN_B2 = P0^4;sbit EN_B = P0^5;//是否行使总开关bit ALL_EN = 0;u8 speedNum = 10;//步进电机转动int harfMotoNum = 10;int currentMotoNum = 15;sbit r_pwm = P3^7;void initFunc(){	P0 = 0x00;	//首先设置TMOD工作方式	TMOD = 0x21;	PCON = 0x00;	SCON = 0x50;	//计算初始值设定	TH1 = 0xFD;	TL1 = 0xFD;	//打开	TR1 = 1;	//打开串口中断	ES = 1;	TH0=0XFF;	//给定时器赋初值,定时1ms	TL0=0XA3;		ET0=1;//打开定时器0中断允许	EA=1;//打开总中断	TR0=1;//打开定时器	}void checkRun(){	if(isFoward){	  	EN_A = ALL_EN;		IN_A1 = 0;		IN_A2 = 1;	}  else if(isBack){		EN_A = ALL_EN;		IN_A1 = 1;		IN_A2 = 0;	}	else{		EN_A = ALL_EN;		IN_A1 = 0;		IN_A2 = 0;	}}u8 motoDriveRecord = 0;bit motoNeedRun = 0;void main(){		initFunc();	while(1)	{			checkRun();	}		}u8 systemCount = 0;u8 row = 0;u8 colum = 0;u8 judgeNum = 0;bit leftOrRight = 0;  //旋转方向,0左,1右void Timer0() interrupt 1{	TH0=0XFF;	//给定时器赋初值,定时1ms	TL0=0XA3;/*********************** 定时器控制PWM调速 **************************/	/* 		(/0)(/1)(/2)(/3)   	(/19)	(%0)	0	5 	10	15	...	95			(%1)	1	6	11	16 	...	96		(%2)	2	7	12	17 	...	97		(%3)	3	8	13	18 	...	98	(%4)	4+	9+	14+	19+	...	99	*/	if(systemCount >= 1000)	 	systemCount = 0;	row = ((systemCount/10) % 5);//这时候取值是0,1,2	colum = (systemCount/10) / 5;//取值为0-19;	judgeNum = row * 20 + colum;	if(judgeNum < speedNum){		ALL_EN = 1;	}else{		ALL_EN = 0;	}/*********************** 舵机控制 **************************/	if((systemCount%200) == 0){		r_pwm = 1;	}	if((systemCount%200) >= currentMotoNum){		r_pwm = 0;	}	systemCount++;}//中断事件u8 receivedData;u8 operation;void Usart() interrupt 4{	if(RI == 1) {  //如果收到.       	//定义一个变量,接收数据		receivedData = SBUF;			if (receivedData == 101){			//前进			isFoward = 1;			isBack = 0;			ALL_EN = 1;		}else if(receivedData == 102){			//后退			isFoward = 0;			isBack = 1;			ALL_EN = 1;		}else if(receivedData == 103){			//停止			isFoward = 0;			isBack = 0;			ALL_EN = 0;		}else if(receivedData == 104){			//调整行驶速度			operation = receivedData;		}else if(receivedData == 105){			//调整方向			operation = receivedData;		}else if(receivedData == 106){			//校准方向			currentMotoNum = harfMotoNum + 5;		}else if(receivedData <= 100){			if(operation == 104){				if(receivedData >= 0 && receivedData <= 100){					speedNum = receivedData;				}			}else if(operation == 105){				if(receivedData >= 0 && receivedData <= harfMotoNum * 2){					currentMotoNum = receivedData + 5;				}			}		} 			RI = 0;    }    }

 

转载于:https://www.cnblogs.com/yangzigege/p/10452806.html

你可能感兴趣的文章
JavaScript动画打开半透明提示层
查看>>
Mybatis生成resulteMap时的注意事项
查看>>
jquery-jqzoom 插件 用例
查看>>
1007. Maximum Subsequence Sum (25)
查看>>
iframe的父子层跨域 用了百度的postMessage()方法
查看>>
图片生成缩略图
查看>>
动态规划 例子与复杂度
查看>>
查看oracle数据库的连接数以及用户
查看>>
【数据结构】栈结构操作示例
查看>>
中建项目环境迁移说明
查看>>
三.野指针和free
查看>>
activemq5.14+zookeeper3.4.9实现高可用
查看>>
TCP/IP详解学习笔记(3)IP协议ARP协议和RARP协议
查看>>
简单【用户输入验证】
查看>>
python tkinter GUI绘制,以及点击更新显示图片
查看>>
HDU4405--Aeroplane chess(概率dp)
查看>>
CS0103: The name ‘Scripts’ does not exist in the current context解决方法
查看>>
20130330java基础学习笔记-语句_for循环嵌套练习2
查看>>
Spring面试题
查看>>
窥视SP2010--第一章节--SP2010开发者路线图
查看>>