標題: 智能小車循跡程序 [打印本頁]

作者: w9610    時間: 2015-10-6 18:24
標題: 智能小車循跡程序
#include<reg52.h>
sbit IN1 = P0^0;
sbit IN2 = P0^1;
sbit IN3 = P0^2;
sbit IN4 = P0^3;
sbit PWM1 = P0^5;
sbit PWM2 = P0^4;
sbit L1 = P3^5;
sbit G1 = P3^4;
sbit G2 = P3^3;
sbit R1 = P3^2;
unsigned char count1 = 0;
unsigned char count2 = 0;
void left1()//左前
{
  IN1 = 1;IN2 = 0;
}
void left2()//左退
{
IN1 = 0;IN2 = 1;
}
void right1()//右前
{
IN3 = 1;IN4 = 0;
}
void right2()//右退
{
IN3 = 0;IN4 = 1;
}
void sleep1(int c,int d)
{ if(c<=d)
PWM1 = 1;
else PWM1 = 0;
}
void sleep2(int c,int d)
{ if(c<=d)
PWM2 = 1;
else PWM2 = 0;
}
void str(int c1 ,int d1,int c2, int d2)
{
right1();
left1();
sleep1(c1,d1);
sleep2(c2,d2);
}
void left(int c1, int d1,int c2 ,int d2)
{
right1();
left2();
sleep1(c1,d1);
sleep2(c2,d2);
}
void right(int c1, int d1,int c2, int d2)
{
right2();
left1();
sleep1(c1,d1);
sleep2(c2,d2);
}
void main()
{
  TMOD = 0x11;
TH0 = (65536-1000)/256;
TL0 = (65536-1000)%256;
EA = 1;
TR0 = 1;
ET0 = 1;
TH1 = (65536-1000)/256;
TL1 = (65536-1000)%256;
TR1 = 1;
ET1 = 1;
while(1)
{{ if(L1==0&&G1==0&&G2==0&&R1==0)
  str(count1, 500,count2 ,500);
  }
  { if(L1==0&&G1==0&&G2==0&&R1==1)
  left(count1, 200,count2 ,700);
  }
  { if(L1==0&&G1==1&&G2==1&&R1==0)
  str(count1 ,500,count2 ,500);
  }
  { if(L1==1&&G1==0&&G2==0&&R1==0)
  right(count1, 700,count2 ,200);
  }
}
}
void time0() interrupt 1
{ TH0 = (65536-1000)/256;
TL0 = (65536-1000)%256;
count1++;
if(count1 >= 1000)
count1 = 0;
}
void time1() interrupt 1
{ TH1 = (65536-1000)/256;
TL1 = (65536-1000)%256;
count2++;
if(count2 >= 1000)
count2 = 0;
}

作者: admin    時間: 2015-10-6 19:30
非常好,謝謝分享,樓主能提供一下,這個程序的硬件圖嗎
作者: 934451846    時間: 2016-2-16 10:37
好,學到東西了
作者: 柒oo怨    時間: 2016-10-16 11:23
使能端不需要嗎?ENA ENB???
作者: 柒oo怨    時間: 2016-10-16 11:24
使能端不要嗎? ENA  ENB??
作者: 杰HJ    時間: 2016-12-25 16:48
大神能加點文字說明嗎?你那有兩個中斷舵機要一個請問另外那個是控制紅外線的嗎?






歡迎光臨 (http://www.torrancerestoration.com/bbs/) Powered by Discuz! X3.1