标题: 超声波控制舵机 [打印本页]

作者: pokerface1234    时间: 2018-8-30 03:34
标题: 超声波控制舵机
#include<reg52.h>
#include<intrins.h>
sbit trig=P2^0;
sbit echo=P2^1;
sbit pwm1=P2^2;
sbit pwm2=P2^4;
unsigned long S=0;
unsigned long time;
unsigned long timer;
unsigned char count0;
  unsigned char count1;
unsigned char jd,jd1;
unsigned char w0=0,w1=0;

bit    flag =0;
void delay1s(void)   //误差 0us
{
    unsigned char a,b,c;
    for(c=167;c>0;c--)
        for(b=171;b>0;b--)
            for(a=16;a>0;a--);
    _nop_();
}



void count()
{          while(!echo);
          TR0=1;
          while(echo);
          TR0=0;

          time=TH0*256+TL0;
          TH0=0;
          TL0=0;
          S=(time*1.7)/100;
}       
void Start()
{
           trig=1;
          _nop_();
          _nop_();
          _nop_();
          _nop_();
          _nop_();
          _nop_();
          _nop_();
          _nop_();
          _nop_();
          _nop_();
          _nop_();
          _nop_();
          _nop_();
          _nop_();
          _nop_();
          _nop_();
          _nop_();
          _nop_();
          _nop_();
          _nop_();
          _nop_();
          trig=0;
}
    void zd0() interrupt 1                  
  {
    flag=1;                                                       
  }
  void timer0() interrupt 3
  {
        static unsigned char zjd=5,zjd1=5;
          TH1=(65536-500)/256;                   //2MS定时
        TL1=(65536-500)%256;

        if(w0==5)
{
        if(zjd > jd)zjd--;
        else if(zjd<jd)zjd++;
}
if(w0==5)
{
if(zjd1 > jd1)zjd1--;
        else if(zjd1<jd1)zjd1++;
}

         if(count0< zjd)              
      pwm1=1;
                           
    else
      pwm1=0;  
          count0=(count0+1);         
    count0=count0%40;
         if(count1< zjd1)
          pwm2=1;
         else
          pwm2=0;                       
     count1=(count1+1);         
    count1=count1%40;
     w0++;
        if(w0==6)w0 = 0;
  }
  void zhuaqu()
  {       
                  //Start();
        //count();
                  if(S<20)
                jd=1 ;
       
                  }
void taisheng()
  {       
                  //Start();
      //  count();
                  //if(S>20)
                jd1=4 ;
       
                  }
void xiajiang()
  {       
                  //Start();
        //count();
                  if(S<20)
                jd1=2.5 ;
       
                  }
void songkai()
                 {
                // Start();
                // count();
                // if(S>20)
                 jd=5;
                }
       

void main()
  {
          TMOD=0x11;                   //设T0为方式1,GATE=1;
        TH0=0;                                                                                                                                                       
        TL0=0;         
        TH1=(65536-500)/256;                   //2MS定时
        TL1=(65536-500)%256;
        ET0=1;             //允许T0中断
        ET1=1;                           //允许T1中断
        TR1=1;                           //开启定时器
        EA=1;
       
        jd=5;
        jd1=4;
        count0=0;
        count1=0;
    while(1)
        {
       
                  Start();
        count();
        xiajiang();delay1s();
         zhuaqu();delay1s();
        taisheng();delay1s();
         songkai();delay1s();
          
          }
                       
  }

       


作者: 1443405283    时间: 2021-7-8 11:55
有几个问题问您




欢迎光临 (http://www.51hei.com/bbs/) Powered by Discuz! X3.1