|
不能一起前進(jìn),要不一個(gè)轉(zhuǎn)完另一個(gè)再轉(zhuǎn),要不只有一個(gè)轉(zhuǎn),程序如下:
#include "reg52.h"
#define uchar unsigned char
uchar count=15,LS=10,RS=10; //LS:
sbit pwm_L=P2^7; //左舵機(jī)
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開(kāi)放總中斷
ET0=1; //允許T0中斷
TR0=1; } //允許T0計(jì)數(shù)
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 //定時(shí)器中斷,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; //定時(shí)20ms 頻率50hz
}
|
|