這是51單片機尋跡程序,四驅小車,用的l298n,LM2596S DC-DC 降壓電源模塊BUCK 3A可調降壓模塊,51單片機小板, 尋跡避障模塊 TCRT5000紅外反射傳感器
程序如下
#include <reg52.h>
sbit IN1 = P1^0;
sbit IN2 = P1^1;
sbit IN3 = P1^2;
sbit IN4 = P1^3;
sbit xun1 = P2^6;
sbit xun2 = P2^7;
void delay(int n)
{int i,j;
for(i = 0; i < n; i++)
for(j=0;j<120;j++);
}
typedef unsigned char u8 ;
typedef unsigned int u16;
u8 pwm1;
u8 pwm2;
u16 t;
void main()
{
pwm1=0;
pwm2=0;
t=0;
IN2=0;
IN4=0;
TMOD=0X02;
TH0=210;
TL0=210;
TR0=1;
ET0=1;
EA=1;
while(1)
{
if(xun1==0&&xun2==1)
{
pwm1=150;
pwm2=0;
delay(5);
}
if(xun1==1&&xun2==0)
{
pwm1=0;
pwm2=150;
delay(5);
}
if(xun1==1&&xun2==1)
{
pwm1=90;
pwm2=90;
delay(10);
}
}
}
tieryo() interrupt 1
{
t++;
if(t==250)
{
t=0;
IN1=1;
IN3=1;
}
if(pwm1<=t)
{
IN1=0;
}
if(pwm2<=t)
{
IN3=0;
}
}
|