|
51藍(lán)牙小車,通過藍(lán)牙APP發(fā)送指令讓小車前進(jìn)后退,左轉(zhuǎn),右轉(zhuǎn)。
模塊化的插件,讓你低成本,低基礎(chǔ),完成這個(gè)設(shè)計(jì)。
電路原理圖如下:
單片機(jī)源程序如下:
#include <reg51.h>
#include <intrins.h>
#define uchar unsigned char
#define uint unsigned int
unsigned char tmp;
unsigned int c=0;
uint Count;
uint Degree=50;
sbit PWMA=P2^0;
sbit AIN2=P2^1;
sbit AIN1=P2^2;
sbit STBY=P2^3;
sbit BIN1=P2^4;
sbit BIN2=P2^5;
sbit PWMB=P2^6;
sbit beep=P2^7; //蜂鳴器
void init(); //串口初始化
void send(unsigned char a); //單字節(jié)發(fā)送函數(shù)
void ctrl(); //接收處理函數(shù)
void delay(unsigned int x)
{
unsigned int i,j;
for(i=x;i>0;i--)
for(j=110;j>0;j--);
}
void Initialize_time(void)
{
EA=1;
ET0=1; //EA=1;ET0=1;開啟總中斷、定時(shí)器0
// TMOD=0x22; //M1=1,M1=0;方式2
TH0 = 0XA3; //定時(shí)時(shí)間為100us
TL0 = 0XA3;
TR0=1; //啟動(dòng)定時(shí)器0,開始計(jì)數(shù)
}
void timer0 (void) interrupt 1 //定時(shí)中斷0子程序
{
Count++;
if(Count>=0 && Count<=Degree)
{
PWMA=0;
PWMB=0;
}
else
{
PWMA=1;
PWMB=1;
}
if(Count==200)
Count=0;
}
void init() //串口初始化
{
ES=0; //關(guān)中斷
SCON = 0x50; // REN=1允許串行接受狀態(tài),串口工作模式1,
//10位UART(1位起始位,8位數(shù)據(jù)位,1位停止位,無奇偶校驗(yàn)),波特率可
TMOD = 0x22; // 定時(shí)器1工作于方式2,8位自動(dòng)重載模式, 用于產(chǎn)生波特率
TH1=TL1=0xFD; // 波特率9600 (本次測(cè)試采用晶振為11.0592)
PCON &= 0x7f; // 波特率不倍增
TR1 = 1; //定時(shí)器1開始工作,產(chǎn)生波特率
//發(fā)送標(biāo)志位置0
TI=0; //接收標(biāo)志位置0
RI=0;
PT1=1;
ES=1;
}
void send(unsigned char a) //單字節(jié)數(shù)據(jù)發(fā)送
{ //注意:若單片機(jī)TXD(P3.1)無上拉能力,必須在P3.1端接上拉電阻。本次測(cè)試需要接上拉電阻
TI=0;
SBUF=a;
while(TI==0);
TI=0;
}
void ctrl() //接收處理函數(shù)
{
switch(tmp)
{
case '1':
{
AIN1=1;
AIN2=0;
BIN1=1;
BIN2=0;
PWMA=1;
PWMB=1;
send(tmp);
break;
}
case '8':
{
PWMA=0;
PWMB=0;
send(tmp);
}
break;
case '3':
{
AIN1=0;
AIN2=0;
BIN1=1;
BIN2=0;
PWMA=1;
PWMB=1;
send(tmp);
}
break;
case '4':
{
AIN1=1;
AIN2=0;
BIN1=0;
BIN2=0;
PWMA=1;
PWMB=1;
send(tmp);
}
break;
case '5':
{
AIN1=0;
AIN2=1;
BIN1=0;
BIN2=1;
PWMA=1;
PWMB=1;
send(tmp);
}
break;
case '6':
{
beep=0;
delay(50);
beep=1;
send(tmp);
}
break;
case '7':
{
Degree++;
if(Degree==180)
{
Degree=0;
}
send(tmp);
}
break;
}
}
void main()
{
PWMA=1;
PWMB=1;
STBY=1;
init();
// Initialize_time();
while(1)
{
if(RI==1) // 是否有數(shù)據(jù)到來
{
RI = 0;
tmp = SBUF; // 暫存接收到的數(shù)據(jù)
ctrl();
}
}
}
|
評(píng)分
-
查看全部評(píng)分
|