標題:
科技立項--藍牙小車
[打印本頁]
作者:
圣艾爾摩之火
時間:
2014-4-17 09:40
標題:
科技立項--藍牙小車
#include<reg52.h>
#include<stdio.h>
#include<stdlib.h>
#define uint unsigned int
#define uchar unsigned char
#define ulong unsigned long
#define t1n 100
/*sbit L_on=P1^4;
sbit L1=P0^1;
sbit L2=P0^2;
sbit L3=P0^3;
sbit L4=P0^4;
sbit L5=P0^5;
sbit L6=P0^6; */
//循跡紅外對管信號反饋
sbit Leftled = P3^2;
sbit Rightled = P3^4;
sbit Leftled2 = P3^3;
sbit Rightled2 = P3^5;
//超聲波管腳定義
sbit Trig = P2^0; //產(chǎn)生脈沖引腳
sbit Echo = P2^1; //回波引腳
//左邊輪子
sbit in1=P0^0;
sbit in2=P0^1;
sbit in3=P0^2 ;
sbit in4=P0^3 ;
//右邊輪子
sbit in11=P0^4 ;
sbit in22=P0^5 ;
sbit in33=P0^6 ;
sbit in44=P0^7 ;
sbit Beef = P3^6;
uint ZKB = 70;
uchar BK =0;
uchar z=0,time=0; //計數(shù)器
char Buffer;
char BufferFunction = 0x63; //默認模式限定藍牙控制
char BufferSpeed;
char BufferDirection;
bit flag=0; //用于超聲波
bit flag_b=0;
bit flag1=0;
ulong t0,count,tc;
ulong distance;
ulong C;
void Bluetooth_Move();
void BTControl();
void xunji();
void bizhang();
//超聲波
void delay(uint z);
void delay1(void);
void DelayMs(uint t);
void DelayUs(uint t);
void celiang();
void zhuanxiang();
/********************************************************************
* 名稱 : Delay_1ms()
* 功能 : 延時子程序,延時時間為 1ms * x
* 輸入 : x (延時一毫秒的個數(shù))
* 輸出 : 無
***********************************************************************/
void Delay_1ms(int i)//1ms延時
{
int x,j;
for(j=0;j<i;j++)
for(x=0;x<=148;x++);
}
/********************************************************************
* 名稱 : Com_Int()
* 功能 : 串口中斷子函數(shù)
* 輸入 : 無
* 輸出 : 無
***********************************************************************/
//當IO口有數(shù)據(jù)流入時會自動執(zhí)行
void Com_Int(void) interrupt 4
{
//ES = 0; //總中斷關(guān)閉
if(RI == 1) //當硬件接收到一個數(shù)據(jù)時,RI會置位串口有發(fā)送和接收兩種不同的情況
//(不論是發(fā)送還是接收,只要發(fā)送或接收完成,都會置位串口中斷標志位(發(fā)送就置位TI,接收則置位RI),
//所以=1表示的是兩個標志位只要有一個為1(即是兩種情況有一種發(fā)生)且此時系統(tǒng)允許串口中斷,
//則會轉(zhuǎn)向中斷服務(wù)程序,在服務(wù)程序里判斷中斷是TI/RI哪個為1觸發(fā)的,相應(yīng)的做出處理,如可以把緩沖區(qū)的數(shù)據(jù)讀出來。)
{
Buffer=SBUF;//SBUF:51串口通信寄存器
if(0x62<=Buffer&&Buffer<=0x69){
BufferFunction = Buffer;
B = 1;
}
if(0x30<=Buffer&&Buffer<=0x39) BufferSpeed = Buffer;
if(0x41<=Buffer&&Buffer<=0x61) BufferDirection = Buffer;
RI = 0; //關(guān)閉串口中斷
}
//ES = 1;
}
/********************************************************************
* 名稱 : Com_Init()
* 功能 : 串口初始化,晶振11.0592,波特率9600,使串口中斷
* 輸入 : 無
* 輸出 : 無
***********************************************************************/
void Com_Init(void)
{
TMOD = 0x21; //選擇使用8位定時器1
PCON = 0x00; //是設(shè)置波特率不加倍
SCON = 0x50; //選擇8位異步通信方式,波特率可變,由定時器控制;允許接受;發(fā)送中斷標志、接收中斷標志分別為0、0
TH1 = 0xfd; //設(shè)置波特率 9600= 11.0592*1000000/12/(0x100-0xfd)/32
TL1 = 0xfd;
TH0 = (65536-100)/256;
TL0 = (65536-100)/256;
TR0 = 0; //先不啟動定時器0
TR1 = 1; //啟動定時器1
ES = 1; //開串口中斷
EA = 1; //開總中斷
//超聲波管腳
Trig=0;
Echo=0;
}
//避障超聲波定時器
void timer0()interrupt 1 using 2
{
TH0 = (65536-100)/256; //定時器0無法溢出
TL0 = (65536-100)/256;
}
void timer1()interrupt 2 using 2
{
TH1 = (65536-100)/256;
TL1 = (65536-100)/256;
z++;
if(z>=100) z=0;
}
/**********速度變化*******************/
void SpeedChoice()
{
switch(BufferSpeed){
case '0': ZKB = 10;break;
case '1': ZKB = 20;break;
case '2': ZKB = 30;break;
case '3': ZKB = 40;break;
case '4': ZKB = 50;break;
case '5': ZKB = 60;break;
case '6': ZKB = 70;break;
case '7': ZKB = 80;break;
case '8': ZKB = 90;break;
case '9': ZKB = 100;break;
}
}
/*******左邊輪驅(qū)動***************/
void pwm_out_left_moto(uchar a,uchar b,uchar c,uchar d )
{
if(z<a) in1=1;
else in1=0;
if(z<b) in2=1;
else in2=0;
if(z<c) in3=1;
else in3=0;
if(z<d) in4=1;
else in4=0;
}
/*********右邊輪驅(qū)動*******************/
void pwm_out_right_moto(uchar a,uchar b,uchar c,uchar d )
{
if(z<a) in11=1;
else in11=0;
if(z<b) in22=1;
else in22=0;
if(z<c) in33=1;
else in33=0;
if(z<d) in44=1;
else in44=0;
}
/************超聲波********************/
//*************************啟動超生波模塊***************************************
void startmodule()
{
count=0;
Trig=1;
DelayUs(18);
Trig=0;
while(!Echo) // 等待高電平
{
count++; //超時檢測
if(count>26000)
{count=0;
flag1=1;break;}
}
if(!flag1)
{
TR0=1; //啟動定時器0
while(Echo); //beep=0;
TR0=0; //關(guān)閉定時器0
}
else
{flag1=0;
TH0=30000;
TL0=30000;
}
}
//*******************測距模塊*********************************************
void celiang()
{
t0=TH0*256+TL0;
TH0=0;
TL0=0;
distance=(t0*1.7)/100; //算出來是CM
if((distance>=700)||(flag==1)) //超出測量范圍
{
flag=0;distance=700;
}
}
void zhuanxiang()
{
startmodule();
celiang();
if(distance<25)
{
if(distance<15)
{
pwm_out_left_moto(0,100,0,100); //后退
pwm_out_right_moto(0,100,0,100);
delay1();
pwm_out_left_moto(0,100,0,100); //左轉(zhuǎn)
pwm_out_right_moto(100,0,100,0);
delay1();
}
else
{
pwm_out_left_moto(0,100,0,100); //左轉(zhuǎn)
pwm_out_right_moto(100,0,100,0);
delay1();
}
}
else
pwm_out_left_moto(100,0,100,0);
pwm_out_right_moto(100,0,100,0);
}
/*******************延時函數(shù)*********************************************/
//***************us延時函數(shù)
void delay(uint z)
{
uint x,y;
for(x=z;x>0;x--)
for(y=110;y>0;y--);
}
void DelayUs(uint t)
{
while(--t);
}
void DelayMs(uint t)
{
while(t--) //大致延時mS
{
DelayUs(250);
DelayUs(250);
}
}
void delay1(void)
{
DelayMs(60);DelayMs(60);
}
/********主函數(shù)********************/
void main()
{
// L_on = 0;
Com_Init();
while(1)
{
SpeedChoice();
Bluetooth_Move();
}
}
/*****************************/
void Bluetooth_Move(){
switch(BufferFunction){
case 0x62: //遙控
BTControl();
break;
case 0x63: //循跡
xunji();
break;
case 0x64: //紅外
break;
case 0x65: //金探
break;
case 0x66: //測溫
break;
case 0x67: //測濕
break;
case 0x68: //測速
break;
case 0x69: //避障
bizhang();
break;
}
}
/*************各種小車功能***********************/
//藍牙遙控
void BTControl(){
switch(BufferDirection){
case 0x41: pwm_out_left_moto(20,0,20,0);
pwm_out_right_moto(20,0,20,0);
break; //前進
case 0x42: pwm_out_left_moto(0,ZKB,0,ZKB);
pwm_out_right_moto(0,ZKB,0,ZKB);
break; //后退
case 0x43: pwm_out_left_moto(0,80,0,80);
pwm_out_right_moto(80,0,80,0);
break; //左上
case 0x44: pwm_out_left_moto(80,0,80,0);
pwm_out_right_moto(0,80,0,80);
break; //右上
case 0x45: pwm_out_left_moto(80,0,80,0);
pwm_out_right_moto(0,80,0,80);
break; //左下
case 0x46: pwm_out_left_moto(0,80,0,80);
pwm_out_right_moto(80,0,80,0);
break; //右下
case 0x61: pwm_out_left_moto(0,0,0,0);
pwm_out_right_moto(0,0,0,0);
break; //手離開屏幕停止
default : pwm_out_left_moto(0,0,0,0);
pwm_out_right_moto(0,0,0,0);
break;
}
}
//循跡
void xunji(){
//有信號為0 沒有信號為1(碰黑)
if(Leftled==0&&Rightled==0&&Leftled2==0&&Rightled2==0)
{
pwm_out_left_moto(20,0,20,0);
pwm_out_right_moto(20,0,20,0);
}
else
{
if(Rightled==1||Rightled2==1) //右邊檢測到黑線
{
if(Leftled==0||Leftled2==0)
{
pwm_out_left_moto(100,0,100,0);
pwm_out_right_moto(0,100,0,100);
}
}
if(Leftled==1||Leftled2==1) //左邊檢測到黑線
{
if(Rightled==0||Rightled==0)
{
pwm_out_left_moto(0,100,0,100);
pwm_out_right_moto(100,0,100,0);
}
}
if(Leftled==1&&Rightled==1&&Leftled2==1&&Rightled2==1) //兩邊檢測到黑線
{
pwm_out_left_moto(0,0,0,0);
pwm_out_right_moto(0,0,0,0);
}
}
}
void bizhang() //避障
{
zhuanxiang();
}
復(fù)制代碼
作者:
愛國一猿
時間:
2014-4-19 19:56
最近也要做這個,能告訴我你QQ號嗎?我qq1519051636
作者:
dzlyxzy
時間:
2016-6-2 14:36
無敵大神,這就是我的畢業(yè)設(shè)計內(nèi)容,但是要答辯了還是有很多沒做出來,現(xiàn)在只能看看你給的這些程序了。。
作者:
深度中毒
時間:
2016-9-3 14:30
為什么會對兩個輪子的分開編譯
作者:
騰飛的龍
時間:
2016-11-24 20:02
樓主你好,手機APP可以分享一下嗎?謝謝
歡迎光臨 (http://www.torrancerestoration.com/bbs/)
Powered by Discuz! X3.1