智能小车源代码.docx

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

智能小车源代码.docx

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

智能小车源代码.docx

智能小车源代码

#include

#include<intrins、h>

//#include"lcd、h"

sbit TX=P2^7;

sbitRX=P2^6;

sbitPWM=P1^4;

sbitBeep=P3^7;

sbit led1=P3^3;

sbitled2=P3^4;

sbitled3=P3^5;

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

sbit k1=P3^6;

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

#defineleftdata0x11

#definerightdata 0x22

#defineforwarddata0x33

#definebackdata0x44

#define stopdata0x55

#defineucharunsigned char

#defineuintunsigned int

unsignedcharreceiveData,high_velosity,low_velosity; 

voidUsartConfiguration();

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

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

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

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

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

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

sbit Key_A =P2^0;ﻩ//B键信号端ﻩ对应D0

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

sbit Key_C=P2^2;ﻩ//C键信号端对应D2

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

voiddelay0(inta);

void delay15us(void);

voiddelay1ms(intz);

void di(); //蜂鸣器函数声明

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

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

char flag=0;  //量程溢出标志位

charf1,f2,f3,f4,n0;

inta;

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

ints_left,s_right;

voidDelay10us(unsigned chari)

ﻩunsigned charj;

ﻩ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;

}

//小车右转

void runright()

{

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;

//}

//小车停止函数

void stop()

{

 IN1=0;ﻩ//左电机

IN2=0;

IN3=0;ﻩ//右电机

IN4=0;

}

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

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

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

voiddelay(unsignedinta)

{

 unsigned intb; 

 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);ﻩﻩ

}

void right_motor_runfront(unsigned inta)

{

  IN3=0;//右电机

 IN4=1;

delay(a);

IN3=0;

IN4=0;

 delay(100-a);ﻩﻩﻩﻩ

}

voidleft_motor_back(unsignedint a)

IN1=0;//左电机

 IN2=1;

 delay(a);

 IN1=0;

IN2=0;

 delay(100-a);ﻩﻩﻩ

}

voidright_motor_back(unsigned int a)

{

 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,unsigned intb)//a属于0~100

{

 left_motor_back(a);

ﻩright_motor_back(b);

}

void PWM_RUNLEFT(unsigned inta)ﻩﻩﻩ//a属于0~100

  right_motor_runfront(a);

ﻩleft_motor_stop();

}

void PWM_RUNRIGHT(unsignedint a)//a属于0~100

{

 right_motor_stop();

left_motor_runfront(a);

}

void bluestop()

left_motor_stop() ;

 right_motor_stop() ;

}

void UsartConfiguration()

{

ﻩ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(unsigned inti)

*函数功能ﻩﻩ :

延时函数

*输入  :

*输出 ﻩ:

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

voidUsart() interrupt4

{

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

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

if(receiveData==0x66)

{

ﻩ  low_velosity=1;

high_velosity=0;

// write_(0x80);

ﻩ}

if(receiveData==0x77)

{

  low_velosity=0;

ﻩhigh_velosity=1;

ﻩ}

}

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

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

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

voiddelayx(int t) 

{

ﻩ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()

{

ﻩint i;

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

ﻩ{

ﻩPWM=1;

delayx(24);//50

ﻩﻩPWM=0;

ﻩdelayx(3

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

当前位置:首页 > PPT模板 > 商务科技

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

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