找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 2083|回复: 0
打印 上一主题 下一主题
收起左侧

MC9S12XS128智能小车测试源程序

[复制链接]
跳转到指定楼层
楼主
s128智能小车测试程序源码


单片机源程序如下:
  1. #include <hidef.h>      /* common defines and macros */
  2. #include <MC9S12XS128.h>     /* derivative information */
  3. #include "math.h"
  4. #pragma LINK_INFO DERIVATIVE "mc9s12xs128"


  5. /*********************DEFINE************************/
  6. #define  WHITE              50
  7. #define  BLACK              40        
  8. #define  KD                  0                  
  9. #define  EnableSCIReInt     SCI0CR2 |= 0x20     
  10. #define  DisableSCIReInt    SCI0CR2 &= 0xDF
  11. #define  RTI_SWITCH_ON      CRGINT_RTIE=1
  12. #define  RTI_SWITCH_OFF     CRGINT_RTIE=0
  13. #define  IC_SWITCH_ON       TSCR1_TEN=1                                
  14. #define  IC_SWITCH_OFF      TSCR1_TEN=0
  15. //-----------------define key---------------------//
  16. #define  KEY_RUN_OR_LCD     PORTK_PK0
  17. #define  KEY_START_RUN      PORTA_PA1
  18. #define  KEY_UP             PORTA_PA2
  19. #define  KEY_DOWN           PORTA_PA3
  20. #define  KEY_ENTER          PORTA_PA4
  21. //-----------------part of lcd---------------------//
  22. #define         LINE1                                              0
  23. #define         LINE2                                              1
  24. #define         LINE1_HEAD                                  0x80
  25. #define         LINE2_HEAD                                  0xC0
  26. #define         DATA_MODE                                    0x38        // 8 bit data transmission mode, double line
  27. #define         CLR                                                      0x01
  28. #define                BUSY                                               0x80                     // LCD Busy Tag
  29. #define   CURSOR_RESET        0x02
  30. #define   INPUTMODE_CUR_R     0x06
  31. #define   INPUTMODE_CUR_L     0x04
  32. #define   INPUTMODE_ALL_E     0x05
  33. #define   INPUTMODE_ALL_D     0x04
  34. #define         SCREEN_OPEN                                 0x0C
  35. #define         SCREEN_OPEN_CUR                        0x02
  36. #define         SCREEN_OPEN_FLASH                0x01
  37. #define   SCSHIFT_CUR_L       0x10        // Screen or Cursor shift
  38. #define   SCSHIFT_CUR_R       0x14
  39. #define   SCSHIFT_SCR_L       0x18
  40. #define   SCSHIFT_SCR_R       0x1C

  41. //-----------------define lcd pins--------------------//
  42. #define   RS                  PORTA_PA5         //Data Command Pin                1 data                0 command
  43. #define   RW                  PORTA_PA6         //Read Write Pin                  1 read                 0 write
  44. #define   EN                  PORTA_PA7         //LCD Enable Signal
  45. #define   LCD_DATA            PTH
  46. #define   LCD_DATA_DIR        DDRH
  47. #define   LCD_DIR_IN          0x00
  48. #define   LCD_DIR_OUT         0xff
  49. /*********************END************************/



  50. /***************HARDWARE INIT FUN DECLARE******************/
  51. void uart_init(void);
  52. extern void uart_PutChar(unsigned char);
  53. unsigned char TERMIO_GetChar(void);
  54. void printp( char * , ...);  
  55. void CPU_Init(void);
  56. void IO_Init(void);
  57. void SciTx(unsigned char text);
  58. char SciRx(void);                              
  59. void RTI_Init(void);
  60. void IC_Init(void);
  61. void OC7_Init(void);
  62. void PTI0_Init(void);
  63. void PWM_Init(void);
  64. void AD_Init(void);
  65. //-----------------part of lcd---------------------//
  66. void LcdCommand(unsigned char command,unsigned char BusyC);
  67. void LcdData(unsigned char temp,unsigned char BusyC);               
  68. void LcdInit(void);                                                                                          
  69. unsigned char ReadStatus(void);                        
  70. void GotoXY( unsigned char x, unsigned char y );                       
  71. void Print(unsigned char *str);
  72. void Lcd_Print_Str(unsigned char x,unsigned char y,char *s);   
  73. void Lcd_Print_Int( unsigned char x,unsigned char y,unsigned int data);
  74. void Lcd_Print_Char( unsigned char x,unsigned char y,unsigned char data);
  75. void Lcd_Print_uChar( unsigned char x,unsigned char y,char data);
  76. /*********************END************************/
  77.             
  78.             
  79.                              
  80. /***************SOFTWARE FUN DECLARE******************/
  81. void light_flip(void);
  82. void AD_DATA_HIGHT(void);
  83. void AD_DATA_LOW(void);
  84. void VOL_Normaliz(void);
  85. void err_calculate_hight(void);
  86. void err_calculate_low(void);
  87. signed int PID(void);
  88. void key_scan_deal(void);
  89. void LCD_DISPLAY(void);
  90. uint err_Std(void);
  91. void check_line(void);
  92. void LcdDelay(byte k);
  93. void Dly_ms(int ms);
  94. void delay(unsigned int ref);
  95. /*********************END************************/



  96. /*********************DEFINE VAR************************/
  97. //---------------sensor data var-----------------//                              
  98. uchar THRESHOLD_black=40;   
  99. uchar max_vol[9],min_vol[9]={255,255,255,255,255,255,255,255,255};
  100. uchar WHITE_MIN=240,WHITE_MAX=250;
  101. uchar percent[9],data[9],det_vol[9],flip_data[9];
  102. schar err[100],err_e[3];
  103. uchar L_times=130;
  104. byte  flip=0,max_num_p=0;

  105. //---------------sensor_2 data var-----------------//                              
  106. uchar data_2[5],THRESHOLD_2=200,flip_data_2[5],err_e2[3];

  107. //-----------------speed var---------------------//                             
  108. byte  load_bit=0;
  109. uchar pluse_1=0,pluse_2=0,get_speed=0,set_speed=30,now_speed=0;
  110. uchar MAX_SPEED=30;

  111. //-----------------check var-------------------- //
  112. uint  check_pluse=0;
  113. uchar start_line=0,stop=0,stop_delay=0,slope_time=0,slope_timing=0;
  114. byte  check=0,check_bit=0,line_num=0,triangle_status=0,slope_bit=0,black_bit=0;


  115. //------------------key&lcd var----------------------//
  116. byte key_r_o_l=0,lcd_print=1;
  117. uchar menu=1,menu_option=1,bits=0;

  118. //----------------- calc var---------------------//
  119. uchar KP=8,period_1=0,period_2=0;
  120. char  std_err[10];
  121. int  temp_std;

  122. /*********************END************************/



  123. void main()        
  124. {                  
  125.     DisableInterrupts;
  126.     CPU_Init();
  127.     IO_Init();                                       
  128.     uart_init();
  129.     //EnableSCIReInt;
  130.     LcdInit();         
  131.     PWM_Init();                        
  132.     AD_Init();
  133.     VOL_Normaliz();
  134.     Dly_ms(3000);
  135.     Dly_ms(2000);
  136.     RTI_Init();
  137.     IC_Init();  
  138.     for(;;)
  139.     {
  140.       EnableInterrupts;
  141.       //err_calculate_hight();
  142.       key_scan_deal();
  143.     }
  144. }


  145. //--------------------------------------------------------------
  146. //                    HARDWARE FUN START                        
  147. //--------------------------------------------------------------

  148. void CPU_Init()
  149. {
  150.   CLKSEL=0X00;
  151.   PLLCTL_PLLON=1;
  152.   REFDV=0x01;   //BUS_CLOCK = 40M
  153.   SYNR=0x04;
  154.   _asm(nop);
  155.   _asm(nop);
  156.   while(!(CRGFLG_LOCK==1));
  157.   CLKSEL_PLLSEL=0x01;  
  158.   FCLKDIV=0x0F;//Flash Clock Divide Factor 40M/16=?M
  159. }

  160. void SciTx(unsigned char text)
  161. {
  162.         unsigned char temp;
  163.         temp=SCI0SR1;      /*clear flag*/
  164.         while (!(SCI0SR1&0x80));  /* wait for output buffer empty */
  165.         SCI0DRH=0;
  166.         SCI0DRL=text;
  167. }

  168. char SciRx(void)
  169. {
  170.         unsigned char result,temp;
  171.         temp=SCI0SR1;      /*clear flag*/
  172.         while(!(SCI0SR1&0x20));
  173.         result=SCI0DRL;
  174.         return result;
  175. }

  176. void RTI_Init()
  177. {              
  178.           RTICTL=0b10010011;     //0010000=2*10^3,10010011=8*10^3=0.5ms,10001111=16*10^3=1ms,11000111=160x10^3=10ms ,11101111=1.6*10^6=100ms
  179.           //CRGINT_RTIE=1;  
  180. }

  181. void IC_Init(){
  182.   DisableInterrupts;
  183.   TIOS_IOS0=0;     //channel 0 input capture
  184.   TCTL4_EDG0B=1;
  185.   TCTL4_EDG0A=1;
  186.   TIE_C0I=1;      //channel 0 interrupt enable
  187.   TSCR2=0x03;     //TIMEclock = 40M / 8 =0.2us
  188.   TFLG1        =        0xFF;
  189.   TFLG2        =        0x80;
  190.   //TSCR1_TEN=1;   //Timer enable
  191.   EnableInterrupts;
  192. }


  193. void OC7_Init()
  194. {
  195.    TSCR2_TCRE=1;
  196.    TSCR2_PR=0;  
  197.    TIOS_IOS7=1;
  198.    TC7=0x5dc0;  //24000,1ms
  199.    TCTL1=0x00;                    
  200.    TIE_C7I=1;  
  201.    TSCR1_TEN  = 1;     
  202. }

  203. /*
  204. void PTI0_Init()   
  205. {
  206.   PITCFLMT=0x00;
  207.   PITCE_PCE0=1;
  208.   PITMUX_PMUX0=0;
  209.   PITMTLD0=0x01;
  210.   PITLD0=0x0001;    //2x2x25ns=1us;
  211.   PITINTE_PINTE0=1;
  212.   PITCFLMT_PITE=1;
  213. }
  214. */

  215. void PWM_Init()
  216. {
  217.   PWME=0xaa;        //enable c_1,c_3,c_5,c_7
  218.   PWMPOL=0xaa;      
  219.   PWMCAE=0X00;
  220.   PWMCTL=0xf0;      //com 0&1 2&3 4&5
  221.   PWMPRCLK=0x00;
  222.   PWMCLK_PCLK1=1;      //0&1---SA
  223.   PWMSCLA=0x04;     //SA = 5M
  224.   PWMPER01=50000;   //舵机: 5000000/50000=100Hz ;
  225.   PWMDTY01=7100;
  226.   
  227.   PWMPER23=5000;    //电机: 24000000/12000=2000Hz ;24000000/2400=10000Hz ;40000000/5000=8000Hz
  228.   PWMDTY23=2500;
  229.   PWMPER45=5000;   
  230.   PWMDTY45=2500;
  231. }

  232. void AD_Init()
  233. {
  234.   //[3:0]---WRAP  
  235.   //ATD0CTL0=0x00;
  236.   
  237.   //7---ETRIGSEL, [6:5]---Resolution Select, 4---Discharge, [3:0]---External Trigger Channel Select
  238.   ATD0CTL1=0x00;
  239.    
  240.   //6---AFFC;5---ICLKSTP;  [4:3]---External Trigger Edge/Polarity;  2---ETRIGE;
  241.   //1---ASCIE,Sequence Complete Interrupt Enable;  0---ACMPIE,Compare Interrupt Enable
  242.   ATD0CTL2=0x40;
  243.   
  244.   //7---DJM,Data Justification; [6:3]---S8C,S4C,S2C,S1C,Sequence Length;2---FIFO;
  245.   //[1:0]---Background Debug Freeze Enable;   
  246.   ATD0CTL3=0x98;
  247.         
  248.   //[7:5]---SMP,Sample Time Select; [4:0]---PRS,fATDCLK=fBUS/2/(PRS + 1)
  249.   ATD0CTL4=0x01;  
  250.   
  251.   //6---SC5,Special Channel Conversion; 5---SCAN,Continuous Conversion Sequence Mode
  252.   //4---MULT,Multi-Channel Sample Mode; [3:0]---Analog Input Channel Select Code CD,CC,CB,CA
  253.   //ATD0CTL5=0x01;
  254.   
  255.   //ATD0CMPE_CMPE0=1;
  256.   //ATD0CMPHT_CMPHT0=0; //>AD0
  257.   //ATD0DR0L=0x000f;   
  258.   ATD0DIEN=0x00;  
  259. }

  260. void IO_Init(){
  261.    // SENSOR
  262.    DDRB=0xff;
  263.    PORTB=0x00;
  264.    DDRA=0x01;
  265.    PORTA_PA0=0;
  266.    // KEY DEFINE
  267.    DDRK_DDRK0 = 0;
  268.    DDRA_DDRA1 = 0;
  269.    DDRA_DDRA2 = 0;
  270.    DDRA_DDRA3 = 0;
  271.    DDRA_DDRA4 = 0;
  272.    DDRK_DDRK1 = 0;
  273.    DDRK_DDRK2 = 0;
  274.    DDRK_DDRK3 = 0;
  275.    // LCD CONTORL
  276.    DDRA_DDRA5 = 1;
  277.    DDRA_DDRA6 = 1;
  278.    DDRA_DDRA7 = 1;
  279.    //LED
  280.    DDRK_DDRK4 = 1;
  281.    DDRK_DDRK5 = 1;
  282.    PORTK_PK4 = 0;
  283.    PORTK_PK5 = 0;
  284.    //sensor_2
  285.    DDRK_DDRK7 = 1;
  286.    PORTK_PK7 = 0;
  287. }

  288. void Dly_ms(int ms)
  289. {
  290.    int ii,jj;
  291.    if (ms<1) ms=1;
  292.    for(ii=0;ii<ms;ii++)
  293.      for(jj=0;jj<2670;jj++);    //busclk:16MHz--1ms
  294. }
  295.                                     
  296. void delay(unsigned int ref){
  297.   uint kk;
  298.   for(kk=0;kk<ref;kk++){
  299.     asm nop;
  300.   }
  301. }

  302. /************************LCD drive start*************************/
  303. void LcdInit(){   
  304.     LCD_DATA_DIR = LCD_DIR_OUT;   
  305.     LcdDelay(21);
  306.     LcdCommand(DATA_MODE,1);    //set 8 bit data transmission mode
  307.     LcdDelay(7);
  308.     LcdCommand(DATA_MODE,1);    //set 8 bit data transmission mode
  309.     LcdDelay(7);
  310.     LcdCommand(DATA_MODE,1);    //set 8 bit data transmission mode
  311.            
  312.     LcdCommand(CLR,0);          //clear screen   
  313.     LcdCommand(DATA_MODE,1);    //set 8 bit data transmission mode   
  314.     LcdCommand(INPUTMODE_CUR_R | INPUTMODE_ALL_D, 1);   // cursor right, disable moving   
  315.     LcdCommand(SCREEN_OPEN, 1);                     //open display (enable lcd display)   
  316.     LcdCommand(LINE1_HEAD,1);   //set lcd first display address   
  317.     LcdCommand(CLR,1);          //clear screen   
  318. }
  319.    
  320. unsigned char ReadStatus(void)   
  321. {   
  322.     unsigned char cRtn;   
  323.     LCD_DATA_DIR = LCD_DIR_IN;   
  324.     RS = 0;   
  325.     RW = 1;   
  326.     EN = 0;
  327.     delay(500);   
  328.     EN = 1;   
  329.     while (LCD_DATA & BUSY); //Test Busy State   
  330.     cRtn = LCD_DATA_DIR;     // if Not save the port value, it should be change   
  331.     LCD_DATA_DIR = LCD_DIR_OUT;   
  332.     return(cRtn);   
  333. }   
  334.      
  335. void LcdCommand(unsigned char command,unsigned char BusyC){   
  336.     if (BusyC)
  337.     ReadStatus(); //Test it busy or not   
  338.     LCD_DATA=command;   
  339.     RS=0;      
  340.     RW=0;   
  341.     EN=0;   
  342.     delay(500);   
  343.     EN=1;   
  344. }

  345. void LcdData(unsigned char dat,unsigned char BusyC)   
  346. {   
  347.     if (BusyC)
  348.     ReadStatus(); //Test it busy or not   
  349.     LCD_DATA=dat;   
  350.     RS=1;   
  351.     RW=0;   
  352.     EN=0;   
  353.     delay(500);   
  354.     EN=1;   
  355. }  
  356.    
  357. void LcdDelay(byte k){      //k=7=5ms   
  358.     uint i, j;   
  359.     for (i = 0; i < k; i++)   
  360.       for(j = 0; j < 4780; j++);   
  361. }      

  362. void GotoXY( unsigned char x, unsigned char y ){   
  363.     unsigned char address;   
  364.     if (y == LINE1)   
  365.     address = LINE1_HEAD + x;   
  366.     else   
  367.     address = LINE2_HEAD + x;   
  368.     LcdCommand(address,1);   
  369. }

  370. void Lcd_Print_Int( unsigned char x,unsigned char y,unsigned int data){   
  371.     char i=0;
  372.           unsigned char a[5];
  373.           a[0]=(data/10000)%10+'0';                
  374.           a[1]=(data/1000)%10+'0';                                            
  375.           a[2]=(data/100)%10+'0';                                             
  376.           a[3]=(data/10)%10+'0';                                              
  377.           a[4]=data%10+'0';
  378.           for(i=0; i<5; i++)                     
  379.           {
  380.             GotoXY(i+x, y);
  381.             LcdData(a[i],1);   
  382.           }      
  383. }

  384. void Lcd_Print_Char( unsigned char x,unsigned char y,unsigned char data)
  385. {   
  386.     char j=0;
  387.           unsigned char a[3];
  388.           a[0]=(data/100)%10+'0';                
  389.           a[1]=(data/10)%10+'0';                                            
  390.           a[2]=data%10+'0';                                             
  391.           for(j=0; j<3; j++)                     
  392.           {
  393.             GotoXY(j+x, y);
  394.             LcdData(a[j],1);   
  395.           }      
  396. }

  397. void Lcd_Print_uChar( unsigned char x,unsigned char y,char data)
  398. {   
  399.     char j=0,k=0;
  400.     unsigned char a[3];
  401.     if(data>=0){      
  402.             a[0]=(data/100)%10+'0';                
  403.             a[1]=(data/10)%10+'0';                                            
  404.             a[2]=data%10+'0';                                             
  405.             for(j=0; j<3; j++)                     
  406.             {
  407.               GotoXY(j+x, y);
  408.               LcdData(a[j],1);   
  409.             }
  410.     }
  411.     else{
  412.       data=~data;
  413.       data++;
  414.       a[0]=(data/100)%10+'0';                
  415.             a[1]=(data/10)%10+'0';                                            
  416.             a[2]=data%10+'0';
  417.             Lcd_Print_Str(x,y,"-");                                             
  418.             for(k=0; k<3; k++)                     
  419.             {
  420.               GotoXY(k+x+1, y);
  421.               LcdData(a[k],1);   
  422.             }
  423.     }
  424. }

  425. void Lcd_Print_Str(unsigned char x,unsigned char y,char *s)   
  426. {   
  427.     GotoXY(x,y); //set address   
  428.     while(*s!='\0')  // write character   
  429.     {   
  430.       LcdData(*s, 1);      
  431.       s++;   
  432.     }   
  433. }
  434. /************************LCD drive end***************************/

  435. //--------------------------------------------------------------
  436. //                    HARDWARE FUN END                          
  437. //--------------------------------------------------------------


  438. //--------------------------------------------------------------
  439. //                    STARTEGY FUN START                        
  440. //--------------------------------------------------------------
  441. void light_flip(){
  442.       ATD0CTL3=0xc8;
  443.       ATD0CTL5=0x10;
  444.       while(!ATD0STAT0_SCF);
  445.       flip_data[0]=ATD0DR0L;
  446.       flip_data[1]=ATD0DR1L;
  447.       flip_data[2]=ATD0DR2L;
  448.       flip_data[3]=ATD0DR3L;
  449.       flip_data[4]=ATD0DR4L;
  450.       flip_data[5]=ATD0DR5L;
  451.       flip_data[6]=ATD0DR6L;
  452.       flip_data[7]=ATD0DR7L;
  453.       flip_data[8]=ATD0DR8L;  
  454. }

  455. void AD_DATA_HIGHT()
  456. {                     
  457.       //light_flip();
  458.             
  459.       /*
  460.       ATD0CTL3=0x98;
  461.       ATD0CTL5=0x13;
  462.       while(!ATD0STAT0_SCF);
  463.       flip_data[3]=ATD0DR0L;
  464.       flip_data[4]=ATD0DR1L;
  465.       flip_data[5]=ATD0DR2L;
  466.       */
  467.       PORTB = 0x08|0x10|0x20;
  468.       delay(L_times);
  469.       ATD0CTL3=0x98;   
  470.       ATD0CTL5=0x13;
  471.       while(!ATD0STAT0_SCF);
  472.       data[3]= ATD0DR0L;
  473.       data[4]= ATD0DR1L;
  474.       data[5]= ATD0DR2L;
  475.       PORTB = 0x00;
  476.       
  477.       /*
  478.       ATD0CTL3=0x98;
  479.       ATD0CTL5=0x10;
  480.       while(!ATD0STAT0_SCF);
  481.       flip_data[0]=ATD0DR0L;
  482.       flip_data[1]=ATD0DR1L;
  483.       flip_data[2]=ATD0DR2L;
  484.       */
  485.       PORTB = 0x01|0x02|0x04;
  486.       delay(L_times);
  487.       ATD0CTL3=0x98;         
  488.       ATD0CTL5=0x10;
  489.       while(!ATD0STAT0_SCF);
  490.       data[0]= ATD0DR0L;
  491.       data[1]= ATD0DR1L;
  492.       data[2]= ATD0DR2L;
  493.       PORTB = 0x00;

  494.       /*
  495.       ATD0CTL3=0x98;
  496.       ATD0CTL5=0x16;
  497.       while(!ATD0STAT0_SCF);
  498.       flip_data[6]=ATD0DR0L;
  499.       flip_data[7]=ATD0DR1L;
  500.       flip_data[8]=ATD0DR2L;
  501.       */
  502.       PORTB = 0x40|0x80;
  503.       PORTA_PA0=1;
  504.       delay(L_times);
  505.       ATD0CTL3=0x98;            
  506.       ATD0CTL5=0x16;
  507.       while(!ATD0STAT0_SCF);
  508.       data[6]= ATD0DR0L;
  509.       data[7]= ATD0DR1L;
  510.       data[8]= ATD0DR2L;
  511.       PORTB = 0x00;
  512.       PORTA_PA0=0;

  513.       //printp("\n %D,%D,%D,%D,%D,%D,%D,%D,%D \n",flip_data[0],flip_data[1],flip_data[2],flip_data[3],flip_data[4],flip_data[5],flip_data[6],flip_data[7],flip_data[8]);                    
  514. }
  515. void AD_DATA_LOW(){
  516.       ATD0CTL3=0xa8;   
  517.       ATD0CTL5=0x19;
  518.       while(!ATD0STAT0_SCF);
  519.       data_2[0]=ATD0DR0L;
  520.       data_2[1]=ATD0DR1L;
  521.       data_2[2]=ATD0DR2L;
  522.       data_2[3]=ATD0DR3L;
  523.       data_2[4]=ATD0DR4L;
  524. }

  525. //----------------------------压差归一------------------------------//

  526. void VOL_Normaliz(){  
  527.   uchar aa;
  528.   char show_vol=0;
  529.   if(PORTK_PK3==1&&PORTK_PK2==1&&PORTK_PK1==1)  line_num=0;
  530.   else if(PORTK_PK3==1&&PORTK_PK2==1&&PORTK_PK1==0) line_num=6;
  531.   else if(PORTK_PK3==1&&PORTK_PK2==0&&PORTK_PK1==1) line_num=7;
  532.   else if(PORTK_PK3==1&&PORTK_PK2==0&&PORTK_PK1==0) line_num=8;
  533.   else if(PORTK_PK3==0&&PORTK_PK2==1&&PORTK_PK1==1) line_num=9;
  534.   else if(PORTK_PK3==0&&PORTK_PK2==1&&PORTK_PK1==0) line_num=10;
  535.   else if(PORTK_PK3==0&&PORTK_PK2==0&&PORTK_PK1==1) line_num=11;
  536.   else if(PORTK_PK3==0&&PORTK_PK2==0&&PORTK_PK1==0) line_num=12;
  537.   while(KEY_RUN_OR_LCD==1) {
  538.     LCD_DISPLAY();
  539.   }
  540.   key_r_o_l=0xff;
  541.   LcdCommand(CLR,1);
  542.   while(KEY_START_RUN ==1){
  543.     AD_DATA_HIGHT();
  544.     for(aa=0;aa<9;aa++){
  545.       if(data[aa]>max_vol[aa])  max_vol[aa]=data[aa];
  546.       if(data[aa]<min_vol[aa])  min_vol[aa]=data[aa];
  547.       if((max_vol[aa]-min_vol[aa])>det_vol[aa]){                //det_vol impossible zero      
  548.         det_vol[aa]=max_vol[aa]-min_vol[aa];
  549.       }
  550.     }
  551.    
  552.     if(lcd_print==1) {
  553.       if(KEY_DOWN==0)
  554.       {
  555.         Dly_ms(1);
  556.         if(KEY_DOWN==0){
  557.           show_vol++;
  558.           if(show_vol>10) show_vol=0;
  559.           while(!KEY_DOWN);  
  560.         }
  561.       }
  562.       if(KEY_UP==0){
  563.         Dly_ms(1);
  564.         if(KEY_UP==0){
  565.           show_vol--;
  566.           if(show_vol<0) show_vol=10;  
  567.           while(!KEY_UP);
  568.         }  
  569.       }
  570.       if(show_vol==0){
  571.         Lcd_Print_Str(0,0,"data[0]-data[2]:");
  572.         Lcd_Print_Char(0,1,data[0]);
  573.         Lcd_Print_Char(6,1,data[1]);
  574.         Lcd_Print_Char(12,1,data[2]);
  575.       }
  576.       if(show_vol==1){
  577.         Lcd_Print_Str(0,0,"data[3]-data[5]:");
  578.         Lcd_Print_Char(0,1,data[3]);
  579.         Lcd_Print_Char(6,1,data[4]);
  580.         Lcd_Print_Char(12,1,data[5]);
  581.       }
  582.       if(show_vol==2){
  583.         Lcd_Print_Str(0,0,"data[6]-data[8]:");
  584.         Lcd_Print_Char(0,1,data[6]);
  585.         Lcd_Print_Char(6,1,data[7]);
  586.         Lcd_Print_Char(12,1,data[8]);
  587.       }
  588.       if(show_vol==3){
  589.         Lcd_Print_Str(0,0," det[0]-det[2]: ");
  590.         Lcd_Print_Char(0,1,det_vol[0]);
  591.         Lcd_Print_Char(6,1,det_vol[1]);
  592.         Lcd_Print_Char(12,1,det_vol[2]);
  593.       }
  594.       if(show_vol==4){
  595.         Lcd_Print_Str(0,0," det[3]-det[5]: ");
  596.         Lcd_Print_Char(0,1,det_vol[3]);
  597.         Lcd_Print_Char(6,1,det_vol[4]);
  598.         Lcd_Print_Char(12,1,det_vol[5]);
  599.       }
  600.       if(show_vol==5){
  601.         Lcd_Print_Str(0,0," det[6]-det[8]: ");
  602.         Lcd_Print_Char(0,1,det_vol[6]);
  603.         Lcd_Print_Char(6,1,det_vol[7]);
  604.         Lcd_Print_Char(12,1,det_vol[8]);
  605.       }
  606.       if(show_vol==6){
  607.         Lcd_Print_Str(0,0," flip[0]---[2]: ");
  608.         Lcd_Print_Char(0,1,flip_data[0]);
  609.         Lcd_Print_Char(6,1,flip_data[1]);
  610.         Lcd_Print_Char(12,1,flip_data[2]);
  611.       }
  612.       if(show_vol==7){
  613.         Lcd_Print_Str(0,0," flip[3]---[5]: ");
  614.         Lcd_Print_Char(0,1,flip_data[3]);
  615.         Lcd_Print_Char(6,1,flip_data[4]);
  616.         Lcd_Print_Char(12,1,flip_data[5]);
  617.       }
  618.       if(show_vol==8){
  619.         Lcd_Print_Str(0,0," flip[6]---[8]: ");
  620.         Lcd_Print_Char(0,1,flip_data[6]);
  621.         Lcd_Print_Char(6,1,flip_data[7]);
  622.         Lcd_Print_Char(12,1,flip_data[8]);
  623.       }
  624.       if(show_vol==9)
  625.                         {
  626.         Lcd_Print_Str(0,0,"data_2[0--3]");
  627.                                 Lcd_Print_Char(0,1,data_2[0]);
  628.                                 Lcd_Print_Char(6,1,data_2[1]);
  629.                                 Lcd_Print_Char(12,1,data_2[2]);
  630.                         }
  631.                         if(show_vol==10)
  632.                         {
  633.         Lcd_Print_Str(0,0,"data_2[4--5]");
  634.                                 Lcd_Print_Char(0,1,data_2[3]);
  635.                                 Lcd_Print_Char(6,1,data_2[4]);
  636.                         }
  637.     }
  638.     if(lcd_print==2){
  639.       printp("\n %D %D %D %D %D %D %D %D %D \n",data[0],data[1],data[2],data[3],data[4],data[5],data[6],data[7],data[8]);                     
  640.       printp("\n det  :%D %D %D %D %D %D %D %D %D \n",det_vol[0],det_vol[1],det_vol[2],det_vol[3],det_vol[4],det_vol[5],det_vol[6],det_vol[7],det_vol[8]);
  641.     }
  642.   }
  643.   
  644. }

  645. void err_calculate_low(){
  646.     uchar jj,b_num=0;
  647.     AD_DATA_LOW();
  648.      
  649.     for(jj=0;jj<5;jj++){
  650.       if(data_2[jj]<THRESHOLD_2)  b_num++;   
  651.     }
  652.     if(b_num==1||b_num==2)
  653.     {
  654.       if(data_2[0]<THRESHOLD_2&&data_2[1]<THRESHOLD_2) err_e2[0]=37;
  655.       else if(data_2[1]<THRESHOLD_2&&data_2[2]<THRESHOLD_2) err_e2[0]=15;
  656.       else if(data_2[2]<THRESHOLD_2&&data_2[3]<THRESHOLD_2) err_e2[0]=-15;
  657.       else if(data_2[3]<THRESHOLD_2&&data_2[4]<THRESHOLD_2) err_e2[0]=-37;
  658.       
  659.       else if(data_2[2]<THRESHOLD_2)  err_e2[0]=0;
  660.       else if(data_2[0]<THRESHOLD_2)  err_e2[0]=45;
  661.       else if(data_2[1]<THRESHOLD_2)  err_e2[0]=30;
  662.       else if(data_2[3]<THRESHOLD_2)  err_e2[0]=-30;
  663.       else if(data_2[4]<THRESHOLD_2)  err_e2[0]=-45;
  664.       err[0]=err_e2[1];
  665.       err_e2[1]=err_e2[0];
  666.     }
  667. }

  668. void err_calculate_hight()
  669. {
  670.     byte  black_l=0,black_r=8,slope_num=0,min_slope=0,black=0;
  671.     uchar ii=0,max_num=0,max_black=100,n=0;
  672.     schar det_spacing=0,spacing=0;
  673.     uchar up_edge=0,down_edge=0;
  674.     byte  black_num=0,white_num=0,zero_num=0;
  675.     //flip=0;  
  676.     AD_DATA_HIGHT();            
  677.     for(ii=0;ii<9;ii++)
  678.     {
  679.       if(data[ii]<=min_vol[ii])  percent[ii]=1;
  680.       else if(data[ii]>=max_vol[ii])  percent[ii]=100;
  681.       else percent[ii]=(data[ii]-min_vol[ii])*100/det_vol[ii];
  682.   
  683.       if(percent[ii]<max_black&&percent[ii]<=40)   
  684.       {
  685.         max_black=percent[ii];     
  686.         max_num=ii;
  687.       }
  688.       if(percent[ii]<=40) {
  689.         black_num++;      
  690.       }
  691.       if(data[ii]-max_vol[ii]>25) slope_num++;
  692.       if(percent[ii]<=20) zero_num++;
  693.       if(percent[ii]>=50) white_num++;
  694.     }
  695.     for(ii=0;ii<9;ii++){
  696.       if(percent[ii]<40)  {
  697.         black_l=ii;
  698.         break;
  699.       }
  700.     }
  701.     for(ii=8;ii>0;ii--){
  702.       if(percent[ii]<40){
  703.         black_r=ii;
  704.         break;
  705.       }
  706.     }
  707.    
  708. //----------------------flip----------------------//   
  709.     if(black_r-black_l==0||black_r-black_l==1)  {
  710.       flip=1;    //黑线连续,个数规定,数据可用?
  711.       PORTK_PK5=1;
  712.     }
  713.     else if((black_r-black_l)==2&&(black_r+black_l)/2<40)   {
  714.       flip=1;
  715.       PORTK_PK5=1;
  716.     }
  717.     else  {
  718.       flip=0;
  719.       PORTK_PK5=0;
  720.     }                     
  721. //------------------------slope------------------------//   
  722.       //前排为上坡
  723.       if(check==0&&slope_num>=7&&slope_bit==0){   
  724.         slope_bit=1;     //连续检测到正常时清除标志
  725.         PORTK_PK7=1;
  726.         //PORTK_PK4=1;
  727.         KP=13;
  728.         check=0;
  729.       }
  730. //------------------------start-line------------------------//   
  731.       else if(slope_bit==0&&zero_num>=2&&black_num>=6&&check==0&&abs(err_e[0]-err_e[1])<=120){
  732.         slope_bit=0;
  733.         check=1;
  734.         PORTK_PK7 = 1; //打开下排
  735.         //PORTK_PK4=1;
  736.       }
  737.            
  738. //----------------------前排检测到跑道-------------------------//   
  739.         
  740.         if(flip==1&&abs(max_num-max_num_p)<=4)  
  741.         {
  742.          
  743.           if(slope_bit==1){           //连续地看到了数值
  744.             slope_time=1;
  745.           }
  746.           else if(slope_bit==0){
  747.             
  748.             if(max_num==0)   err_e[0]=120-30*percent[max_num]/(percent[max_num]+percent[max_num+1]);
  749.             else if(max_num==8)   err_e[0]=-120+30*percent[max_num]/(percent[max_num]+percent[max_num-1]);
  750.             else
  751.             {
  752.                 switch(max_num)
  753.                 {
  754.                     case 1:spacing=90;    break;
  755.                     case 2:spacing=60;    break;
  756.                     case 3:spacing=30;    break;
  757.                     case 4:spacing=0;     break;                             
  758.                     case 5:spacing=-30;   break;
  759.                     case 6:spacing=-60;   break;
  760.                     case 7:spacing=-90;   break;
  761.                     default:
  762.                     break;
  763.                 }
  764.                 if(percent[max_num+1]==percent[max_num-1])  err_e[0]=spacing;  
  765.                 else if(percent[max_num+1]<percent[max_num-1])
  766.                 {
  767.                   err_e[0]=spacing-30*percent[max_num]/(percent[max_num+1]+percent[max_num]);
  768.                 }
  769.                 else if(percent[max_num+1]>percent[max_num-1])   
  770.                 {
  771.                   err_e[0]=spacing+30*percent[max_num]/(percent[max_num]+percent[max_num-1]);
  772.                 }
  773.             }
  774.               err[0]=err_e[0];
  775.               err_e[1]=err_e[0];
  776.               max_num_p=max_num;  
  777.           }
  778.         }

  779.         else if(slope_bit==1){
  780.           err_calculate_low();  
  781.         }
  782.         /*
  783.         if(slope_bit==0&&white_num==9&&flip==0&&abs(max_num-max_num_p)<=4){
  784.           if(err_e[0]>=50)  {
  785.             err_e[0]=120;
  786.             err[0]=err_e[0];
  787.           }
  788.           else if(err_e[0]<=-50){
  789.             err_e[0]=-120;
  790.             err[0]=err_e[0];
  791.           }
  792.         }
  793.         */
  794.         if(check==1)
  795.         {
  796.           AD_DATA_LOW();
  797.           for(ii=0;ii<5;ii++){
  798.             if(data_2[ii]<THRESHOLD_2)  black++;  
  799.           }
  800.           if(black>=3){
  801.             black_bit=1;
  802.             //check_pluse=0;
  803.             //check=0;
  804.             //PORTK_PK7=0;  
  805.           }
  806.            
  807.          
  808.         }     
  809. }
  810.                   
  811. //-------------------------------------------------------------------------//
  812.                                              
  813. signed int PID(){
  814.   uchar kk=0,low=0;
  815.   sint u;
  816.   
  817.   if(err[0]<=45&&err[0]>=-45) {
  818.     KP=5;
  819.     u=7000+KP*err[0];                 
  820.   }
  821.   else if(err[0]>45){
  822.     KP=9;                                 
  823.           u=6920+KP*err[0];  
  824.   }
  825.   else if(err[0]<-45) {
  826.     KP=9;                                 
  827.           u=7180+KP*err[0];  
  828.   }
  829.   
  830.   //u=7000+KP*err[0]+KD*(err[0]-2*err[1]+err[2]);
  831.         if(u>=8050)  u=8050;
  832.         if(u<=6100)  u=6100;
  833.         for(kk=100;kk>0;kk--) {
  834.           err[kk]=err[kk-1];
  835.         }
  836.         return(u);
  837. }
  838. void key_scan_deal(void){
  839.           /*
  840.           if(PORTK_PK3==0&&PORTK_PK2==0&&PORTK_PK1==0)  line_num=6;
  841.           else if(PORTK_PK3==0&&PORTK_PK2==0&&PORTK_PK1==1) line_num=7;
  842.           else if(PORTK_PK3==0&&PORTK_PK2==1&&PORTK_PK1==0) line_num=8;
  843.           else if(PORTK_PK3==0&&PORTK_PK2==1&&PORTK_PK1==1) line_num=9;
  844.           else if(PORTK_PK3==1&&PORTK_PK2==0&&PORTK_PK1==0) line_num=10;
  845.           else if(PORTK_PK3==1&&PORTK_PK2==0&&PORTK_PK1==1) line_num=11;
  846.           else if(PORTK_PK3==1&&PORTK_PK2==1&&PORTK_PK1==0) line_num=12;
  847.           else if(PORTK_PK3==1&&PORTK_PK2==1&&PORTK_PK1==1) line_num=13;
  848.           */
  849.           if(KEY_RUN_OR_LCD==0)
  850.                 {
  851.                         LcdDelay(14);
  852.                         if(KEY_RUN_OR_LCD==0)
  853.                         {   
  854.                                 key_r_o_l=~key_r_o_l;
  855.                                 while(!KEY_RUN_OR_LCD);
  856.                         }
  857.                 }
  858.                 if(key_r_o_l==0){
  859.       //DisableInterrupts;
  860.       RTI_SWITCH_OFF;
  861.       IC_SWITCH_OFF;
  862.       LCD_DISPLAY();
  863.       PWMDTY45=PWMDTY23;
  864.     }
  865.     else{
  866.       //EnableInterrupts;
  867.       LcdCommand(CLR,1);
  868.       RTI_SWITCH_ON;
  869.       IC_SWITCH_ON;
  870.     }  
  871. }


  872. /*************************LCD show start************************/

  873. void LCD_DISPLAY(void)
  874. {               
  875. /************************KEY_DOWN*************************/               
  876.                 if(KEY_DOWN==0)
  877.                 {
  878.                         LcdDelay(7);
  879.                         if(KEY_DOWN==0)
  880.                         {
  881.                                 //控制主菜单向下   1---9
  882.                                
  883. ……………………

  884. …………限于本文篇幅 余下代码请从51黑下载附件…………
复制代码

所有资料51hei提供下载:
7.15(2).rar (583.2 KB, 下载次数: 14)


分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享淘帖 顶 踩
回复

使用道具 举报

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

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

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