/******************** (C) COPYRIGHT 2011 野火嵌入式开发工作室 ********************
 * 文件名       ：isr.c
 * 描述         ：中断处理例程
 *
 * 实验平台     ：野火kinetis开发板
 * 库版本       ：
 * 嵌入系统     ：
 *
 * 作者         ：野火嵌入式开发工作室
 * 淘宝店       ：http://firestm32.taobao.com
 * 技术支持论坛 ：http://www.ourdev.cn/bbs/bbs_list.jsp?bbs_id=1008
**********************************************************************************/



#include "common.h"
#include "include.h"


extern u32 i1;/***********************ov7725帧数、工作状态测定*****************************/
extern u32 i2;/***********************ov7725帧数、工作状态测定*****************************/
extern u32 i3;/***********************ov7725帧数、工作状态测定*****************************/
extern u32 i4;/***********************ov7725帧数、工作状态测定*****************************/
extern u32 i5;/***********************ov7725帧数、工作状态测定*****************************/
extern u32 i6;
extern u32 i7;
extern u32 i8;

extern u8  nrf_buff3[ MAX_ONCE_TX_NUM];     //预多 2401发送包   发送示波器参数
extern u8   *nrf_3u32;            //发送指针
extern s16  ppy_;
//PWM输出部分
extern s32   uPWML; 
extern s32   uPWMR;
extern s32   PWML;
extern s32   PWMR;
extern s16 hope_speed;   //60侦对应脉冲数 //1M 接受2900个脉冲 10MS 1M/S  48  2M/S 96 3M/S 144 4M/S 192
u32  whitch_lpt=0; 
extern u8  ppy_qianzhan;
extern volatile u32 LPT_INT_count;//脉冲计数
extern u16 KP,KI,KD;        //速度PID
s16 L_uid=0,R_uid=0;//左右speed控制量
s16 R_E=0;//error积分
s16 L_E=0;//error积分
s16 L_speed;//示波器全局值
s16 R_speed;//示波器全局值
u8 speed_PI_flag=0;
extern u8 get_img_flag;
 u8 g_nSpeedControlPeriod=0;
extern u8 A_S_D_flag;
extern u8 new_img_flag;
extern u8 img_buf[CAMERA_W*CAMERA_H];	     //非压缩的二值化图像（用于识别）
extern s16 img_mid[CAMERA_H];                 //中线数组 有负数
extern u8 *img_bin_buff;  //二值化图像的buf指针，由于开头有 COM_LEN 个字节是留给校验，所以需要加 COM_LEN
s32 D_PWM=0;
extern u8 count_flage;

//角度部分
extern s32   Angle_set;
extern s32   Angle_get;

extern s32   Angle_w_get;
extern s32   Angle_w_set;

extern s32   Angle_error;

extern s32   Angle_p;
extern s32   Angle_d;
u8 zhili_flag=0;

extern s32   Angle_PWM;
extern s32   Dead_PWM;
//速度部分
//速度部分
extern s32   Speed_PWM;

extern 
s32   g_nLeftMotorPulseSigma;
  extern 
    s32   g_nRightMotorPulseSigma;

extern s32   lpt1;
extern s32   lpt2;

extern s32   SPEED_CONTROL_P;
extern s32   SPEED_CONTROL_I;
extern s32   g_fSpeedControlIntegral ;
extern s32   g_fSpeedControlOutNew;
extern s32   g_fSpeedControlOutOld;

extern s8    CAR_SPEED_SETfDelta;

extern s32   Speed_error0;
extern s32   Speed_error_dt;
extern float g_fCarSpeed;
       s32   SpeedLimit= 6000;
       s32   DirectLimit = 2800;
       s32   SpeedFlag=0;
       s32   WriteMIN = 55;
       s32   BlackMAX = 30;
extern float g_fSpeedControlOut;
extern u8   a;
extern u8   b;
extern u8   c;
extern u8   d;
extern u8   e;
extern s8   speed;
       s8   StartingLine = 0;
       s8   StartingFlag = 0;
       s8   StartingFlag1= 1;
//方向控制
extern u8    TSL[128];
       u8    TslOld[128];
       s32   leftsum;
       s32   rightsum;
       s32   mid1=0;
       s32   mid2=64;
       s32   LeftLine;
       s32   RightLine;
       
       
       s32   Direct_error0;
       s32   Direct_error1;   
       s32   WriteSum = 120;
extern s32   Direct_PWM;
       s32   Direct_PWM1;
extern s32   Direct_P;
extern s32   Direct_D;
extern s32   TSL_MAX;
extern s32   TSL_MIN;
       s32   TSLMIN[15];
extern u8    x=1;
extern u8    oledend;
       u32 tsl=0;
//无线模块
extern u8   *nrf_u32;
extern u8   nrf_buff2[ MAX_ONCE_TX_NUM];
extern s32  Start;
       
/************************************************按键调参****************************************************/

void PORTC_IRQHandler()
{ 
    u8  n;    //引脚号 
    n = 8;
    if(PORTC_ISFR & (1 << n))           //PTC8触发中断
    {
        PORTC_ISFR  |= (1 << n);        //写1清中断标志位
     //   disable_irq(PIT1+ 68);          //关闭PIT1中断，避免影响无线模块收发
        
        NRF_Handler();                  //无线模块中断处理函数
     //   enable_irq(PIT1+ 68);          //关闭PIT1中断，避免影响无线模块收发
    }
}

void PORTB_IRQHandler()                        
{
    u8 n=0;
    n=0;                                                     //b0中断
    if(PORTB_ISFR & (1<<n)) 
    { 
      
       
        PORTB_ISFR |= (1<<n);
        Angle_set=Angle_set-1 ;
 
    } 
    /*==============================================================================
	注意：
		场中断放在行中断前面，即优先级高于行中断。
		场中断用户任务里，全部都return，确保不进入行中断的用户任务。
	
	==============================================================================*/
    n = 1;											//场中断
    if(PORTB_ISFR & (1 << n))           	//PTB1触发中断
    {   
     // gpio_turn(PORTB,21);
        //
        /*  以下为用户任务  */
		
		//场中断需要判断是场结束还是场开始
		if(img_flag == IMG_START)         //需要开始采集图像
		{
                    img_flag = IMG_GATHER;	  //标记图像采集中
                    
                    disable_irq(88);

	            DMA_EN(CAMERA_DMA_CH);        //使能通道CHn 硬件请求
            
                    DMA_DADDR(CAMERA_DMA_CH) = (u32)IMG_BUFF;    //恢复地址
			
		}
#ifdef DEBUG
		else if(img_flag == IMG_GATHER)	  //图像采集中进入场中断，即图像采集完毕
		{
			while(1);                 //DMA采集异常
		}
#endif
		else										//图像采集错误
		{
                        disable_irq(88); 						//关闭PTA的中断
            //DMA_IRQ_DIS(CAMERA_DMA_CH);	 //关闭通道CHn 中断请求 
            //LCD_Str(site,"ERROR",BLUE,RED);
			img_flag = IMG_FAIL;	 //标记图像采集失败
		}
        PORTB_ISFR  = ~0;                        //场中断里，全部都要清中断标志位
        i3++;
		return;										//场中断触发了，就不需要处理行中断
		
        /*  以上为用户任务  */
    }
     n=9;                                                     //b9中断
    if(PORTB_ISFR & (1<<n)) 
    { 
         PORTB_ISFR |= (1<<n);
         Angle_set=Angle_set+1;
    }
     n=2;                                                  //b2中断
    if(PORTB_ISFR & (1<<n)) 
    { 
      PORTB_ISFR |= (1<<n);
      BlackMAX = BlackMAX+ 5;
      WriteMIN = WriteMIN+ 5;
    }
  /*  n=3;                                                    //b3中断
    if(PORTB_ISFR & (1<<n)) 
    { 
      PORTB_ISFR |= (1<<n);
      pit_init_ms(PIT3 ,1000);
    }*/
    
    PORTB_ISFR  = ~0;        			//写1清中断标志位
    
}

volatile u16 Vnum=0;

void DMA0_IRQHandler()
{  
 A_S_D_flag=8;
 disable_irq(68);
  i4++;
   volatile u8 i;
    img_flag = IMG_FINISH ;
    //disable_irq(87);                      //场中断开始采集图片时，就关闭了中断，这里就不需要再关闭
    //DMA_DIS(CAMERA_DMA_CH);            	//关闭通道CHn 硬件请求
    DMA_IRQ_CLEAN(CAMERA_DMA_CH);           //清除通道传输中断标志位
    Vnum++;                             //测试用
    i++;
    i++;    

 //   i++;
  //  i++;
  //   i++;
  //  i++;
  //  i++;    

    i++;
    i++;
    i++;
    i++;
    //延时，避免超频时，DMA不稳定而上访
}
/********************************************************************************************************/
  
void  RD_enc_03(void)                                     // 陀螺仪加速度计读取
{
       Angle_get    =ad_mid(ADC1,SE4a,ADC_12bit);// 加速度计读取PTE0
       Angle_w_get  =ad_mid(ADC1,SE6a,ADC_12bit);// 陀螺仪读取PTE2
       Angle_error  = Angle_get-Angle_set;     //加速度计误差      
}

void  Angle_Control(void)
{
      s32 fValue;                                 //陀螺仪误差
      fValue = Angle_p * Angle_error - Angle_d * (Angle_w_get - Angle_w_set);       
      if(fValue<-8000)  
      {
       fValue=-8000;  
      }                     
                                                                               //角度调节pwm下限
      if(fValue> 8000) 
      {
        fValue= 8000;
      }                                                                         //角度调节pwm上限
      Angle_PWM = fValue;
}

void SpeedControl(void)                                                         //速度控制
{   
     float fDelta;
     float fP, fI;
     //if(Angle_error<0)
    // {
      //           g_nLeftMotorPulseSigma =-g_nLeftMotorPulseSigma ;
     
     //            g_nRightMotorPulseSigma=-g_nRightMotorPulseSigma;
     //}
     g_fCarSpeed = (g_nLeftMotorPulseSigma + g_nRightMotorPulseSigma) ;
     g_nLeftMotorPulseSigma = g_nRightMotorPulseSigma = 0; 
     if(g_fCarSpeed>500)
     {
        DisableInterrupts;
       FTM_PWM_init(FTM0,CH1,10000,0);
       FTM_PWM_init(FTM0,CH0,10000,0);
       FTM_PWM_init(FTM0,CH2,10000,0); 
       FTM_PWM_init(FTM0,CH3,10000,0);   
     }
     g_fCarSpeed = g_fCarSpeed*10/95;
     //*nrf_u32 = c = g_fCarSpeed;
    // NRF_MSG_send(COM_SP,nrf_buff2); 
     fDelta = CAR_SPEED_SETfDelta - g_fCarSpeed; 
     fP = fDelta * SPEED_CONTROL_P; 
     if(fP>6000)
     {
         fP=6000;
     }
     fI = fDelta * SPEED_CONTROL_I;
     g_fSpeedControlIntegral += fI;
     if(g_fSpeedControlIntegral>6000)
     {
          g_fSpeedControlIntegral=6000;
     }
     if(g_fSpeedControlIntegral<-6000)
     {
          g_fSpeedControlIntegral=-6000;
     }
     g_fSpeedControlOutOld = g_fSpeedControlOutNew;
     g_fSpeedControlOutNew = fP + g_fSpeedControlIntegral;
}





void SpeedControlOutput(void)
{
     s32 fValue;
     fValue = (g_fSpeedControlOutNew - g_fSpeedControlOutOld)/20;
     g_fSpeedControlOut = fValue * (g_nSpeedControlPeriod+1)  + g_fSpeedControlOutOld;
     g_nSpeedControlPeriod++;
     if(g_nSpeedControlPeriod==20)
     {
         g_nSpeedControlPeriod=0;
     }
     if(g_fSpeedControlOut>SpeedLimit) 
     {
         g_fSpeedControlOut=SpeedLimit;
     }  
     if(g_fSpeedControlOut<-4000)
     {
         g_fSpeedControlOut=-4000;
     }
       
}
void RD_TSL1401(void)
{    
    u8 ch;
    u8 i=0,tslp=0,a,b;
    
    s32 BlackSpotSum=0;
    s32 LeftFlag=0;
    s32 RightFlag=0;
    s32 LineFlag=0; 
    gpio_set(PORTB,9,1);
    gpio_set(PORTB,10,0);   
            for( b=0;b<50;b++)
            {}     
    gpio_set(PORTB,9,0);
    
    gpio_set(PORTB,10,1);
   
            for( b=0;b<50;b++)
            {}
               
    gpio_set(PORTB,9,1);
     
    gpio_set(PORTB,10,0);
   
            for( b=0;b<50;b++)
            {}  
    for(i=0;i<128;i++)
    {
        gpio_set(PORTB,9,0);
       TSL[tslp] = ad_once(ADC1,SE5a,ADC_8bit);
     //  if(TSL[tslp]>70)
     //  {
      //    TSL[tslp]=TslOld[tslp];
      // }
       TslOld[tslp] = TSL[tslp];
       *nrf_u32 = TSL[tslp];
       nrf_u32++;
       ++tslp;
       gpio_set(PORTB,9,1);
            for( b = 0;b<50;b++)
            {} 
    }
    nrf_u32=nrf_u32-128; 
     //无线发送
    TSL_MAX=TSL[0];
    TSL_MIN=TSL[0];
    for(i=0;i<128;i++)
    {
        if(TSL_MAX<TSL[i])
        {
           TSL_MAX = TSL[i];
        }
        if(TSL_MIN>TSL[i])
        {
           TSL_MIN = TSL[i];
        }
    }
     
    tsl=(TSL_MIN+TSL_MAX)/2-2;
    if(tsl>WriteMIN)
    {
      tsl=WriteMIN;
    }
    if(tsl<BlackMAX)
    {
       tsl=BlackMAX;
    }
    for(i=0;i<128;i++)
    {
       if(TSL[i]>tsl)
       {
          TSL[i]=1; 
       }
       else
       {
           TSL[i]=0;
           BlackSpotSum++;
       }
    }
    
    for(i=mid2;i>1;i--)
    {
       if((TSL[i]==1)&&(TSL[i-1]==0)&&(TSL[i-2]==0))
       {
              LeftLine = i;  
              LeftFlag = 1;
              break;
       }
    }
    
    for(i=mid2;i<126;i++)
    {
        if((TSL[i]==1)&&(TSL[i+1]==0)&&(TSL[i+2]==0))
        {
              RightLine = i;
              RightFlag = 2;
              break;
        }
    }
    
    LineFlag = LeftFlag + RightFlag;
      if(LineFlag==0)                                     //都没有找到
      {            
            Direct_error0 =0;
            if(PTD7_IN==0)
            {
              if(BlackSpotSum>100)
              {
                 SpeedLimit=0;
              }
            }
      }
    else
      if(LineFlag==1)                                      //找到左线
      {
           RightLine = LeftLine + WriteSum;
           mid1 = (LeftLine + RightLine) / 2;
           Direct_error0 = 64 - mid1;
      }
    else
      if(LineFlag==2)                                       //找到右线
      {
           LeftLine = RightLine - WriteSum;
           mid1 = (LeftLine + RightLine) / 2;
           Direct_error0 = 64 - mid1;
      }
    else
      if(LineFlag==3)                                        //左右两线都找到
      {     
            mid1 = (LeftLine + RightLine) / 2; 
            StartingLine = RightLine - LeftLine;
            if((StartingLine>15)&&(StartingLine<50)&&(e>14))
            {
                 //  pit_init_ms(PIT3 ,1000);
                   StartingFlag=1;
                   
            }
            Direct_error0 = 64 - mid1;
            
      }
     
      LeftLine=0;
      RightLine=0;
      StartingLine=0;
}
void DirectionControl(void)                                                    //方向控制
{     Direct_error0=80- ppy_;
      Direct_PWM = Direct_P * Direct_error0 + Direct_D * ( Direct_error0 - Direct_error1 );
      Direct_error1 = Direct_error0;
      if(Direct_PWM>3800)
      {
        Direct_PWM = 3800;
      }
     if(Direct_PWM<-3800)
      {
        Direct_PWM = -3800;
      }
     /* mid2=mid1;
      if(mid2>=126)
      {
        mid2=126;
      }
      if(mid2<0)
      {
          mid2=1;
      }*/
      
}


s16 ysz_L_speedcol(void)
{ 
  
  u32 count;
  s16 speed_1 ,E_,p_,i_;
  s16 uid=0;             //控制量
  s16 d_uid;
  s16 uid_1;
  s16 uid_out;
   count=LPTMR0_CNR;
   LPTMR0_CNR=0;
   lptmr_counter_clean();
  speed_1=(s16)(LPT_INT_count*100 + (s16)count-1);  //当前
  
  // oled_bl( 32,5, speed_1);
  LPT_INT_count=0;               //各种清0
  
  lptmr_counter_init(LPT0_ALT1,PPY_LPT,2,LPT_Falling);     //////重要！！！！
  lptmr_counter_init(LPT0_ALT2,PPY_LPT,2,LPT_Falling);     //////重要！！！！
   E_=(s16)(hope_speed-speed_1);                //期望
 
   L_E+=E_;//error 积分
   
   
   p_= (s16)(KP*(E_));
   i_=(s16)(KI*L_E);
  // if(p_>=60)//防止积分饱和
   // p_=60;
 // if(p_<=-60)//防止积分负饱和
   // p_=-60;
  if(i_>=2000)//防止积分饱和
    i_=2000;
  if(i_<=-2000)//防止积分负饱和
    i_=-2000;

  uid_1 = uid;
  uid=p_ + i_ ;
  uid_out=uid;
   /*  d_uid = (uid - uid_1)/3;
     uid_out = d_uid * (g_nSpeedControlPeriod+1)  + uid_1;
     g_nSpeedControlPeriod++;
     if(g_nSpeedControlPeriod==3)
     {
         g_nSpeedControlPeriod=0;
     }*/
 if(uid_out>3000)
     {
          uid_out=3000;
     }
     if(uid_out<-3000)
     {
          uid_out=-3000;
     }
  //   if(Angle_error<0)
   //    uid_out=uid_out+1;
   //  else
    //   uid_out=-uid_out;
     
  return uid_out;
}
s16 ysz_R_speedcol(void)
{ 
  
  static u32 a=0; //测试每秒次数
  u32 count;
  s16 speed_1 ,E_,p_,i_,d;
  static s16 uid=0;             //控制量
  static s16 E_1,    E_2;
  volatile u32 uid2;

      count=LPTMR0_CNR;
    lptmr_counter_clean();
  speed_1=(s16)(LPT_INT_count*1 /*+ (s16)count*/);  //当前
  R_speed=speed_1;
  LPT_INT_count=0;               //各种清0
   
  lptmr_counter_init(LPT0_ALT1,PPY_LPT,2,LPT_Falling);     //////重要！！！！
  lptmr_counter_init(LPT0_ALT2,PPY_LPT,2,LPT_Falling);     //////重要！！！！
   E_=(s16)(hope_speed-speed_1);                //期望
 
    R_E+=E_;
  
   
   p_= (s16)(KP*(E_));
   i_=(s16)(KI*R_E);
    if(p_>=4000)//防止积分饱和
    p_=4000;
  if(p_<=-4000)//防止积分负饱和
    p_=-4000;
  if(i_>=20)//防止积分饱和
    i_=20;
  if(i_<=-20)//防止积分负饱和
    i_=-20;
 
  uid=uid+ p_ + i_ ;
 
  return uid;
}
      
void MotorOutput(void)                                                         //电机输出
{
    s32 fLeft, fRight;
   // if(count_flage==1)
    //   {
      fLeft  =Angle_PWM +L_uid+150/*- g_fSpeedControlOut */- Direct_PWM;
      fRight =Angle_PWM +R_uid/*- g_fSpeedControlOut*/ + Direct_PWM;
     // D_PWM=Direct_PWM;
   // }
  //  else if(count_flage==0)
   // {
   //   fLeft  =Angle_PWM +L_uid+150/*- g_fSpeedControlOut */- D_PWM;
   //   fRight =Angle_PWM +R_uid/*- g_fSpeedControlOut*/ + D_PWM;
   // }
     
     
     uPWML = fLeft;
     uPWMR = fRight;
}
void MotorSpeedOut(void)
{
      s32 fLeftVal, fRightVal; 
      fLeftVal  = uPWML; 
      fRightVal = uPWMR; 
     /* if(fLeftVal  > 0) 
        fLeftVal += 500;                                  //电机死区加入
      else 
      if(fLeftVal  < 0)
        fLeftVal -= 500; 
      if(fRightVal > 0) 
        fRightVal +=500;
      else 
      if(fRightVal < 0)
        fRightVal -=500;  */  
      if(fLeftVal  >  9900) fLeftVal  = 9900;
      if(fLeftVal  < -9900) fLeftVal  =-9900; 
      if(fRightVal >  9900) fRightVal = 9900; 
      if(fRightVal < -9900) fRightVal =-9900; 
      PWML=fLeftVal;
      PWMR=fRightVal;
}
/*void LPT_Handler(void)
{  
     LPTMR0_CSR|=LPTMR_CSR_TCF_MASK;
     lpt2++;
}*/
void LPT_Handler(void) 
{ 
  LPTMR0_CSR|=LPTMR_CSR_TCF_MASK; //清除LPTMR比较标志 
  LPT_INT_count++; //中断溢出加1 
}


void PIT0_IRQHandler()
{   i1++;
  PIT_Flag_Clear(PIT0);
 // if(get_img_flag==1)
   // return;
   zhili_flag=1;
   A_S_D_flag++;
  //speed_PI_flag=1;

   // whitch_lpt++;
 //if(whitch_lpt==1||whitch_lpt==2||whitch_lpt==3||whitch_lpt==4)
    L_uid= ysz_L_speedcol();
  // if(whitch_lpt==5||whitch_lpt==6||whitch_lpt==7||whitch_lpt==8)  
    R_uid=L_uid ;
  
  /* if(whitch_lpt==8)
     whitch_lpt=0;*/
     
    
      RD_enc_03();        //采样
      Angle_Control();     //PD运算
      
      
      
     
      DirectionControl();
                           //SpeedControlOutput();
      MotorOutput();
      MotorSpeedOut();
      if(PWML>0) 
      {FTM_PWM_Duty(FTM0,CH1,0);
        FTM_PWM_Duty(FTM0,CH0, PWML);
        
      }
      if(PWML<0)  
      {
        PWML=-PWML;
        FTM_PWM_Duty(FTM0,CH0,0);
        FTM_PWM_Duty(FTM0,CH1,PWML);
      }
      if(PWMR>0) 
      {
       FTM_PWM_Duty(FTM0,CH2, 0);
       FTM_PWM_Duty(FTM0,CH3, PWMR);
      }
     if(PWMR<0)
      {
        PWMR=-PWMR;
        FTM_PWM_Duty(FTM0,CH3, 0);
        FTM_PWM_Duty(FTM0,CH2,PWMR);
        
     }
        
  
      //以下为测试代码看示波器   //不能与SD卡同时用  60侦
 /*  * ((u16 *) nrf_3u32)=A_S_D_flag;         //期望
        *((u16 *)(nrf_3u32+1))=L_uid;   //当前
        *((u16 *)(nrf_3u32+2))=L_uid;   //控制量
        
        *((u16 *)(nrf_3u32+4))=Angle_w_set;
          *((u16 *)(nrf_3u32+5))=Angle_w_get;
            *((u16 *)(nrf_3u32+6))=Angle_PWM;
              
              *((u16 *)(nrf_3u32+8))=Angle_set;
                *((u16 *)(nrf_3u32+10))=Angle_get;
                  *((u16 *)(nrf_3u32+14))=PWML;
                    *((u16 *)(nrf_3u32+16))=PWMR;
        
        
        
       disable_irq(89); //PTC无线中断 
        NRF_MSG_send(COM_3U32,nrf_buff3);    		//发送数据，发送长度由com对应的函数决定
   enable_irq(89); 	//C中断*/
   zhili_flag=0;
// speed_PI_flag=0;

}
void LCD_TSL1401_SHOW(void)
{
    u8  i;
    u8  bmp;
    LCD_Set_Pos(0,7);
    for(i=0;i<128;i++)
    {
        if(TSL[i]==1)
        {
          bmp=0x03;
        }
         if(TSL[i]==0)
        {
         bmp=0xC0;
        }
        LCD_WrDat(bmp);
    }
   

}
/*void PIT1_IRQHandler()
{     
     PIT_Flag_Clear(PIT1);
     RD_TSL1401();
     DirectionControl();
}*/
/*void PIT2_IRQHandler()
{     
     PIT_Flag_Clear(PIT2); 
    if(x);
    {
     lptmr_counter_init(LPT0_ALT1,5,2,LPT_Falling);
     x--;
    }
    if(lpt1%2==0)
    {             
            g_nLeftMotorPulseSigma  = lpt2*5+LPTMR0_CNR-1;         
            lptmr_counter_clean();
            lpt2=0;
            lptmr_counter_init(LPT0_ALT2,5,2,LPT_Falling);
    }
   if(lpt1%2==1)
    {     
            g_nRightMotorPulseSigma = lpt2*5+LPTMR0_CNR-1;
            lptmr_counter_clean();
            lpt2=0;
            lptmr_counter_init(LPT0_ALT1,5,2,LPT_Falling);
            SpeedControl();
    }        
    lpt1++;    
    if(SpeedFlag==1&&SpeedLimit<6000)
    {
           SpeedLimit = SpeedLimit + 40;
    }
}*/

void SHOW_data(void)
{
   oled_bl( 0,0, WriteSum);
   oled_bl(30,0, TSL_MIN); 
   oled_bl(60,0, speed);
   oled_bl(90,0, WriteMIN);
   oled_bl( 0,3, TSL_MAX);
   oled_bl(30,3, tsl);
   oled_bl(60,3, 0);
   oled_bl(90,3, BlackMAX);
}

/*void PIT3_IRQHandler()
{
    PIT_Flag_Clear(PIT3);
    if(e==3)
    {
        
       pit_init_ms(PIT2 ,50);
       SPEED_CONTROL_P=850;
       SPEED_CONTROL_I=3;
       
       Start=1;
     //disable_irq(71);
    }
    if(StartingFlag==1)
    {
       DisableInterrupts;
       FTM_PWM_init(FTM0,CH1,10000,0);
       FTM_PWM_init(FTM0,CH0,10000,0);
       FTM_PWM_init(FTM0,CH2,10000,0); 
       FTM_PWM_init(FTM0,CH3,10000,0);      
    }
    if(e==3)
    {
        SpeedLimit = 6000;
    }
    if(e==4)
   {
        SpeedLimit = 1000;
         
   }
  if(e==5)
   {
        SpeedLimit = 1000;
         SpeedFlag=1; 
   }
    if(e==15)
    {
       disable_irq(71);
    }
    e++;
}*/



