wifi小车下位机程序
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
#include <reg52.h>
#define uchar unsigned char
#define uint unsigned int
bit temp1=0,temp2=0;
uchar Buffer[4] = {0}; //从串口接收的数据
uinti,j;
/***************************************端口定义****/ //l298n
sbit EN1=P2^0;
sbit IN1=P2^1;
sbit IN2=P2^2;
sbit EN2=P2^3;
sbit IN3=P2^4;
sbit IN4=P2^5;
//照明用继电器控制的
sbit LED=P1^0;
//避障
sbitzuo=P2^6;
sbit you=P2^7;
/*
变量定义0
*/
ucharkey_stime_counter,hight_votage=15,timeT_counter;
bit key_stime_ok;
/*
变量定义1
*/
uchar key_stime_counter1,hight_votage1=15,timeT_counter1; bit key_stime_ok1;
/*
引脚定义
*/
//舵机
sbitcontrol_signal=P3^6;
sbit control_signa2=P3^7;
ucharnum;//全局变量
void TimerInit()
{
control_signal=0;
TMOD=0x01; //设置定时器0为工作方式1 EA=1; //开总中断
ET0=1; //定时器0中断允许
TH0 = 0xFF; //定时器装初值
TL0 = 0xA3;
TR0=0;
}
/**********定时器2********/
void Timer2Init()
{
control_signa2=0;
T2CON=0x01;
EA = 1;
ET2 = 1;
TL2 = 0xFF;
TH2 = 0x3A;
TR2 = 0;
}
void Delay_1ms(uint i)//1ms延时
{
ucharx,j;
for(j=0;j<i;j++)
for(x=0;x<=148;x++);
}
void Com_Int(void) interrupt 4
{
EA = 0;
if(RI == 1)
{
Buffer[0] = SBUF - 48;
RI = 0;
}
EA = 1;
}
void Com_Init(void)
{
TMOD = 0x20;
PCON = 0x00;
SCON = 0x50;
TH1 = 0xFd; //设置波特率9600
TL1 = 0xFd;
TR1 = 1; //启动定时器1
ES = 1; //开串口中断
EA = 1; //开总中断
}
/*********************************l198n控制************************/ void Moto_Forward()
{
IN1=1;
IN2=0;
IN3=1;
IN4=0;
EN1=1;
EN2=1;
}
void Moto_Backward()
{
IN1=0;
IN2=1;
IN3=0;
IN4=1;
EN1=1;
EN2=1;
}
void Moto_TurnLeft()
{
IN1=0;
IN2=1;
IN3=1;
IN4=0;
EN1=1;
EN2=1;
}
void Moto_TurnRight()
{
IN1=1;
IN2=0;
IN3=0;
IN4=1;
EN1=1;
EN2=1;
}
void Moto_Stop()
{
EN1=0;
EN2=0;
}
/*****************************************/
/*主程序*/
void main(void)
{
LED=0;
Delay_1ms(1);
TimerInit();
Timer2Init();
Com_Init();
while(1)
{
switch (Buffer[0]) //根据键值不同,执行不同的内容{
case 0:
TR0=0;
TR2=0;
Moto_Stop();
LED=1;
break;
case 6:
ET2=0;
ET0=1;
TR2=0;
TR0=1;
Delay_1ms(40);
hight_votage-=1;
if(hight_votage<5)
hight_votage=5;
break;
case 7:
ET2=0;
ET0=1;
TR2=0;
TR0=1;
Delay_1ms(40);
hight_votage+=1;
if(hight_votage>25)
hight_votage=25;
break;
case 8:
ET0=0;
ET2=1;
TR2=1;
Delay_1ms(5);
hight_votage1-=1;
if(hight_votage1<5)
hight_votage1=5;
break;
case 9:
ET0=0;
ET2=1;
TR2=1;
Delay_1ms(5);
hight_votage1+=1;
if(hight_votage1>25)
hight_votage1=25;
break;
case 1: for(num=0;num<45;num++)
{
Moto_Forward();
}
break;
case 2:
for(num=0;num<45;num++)
{
Moto_Backward();
}
break;
case 3:
for(num=0;num<45;num++)
{
Moto_TurnLeft();
}
break;
case 4:
for(num=0;num<45;num++)
{
Moto_TurnRight();
}
break;
case 5: LED=~LED;break;
default : TR0=0;TR2=0;
break;
}
while((you==0)&&(zuo==0)) //死角,或前面有障碍,退 {
Moto_Backward();
}
while(zuo==0) //右转
{
Moto_TurnRight();
}
while(you==0) //左转
{
Moto_TurnLeft();
}
}
}
void timer0(void) interrupt 1 using 0
{
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 Timer2() interrupt 5
{
TH2=0xFF;
TL2=0xA3;
if (++key_stime_counter1>=200)
{
key_stime_counter1=0;
control_signa2=1;
key_stime_ok1 = 1; // 20ms到
timeT_counter1=0;
}
if (key_stime_ok1&&(++timeT_counter1>=hight_votage1))
{
key_stime_ok1=0;
timeT_counter1=0;
control_signa2=0; // hight_votage1*0.1ms到
}
TR2=0; }。