#include[reg52.h> //注意请把‘ [ ’换成 "<",下同。否则编译时会出错。 #include [intrins.h> //
#define uchar unsigned char #define uint unsigned int sbit en1=P2^0; sbit en2=P2^1; sbit s1=P2^2; sbit s2=P2^3; sbit s3=P2^4; sbit s4=P2^5; uchar t=0; uchar m1=0; uchar m2=0; uchar tmp1,tmp2;
void motor(uchar index, char speed) { if(speed>=-100 && speed<=100) { if(index==1) { m1=abs(speed); if(speed<0) { s1=0; s2=1; } else { s1=1; s2=0; } } if(index==2) { m2=abs(speed); if(speed<0) { s3=0; s4=1; } else { s3=1; s4=0; } } } } void delay(uint j) { for(j;j>0;j--); } void main() { char i; TMOD=0x02; TH0=0x9B; TL0=0x9B; EA=1; ET0=1; TR0=1; while(1) { for(i=0;i<=100;i++) { motor(1,i); motor(2,i); delay(5000); } for(i=100;i>0;i--) { motor(1,i); motor(2,i); delay(5000); } for(i=0;i<=100;i++) { motor(1,-i); motor(2,-i); delay(5000); } for(i=100;i>0;i--) { motor(1,-i); motor(2,-i); delay(5000); } } } void timer0() interrupt 1 { if(t==0) { tmp1=m1; tmp2=m2; } if(t if(t t++; if(t>=100) t=0; }
|