赞
踩
用超声波和sg90舵机模块实现避障
分文件编程
代码如下:
#include “reg52.h”
#include “delay.h”
#include “hc04.h”
#include “sg90.h”
#include “motor.h”
#define MIDDLE 0
#define LEFT 1
#define RIGHT 2
void main()
{
char dir;
double disMiddle;//中间距离
double disLeft;//左边距离
double disRight;//右边距离
Time0Init();//初始化定时器0
Time1Init();//初始化定时器1
sgMiddle();
Delay450ms();
Delay450ms();
dir = MIDDLE;
while(1){ if(dir != MIDDLE){ sgMiddle(); Delay450ms(); dir = MIDDLE; } disMiddle = get_distance(); if(disMiddle > 35){ //前进 goForword(); }else if(disMiddle<10){ goBack(); }else{ //停止 Stop(); //测左边距离 sgLeft(); dir = LEFT; Delay450ms(); disLeft = get_distance(); //回到中间 sgMiddle(); Delay450ms(); //测右边距离 sgRight(); dir = RIGHT; Delay450ms(); disRight = get_distance(); if(disLeft<disRight){ goRight(); Delay150ms(); Stop(); } if(disLeft>disRight){ goLeft(); Delay150ms(); Stop(); } } }
}
#include “reg52.h”
#include “delay.h”
sbit Trig = P2^1;
sbit Echo = P2^2;
void Time1Init()
{
TMOD &= 0x0F; //设置定时器1模式为16位
TMOD |= 0x10;
TL1 = 0;//设置定时器1从0开始数数,不着急启动定时器
TH1 = 0;
TF1 = 0;
}
void startHC()
{
Trig = 0;
Trig = 1;
Delay10us();
Trig = 0;
}
double get_distance()
{
double time;//时间
TL1 = 0;//定时器数据要清零,以便下一次测距 TH1 = 0; //1、Trig,给Trig端口至少10us的高电平,提醒超声波准备发波 startHC(); //2、Echo,从低变为高,表示开始发波 while(Echo == 0); //2.1 波发出去的时候,启动定时器 TR1 = 1;//开始计时 //3、Echo,由高变为低,表示接收到返回来的波 while(Echo == 1); //3.1 波回来的时候,停止定时器 TR1 = 0;//停止定时器 //4、计算中间经过的时间 time = (TH1 * 256 + TL1) * 1.085;//us为单位 //5、计算距离 距离= (340m/s) * (时间)/2 // 340m/s=34cm/ms=0.034cm/us return (0.017 * time);
}
#include “Intrins.h”
void Delay2000ms() //@11.0592MHz
{
unsigned char i, j, k;
_nop_();
i = 15;
j = 2;
k = 235;
do
{
do
{
while (--k);
} while (--j);
} while (--i);
}
void Delay150ms() //@11.0592MHz
{
unsigned char i, j, k;
_nop_();
i = 2;
j = 13;
k = 237;
do
{
do
{
while (--k);
} while (--j);
} while (--i);
}
void Delay10us() //@11.0592MHz
{
unsigned char i;
i = 2;
while (--i);
}
void Delay450ms() //@11.0592MHz
{
unsigned char i, j, k;
_nop_();
i = 4;
j = 39;
k = 209;
do
{
do
{
while (--k);
} while (--j);
} while (--i);
}
#include “reg52.h”
#include “delay.h”
sbit sg90_con = P1^1;
int count = 0;
int angle ;//定义舵机角度
void Time0Init()
{
//1. 配置定时器0工作模式位16位计时
TMOD &= 0xF0;
TMOD |= 0x01;
//2. 给初值,定一个0.5ms出来
TL0=0x33;
TH0=0xFE;
//3. 开始计时
TR0 = 1;
TF0 = 0;
//打开定时器0的中断
ET0 = 1;
//打开中断总开关
EA = 1;
}
void sgLeft()
{
angle = 6;//中间
count = 0;
}
void sgMiddle()
{
angle = 3;//中间
count = 0;
}
void sgRight()
{
angle = 1;//向右转
count = 0;
}
void initSG90_0()
{
angle = 1;//初始化角度为0度,0.5ms的高电平
count = 0;//初始化周期
sg90_con = 1;//一上电就给sg90舵机一个高电平
}
void Time0Handler() interrupt 1//中断
{
count++;//统计爆表的次数 TL0 = 0x33;//重新给初值,使其重新计算0.5ms TH0 = 0xFE; //控制PWM波形 if(count < angle){ sg90_con = 1; }else{ sg90_con = 0; } if(count == 40){//爆表了,给变量加1,加个40次就是20ms count = 0;//当count为40时,表示20ms,此时让count=0,重新计算下一次的20ms,20ms为舵机一个周期 sg90_con = 1; }
}
#include “reg52.h”
sbit RightCon1A = P3^2;
sbit RightCon1B = P3^3;
sbit LeftCon1A = P3^4;
sbit LeftCon1B = P3^5;
void goForword()
{
RightCon1A = 0;
RightCon1B = 1;
LeftCon1A = 0;
LeftCon1B = 1;
}
void goBack()
{
RightCon1A = 1;
RightCon1B = 0;
LeftCon1A = 1;
LeftCon1B = 0;
}
void goRight()
{
RightCon1A = 0;
RightCon1B = 0;
LeftCon1A = 0;
LeftCon1B = 1;
}
void goLeft()
{
RightCon1A = 0;
RightCon1B = 1;
LeftCon1A = 0;
LeftCon1B = 0;
}
void Stop()
{
RightCon1A = 0;
RightCon1B = 0;
LeftCon1A = 0;
LeftCon1B = 0;
}
double get_distance();
void Time1Init();
void Delay2000ms();
void Delay150ms();
void Delay10us();
void Delay450ms();
void Time0Init();
void initSG90_0();
void sgMiddle();
void sgRight();
void sgLeft();
void goForword();
void goBack();
void goRight();
void goLeft();
void Stop();
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。