登录|立即注册|使用QQ帐号登录
论坛 > 51单片机
发帖|
看4847|回5|收藏
楼主 ID:91738 只看他
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;
}
沙发 ID:1 只看他
2015-10-6 19:30
非常好,谢谢分享,楼主能提供一下,这个程序的硬件图吗
板凳 ID:104790 只看他
2016-2-16 10:37
好,学到东西了
地板 ID:142920 只看他
2016-10-16 11:23
使能端不需要吗?ENA ENB???
5# ID:142920 只看他
2016-10-16 11:24
使能端不要吗? ENA  ENB??
6# ID:157513 只看他
2016-12-25 16:48
大神能加点文字说明吗?你那有两个中断舵机要一个请问另外那个是控制红外线的吗?

51黑电子论坛

Powered by Discuz! X3.1

首页|标准版|触屏版|电脑版