第七届飞思卡尔智能车光电组代码

没有加入速度检测模块,初次参赛者可以学习一下,程序写的比较简单

  
  
  
  
  1. #include <hidef.h>               
  2. #include "derivative.h"       
  3.  
  4.  
  5. #define uint unsigned int  
  6. #define uchar unsigned char  
  7.  
  8.  
  9. uchar Data[19][5]={0};   
  10. uchar Date[19]={0};                   
  11. uchar num1=0;  
  12. uint num2=0;  
  13. uchar flag=0;  
  14. byte stop_car=0;  
  15. uchar num67=0;    
  16. uchar stop_heidian=0;  
  17. byte m0=0,m1=0,n0=0,n1=0,n2=0,n3=0,n4=0,n5=0,n6=0,n7=0;  
  18.  
  19. byte kk=0;  
  20. uint k2=0;  
  21. uint t;  
  22. static uchar twice=0;  
  23.      
  24. uint rudder[]={1550,1580,1610,1670,1750,1800,1900,1900,2000,2000,  
  25.                    1100,1100,1200,1200,1300,1350,1430,1490,1520};  
  26.        
  27.  
  28. void delay()  
  29. {  
  30.  unsigned int m,n;  
  31.  for(m=0;m<40000;m++)  
  32.   for(n=0;n<3;n++) ;  
  33. }  
  34.  
  35.  
  36. void SetBusClk_64M()  
  37. {  
  38.    CLKSEL=0X00;      //时钟选择寄存器  
  39.    PLLCTL_PLLON=1;   //锁相环电路允许  
  40.    SYNR=0XC0|0X07;   //时钟合成寄存器  
  41.    REFDV=0X80|0X01;  //pllclok=2osc(1+SYNR)(1+REFDV)=128M  
  42.    POSTDIV=0X00;  
  43.    _asm(nop);  
  44.    _asm(nop);  
  45.    while(!(CRGFLG_LOCK==1)); //等待锁相环稳定  
  46.    CLKSEL_PLLSEL=1;          //应用锁相环  
  47. }  
  48.  
  49.    
  50. void IO_Init()  
  51. {  
  52.   DDRA =0XFF;   //A口输出 亮激光管  
  53.   PORTA=0X00;  
  54.   DDRB =0XFF;   //B口输出 亮激光管  
  55.   PORTB=0X00;  
  56.   DDRE_DDRE3=1;//PE3口输出 亮激光管  
  57.   PORTE_PE3=0;  
  58.   DDRE_DDRE4=1;//PE4口输出 亮激光管  
  59.   PORTE_PE4=0;  
  60.   DDRM_DDRM0=0;  
  61.   DDRM_DDRM1=0;  
  62.   DDRM_DDRM2=0;  
  63.   DDRM_DDRM3=0;  
  64.   DDRM_DDRM4=0;  
  65.   DDRM_DDRM5=0;  
  66. }    
  67.  
  68.    
  69. void ECT_Init()  
  70. {  
  71.     PITCFLMT_PITE=0;  //PIT模块禁止  
  72.     PITCE_PCE0=1;     //0通道使能  
  73.     PITCE_PCE1=1;     //1通道使能  
  74.     PITMTLD0=256-1;   //0通道微装载为255  
  75.     PITMTLD1=256-1;   //1通道微装载为255  
  76.     PITMUX_PMUX0=0;   //通道0采用微时间基准0  
  77.     PITMUX_PMUX1=1;   //通道1采用微时间基准01  
  78.     PITLD0=250-1;     //0通道装载为249           256*250/64000000=1ms  
  79.     PITLD1=250-1;     //1通道装载为249           256*250/64000000=1ms  
  80.     PITINTE_PINTE0=1; //0通道超时使能  
  81.     PITINTE_PINTE1=1; //1通道超时使能  
  82.     PITCFLMT_PITE=1;  //PIT模块使能  
  83. }  
  84.  
  85.  
  86.    
  87. void PWM_Init()  
  88. {  
  89.    PWME=0x00;            //关闭PWM通道  
  90.    PWMCTL_CON01=1;       //级联为16位PWM  
  91.    PWMCTL_CON23=1;  
  92.    PWMCTL_CON45=1;  
  93.    PWMPRCLK=0x00;        //COLKA=64MHZ  COLKB=64MHZ  
  94.    PWMSCLA=0x20;         //COLKSA=1MHZ  
  95.    PWMSCLB=0x20;         //COLKSB=1MHZ  
  96.    PWMCLK=0xa0;          //COLKA作为PWM01,COLKB作为PWM23时钟源  COLKSA作为PWM45时钟源  
  97.    PWMPOL=0xff;          //通道1,3,5波形极性为1.先输出高电平  
  98.    PWMCAE=0x00;          //通道1,3,5,7输出模式为左对齐输出模式  
  99.    PWMCNT01=0;           //计数器清0  
  100.    PWMCNT23=0;  
  101.    PWMCNT45=0;  
  102.    PWMPER01=3200;        //PWM01周期为 64000000/3200  20KHZ  
  103.    PWMPER23=3200;        //PWM23周期为 64000000/3200  20KHZ  
  104.    PWMPER45=20000;       //PWM45周期为 64000000/20000 50HZ 20ms   
  105.    PWMDTY01=0;        //控制电机  
  106.    PWMDTY23=0;           //控制电机   
  107.    PWMDTY45=1550;        //控制舵机初始位置   
  108.    PWME_PWME1=1;  
  109.    PWME_PWME3=1;  
  110.    PWME_PWME5=1;  
  111. }  
  112.  
  113.  
  114. void Stop()              
  115. {                                      
  116.   uint num_stop_a=0,num_stop_b=0;            
  117.   uchar j67=0;  
  118.   static uchar twice=0;  
  119.   stop_heidian=0;  
  120.   num_stop_b=Date[1];                                  
  121.   for(j67=3;j67<19;j67+=2)  
  122.         {  
  123.             num_stop_a=Date[j67];  
  124.             if(num_stop_b!=num_stop_a)  
  125.              {  
  126.                 num67++;  
  127.              }  
  128.             num_stop_b=num_stop_a;  
  129.         }  
  130.   for(j67=1;j67<19;j67++)   
  131.     {  
  132.       if(Date[j67]==1) stop_heidian++;  
  133.     }  
  134.   if((num67>3)&&(stop_heidian>=6))  
  135.   {     
  136.      stop_car=1;  
  137.   }  
  138.   else num67=0;  
  139.          
  140. }  
  141.    
  142. unsigned char lvbo(uchar h1,uchar h2,uchar h3,uchar h4,uchar h5)  
  143. {  
  144.      uchar j=0,shi1=0,shi0=0;  
  145.      uchar out_data,data_temp[5]={0};  
  146.      data_temp[0]=h1;  
  147.      data_temp[1]=h2;  
  148.      data_temp[2]=h3;  
  149.      data_temp[3]=h4;  
  150.      data_temp[4]=h5;  
  151.      for(j=0;j<5;j++)  
  152.       {  
  153.          if(data_temp[j]==0)  
  154.             shi0++;  
  155.          else 
  156.             shi1++;    
  157.       }  
  158.      if(shi0>shi1)  
  159.         out_data=1;  
  160.      else 
  161.         out_data=0;  
  162.      return out_data;  
  163. }  
  164.  
  165.  
  166. void car(void)  
  167. {  
  168.       uchar i,j;  
  169.       int k=0;  
  170.       for(i=1;i<19;i++)  
  171.       {  
  172.           Date[i]=lvbo(Data[i][0],Data[i][1],Data[i][2],Data[i][3],Data[i][4]);  
  173.       }  
  174.       for(j=18;j>9;j--)  
  175.       {  
  176.           if(Date[j]==1)  
  177.           {  
  178.               k=j;            
  179.           }  
  180.       }  
  181.       for(j=1;j<10;j++)  
  182.        {  
  183.           if(Date[j]==1)                  
  184.           {  
  185.               k=j;              
  186.           }  
  187.         }  
  188.        
  189.                       
  190.       if(m1==0)  
  191.         {  
  192.            
  193.           if(Date[9]==1)  
  194.             {  
  195.              m0=1;  
  196.             }  
  197.  
  198.           if((m0==1)&&(n0==0))  
  199.             {  
  200.               if(Date[1]==1)  n4=1;  
  201.               if((n4==1)&&(Date[1]==0)) {n4=0;n5=1;m0=0;}      
  202.             }  
  203.           if(n0==1)  
  204.             {  
  205.                 if(Date[12]==1)  n1=1;  
  206.                 if((n1==1)&&(Date[12]==0)) {n0=0;n1=0;}  
  207.             }  
  208.           if(m0==1)  
  209.             {  
  210.                PWMDTY45=2000;  
  211.                if(Date[16]==1) n0=1;                               
  212.             }  
  213.           else 
  214.              {  
  215.               PWMDTY45=rudder[k];  
  216.              }  
  217.         }  
  218.          
  219.                  
  220.       if(m0==0)  
  221.         {  
  222.           if(Date[10]==1)  
  223.           {  
  224.               m1=1;  
  225.           }  
  226.           if(m1==1&&(n2==0))  
  227.           {  
  228.               if(Date[18]==1)  n6=1;  
  229.               if((n6==1)&&(Date[1]==0)) {n6=0;n7=1;m1=0;}  
  230.           }  
  231.           if(n2==1)  
  232.             {  
  233.                 if(Date[7]==1)  n3=1;  
  234.                 if((n3==1)&&(Date[7]==0)) {n2=0;n3=0;}  
  235.             }  
  236.           if(m1==1)  
  237.            {  
  238.               PWMDTY45=1100;  
  239.               if(Date[3]==1) n2=1;  
  240.            }  
  241.           else 
  242.            {  
  243.             PWMDTY45=rudder[k];   
  244.            }  
  245.         }  
  246. }  
  247.  
  248.  
  249.  
  250. void main(void)                           
  251. {   
  252.       DisableInterrupts;  
  253.       SetBusClk_64M();  
  254.       IO_Init();  
  255.       ECT_Init();                            
  256.       PWM_Init();   
  257.       for(t=0;t<=85;t++)  
  258.       {  
  259.           delay();  
  260.       }  
  261.       EnableInterrupts;  
  262.       PWMDTY23=800;  
  263.       for(;;)  
  264.       {    
  265.         if(flag==1)  
  266.         {    
  267.              Stop();  
  268.         }  
  269.         if(stop_car==0)   
  270.              car();              
  271.         else if(stop_car==1)                                   
  272.         {  
  273.              PWMDTY45=1550;  
  274.              PWMDTY23=0;  
  275.              car();           
  276.         }  
  277.       }  
  278. }  
  279.  
  280.  
  281. #pragma CODE_SEG NON_BANKED  
  282. interrupt 66 void ReadSpeed()     //通道0定时中断 10ms  用于控制激光管亮灭  
  283. {  
  284.      uchar i=0;  
  285.      PITTF_PTF0=1;      //清除标志位       
  286.      num1++;  
  287.      num2++;  
  288.      if(num2==5000)  
  289.      {  
  290.           flag=1;  
  291.           num2=0;  
  292.      }    
  293.      if(num1==1)                            //发射第1组激光管  
  294.      {  
  295.             PORTA=0X41;  
  296.             PORTB=0X10;  
  297.       }  
  298.       else if(num1==2)                       //发射第2组激光管  
  299.       {  
  300.             for(i=0;i<5;i++)                   //此循环的作用是多次读取 消除偶然因素  
  301.             {  
  302.                  Data[1][i]=PTM_PTM0;             
  303.                  Data[7][i]=PTM_PTM2;  
  304.                  Data[13][i]=PTM_PTM4;  
  305.              }  
  306.              PORTA=0X82;  
  307.              PORTB=0X20;  
  308.       }  
  309.       else if(num1==3)                       //发射第3组激光管  
  310.       {  
  311.              for(i=0;i<5;i++)  
  312.              {  
  313.                  Data[2][i]=PTM_PTM0;  
  314.                  Data[8][i]=PTM_PTM2;  
  315.                  Data[14][i]=PTM_PTM4;  
  316.              }  
  317.              PORTA=0X04;  
  318.              PORTB=0X41;  
  319.        }  
  320.        else if(num1==4)                       //发射第4组激光管  
  321.        {  
  322.              for(i=0;i<5;i++)  
  323.              {  
  324.                  Data[3][i]=PTM_PTM0;  
  325.                  Data[9][i]=PTM_PTM2;  
  326.                  Data[15][i]=PTM_PTM4;  
  327.               }  
  328.               PORTA=0X08;  
  329.               PORTB=0X82;  
  330.        }  
  331.        else if(num1==5)                       //发射第5组激光管  
  332.        {  
  333.               for(i=0;i<5;i++)  
  334.               {  
  335.                  Data[4][i]=PTM_PTM1;  
  336.                  Data[10][i]=PTM_PTM3;  
  337.                  Data[16][i]=PTM_PTM5;  
  338.               }  
  339.               PORTA=0X10;  
  340.               PORTB=0X04;  
  341.               PORTE |=(1<<3);   
  342.        }  
  343.        else if(num1==6)                       //发射第6组激光管  
  344.        {  
  345.              for(i=0;i<5;i++)  
  346.              {  
  347.                  Data[5][i]=PTM_PTM1;  
  348.                  Data[11][i]=PTM_PTM3;  
  349.                  Data[17][i]=PTM_PTM5;  
  350.               }  
  351.               PORTA=0X20;  
  352.               PORTB=0X08;  
  353.               PORTE &=~(1<<3);   
  354.               PORTE |=(1<<4);    
  355.         }  
  356.         else if(num1==7)                       //灭掉所有激光管  
  357.         {  
  358.              for(i=0;i<5;i++)  
  359.              {  
  360.                  Data[6][i]=PTM_PTM1;  
  361.                   Data[12][i]=PTM_PTM3;  
  362.                   Data[18][i]=PTM_PTM5;  
  363.                }  
  364.                PORTA=0X00;  
  365.                PORTB=0X00;  
  366.                PORTE=0X00;  
  367.                num1=0;  
  368.         }      
  369. }  

 

你可能感兴趣的:(智能车竞赛,光电源码,光电组)