智能小车源代码.docx

上传人:b****6 文档编号:3336121 上传时间:2022-11-21 格式:DOCX 页数:16 大小:17.35KB
下载 相关 举报
智能小车源代码.docx_第1页
第1页 / 共16页
智能小车源代码.docx_第2页
第2页 / 共16页
智能小车源代码.docx_第3页
第3页 / 共16页
智能小车源代码.docx_第4页
第4页 / 共16页
智能小车源代码.docx_第5页
第5页 / 共16页
点击查看更多>>
下载资源
资源描述

智能小车源代码.docx

《智能小车源代码.docx》由会员分享,可在线阅读,更多相关《智能小车源代码.docx(16页珍藏版)》请在冰豆网上搜索。

智能小车源代码.docx

智能小车源代码

#include

#include

//#include"lcd、h"

sbitTX=P2^7;

sbitRX=P2^6;

sbitPWM=P1^4;

sbitBeep=P3^7;

sbitled1=P3^3;

sbitled2=P3^4;

sbitled3=P3^5;

/***********蓝牙切换按键**********/

sbitk1=P3^6;

/***自定义一些数据,注意这些数据对应“51智能车蓝牙无线遥控、exe”软件上的数据设置**/

#defineleftdata0x11

#definerightdata0x22

#defineforwarddata0x33

#definebackdata0x44

#definestopdata0x55

#defineucharunsignedchar

#defineuintunsignedint

unsignedcharreceiveData,high_velosity,low_velosity;

voidUsartConfiguration();

/***********电机端口定义************************/

sbitIN1=P1^0;//为高电平时,左电机后转

sbitIN2=P1^1;//为高电平时,左电机正转

sbitIN3=P1^2;//为高电平时,右电机正转

sbitIN4=P1^3;//为高电平时,右电机后转

/**************无线遥控模块定义****************/

sbitKey_A=P2^0;//B键信号端对应D0

sbitKey_B=P2^1;//D键信号端对应D1

sbitKey_C=P2^2;//C键信号端对应D2

sbitKey_D=P2^3;//C键信号端

voiddelay0(inta);

voiddelay15us(void);

voiddelay1ms(intz);

voiddi();//蜂鸣器函数声明

unsignedinttime;//用于存放定时器时间值

unsignedintS;//用于存放距离的值

charflag=0;//量程溢出标志位

charf1,f2,f3,f4,n0;

inta;

intn;//运行次数判断标志

ints_left,s_right;

voidDelay10us(unsignedchari)

{

unsignedcharj;

do{

j=10;

do{

_nop_();

}while(--j);

}while(--i);

}

/*********************************************************************************************/

/*****************电机程序********************/

/********************************************************************************************/

voidloop()

{

IN1=1;//左电机

IN2=0;

IN3=1;//右电机

IN4=0;

}

voidrunfront()

{

IN1=1;//左电机

IN2=0;

IN3=0;//右电机

IN4=1;

}

//小车后退函数

voidrunback()

{

IN1=0;//左电机

IN2=1;

IN3=1;//右电机

IN4=0;

}

//小车左转

voidrunleft()

{

IN1=0;//左电机

IN2=0;

IN3=0;//右电机

IN4=1;

}

//小车右转

voidrunright()

{

IN1=1;//左电机

IN2=0;

IN3=0;//右电机

IN4=0;

}

////小车左后转

//voidrunbackleft()

//{

//IN1=0;//左电机

//IN2=0;

//IN3=0;//右电机

//IN4=1;

//}

////小车右后转

//voidrunbackright()

//{

//IN1=1;//左电机

//IN2=0;

//IN3=0;//右电机

//IN4=0;

//}

//小车停止函数

voidstop()

{

IN1=0;//左电机

IN2=0;

IN3=0;//右电机

IN4=0;

}

/*********************************************************************************************/

/***********蓝牙程序************/

/********************************************************************************************/

voiddelay(unsignedinta)

{

unsignedintb;

for(;a>0;a--)

for(b=3;b>0;b--);

}

//左电机转

voidleft_motor_runfront(unsignedinta)

{

IN1=1;//左电机

IN2=0;

delay(a);

IN1=0;

IN2=0;

delay(100-a);

}

voidright_motor_runfront(unsignedinta)

{

IN3=0;//右电机

IN4=1;

delay(a);

IN3=0;

IN4=0;

delay(100-a);

}

voidleft_motor_back(unsignedinta)

{

IN1=0;//左电机

IN2=1;

delay(a);

IN1=0;

IN2=0;

delay(100-a);

}

voidright_motor_back(unsignedinta)

{

IN3=1;//右电机

IN4=0;

delay(a);

IN3=0;

IN4=0;

delay(100-a);

}

voidleft_motor_stop()

{

IN1=0;//左电机

IN2=0;

}

voidright_motor_stop()

{

IN3=0;//左电机

IN4=0;

}

//PWM调速

voidPWM_RUNFRONT(unsignedinta,unsignedintb)//a属于0~100

{

left_motor_runfront(a);

right_motor_runfront(b);

}

voidPWM_RUNBACK(unsignedinta,unsignedintb)//a属于0~100

{

left_motor_back(a);

right_motor_back(b);

}

voidPWM_RUNLEFT(unsignedinta)//a属于0~100

{

right_motor_runfront(a);

left_motor_stop();

}

voidPWM_RUNRIGHT(unsignedinta)//a属于0~100

{

right_motor_stop();

left_motor_runfront(a);

}

voidbluestop()

{

left_motor_stop();

right_motor_stop();

}

voidUsartConfiguration()

{

SCON=0X50;//设置为工作方式1

TMOD=TMOD|0x21;//设置计数器工作方式2

PCON=0X80;//波特率加倍

TH1=0XFA;//计数器初始值设置,注意波特率就是4800的

TL1=0XFA;

ES=1;//打开接收中断

EA=1;//打开总中断

TR1=1;

TH0=0;

TL0=0;

ET0=1;//打开计数器

}

/*******************************************************************************

*函数名:

Delay(unsignedinti)

*函数功能:

延时函数

*输入:

*输出:

*******************************************************************************/

voidUsart()interrupt4

{

receiveData=SBUF;//出去接收到的数据

RI=0;//清除接收中断标志位

if(receiveData==0x66)

{

low_velosity=1;

high_velosity=0;

//write_com(0x80);

}

if(receiveData==0x77)

{

low_velosity=0;

high_velosity=1;

}

}

/*********************************************************************************************/

/***********舵机程序************/

/********************************************************************************************/

voiddelayx(intt)

{

charj;

for(;t>0;t--)

for(j=19;j>0;j--);

}

voiddegree0()

{

inti;

for(i=0;i<10;i++)

{

PWM=1;

delayx(10);//1ms

PWM=0;

delayx(390);//19ms

}

}

voiddegree90()

{

inti;

for(i=0;i<10;i++)

{

PWM=1;

delayx(24);//50

PWM=0;

delayx(376);//18、5ms

}

}

voiddegree180()

{

inti;

for(i=0;i<15;i++)

{

PWM=1;

delayx(40);//1、5ms

PWM=0;

delayx(360);//18、5ms

}

}

/*********************************************************************************************/

/**************超声波程序**************/

/*********************************************************************************************/

/****A键被按下后,前进*****/

voidCount1(void)

{

time=TH1*256+TL1;

TH1=0;

TL1=0;

S=(time*1、7)/100;

n++;

if(S<=30&&n==1)

{

led1=0;

stop();

degree0();di();

}

if(S>30&&n==1)

{

led1=1;

runfront();

n=0;

}

if(n==2)

{

s_left=S;

degree180();di();

}

if(n==3)

{

s_right=S;

degree90();di();

n=0;//标志位清零

if(s_left>s_right)

{

led2=1;delay1ms(50);led2=0;delay1ms(50);

runright();

delayx(1250);

}

else

{

led3=1;delay(800);led3=0;delay(500);

runleft();

delayx(1250);

}

}

}

/********超声波高电平脉冲宽度计算程序***************/

voidTimer_Count(void)

{

TR1=1;//开启计数

while(RX);//当RX为1计数并等待

TR1=0;//关闭计数

//Count1();//计算

}

/********************************************************/

voidStartModule()//启动模块

{

TX=1;//启动一次模块

Delay10us

(2);

TX=0;

}

/*********************************************************************************************/

/********************主程序*******************/

/*********************************************************************************************/

voidmain(void)

{

UsartConfiguration();

while

(1)

{

if(k1==0&&n0==0)

{

delay1ms(20);

if(k1==0)

{

n0++;

}

}

elseif(k1==0&&n0==1)

{

delay1ms(20);

if(k1==0)

{

n0--;

}

}

if(n0==0)

{

if(Key_B==0&&Key_D==0&&Key_C==0&&Key_A==1)//A键被按下

{

f1=1;f2=0;f3=0;f4=0;di();

}

elseif(Key_A==0&&Key_D==0&&Key_B==1&&Key_C==0)//B键被按下

{

f1=0;f2=1;f3=0;f4=0;di();

}

elseif(Key_B==0&&Key_D==0&&Key_A==0&&Key_C==1)//C键被按下

{

f1=0;f2=0;f3=1;f4=0;di();

}

elseif(Key_D==1&&Key_B==0&&Key_A==0&&Key_C==0)//D键被按下

{

f1=0;f2=0;f3=0;f4=1;di();

}

if(f1)

{

StartModule();

for(a=951;a>0;a--)

{

if(RX==1)

{

Timer_Count();

Count1();

}

}

}

if(f2)

{

runback();

}

if(f3)

{

loop();

}

if(f4)

{

stop();

}

}

if(n0==1)

{

if(low_velosity)

{

switch(receiveData)

{

caseleftdata:

PWM_RUNLEFT(30);break;

caserightdata:

PWM_RUNRIGHT(30);break;

caseforwarddata:

PWM_RUNFRONT(64,70);break;

casebackdata:

PWM_RUNBACK(64,70);break;

casestopdata:

bluestop();break;

}

}

if(high_velosity)

{

switch(receiveData)

{

caseleftdata:

PWM_RUNLEFT(50);break;

caserightdata:

PWM_RUNRIGHT(50);break;

caseforwarddata:

PWM_RUNFRONT(94,100);break;

casebackdata:

PWM_RUNBACK(94,100);break;

casestopdata:

stop();break;

}

}

}

}

}

/*****************蜂鸣器*********************/

voiddi()

{

inti;

for(i=0;i<150;i++)

{

Beep=1;

delay0(20);

}

Beep=0;

}

voiddelay0(inta)

{

charj;inti;

for(i=a;i>0;i--)

for(j=200;j>0;j--);

}

voiddelay1ms(intz)//延时1ms

{

intx,y;

for(x=z;x>0;x--)

for(y=110;y>0;y--);

}

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 小学教育 > 语文

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1