您的位置 首页 解答

51单片机wifi小车代码

51单片机wifi小车代码includeincludedefineucharunsignedchardefineuintunsignedintucharBuffer=0;从串口接收的数

51单片机wifi小车代码

#include

#include

#define uchar unsigned char

#define uint unsigned int

uchar Buffer =0; //从串口接纳的数据

uint i,j;

uchar flag=0;

uchar key_stime_counter,hight_votage=15,timeT_counter;

bit key_stime_ok;

sbit control_signal=P3^6;

sbit StatusLight=P2^2;

sbit MainLight=P2^3;

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

* 称号 : Delay_1ms()

* 功用 : 延时子程序,延时时刻为 1ms * x

* 输入 : x (延时一毫秒的个数)

* 输出 : 无

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

void Delay_1ms(uint i)//1ms延时

{

uchar x,j;

for(j=0;j

for(x=0;x<=148;x++);

}

void Turn*****tatusLight()

{

StatusLight=0;

}

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

* 称号 : Com_Int()

* 功用 : 串口中止子函数

* 输入 : 无

* 输出 : 无

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

void Com_Int(void) interrupt 4

{

EA = 0;

if(RI == 1) //当硬件接纳到一个数据时,RI会置位

{

Buffer = SBUF-48; //这儿减去48是因为从电脑中发送过来的数据是ASCII码。

RI = 0;

}

EA = 1;

}

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

* 称号 : Com_Init()

* 功用 : 串口初始化,晶振11.0592,波特率9600,使能了串口中止

* 输入 : 无

* 输出 : 无

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

void Com_Init(void)

{

TMOD = 0x21;

PCON = 0x00;

SCON = 0x50;

TH1 = 0xFd; //设置波特率 9600

TL1 = 0xFd;

TR1 = 1; //发动定时器1

ES = 1; //开串口中止

EA = 1; //开总中止

IT0=0;

EX0=1;

}

void TimerInit()

{

control_signal=0;

EA=1; //开总中止

ET0=1; //定时器0中止答应

TH0 = 0xFF; //定时器装初值

TL0 = 0xA3;

TR0=0;

}

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

* 称号 :Moto_Forward()

* 功用 : 电机1、2发动,都是行进,整车表现为行进。

* 输入 : 无

* 输出 : 无

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

void Moto_Forward()

{

P1= 0x06;

Delay_1ms(100);

}

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

* 称号 :Moto_Backward()

* 功用 : 电机1、2发动,都是撤退,整车表现为撤退。

* 输入 : 无

* 输出 : 无

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

void Moto_Backward()

{

P1= 0x05;

Delay_1ms(100);

}

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

* 称号 :Moto_TurnLeft()

* 功用 : 电机1撤退,电机2行进,整车表现为左转。

* 输入 : 无

* 输出 : 无

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

void Moto_TurnLeft()

{

P1= 0x09;

Delay_1ms(100);

}

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

* 称号 :Moto_TurnRight()

* 功用 : 电机1行进,电机2撤退,整车表现为右转。

* 输入 : 无

* 输出 : 无

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

void Moto_TurnRight()

{

P1= 0x0A;

Delay_1ms(100);

}

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

* 称号 :Moto_Stop()

* 功用 : 电机1中止,电机2中止,整车表现为中止。

* 输入 : 无

* 输出 : 无

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

void Moto_Stop()

{

P1= 0x00;

Delay_1ms(100);

}

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

* 称号 :SteerEngineTurningUp()

* 功用 : 舵机向上转。

* 输入 : 无

* 输出 : 无

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

void SteerEngineTurningUp()

{

ET0=1;

TR0=1;

Delay_1ms(40);

hight_votage-=1;

if(hight_votage<5)

hight_votage=5;

}

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

* 称号 :SteerEngineTurningDown()

* 功用 : 舵机向下转。

* 输入 : 无

* 输出 : 无

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

void SteerEngineTurningDown()

{

ET0=1;

TR0=1;

Delay_1ms(40);

hight_votage+=1;

if(hight_votage>20)

hight_votage=20;

}

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

* 称号 : SteerEngine()

* 功用 : 舵机PWM中止

* 输入 : 无

* 输出 : 无

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

void SteerEngine() interrupt 1

{

TH0=0xFF;

TL0=0xA3;

if (++key_stime_counter>=200)

{

key_stime_counter=0;

control_signal=1;

key_stime_ok = 1; // 20ms到

timeT_counter=0;

}

if (key_stime_ok&&(++timeT_counter>=hight_votage))

{

key_stime_ok=0;

timeT_counter=0;

control_signal=0; // hight_votage*0.1ms到

}

}

void main()

{

MainLight=0;

Delay_1ms(200);

Com_Init();//串口初始化

TimerInit();//舵机初始化

while(1)

{

switch (Buffer) //依据键值不同,履行不同的内容

{

case 0:

Moto_Stop();

break;

case 1:

Moto_Forward();

break;

case 2:

Moto_Backward();

break;

case 3:

Moto_TurnLeft();

break;

case 4:

Moto_TurnRight();

break;

case 5:

SteerEngineTurningUp();

break;

case 6:

SteerEngineTurningDown();

break;

case 8:

MainLight=1;

break;

case 9:

MainLight=0;

break;

default : TR0=0;TR2=0;

break;

}

}

}

//红外壁障

void extern_int0(void) interrupt 0 using 0

{

EX0=0;

Moto_TurnLeft();

Buffer=0;

EX0=1;

IE0=0;

}

声明:本文内容来自网络转载或用户投稿,文章版权归原作者和原出处所有。文中观点,不代表本站立场。若有侵权请联系本站删除(kf@86ic.com)https://www.86ic.net/changshang/jieda/262159.html

为您推荐

联系我们

联系我们

在线咨询: QQ交谈

邮箱: kf@86ic.com

关注微信
微信扫一扫关注我们

微信扫一扫关注我们

返回顶部