標(biāo)題:
(原創(chuàng)設(shè)計(jì)程序)藍(lán)牙控制小車源程序
[打印本頁(yè)]
作者:
loading88
時(shí)間:
2018-4-27 14:26
標(biāo)題:
(原創(chuàng)設(shè)計(jì)程序)藍(lán)牙控制小車源程序
/
************************************電機(jī)驅(qū)動(dòng)模塊為L(zhǎng)298N*******************************************
************************************藍(lán)牙模塊為HC-06***********************************************
/
#include<reg52.h>
#include "drive.h"
typedef unsigned char u8;
typedef unsigned int u16;
#define uchar unsigned char
u8 a;
uchar k;
sbit IN1=P1^1;
sbit IN2=P1^2;
sbit IN3=P1^3;
sbit IN4=P1^4;
sbit ENA=P1^0;
void com_init() //串口初始化函數(shù),波特率9600bps
{
TMOD = 0x20;
TH1 = 0xf9;
TL1 = 0xf9;
PCON=0X80;
TR1 = 1;
SCON=0X50;
ES = 1;
EA = 1;
}
void delay(unsigned int z)
{
unsigned int x,y;
for(x=z;x>0;x--)
for(y=100;y>0;y--);
}
void Stop() //停止
{
while(1)
{
unsigned int i;
IN1=0;
IN2=0;
IN3=0;
IN4=0; //停止
for(i=0;i<100;i++)
{
delay(20);
}
}
}
void Backward() //后退
{
while(1)
{
unsigned int i;
IN1=0;
IN2=1;
IN3=0;
IN4=1; //反轉(zhuǎn)
for(i=0;i<100;i++)
{
delay(20);
}
IN1=0;
IN2=0;
IN3=0;
IN4=0;
}
}
void Forward() //前進(jìn)
{
while(1)
{
unsigned int i;
IN1=1;
IN2=0;
IN3=1;
IN4=0; //正轉(zhuǎn)
for(i=0;i<100;i++)
{
delay(20);
}
IN1=0;
IN2=0;
IN3=0;
IN4=0;
}
}
void Wleft() //前左
{
while(1)
{
unsigned int i;
IN1=0;
IN2=0;
IN3=1;
IN4=0; //左轉(zhuǎn)
for(i=0;i<100;i++)
{
delay(20);
}
IN1=0;
IN2=0;
IN3=0;
IN4=0;
}
}
void Wright() //前右
{
while(1)
{
unsigned int i;
IN1=1;
IN2=0;
IN3=0;
IN4=0; //右轉(zhuǎn)
for(i=0;i<100;i++)
{
delay(20);
}
IN1=0;
IN2=0;
IN3=0;
IN4=0;
}
}
void Bright() //后右
{
while(1)
{
unsigned int i;
IN1=0;
IN2=0;
IN3=0;
IN4=1; //后右
for(i=0;i<100;i++)
{
delay(20);
}
IN1=0;
IN2=0;
IN3=0;
IN4=0;
}
}
void Bleft() // 后左
{
while(1)
{
unsigned int i;
IN1=0;
IN2=1;
IN3=0;
IN4=0; //后左
for(i=0;i<100;i++)
{
delay(20);
}
IN1=0;
IN2=0;
IN3=0;
IN4=0;
}
}
void main() //主函數(shù)
{
com_init(); //串口初始化
while(1); //死循環(huán),等待藍(lán)牙傳命令過(guò)來(lái)
}
void Usart() interrupt 4
{
a=SBUF;
SBUF=a;//出去接收到的數(shù)據(jù)
switch(a) //判斷收到的命令是干嘛的
{
case 'A':Stop();break; //停止A
case 'B':Backward();break; //后退B
case 'C':Forward();break; //前進(jìn)C
case 'D':Wleft();break; //前左D
case 'E':Wright();break; //前右E
case 'F':Bright();break; //后右F
case 'G':Bleft();break; // 后左G
}
REN=0;
ES=1;
RI = 0;//清除接收中斷標(biāo)志位
while(!TI); //等待發(fā)送數(shù)據(jù)完成
TI=0; //清除發(fā)送完成標(biāo)志位
}
作者:
admin
時(shí)間:
2018-4-27 16:49
能分享下原理圖 與介紹嗎?
歡迎光臨 (http://www.torrancerestoration.com/bbs/)
Powered by Discuz! X3.1