为啥我的程序舵机只能转一下啊
- #include <reg51.h>
- #include <intrins.h>
- #define uint unsigned int
- #define uchar unsigned char
- sbit led1=P1^0;
- sbit led2=P1^2;
- sbit led3=P1^4;
- sbit led4=P1^6;
- sbit pwm=P3^0;
- sbit trig=P3^2;
- sbit echo=P3^4;
- unsigned long s=0;
- unsigned long s1=0;
- unsigned long s2=0;
- unsigned long s3=0;
- uint timer=0,time=0;
- uchar num=0,sun;
- void qianjin()
- {
- led1=1,led2=0,led3=1,led4=0;
- }
- void houtui()
- {
- led1=0,led2=1,led3=0,led4=1;
- }
- void zuozhuan()
- {
- led1=0,led2=1,led3=0,led4=0;
- }
- void youzhuan()
- {
- led1=0,led2=0,led3=1,led4=0;
- }
- void start()
- {
- trig=1;
- _nop_();
- _nop_();
- _nop_();
- _nop_();
- _nop_();
- _nop_();
- _nop_();
- trig=0;
- }
- void delay(uint max)
- {
- int x,y;
- for(x=0;x<max;x++)
- for(y=0;y<2000;y++);
- }
- void conut()
- {
-
- while(!echo);
- TR0=1;
- while(echo);
- TR0=0;
- time=TH0*256+TL0;
- TH0=0;
- TL0=0;
- s=(time*1.7)/100;
-
- }
-
- void main()
- {
-
- TMOD=0X01;
- TH0=(65536-100)/256;
- TR0=(65536-100)%256;
- EA=1;
- ET0=1;
- TR0=1;
-
-
- sun=16;
- timer=0;
- while(timer<20000);
- start();
- conut();
- s1=s;
-
- sun=8;
- timer=0;
- while(timer<=20000);
- start();
- conut();
- s2=s;
-
- sun=1;
- delay(100);
- start();
- conut();
- s3=s;
-
- }
- void T0_time()interrupt 1
- {
- TH0=(65536-100)/256;
- TL0=(65536-100)%256;
- num++;
- if(num==50) num=0;
- if(num>sun) pwm=0;
- else pwm=1;
- timer++;
- }
复制代码
|