標(biāo)題: 如何把兩個(gè)程序合在一起實(shí)現(xiàn)(小車超聲波避障+蜂鳴器音樂(lè)《天空之城》) [打印本頁(yè)]

作者: 508293391    時(shí)間: 2017-5-3 22:42
標(biāo)題: 如何把兩個(gè)程序合在一起實(shí)現(xiàn)(小車超聲波避障+蜂鳴器音樂(lè)《天空之城》)
如何把兩個(gè)程序合在一起實(shí)現(xiàn)(小車超聲波避障+蜂鳴器音樂(lè)《天空之城》) c51單片機(jī) 小車放音樂(lè).rar (54.85 KB, 下載次數(shù): 34)

小車超聲波程序:
#include <AT89x51.H>
        #include <intrins.h>

        #define Sevro_moto_pwm     P2_7           //接舵機(jī)信號(hào)端輸入PWM信號(hào)調(diào)節(jié)速度

        #define  ECHO  P2_4                                   //超聲波接口定義
        #define  TRIG  P2_5                                   //超聲波接口定義

        #define Left_moto_go      {P1_0=1,P1_1=0,P1_2=1,P1_3=0;}    //左邊兩個(gè)電機(jī)向前走
        #define Left_moto_back    {P1_0=0,P1_1=1,P1_2=0,P1_3=1;}         //左邊兩個(gè)電機(jī)向后轉(zhuǎn)
        #define Left_moto_Stop    {P1_0=0,P1_1=0,P1_2=0,P1_3=0;}    //左邊兩個(gè)電機(jī)停轉(zhuǎn)                     
        #define Right_moto_go     {P1_4=1,P1_5=0,P1_6=1,P1_7=0;}        //右邊兩個(gè)電機(jī)向前走
        #define Right_moto_back   {P1_4=0,P1_5=1,P1_6=0,P1_7=1;}        //右邊兩個(gè)電機(jī)向前走
        #define Right_moto_Stop   {P1_4=0,P1_5=0,P1_6=0,P1_7=0;}        //右邊兩個(gè)電機(jī)停轉(zhuǎn)  


        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;//舵機(jī)歸中,產(chǎn)生約,1.5MS 信號(hào)
    unsigned long S=0;
        unsigned long S1=0;
        unsigned long S2=0;
        unsigned long S3=0;
        unsigned long S4=0;
        unsigned int  time=0;                    //時(shí)間變量
        unsigned int  timer=0;                        //延時(shí)基準(zhǔn)變量
        unsigned char timer1=0;                        //掃描時(shí)間變量                                       

                void delay(unsigned int k)          //延時(shí)函數(shù)
{   
     unsigned int x,y;
           for(x=0;x<k;x++)
             for(y=0;y<2000;y++);
}

    void Display(void)                                  //掃描數(shù)碼管
        {
         if(posit==0)
         {P0=(discode[disbuff[posit]])&0x7f;}//產(chǎn)生點(diǎn)
         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()                       //啟動(dòng)測(cè)距信號(hào)
   {
          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)                   //計(jì)算距離
        {
          while(!ECHO);                       //當(dāng)RX為零時(shí)等待
          TR0=1;                               //開(kāi)啟計(jì)數(shù)
          while(ECHO);                           //當(dāng)RX為1計(jì)數(shù)并等待
          TR0=0;                                   //關(guān)閉計(jì)數(shù)
          time=TH0*256+TL0;                   //讀取脈寬長(zhǎng)度
          TH0=0;
          TL0=0;
          S=(time*1.7)/100;        //算出來(lái)是CM
          disbuff[0]=S%1000/100;   //更新顯示
          disbuff[1]=S%1000%100/10;
          disbuff[2]=S%1000%10 %10;
        }

//前速前進(jìn)
     void  run(void)
{        Left_moto_back ;                           
         //Left_moto_go ;     //左電機(jī)往前走
         Right_moto_go ;    //右電機(jī)往前走
}

//前速后退
     void  backrun(void)
{         Left_moto_go ;
         //Left_moto_back ;   //左電機(jī)往前走
         Right_moto_back ;  //右電機(jī)往前走
}

//左轉(zhuǎn)
     void  leftrun(void)
{         Left_moto_go ;
         //Left_moto_back ;   //左電機(jī)往前走
         Right_moto_go ;   //右電機(jī)往前走
}

//右轉(zhuǎn)
     void  rightrun(void)
{         Left_moto_back ;
         //Left_moto_go ;     //左電機(jī)往前走
         Right_moto_back ;  //右電機(jī)往前走
}
/************************************************************************/
//STOP
     void  stoprun(void)
{
         Left_moto_Stop ;   //左電機(jī)停走
         Right_moto_Stop ;  //右電機(jī)停走
}
/************************************************************************/
void  COMM( void )                       
  {
               
                 
                  push_val_left=5;          //舵機(jī)向左轉(zhuǎn)90度
                  timer=0;
                  while(timer<=4000); //延時(shí)400MS讓舵機(jī)轉(zhuǎn)到其位置
                  StartModule();          //啟動(dòng)超聲波測(cè)距
          Conut();                           //計(jì)算距離
                  S2=S;  

                  push_val_left=23;          //舵機(jī)向右轉(zhuǎn)90度
                  timer=0;
                  while(timer<=4000); //延時(shí)400MS讓舵機(jī)轉(zhuǎn)到其位置
                  StartModule();          //啟動(dòng)超聲波測(cè)距
          Conut();                          //計(jì)算距離
                  S4=S;         
        

                  push_val_left=14;          //舵機(jī)歸中
                  timer=0;
                  while(timer<=4000); //延時(shí)400MS讓舵機(jī)轉(zhuǎn)到其位置

                  StartModule();          //啟動(dòng)超聲波測(cè)距
          Conut();                          //計(jì)算距離
                  S1=S;         

          if((S2<20)||(S4<20)) //只要左右各有距離小于,20CM小車后退
                  {
                  backrun();                   //后退
                  timer=0;
                  while(timer<=4000);
                  }
                  
                  if(S2>S4)                 
                     {
                                rightrun();          //車的左邊比車的右邊距離小        右轉(zhuǎn)        
                        timer=0;
                        while(timer<=4000);
                     }                                      
                       else
                     {
                       leftrun();                //車的左邊比車的右邊距離大        左轉(zhuǎn)
                       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;

}

///*TIMER1中斷服務(wù)子函數(shù)產(chǎn)生PWM信號(hào)*/
        void time1()interrupt 3   using 2
{        
     TH1=(65536-100)/256;          //100US定時(shí)
         TL1=(65536-100)%256;
         timer++;                                  //定時(shí)器100US為準(zhǔn)。在這個(gè)基礎(chǔ)上延時(shí)
         pwm_val_left++;
         pwm_Servomoto();

         timer1++;                                 //2MS掃一次數(shù)碼管
         if(timer1>=20)
         {
         timer1=0;
         Display();        
         }
}

///*TIMER0中斷服務(wù)子函數(shù)產(chǎn)生PWM信號(hào)*/
        void timer0()interrupt 1   using 0
{        
           
}



        void main(void)
{

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

        delay(100);
    push_val_left=14;          //舵機(jī)歸中


        while(1)                       /*無(wú)限循環(huán)*/
        {

         if(timer>=1000)          //100MS檢測(cè)啟動(dòng)檢測(cè)一次
           {
               timer=0;
                   StartModule(); //啟動(dòng)檢測(cè)
           Conut();                  //計(jì)算距離
           if(S<30)                  //距離小于20CM
                   {
                   stoprun();          //小車停止
                   COMM();                   //方向函數(shù)
                   }
                   else
                   if(S>35)                  //距離大于,30CM往前走
                   run();
           }

        }
         
         

}


蜂鳴器音樂(lè)《天空之城》  
#include<reg51.h>   

sbit sound=P2^0;  //將sound位定義為P2.0
unsigned int C;     //儲(chǔ)存定時(shí)器的定時(shí)常數(shù)
//低音的音頻宏定義
#define l_dao 262   //將“l(fā)_dao”宏定義為低音“1”的頻率262Hz
#define l_re 286    //將“l(fā)_re”宏定義為低音“2”的頻率286Hz
#define l_mi 311    //將“l(fā)_mi”宏定義為低音“3”的頻率311Hz
#define l_fa 349    //將“l(fā)_fa”宏定義為低音“4”的頻率349Hz
#define l_sao 392   //將“l(fā)_sao”宏定義為低音“5”的頻率392Hz
#define l_la 440    //將“l(fā)_a”宏定義為低音“6”的頻率440Hz
#define l_xi 494    //將“l(fā)_xi”宏定義為低音“7”的頻率494Hz
//中音的音頻宏定義
#define dao 523     //將“dao”宏定義為中音“1”的頻率523Hz
#define re 587      //將“re”宏定義為中音“2”的頻率587Hz
#define mi 659      //將“mi”宏定義為中音“3”的頻率659Hz
#define fa 698      //將“fa”宏定義為中音“4”的頻率698Hz
#define sao 784     //將“sao”宏定義為中音“5”的頻率784Hz
#define la 880      //將“l(fā)a”宏定義為中音“6”的頻率880Hz
#define xi 987      //將“xi”宏定義為中音“7”的頻率523H      
//高音的音頻宏定義
#define h_dao 1046     //將“h_dao”宏定義為高音“1”的頻率1046Hz
#define h_re 1174      //將“h_re”宏定義為高音“2”的頻率1174Hz
#define h_mi 1318      //將“h_mi”宏定義為高音“3”的頻率1318Hz
#define h_fa 1396     //將“h_fa”宏定義為高音“4”的頻率1396Hz
#define h_sao 1567    //將“h_sao”宏定義為高音“5”的頻率1567Hz
#define h_la 1760     //將“h_la”宏定義為高音“6”的頻率1760Hz
#define h_xi 1975     //將“h_xi”宏定義為高音“7”的頻率1975Hz


void delay1()               
   {
     unsigned char i,j;
          for(i=0;i<250;i++)
            for(j=0;j<250;j++)
                             ;
   }

void main(void)
  {
  unsigned char i,j;                                                                    
//以下是《天空之城》片頭曲的一段簡(jiǎn)譜
   unsigned  int code f[]={dao,l_xi,dao,mi,l_dao,l_mi,                    //【6】1713 7--3(天空之城簡(jiǎn)譜表)
                                                      l_la,l_sao,l_la,dao,l_sao,l_mi,          //【6】6561 5--3
                                                   l_fa,l_mi,l_fa,dao,dao,l_mi,dao,                //【7】43411 3--1
                                                   l_xi,l_fa,l_fa,l_xi,l_xi,l_la,l_xi,        //【7】7447 7-067
                                                   dao,l_xi,dao,mi,l_xi,l_mi,l_mi,                //【7】1713 7--33
                                                   l_la,l_sao,l_la,dao,l_sao,l_mi,                //【6】6561 5--3
                                                   l_fa,dao,l_xi,l_xi,dao,re,mi,dao,        //【8】41771 231-0
                                                   dao,l_xi,l_la,l_xi,l_sao,l_la,dao,re,//【8】17675 6-012
                                                   mi,re,mi,fa,sao,re,l_sao,                        //【7】32345 2--5
                                                   re,dao,l_xi,dao,dao,dao,re,mi,                //【8】21711123
                                                   mi,                                                                        //【1】3---
                                                   l_la,l_xi,dao,l_xi,dao,re,dao,l_sao,l_sao,//【9】671712 155-
                                                   fa,mi,re,dao,mi,l_la,l_xi,                        //【7】4321 3--67
                                                   dao,l_xi,dao,mi,l_xi,l_mi,                        //【6】1713 7--3
                                                   l_la,l_sao,l_la,dao,l_sao,l_mi,                //【6】6561 5--3
                                                   l_fa,dao,l_xi,l_xi,dao,re,mi,dao,dao,//【9】41771 2311-
                                                   dao,l_xi,l_la,l_xi,l_sao,l_la,                //【6】17675 6---
                                                   0xff}; //【1*】以0xff作為音符的結(jié)束標(biāo)志                                       
//以下是簡(jiǎn)譜中每個(gè)音符的節(jié)拍
//"4"對(duì)應(yīng)4個(gè)延時(shí)單位,"2"對(duì)應(yīng)2個(gè)延時(shí)單位,"1"對(duì)應(yīng)1個(gè)延時(shí)單位
unsigned char code JP[ ]={3,2,4,4,10,4,      //【6】
                                                  6,2,4,4,10,4,             //【6】
                                                  6,2,2,2,4,10,4,         //【7】
                                                  6,2,4,4,6,2,2,         //【7】
                                                  6,2,4,4,10,2,2,         //【7】
                                                  6,2,4,4,10,4,                 //【6】  
                                                  4,2,2,4,4,4,2,8,         //【8】
                                                  2,2,4,4,4,10,2,2,         //【8】
                                                  6,4,4,4,2,10,4,         //【7】
                                                  1,1,1,1,4,4,4,4,         //【8】
                                                  10,                                 //【1】
                                                  2,2,4,2,2,4,6,4,8, //   【9】
                                                  4,4,4,4,10,2,2,         //【7】
                                                  4,2,4,4,10,4,                 //【6】        
                                                  6,2,4,4,10,4,                 //【6】
                                                  4,2,2,4,4,4,2,2,8, //【9】
                                                  2,2,4,4,4,10,                 //【6】
                                                  10                                 //【1*】
           };
           EA=1;         //開(kāi)總中斷
           ET0=1;        //定時(shí)器T0中斷允許
      TMOD=0x00;    // 使用定時(shí)器T0的模式1(13位計(jì)數(shù)器)
           while(1)       //無(wú)限循環(huán)
                 {
                         i=0;   //從第1個(gè)音符f[0]開(kāi)始播放
              while(f!=0xff)            //只要沒(méi)有讀到結(jié)束標(biāo)志就繼續(xù)播放
                           {
              C=460830/f;           
              TH0=(8192-C)/32;   //可證明這是13位計(jì)數(shù)器TH0高8位的賦初值方法
              TL0=(8192-C)%32;   //可證明這是13位計(jì)數(shù)器TL0低5位的賦初值方法
              TR0=1;             //啟動(dòng)定時(shí)器T0
                                  for(j=0;j<JP;j++)  //控制節(jié)拍數(shù)
                  delay1();          //延時(shí)1個(gè)節(jié)拍單位
                                  TR0=0;                       //關(guān)閉定時(shí)器T0
                              i++;                 //播放下一個(gè)音符
                                  }                                                                  
                }                        
}

void Time0(void ) interrupt 1 using 1  
  {
    sound=!sound;      //將P1.5引腳輸出電平取反,形成方波  
    TH0=(8192-C)/32;   //可證明這是13位計(jì)數(shù)器TH0高8位的賦初值方法
    TL0=(8192-C)%32;   //可證明這是13位計(jì)數(shù)器TL0低5位的賦初值方法
  }




作者: 咔咔咔噠    時(shí)間: 2018-2-22 22:48
沒(méi)有原理圖




歡迎光臨 (http://www.torrancerestoration.com/bbs/) Powered by Discuz! X3.1