|
#include<reg52.h>
#define uint unsigned int
#define uchar unsigned char
sbit c0=P2^0;//c0右避障---红外传感器
sbit c1=P2^1;//c1左避障
sbit c2=P2^2;//c2中间避障
sbit D1A=P1^0;//右轮电机输出
sbit D1B=P1^1;
sbit D2A=P1^2;//左轮电机输出
sbit D2B=P1^3;
int t=0;
int PWM_T1=0;//右轮电机占空比
int PWM_T2=0;//左轮电机占空比
//延时函数
void delay_ms(int x)
{
int i,j;
for(i=0;i<x;i++)
for(j=0;j<110;j++);
}
/********************************************
主程序
********************************************/
void main(void)
{
D1B=0;
D2B=0;
TMOD=0x02;//定时器0,工作模式2,8位定时模式
TH0=210; //写入预置初值(取值1-255,数值越大PWM频率越高)
TL0=210; //写入预置值(取值1-255,数值越大PWM频率越高)
TR0=1; //启动定时器
ET0=1; //允许定时器0中断
EA=1; //允许总中断
PWM_T1=150;
PWM_T2=150;
delay_ms(10);
while(1)
{
if(c0==0&&c1==1)
{
PWM_T1=0;
PWM_T2=100;
delay_ms(10);
}
if(c0==1&&c1==0)
{
PWM_T1=100;
PWM_T2=0;
delay_ms(10);
}
if(c2==0)
{
PWM_T1=0;
PWM_T2=0;
delay_ms(10);
}
if(c2==1)
{
PWM_T1=100;
PWM_T2=100;
delay_ms(10);
}
if(c0==1&&c1==1)
{
PWM_T1=100;
PWM_T2=100;
delay_ms(10);
}
if(c0==0&&c1==0)
{
PWM_T1=0;
PWM_T2=0;
delay_ms(10);
}
}
}
//部分程序见附件
|
-
-
避障.zip
856 Bytes, 下载次数: 18, 下载积分: 黑币 -5
|