小R科技-WIFI机器人网·机器人创意工作室

 找回密码
 立即注册
查看: 6084|回复: 2

52的舵机超声波小车避障程序改12C5A60S2

[复制链接]
发表于 2015-2-9 11:41:46 | 显示全部楼层 |阅读模式
这个程序在52单片机里可行,换到12C5A60S2就不行了。希望大神帮改下,应该是12C5A60S2是定时器1T的,52是12T的,应该是定时器和延时需要修改吧。


        #include<AT89x51.H>
        #include <intrins.h>

        #define Sevro_moto_pwm     P2_7       

        #define  ECHO  P2_4                                  
        #define  TRIG  P2_5                                  

        #define Left_moto_go      {P1_0=1,P1_1=0,P1_2=1,P1_3=0;}   
        #define Left_moto_back    {P1_0=0,P1_1=1,P1_2=0,P1_3=1;}        
        #define Left_moto_Stop    {P1_0=0,P1_1=0,P1_2=0,P1_3=0;}                        
        #define Right_moto_go     {P1_4=1,P1_5=0,P1_6=1,P1_7=0;}       
        #define Right_moto_back   {P1_4=0,P1_5=1,P1_6=0,P1_7=1;}       
        #define Right_moto_Stop   {P1_4=0,P1_5=0,P1_6=0,P1_7=0;}         


        unsigned char const discode[] ={ 0xC0,0xF9,0xA4,0xB0,0x99,0x92,0x82,0xF8,0x80,0x90,0xBF,0xff/*-*/};
        unsigned char const positon[3]={ 0xfe,0xfd,0xfb};
        unsigned char disbuff[4]          ={ 0,0,0,0,};
    unsigned char posit=0;

           unsigned char pwm_val_left  = 0;//变量定义
        unsigned char push_val_left =14;
    unsigned long S=0;
        unsigned long S1=0;
        unsigned long S2=0;
        unsigned long S3=0;
        unsigned long S4=0;
        unsigned int  time=0;                    //时间变量
        unsigned int  timer=0;                        //延时基准变量
        unsigned char timer1=0;                        //扫描时间变量                                       
/************************************************************************/
                void delay(unsigned int k)          //延时函数
{   
     unsigned int x,y;
           for(x=0;x<k;x++)
             for(y=0;y<2000;y++);
}
/************************************************************************/
    void Display(void)                                  //扫描数码管
        {
         if(posit==0)
         {P0=(discode[disbuff[posit]])&0x7f;}//产生点
         else
         {P0=discode[disbuff[posit]];}

          if(posit==0)
         { P2_1=0;P2_2=1;P2_3=1;}
          if(posit==1)
          {P2_1=1;P2_2=0;P2_3=1;}
          if(posit==2)
          {P2_1=1;P2_2=1;P2_3=0;}
          if(++posit>=3)
          posit=0;
        }
/************************************************************************/
     void  StartModule()                       //启动测距信号
   {
          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 Conut(void)                  
        {
          while(!ECHO);                       //当RX为零时等待
          TR0=1;                               //开启计数
          while(ECHO);                           //当RX为1计数并等待
          TR0=0;                                   //关闭计数
          time=TH0*256+TL0;                   //读取脉宽长度
          TH0=0;
          TL0=0;
          S=(time*1.7)/100;        
          disbuff[0]=S%1000/100;   //更新显示
          disbuff[1]=S%1000%100/10;
          disbuff[2]=S%1000%10 %10;
        }
/************************************************************************/
//前速前进
     void  run(void)
{                                  
         Left_moto_go ;     //左电机往前走
         Right_moto_go ;    //右电机往前走
}
/************************************************************************/
//前速后退
     void  backrun(void)
{
         Left_moto_back ;   //左电机往前走
         Right_moto_back ;  //右电机往前走
}
/************************************************************************/
//左转
     void  leftrun(void)
{
         Left_moto_back ;   //左电机往前走
         Right_moto_go ;   //右电机往前走
}
/************************************************************************/
//右转
     void  rightrun(void)
{
         Left_moto_go ;     //左电机往前走
         Right_moto_back ;  //右电机往前走
}
/************************************************************************/
//STOP
     void  stoprun(void)
{
         Left_moto_Stop ;   //左电机停走
         Right_moto_Stop ;  //右电机停走
}
/************************************************************************/
void  COMM( void )                      
  {
               
                 
                  push_val_left=5;          
                  timer=0;
                  while(timer<=4000);
                  StartModule();          
          Conut();                           //计算距离
                  S2=S;  

                  push_val_left=23;          
                  timer=0;
                  while(timer<=4000);
                  StartModule();          
          Conut();                          //计算距离
                  S4=S;        
       

                  push_val_left=14;          
                  timer=0;
                  while(timer<=4000);
                  StartModule();          
          Conut();                          //计算距离
                  S1=S;        

          if((S2<20)||(S4<20))
                  {
                  backrun();                   //后退
                  timer=0;
                  while(timer<=4000);
                  }
                  
                  if(S2>S4)                 
                     {
                                rightrun();  
                        timer=0;
                        while(timer<=4000);
                     }                                     
                       else
                     {
                       leftrun();               
                       timer=0;
                       while(timer<=4000);
                     }
                                            

}


                void pwm_Servomoto(void)
{  

    if(pwm_val_left<=push_val_left)
               Sevro_moto_pwm=1;
        else
               Sevro_moto_pwm=0;
        if(pwm_val_left>=200)
        pwm_val_left=0;

}

        void time1()interrupt 3   using 2
{       
     TH1=(65536-100)/256;          //100US定时
         TL1=(65536-100)%256;
         timer++;                                  
         pwm_val_left++;
         pwm_Servomoto();

         timer1++;                                 //2MS扫一次数码管
         if(timer1>=20)
         {
         timer1=0;
         Display();       
         }
}

        void timer0()interrupt 1   using 0
{       
          
}

/***************************************************/

        void main(void)
{

        TMOD=0X11;
        TH1=(65536-100)/256;          //100US定时
        TL1=(65536-100)%256;
        TH0=0;
        TL0=0;  
        TR1= 1;
        ET1= 1;
        ET0= 1;
        EA = 1;

        delay(100);
    push_val_left=14;          


        while(1)                     
        {

         if(timer>=1000)       
           {
               timer=0;
                   StartModule();
           Conut();                 
           if(S<20)                 
                   {
                   stoprun();          
                   COMM();                   
                   }
                   else
                   if(S>30)                  
                   run();
           }

        }
          
          

}
        @liuviking万望版主抽那么点小空闲时间,谢谢。

回复

使用道具 举报

发表于 2015-2-12 16:35:40 | 显示全部楼层
路过看看
很好收藏
回复 支持 反对

使用道具 举报

发表于 2015-4-12 18:14:25 | 显示全部楼层
你这个下位机实现什么功能了都?
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关闭

新品特惠推荐上一条 /2 下一条

QQ|QQ技术咨询1|QQ技术咨询2|商务合作微信1:xiaorgeek001|商务合作微信2:XiaoRGEEK|诚聘英才|Archiver|手机版|小R科技-WIFI机器人网·机器人创意工作室 ( 粤ICP备15000788号-6 )

GMT+8, 2024-5-9 08:37 , Processed in 1.068630 second(s), 20 queries .

Powered by XiaoR GEEK X3.4

© 2014-2021 XiaoR GEEK

快速回复 返回顶部 返回列表