不能一起前进,要不一个转完另一个再转,要不只有一个转,程序如下:
#include "reg52.h"
#define uchar unsigned char
uchar count=15,LS=10,RS=10; //LS:
sbit pwm_L=P2^7; //左舵机
sbit pwm_R=P2^4; //右
sbit k1=P3^6;
void delay(uchar x)
{
uchar i;
while(x--){
for (i=0;i<120;i++);
}
}
void init()
{ pwm_L=0;
pwm_R=0;
TMOD=0x01; //模式1
TH0=(65535-100)/256; //T0置初值
TL0=(65535-100)%256;
//IE0=0; //INT0中断
EA=1; //CPU开放总中断
ET0=1; //允许T0中断
TR0=1; } //允许T0计数
main()
{
init();
while(1)
{ TR0=1;
if(k1==1){LS=15;RS=10;delay(100);}
else{LS=10;RS=15;delay(100);}
TR0=0;
}
}
void timer() interrupt 1 //定时器中断,T0
{ TR0=0;
TH0=(65535-100)/256; // T0置初值
TL0=(65535-100)%256;
TR0=1;
count++;
if(count<=LS) {pwm_L=1;}
else {pwm_L=0;}
if(count<=RS) {pwm_R=1;}
else {pwm_R=0;}
count=count%200; //定时20ms 频率50hz
}
|