/**
  ******************************************************************************
  * @file    Project/STM32F10x_StdPeriph_Template/stm32f10x_it.c 
  * @author  MCD Application Team
  * @version V3.5.0
  * @date    08-April-2011
  * @brief   Main Interrupt Service Routines.
  *          This file provides template for all exceptions handler and 
  *          peripherals interrupt service routine.
  ******************************************************************************
  * @attention
  *
  * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
  * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE
  * TIME. AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY
  * DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING
  * FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE
  * CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
  *
  * <h2><center>&copy; COPYRIGHT 2011 STMicroelectronics</center></h2>
  ******************************************************************************
  */

/* Includes ------------------------------------------------------------------*/
#include "stm32f10x_it.h"
#include "stm32f10x_exti.h"
#include "stm32f10x_tim.h"
#include "TypeDefine.h"
#include "Driver.h"
#include "USARTSetup.h"
#include "ControlSystem.h"
#include "SystemAlarm.h"
#include "DrawLCDGUI.h"
#include "lorawan.h"
#include "stm32f10x_dma.h"
#include <stdlib.h> 
#include <stdio.h> 
#include "Bubble_ADCSetup.h"
#include "ControlSystem.h"
#include "sys.h"

uint16_t	IC2Value;
uint16_t	DutyCycle;
uint32_t	Frequency;

uint8_t EXPCA_flag=0;
uint16_t Quiet_Timedata=0;                 //静音键延时时间
uint8_t  Quiet_Timedata_flag=0;            //静音键延时完成标志
extern uint8_t Sound_prepore_flag;         //预报警标志
uint16_t lorawan_overtime=0;                //lorawan网络超时判断
uint8_t  lorawan_overtime_flag=0;           //超时标志
uint8_t  time_1sflag=0;                     //1s时间标志
uint8_t  time_3sflag=0;                     //3秒时间标志
uint16_t time_3s=0;                         //3s时间
uint16_t lorawan_time_5s=0;                         //5s时间,发送数据后，收不到发送完成标志，延时3s进入睡眠模式
uint8_t  lorawan_time_5sflag=0;                     //5秒时间标志
uint8_t test_flag=0;
uint8_t self_check_finish=0;                //自检完成标志
uint32_t random_time=1750000;               //随机发送时间  

uint16_t random_cont=0;                //随机数种子值
uint32_t random_data=0;                //随机数

uint8_t lorawan_send_finish_flag=0;       //数据发送完成标志
extern uint8_t lorawan_send_data_flag;////数据发送标志
extern uart_recv_t uart_recv;  //串口接收缓存 add by wulianwei
/** @addtogroup STM32F10x_StdPeriph_Template
  * @{
  */

/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
/* Private function prototypes -----------------------------------------------*/
/* Private functions ---------------------------------------------------------*/

/******************************************************************************/
/*            Cortex-M3 Processor Exceptions Handlers                         */
/******************************************************************************/

/**
  * @brief  This function handles NMI exception.
  * @param  None
  * @retval None
  */
void NMI_Handler(void)
{
}

/**
  * @brief  This function handles Hard Fault exception.
  * @param  None
  * @retval None
  */
void HardFault_Handler(void)
{
  /* Go to infinite loop when Hard Fault exception occurs */
  while (1)
  {
  }
}

/**
  * @brief  This function handles Memory Manage exception.
  * @param  None
  * @retval None
  */
void MemManage_Handler(void)
{
  /* Go to infinite loop when Memory Manage exception occurs */
  while (1)
  {
  }
}

/**
  * @brief  This function handles Bus Fault exception.
  * @param  None
  * @retval None
  */
void BusFault_Handler(void)
{
  /* Go to infinite loop when Bus Fault exception occurs */
  while (1)
  {
  }
}

/**
  * @brief  This function handles Usage Fault exception.
  * @param  None
  * @retval None
  */
void UsageFault_Handler(void)
{
  /* Go to infinite loop when Usage Fault exception occurs */
  while (1)
  {
  }
}

/**
  * @brief  This function handles SVCall exception.
  * @param  None
  * @retval None
  */
void SVC_Handler(void)
{
}

/**
  * @brief  This function handles Debug Monitor exception.
  * @param  None
  * @retval None
  */
void DebugMon_Handler(void)
{
}

/**
  * @brief  This function handles PendSVC exception.
  * @param  None
  * @retval None
  */
void PendSV_Handler(void)
{
}

/**
  * @brief  This function handles SysTick Handler.
  * @param  None
  * @retval None
  */
void SysTick_Handler(void)
{
	
	if(( motorWorkState == MOTOR_WORK_OFF ) && (GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_13) == 0))  //在所有中断里面处理电机停止，防止中断导致电机关闭延时
	{
		GPIO_SetBits(GPIOB,GPIO_Pin_13);
	}	
	if( TaskSchedulerTimer.delayTimer != 0x00 )//延时函数计时
	{
		TaskSchedulerTimer.delayTimer--;
	}
	if(emSysWorkStep == EnterSetParam)
	{
		if((KeyActionFlag.keyEnter == emKeyflagLongPress)&&(TaskSchedulerFlag.EnterDelayFlag == TASK_FLAG_CLEAR))
		{
			if(TaskSchedulerTimer.EnterDelayTimer != 0)
			{
				TaskSchedulerTimer.EnterDelayTimer --;
			}
			else 
			{
				TaskSchedulerFlag.EnterDelayFlag = TASK_FLAG_SET;
			}
		}
		else if(GPIO_ReadInputDataBit( KEY_GPIO_PORT,ENTER_GPIO_PIN ) != 0)//有问题，需要处理一下
		{
			TaskSchedulerTimer.EnterDelayTimer = EnterDelay_TIMER;
			TaskSchedulerFlag.EnterDelayFlag = TASK_FLAG_CLEAR;
		}
	}
//	else 
//	{
//		TaskSchedulerTimer.EnterDelayTimer = THREE_MSEC_TIMER;
//	}
	if(lorawan_overtime-- < 2)               //lorawan超时判断函数
	{
		 lorawan_overtime_flag=1;
		 lorawan_overtime=2000;
	}
	if(time_3s++ > 5000)                     //5s定时 
	{
		 time_3s=0;
		 time_3sflag=1;
	}
	if(lorawan_send_data_flag ==1)          //当发送数据后，开始计时5s 
	{
		if(lorawan_time_5s++ > 5000)                     //5s定时 lorawan_time_3s
		{
			 lorawan_time_5s=0;
			 lorawan_time_5sflag=1;
		}
	}
	
	if(sysAlarmFlag.VoltLowest == ALARM_PREPARE) //低电量预报警计时器
	{
		if( Timer_VoltLowes != 0x00)
		{
			Timer_VoltLowes--;
		}
		else
		{
			Timer_VoltLowes = FORTY_MINUTE_TIMER;
			VoltLowest_Alarm_Flag = TASK_FLAG_SET;// //计时到，置位欠压报警标志
		}
		
	}
	
	if(lorawan_send_data_flag ==1)
	{	
		if(GPIO_ReadInputDataBit(LORANODE_STAT_GPIO_PORT, LORANODE_STAT_PIN) == 1)
		{
			 lorawan_send_finish_flag = 1; //当收到一个10ms脉冲的时候，说明数据发送完成
		}
	}
	/* 系统上电初始化定时3s */
	if( TaskSchedulerFlag.initFinishFlag == TASK_FLAG_WAIT )
	{
		if( TaskSchedulerTimer.initFinishTimer != 0x00 )
		{
			TaskSchedulerTimer.initFinishTimer--;
		}
		else
		{
			TaskSchedulerFlag.initFinishFlag = TASK_FLAG_SET;
			TaskSchedulerTimer.initFinishTimer = TRI_SECOND_TIMER;
		}
	}
	if( Quiet_Timedata > 0)  //静音键延时
	{
		  Quiet_Timedata--;
		
	}
	else
	{
		  Quiet_Timedata = 1000;
		  Quiet_Timedata_flag=1;
		  time_1sflag=1;
	}
	
	/* 主芯片给从芯片发送数据时间 */ //目前改为喂狗时间用
	if( TaskSchedulerTimer.hostXmitTimer != 0x00 )
	{
		TaskSchedulerTimer.hostXmitTimer--;
	}
	else
	{
		TaskSchedulerTimer.hostXmitTimer = THREE_MSEC_TIMER;
		if(TaskSchedulerFlag.hostXmitFlag == TASK_FLAG_SET)
		{
			TaskSchedulerFlag.hostXmitFlag = TASK_FLAG_CLEAR;
			GPIO_ResetBits(GPIOC,GPIO_Pin_0);
		}
		else
		{
			TaskSchedulerFlag.hostXmitFlag = TASK_FLAG_SET;
			GPIO_SetBits(GPIOC,GPIO_Pin_0);
		}
	}
	/* 时间读取计时 */
	if( TaskSchedulerTimer.timeReadTimer != 0x00 )
	{
		TaskSchedulerTimer.timeReadTimer--;
	}	
	else
	{
		TaskSchedulerFlag.timeReadFlag = TASK_FLAG_SET;
		TaskSchedulerTimer.timeReadTimer = THIRTY_SECOND_TIMER;//ONE_SECOND_TIMER;
// 		if( TaskSchedulerFlag.testDispFlag == TASK_FLAG_SET )
// 		{
// 			testDispCounts++;
// 		}
// 		else
// 		{
// 			testDispCounts = 0;
// 		}
	}
	
	/* 自动关机计时 */
	/*if( TaskSchedulerFlag.autoPowerOffFlag == TASK_FLAG_WAIT )
	{
		if( TaskSchedulerTimer.autoPowerOffTimer != 0 )
		{
			TaskSchedulerTimer.autoPowerOffTimer--;
		}
		else
		{
			TaskSchedulerFlag.autoPowerOffFlag = TASK_FLAG_SET;
			TaskSchedulerTimer.autoPowerOffTimer = THIRTY_MINUTE_TIMER;
		}
	}	*/
    
    /*设置参数界面30min未操作报警提示*/
    if((TaskSchedulerFlag.NoActionHintFlag == TASK_FLAG_WAIT )&&(realTimeData.stateRun != Alarm))
    {
        if(TaskSchedulerTimer.NoActionHintTimer != 0)
        {
            TaskSchedulerTimer.NoActionHintTimer--;
        }
        else 
        {
            TaskSchedulerFlag.NoActionHintFlag = TASK_FLAG_SET;
            TaskSchedulerTimer.NoActionHintTimer = THIRTY_MINUTE_TIMER;
        }
    }
	
	if( ( TaskSchedulerFlag.lcdBlackFlag == TASK_FLAG_SET ) || ( TaskSchedulerFlag.lcdBlackFlag == TASK_FLAG_WAIT ) )
	{
		if( TaskSchedulerTimer.lcdBlackCloseTimer != 0 )
		{
			TaskSchedulerTimer.lcdBlackCloseTimer--;
		}
		else
		{
			TaskSchedulerFlag.lcdBlackFlag = TASK_FLAG_CLEAR;//只要有按键按下背光就亮
			TaskSchedulerTimer.lcdBlackCloseTimer = FIFTEEN_SECOND_TIMER;//TWO_SECOND_TIMER;//LCD背光关闭计时
		}
	}
	
	/* 电机上电自检,只要是4s没有出现自检成功，则认为自检失败，自检完成后电机可进入正常工作状态 */
	if(TaskSchedulerFlag.motorSelfTestFlag == TASK_FLAG_WAIT)
	{
		if( TaskSchedulerTimer.motorSelfTestTimer != 0x00 )
		{
			TaskSchedulerTimer.motorSelfTestTimer--;
		}
		else
		{
			TaskSchedulerFlag.motorSelfTestFlag = TASK_FLAG_OVERTIME;
			TaskSchedulerTimer.motorSelfTestTimer = MOTOR_TESTSELF_TIMER;
		}
	}
	else if( TaskSchedulerFlag.motorSelfTestFlag == TASK_FLAG_SET )
	{
		/* 电机开始工作后停止InfusionStopTimer时间后再启动 */

		if( TaskSchedulerFlag.motorStopFlag == TASK_FLAG_WAIT )
		{
			if( TaskSchedulerTimer.motorStopTimer != 0x00 )
			{
				TaskSchedulerTimer.motorStopTimer--;
			}
			else
			{			
				motorWorkState = MOTOR_WORK_ON;
				TaskSchedulerTimer.motorStopTimer = InfusionStopTimer;
			}
		}	
		
		if(GPIO_ReadInputDataBit( GPIOB,GPIO_Pin_1 ) != 0) //绿色LED点亮时间处理
		{
			if( G_Led_Time != 0x00)
			{
				G_Led_Time--;
			}
			else
			{
				G_LED_OFF;//时间到关闭LED
			}
		}
		if(TaskSchedulerFlag.NoActionHintFlag == TASK_FLAG_WSET)
		{
			if(GPIO_ReadInputDataBit( GPIOB,GPIO_Pin_9 ) != 0) //绿色LED点亮时间处理
			{
				if( Y_Led_Time != 0x00)
				{
					Y_Led_Time--;
					TaskSchedulerFlag.lcdBlackFlag = TASK_FLAG_SET;
				}
				else
				{
					Y_LED_OFF;//时间到关闭LED
					TaskSchedulerFlag.NoActionHintFlag = TASK_FLAG_WAIT;
					TaskSchedulerFlag.lcdBlackFlag = TASK_FLAG_CLEAR;
				}
			}
		}
		//气泡检测传感器定时
		if( motorWorkState == MOTOR_WORK_ON )//在电机运行的时候进行检测
		{
			if(BubbleCheckTime != 0x00)
			{
				BubbleCheckTime--;
			}
			else
			{
				BubbleCheckFlag=1;//定时到，置位该标志
				BubbleCheckTime = TWO_MSEC_TIMER;
			}
			
		}
		
		//电流检测定时
		if( motorWorkState == MOTOR_WORK_ON )//在电机运行的时候进行检测
		{
			if(ElectricityCheckTime != 0x00)
			{
				ElectricityCheckTime--;
			}
			else
			{
				ElectricityCheckFlag=1;//定时到，置位该标志
				ElectricityCheckTime = FIFTY_MSEC_TIMER; //50ms检测一下
			}
			
		}
		//在电机关闭时，检测电流，防止电机失控
		if( motorWorkState == MOTOR_WORK_OFF )//在电机运行的时候进行检测
		{
			if(ElectricityOffTime != 0x00)
			{
				ElectricityOffTime--;
			}
			else
			{
				ElectricityOFFflag=1;//定时到，置位该标志
				ElectricityOffTime = ONE_SECOND_TIMER; //1s检测一下
			}
			
		}
		
		
		
		/* 非自检模式下当电机开始工作后，如果4s时间内霍尔没有收到采集信号，则说明电机操作失败 */
		if( TaskSchedulerFlag.motorRunFlag == TASK_FLAG_WAIT )
		{
			if( TaskSchedulerTimer.motorSignalCollectTimer != 0x00 )
			{
				TaskSchedulerTimer.motorSignalCollectTimer--;
			}
			else
			{			
 				TaskSchedulerFlag.motorRunFlag = TASK_FLAG_OVERTIME;
				TaskSchedulerTimer.motorSignalCollectTimer = MOTOR_TESTSELF_TIMER;
			}
		}
		/*else if( TaskSchedulerFlag.motorRunFlag == TASK_FLAG_SET )
		{
			TaskSchedulerTimer.motorSignalCollectTimer = MOTOR_TESTSELF_TIMER;
		}*/
	}
	
	if( TaskSchedulerFlag.forceConvertFlag == TASK_FLAG_WAIT )//压力AD采集
	{
		if( TaskSchedulerTimer.pressureConvertTimer != 0x00 )
		{
			TaskSchedulerTimer.pressureConvertTimer--;
		}
		else
		{
			TaskSchedulerFlag.forceConvertFlag = TASK_FLAG_SET;
			TaskSchedulerTimer.pressureConvertTimer = TASK_TIMER_PRES_CONV;			
		}
	}
	
	//if( ( emSysWorkStep != EnterRunFirstDose ) && ( emSysWorkStep != EnterRunMain ) )//在非运行界面时每20s采集一次电池电压
	{
		if( TaskSchedulerFlag.sensorPWBatteryFlag == TASK_FLAG_CLEAR )//每20s关闭一次主芯片节能
		{
			if( TaskSchedulerTimer.batteryVoltTimer != 0 )
			{
				TaskSchedulerTimer.batteryVoltTimer--;
			}
			else
			{
				TaskSchedulerFlag.batteryVoltHeaderFlag = TASK_FLAG_SET;
 				TaskSchedulerFlag.sensorPWBatteryFlag = TASK_FLAG_SET;//传感器电源-电压打开
				TaskSchedulerTimer.batteryVoltTimer = TWENTY_SECOND_TIMER;
			}
		}
	}
	
	if( TaskSchedulerTimer.rtdXmitTimer != 0 ) //实时数据发送计时 30s/帧
	{
		TaskSchedulerTimer.rtdXmitTimer--;
		
	}
	else//当所实时数据发送完毕后，关闭zigbee电源
	{

		/*if( TaskSchedulerFlag.zigbeeETXmitFlag == TASK_FLAG_WAIT )
			{
				TaskSchedulerFlag.zigbeeETXmitFlag = TASK_FLAG_SET;
			}
			else*/
			{
				TaskSchedulerFlag.zigbeeXmitFlag = TASK_FLAG_SET;		
				RunToLorawawn_flag=1;
			}
		if(EngineeringModeValue.test == 0)
		{
			TaskSchedulerTimer.rtdXmitTimer = TWENTY_SECOND_TIMER;
		}
		else if(EngineeringModeValue.test == 1)
		{
			TaskSchedulerTimer.rtdXmitTimer = TWENTY_MINUTE_TIMER;  //定时20分钟发送一次
		}
		srand(random_cont--);	
		//random_data=(uint32_t)(rand()%1200001+3000);//从X到Y，有Y－X＋1个数，所以要产生从X到Y的数，只需要这样写：k=rand()%(Y-X+1)+X;
		random_data=(uint32_t)(rand()%20001+3000);//从X到Y，有Y－X＋1个数，所以要产生从X到Y的数，只需要这样写：k=rand()%(Y-X+1)+X;
		random_time=random_data;
	}
	
	if( random_time != 0 ) //实时数据发送计时 30s/帧
	{
		random_time--;	
	}
	else//当所实时数据发送完毕后，关闭zigbee电源
	{
		 // TaskSchedulerFlag.zigbeeXmitFlag = TASK_FLAG_CLEAR;
			random_time=TWO_MINUTE_TIMER;//随机时间发送完成，赋予默认的3分钟，等待下一个三分钟随机时间

//		  if( TaskSchedulerFlag.zigbeeETXmitFlag == TASK_FLAG_WAIT )
//			{
//				TaskSchedulerFlag.zigbeeETXmitFlag = TASK_FLAG_SET;
//			}
//			else
//			{
//				//TaskSchedulerFlag.zigbeeXmitFlag = TASK_FLAG_SET;	
//        //RunToLorawawn_flag=1;				
//			}
	}
	
	// ---- tcp 检测 add by wulianwei
	if( TaskSchedulerTimer.ModuleInitTimer != 0 ) //TCP检查时间
	{
		TaskSchedulerTimer.ModuleInitTimer--;
	
	}
	else
	{
		TaskSchedulerFlag.ModuleInitFlag = TASK_FLAG_SET;
		TaskSchedulerTimer.ModuleInitTimer = FIFTEEN_SECOND_TIMER;
	}
	if(TaskSchedulerTimer.MQTTSendTimer != 0)
	{
		TaskSchedulerTimer.MQTTSendTimer--;
	}
	else
	{
		TaskSchedulerFlag.MqttSendFlag = TASK_FLAG_SET;  //add by wulianwei
		TaskSchedulerTimer.MQTTSendTimer = TWENTY_MINUTE_TIMER;
		if(EngineeringModeValue.test == 0)
		{
			TaskSchedulerTimer.MQTTSendTimer = TWENTY_SECOND_TIMER;
		}
		
	}
	if(TaskSchedulerTimer.MQTTReSendTimer != 0)
	{
		TaskSchedulerTimer.MQTTReSendTimer--;
	}
	else
	{
		TaskSchedulerFlag.MqttReSendFlag = TASK_FLAG_SET;
		TaskSchedulerTimer.MQTTReSendTimer = FIVE_SECOND_TIMER;
	}
	// ---- tcp 检测 add by wulianwei
	
	if( speakerWorkStep != emSpeakerNoneWork )
	{
		if( TaskSchedulerTimer.speakerTimer != 0x00 )//蜂鸣器计时
		{
			TaskSchedulerTimer.speakerTimer--;
		}
		else
		{
			/*if(( sysPromptToneType == WarringTone ) || ( sysPromptToneType == InfusionTone ) || ( sysPromptToneType == Sound_prepore ))
			{
				
				switch( speakerWorkStep )
				{
					case emSpeakerWorkStart:
						TaskSchedulerTimer.speakerTimer = ONE_MSEC_TIMER;//蜂鸣器计时 100ms
						speakerWorkStep = emSpeakerWorkStep1;
					  SpeakerCtl( DRIVER_OFF );
						break;
					case emSpeakerWorkStep1:
						TaskSchedulerTimer.speakerTimer = TWO_MSEC_TIMER;//蜂鸣器计时 200ms
						speakerWorkStep = emSpeakerWorkStep2;
					SpeakerCtl( DRIVER_ON );
						break;
					case emSpeakerWorkStep2:
						TaskSchedulerTimer.speakerTimer = TWO_MSEC_TIMER;//蜂鸣器计时 200ms
						speakerWorkStep = emSpeakerWorkStep3;
					  SpeakerCtl( DRIVER_OFF );
						break;
					case emSpeakerWorkStep3:
						TaskSchedulerTimer.speakerTimer = TWO_MSEC_TIMER;//蜂鸣器计时 200ms
						speakerWorkStep = emSpeakerWorkStep4;
					  SpeakerCtl( DRIVER_ON );
						break;		
					case emSpeakerWorkStep4:
						speakerWorkStep = emSpeakerWorkStep5;
					  SpeakerCtl( DRIVER_OFF );
						break;
					default:break;
				}
			}
			else if(sysPromptToneType == RemindeTone)
			{
				switch( speakerWorkStep )
				{
					case emSpeakerWorkStart:
						TaskSchedulerTimer.speakerTimer = TWO_MSEC_TIMER;//蜂鸣器计时 200ms
						speakerWorkStep = emSpeakerWorkStep1;
						break;
					case emSpeakerWorkStep1:
						TaskSchedulerTimer.speakerTimer = TWO_MSEC_TIMER;//蜂鸣器计时 200ms
						speakerWorkStep = emSpeakerWorkStep2;
						break;
					case emSpeakerWorkStep2:
						speakerWorkStep = emSpeakerWorkStep3;
						break;
				}
			}*/
//			else
			if(( sysPromptToneType == StartupTone ) || ( sysPromptToneType == KeypadShortTone ) || ( sysPromptToneType == KeypadLongTone )
				|| ( sysPromptToneType == VerificationTone )|| ( sysPromptToneType == SoundOff ))
			{
				speakerWorkStep = emSpeakerWorkDone;
			}
		}
	}
	if( sysAlarmFlag.VoltLowest == ALARM_PREPARE )
	//if(  Sound_prepore_flag == 2)
	{
		if( TaskSchedulerTimer.batLowPrepareDispTimer != 0x00 )
		{
			TaskSchedulerTimer.batLowPrepareDispTimer--;
		}
		else
		{
			if( TaskSchedulerFlag.batLowPrepareFlag == TASK_FLAG_WCLEAR )//电池低压预报标志
			{
				TaskSchedulerFlag.batLowPrepareFlag = TASK_FLAG_SET;
			}
			else if( TaskSchedulerFlag.batLowPrepareFlag == TASK_FLAG_WSET )//电池低压预报标志
			{
				TaskSchedulerFlag.batLowPrepareFlag = TASK_FLAG_CLEAR;
			}
			TaskSchedulerTimer.batLowPrepareDispTimer = ONE_SECOND_TIMER;			
		}
	}
	
	/* 整个控制系统的定时都在此处处理 */
	CntlSystemWorkTimer();
	
		if( LimitBeginsTime == 1  )
		{
			if( TaskSchedulerTimer.totalDoseOneHourTimer != 0 )
			{
				TaskSchedulerTimer.totalDoseOneHourTimer--;
			}
			else
			{
				TaskSchedulerTimer.totalDoseOneHourTimer = TWENTY_MINUTE_TIMER;
				TaskSchedulerFlag.limitJudgeFlag = TASK_FLAG_CLEAR;
			}
		}	
	
	//为防止有同时按下的情况，所以没有将按键封装到一个程序里
	if( KeyScanStatus.keyEnter == emKeyActionWait )//enter键消抖和时长判断
	{
		if( GPIO_ReadInputDataBit( KEY_GPIO_PORT,ENTER_GPIO_PIN ) == 0)
		{
			if( TaskSchedulerTimer.enterKeyScanTimer != 0 ) 
			{
				TaskSchedulerTimer.enterKeyScanTimer--;
			}
			else
			{
				KeyScanStatus.keyEnter = emKyeActionScanSet;
				TaskSchedulerTimer.enterKeyScanTimer = KEY_TIMER_SCAN;
			}
		}	
		else//按键抬起，表示此次按键操作完成
		{
			KeyScanStatus.keyEnter = emKeyActionOn;			
			TaskSchedulerTimer.enterKeyScanTimer = KEY_TIMER_SCAN;
		}
	}
	if( KeyScanStatus.keyRunPause == emKeyActionWait )//运行/暂停键消抖和时长判断
	{
		if( GPIO_ReadInputDataBit( KEY_GPIO_PORT,RUNPAUSE_GPIO_PIN ) == 0)
		{
			if( TaskSchedulerTimer.runPauseKeyScanTimer != 0 ) 
			{
				TaskSchedulerTimer.runPauseKeyScanTimer--;
			}
			else
			{
				KeyScanStatus.keyRunPause = emKyeActionScanSet;
				TaskSchedulerTimer.runPauseKeyScanTimer = KEY_TIMER_SCAN;
			}
		}		
		else//按键抬起，表示此次按键操作完成
		{
			KeyScanStatus.keyRunPause = emKeyActionOn;
			TaskSchedulerTimer.runPauseKeyScanTimer = KEY_TIMER_SCAN;
		}
	}		
	if( KeyScanStatus.keyPS == emKeyActionWait )//密码/静音键消抖和时长判断
	{
		if( GPIO_ReadInputDataBit( KEY_GPIO_PORT,PS_GPIO_PIN ) == 0)//还没有定义，引脚需要修改
		{
			if( TaskSchedulerTimer.psKeyScanTimer != 0 ) 
			{
				TaskSchedulerTimer.psKeyScanTimer--;
			}
			else
			{
				KeyScanStatus.keyPS = emKyeActionScanSet;
				TaskSchedulerTimer.psKeyScanTimer = KEY_TIMER_SCAN;
			}
		}
		else//按键抬起，表示此次按键操作完成
		{
			KeyScanStatus.keyPS = emKeyActionOn;
			TaskSchedulerTimer.psKeyScanTimer = KEY_TIMER_SCAN;
		}
	}

	if( KeyScanStatus.keyPower == emKeyActionWait )//电源键消抖和时长判断
	{
		if( GPIO_ReadInputDataBit( KEY_GPIO_PORT,PWR_GPIO_PIN ) == 0)
		{
			if( TaskSchedulerTimer.powerKeyScanTimer != 0 ) 
			{
				TaskSchedulerTimer.powerKeyScanTimer--;
			}
			else
			{
				KeyScanStatus.keyPower = emKyeActionScanSet;
				TaskSchedulerTimer.powerKeyScanTimer = KEY_TIMER_SCAN;
			}
		}
		else//按键抬起，表示此次按键操作完成
		{
			KeyScanStatus.keyPower = emKeyActionOn;
			TaskSchedulerTimer.powerKeyScanTimer = KEY_TIMER_SCAN;
		}
	}
	if( KeyScanStatus.keyPCA == emKeyActionWait )//PCA键消抖和时长判断
	{
		if( GPIO_ReadInputDataBit( KEY_GPIO_PORT,PAC_GPIO_PIN ) == 0 )
		{
			if( TaskSchedulerTimer.pcaKeyScanTimer != 0 ) 
			{
				TaskSchedulerTimer.pcaKeyScanTimer--;
			}
			else
			{
				KeyScanStatus.keyPCA = emKyeActionScanSet;
				TaskSchedulerTimer.pcaKeyScanTimer = KEY_TIMER_SCAN;
			}
		}
		else//按键抬起，表示此次按键操作完成
		{
			KeyScanStatus.keyPCA = emKeyActionOn;
			TaskSchedulerTimer.pcaKeyScanTimer = KEY_TIMER_SCAN;
		}
	}
	/*else//PCA扩展键消抖和时长判断
	{
		if( GPIO_ReadInputDataBit( GPIOB,EXPAC_GPIO_PIN ) == 0 )
		{
			if( TaskSchedulerTimer.pcaKeyScanTimer != 0 ) 
			{
				TaskSchedulerTimer.pcaKeyScanTimer--;
				if( TaskSchedulerTimer.pcaKeyScanTimer < 10)//20170503修改
				{
				   if( GPIO_ReadInputDataBit( GPIOB,EXPAC_GPIO_PIN ) == 0 )	
					 {
						 EXPCA_flag=1;
					 }						 
				}
			}
		}
		else if(EXPCA_flag==1)				
			{
				EXPCA_flag=0;
				KeyScanStatus.keyPCA = emKyeActionScanSet;
				TaskSchedulerTimer.pcaKeyScanTimer = KEY_TIMER_SCAN;
			}
	}*/
	
	if( KeyScanStatus.keyPlus == emKeyActionWait )//Plus键消抖和时长判断
	{
		if( GPIO_ReadInputDataBit( KEY_GPIO_PORT,PLUS_GPIO_PIN ) == 0 )
		{
			if( TaskSchedulerTimer.plusKeyScanTimer != 0 ) 
			{
				TaskSchedulerTimer.plusKeyScanTimer--;
			}
			else
			{
				KeyScanQty.keyPlus++;	
				KeyScanStatus.keyPlus = emKyeActionScanSet;
				TaskSchedulerTimer.plusKeyScanTimer = KEY_TIMER_SCAN;
			}
		}
		else//按键抬起，表示此次按键操作完成
		{
			KeyScanStatus.keyPlus = emKeyActionOn;
			TaskSchedulerTimer.plusKeyScanTimer = KEY_TIMER_SCAN;
		}
	}
	else if( MoreKeyActionFlag.keyPlus == emKeyflagShortPress )
	{
		if( TaskSchedulerTimer.plusKeyScanTimer != 0 ) 
		{
			TaskSchedulerTimer.plusKeyScanTimer--;
		}
		else
		{
			MoreKeyActionFlag.keyPlus = emKeyflagNonePress;
			TaskSchedulerTimer.plusKeyScanTimer = KEY_TIMER_SCAN;
		}
	}
	
	if( KeyScanStatus.keyMinus == emKeyActionWait )//Minus键消抖和时长判断
	{
		if( GPIO_ReadInputDataBit( KEY_GPIO_PORT,MINUS_GPIO_PIN ) == 0 )
		{
			if( TaskSchedulerTimer.minusKeyScanTimer != 0 ) 
			{
				TaskSchedulerTimer.minusKeyScanTimer--;
			}
			else
			{
				KeyScanStatus.keyMinus = emKyeActionScanSet;
				TaskSchedulerTimer.minusKeyScanTimer = KEY_TIMER_SCAN;
			}
		}
		else//按键抬起，表示此次按键操作完成
		{
			KeyScanStatus.keyMinus = emKeyActionOn;
			TaskSchedulerTimer.minusKeyScanTimer = KEY_TIMER_SCAN;
		}
	}
	else if( MoreKeyActionFlag.keyMinus == emKeyflagShortPress )
	{
		if( TaskSchedulerTimer.minusKeyScanTimer != 0 ) 
		{
			TaskSchedulerTimer.minusKeyScanTimer--;
		}
		else
		{
			MoreKeyActionFlag.keyMinus = emKeyflagNonePress;
			TaskSchedulerTimer.minusKeyScanTimer = KEY_TIMER_SCAN;
		}
	}
	
	if( KeyScanStatus.keyAirout == emKeyActionWait )//Airout键消抖和时长判断
	{
		if( GPIO_ReadInputDataBit( KEY_GPIO_PORT,AIROUT_GPIO_PIN ) == 0 )
		{
			if( TaskSchedulerTimer.airoutKeyScanTimer != 0 ) 
			{
				TaskSchedulerTimer.airoutKeyScanTimer--;
			}
			else
			{
				KeyScanStatus.keyAirout = emKyeActionScanSet;
				TaskSchedulerTimer.airoutKeyScanTimer = KEY_TIMER_SCAN;
			}
		}
		else//按键抬起，表示此次按键操作完成
		{
			KeyScanStatus.keyAirout = emKeyActionOn;
			TaskSchedulerTimer.airoutKeyScanTimer = KEY_TIMER_SCAN;
		}
	}	

	if( TaskSchedulerFlag.speakerFlag == TASK_FLAG_WAIT )
	{
		if( TaskSchedulerTimer.speakerWaitTimer != 0 ) 
		{
			TaskSchedulerTimer.speakerWaitTimer--;
		}
		else
		{
			if((SysHornToneType == RemindeTone )||(SysHornToneType == LowPriWarning)||(SysHornToneType == Sound_prepore))
			{
				SysHornToneType = SoundOff;
			}
			TaskSchedulerFlag.speakerFlag = TASK_FLAG_SET;
			TaskSchedulerTimer.speakerWaitTimer = THIRTY_SECOND_TIMER;//为下次进入蜂鸣器计时做准备
		}
	}
	
/* 	if( TaskSchedulerFlag.sysStandbyFlag == TASK_FLAG_WAIT )
	{
		if( TaskSchedulerTimer.sysStandbyTimer != 0 ) 
		{
			TaskSchedulerTimer.sysStandbyTimer--;
		}
		else
		{
			TaskSchedulerFlag.sysStandbyFlag = TASK_FLAG_SET;//开启待机模式
			TaskSchedulerFlag.sensorPWHallFlag = TASK_FLAG_CLEAR;//传感器电源-霍尔关闭
		}
	}*/
}
void EXTI1_IRQHandler(void)
{
/*	if(EXTI_GetITStatus(HALL1_DETECT_EXTI) != RESET)//HALL1中断
  {
		if( ( TaskSchedulerFlag.sensorPWHallFlag == TASK_FLAG_SET ) && ( motorWorkState == MOTOR_WORK_ON ) )
		{
			if( TaskSchedulerFlag.motorSelfTestFlag == TASK_FLAG_WAIT )//上电电机自检
			{
				realTimeData.hall1Counter++;				
			}
			else if( TaskSchedulerFlag.motorSelfTestFlag == TASK_FLAG_SET )
			{
				realTimeData.hall1Counter++;
			}
		}
		//Clear EXTI Line Pending Bit 
		EXTI_ClearFlag(HALL1_DETECT_EXTI);  
    EXTI_ClearITPendingBit(HALL1_DETECT_EXTI);		
  }*/
}
/**
  * @brief  This function handles the PECAHead detection interrupt request.
  * @param  None
  * @retval None
  */
void EXTI9_5_IRQHandler(void)
{
	if(EXTI_GetITStatus(HALL2_DETECT_EXTI) != RESET)//HALL2中断
  {
		if( ( TaskSchedulerFlag.sensorPWHallFlag == TASK_FLAG_SET ) && ( motorWorkState == MOTOR_WORK_ON ) )
		{
			if( TaskSchedulerFlag.motorSelfTestFlag == TASK_FLAG_WAIT )//上电电机自检
			{
				realTimeData.hall2Counter++;
				//电机自检成功，只让电机转到一圈并且归零起点，自检OK
				if( realTimeData.hall2Counter >= 1 ) 
				{						
					realTimeData.hall2Counter = 0;
					GPIO_SetBits(GPIOB,GPIO_Pin_13);
					motorWorkState = MOTOR_WORK_OFF;
					TaskSchedulerFlag.motorSelfTestFlag = TASK_FLAG_SET;
					self_check_finish=1;
					ElectricityOffTime = ONE_SECOND_TIMER; //1s检测一下
					ElectricityOFFflag=0;//定时到，置位该标志
					TaskSchedulerTimer.dispAutoJumpTimer = TRI_SECOND_TIMER;
				}			
			}
			else if( TaskSchedulerFlag.motorSelfTestFlag == TASK_FLAG_SET )
			{
				realTimeData.hall2Counter++;
				if( realTimeData.hall2Counter >= 2 ) 
				{
					realTimeData.hall2Counter = 0;
					if((realTimeData.stateRun == Airout)&&(TaskSchedulerFlag.AirOutFlag == TASK_FLAG_SET))
					{
						motorWorkState = MOTOR_WORK_ON;
					}
					else 
//					if((realTimeData.stateRun == FirstRun)||(realTimeData.stateRun==MainRun)||(realTimeData.stateRun==PCARun))
					{
						motorWorkState = MOTOR_WORK_OFF;
						GPIO_SetBits(GPIOB,GPIO_Pin_13);
					}
					TaskSchedulerTimer.motorSignalCollectTimer = MOTOR_TESTSELF_TIMER;//机械故障时间赋值
					TaskSchedulerFlag.ipnutFlag = TASK_FLAG_SET;//这个标志位应该用在hall1采集到一次变化的时候
				}				
			}
		}
		/* Clear EXTI Line Pending Bit */
		EXTI_ClearFlag(HALL2_DETECT_EXTI);  
		EXTI_ClearITPendingBit(HALL2_DETECT_EXTI);		
  }
//	if(EXTI_GetITStatus(EXPAC_DETECT_EXTI) != RESET)//PCA扩展控制中断
//  {
//		if( KeyScanStatus.keyPCA == emKeyActionOff )
//		{
//			KeyScanStatus.keyPCA = emKeyActionWait;	
//		}
//		/* Clear EXTI Line Pending Bit */
//		EXTI_ClearFlag(EXPAC_DETECT_EXTI);  
//    EXTI_ClearITPendingBit(EXPAC_DETECT_EXTI);
//  }	
//	if(EXTI_GetITStatus(PAC_DETECT_EXTI) != RESET)//PCA控制中断
//	{
//		if( KeyScanStatus.keyPCA == emKeyActionOff )
//		{
//			KeyScanStatus.keyPCA = emKeyActionWait;	
//		}
//		/* Clear EXTI Line Pending Bit */
//		EXTI_ClearFlag(PAC_DETECT_EXTI);  
//		EXTI_ClearITPendingBit(PAC_DETECT_EXTI);
//	}
//	if(EXTI_GetITStatus(AIROUT_DETECT_EXTI) != RESET)//Airout控制中断
//	{
//		if( KeyScanStatus.keyAirout == emKeyActionOff )
//		{
//			KeyScanStatus.keyAirout = emKeyActionWait;					
//		}
//		/* Clear EXTI Line Pending Bit */
//		EXTI_ClearFlag(AIROUT_DETECT_EXTI);  
//		EXTI_ClearITPendingBit(AIROUT_DETECT_EXTI);		
//	}
	if(EXTI_GetITStatus(PAC_DETECT_EXTI) != RESET)//PCA控制中断
	{
		if( KeyScanStatus.keyPCA == emKeyActionOff )
		{
			KeyScanStatus.keyPCA = emKeyActionWait;	
		}
		/* Clear EXTI Line Pending Bit */
		EXTI_ClearFlag(PAC_DETECT_EXTI);  
		EXTI_ClearITPendingBit(PAC_DETECT_EXTI);
	}
	if(EXTI_GetITStatus(RUNPAUSE_DETECT_EXTI) != RESET)//运行/停止控制中断
	{
		if( KeyScanStatus.keyRunPause == emKeyActionOff )
		{
			KeyScanStatus.keyRunPause = emKeyActionWait;
		}
		/* Clear EXTI Line Pending Bit */
		EXTI_ClearFlag(RUNPAUSE_DETECT_EXTI);  
		EXTI_ClearITPendingBit(RUNPAUSE_DETECT_EXTI);
	}	
}

/**
  * @brief  This function handles the PECAHead detection interrupt request.
  * @param  None
  * @retval None
  */
void EXTI15_10_IRQHandler(void)
{		
	if(( motorWorkState == MOTOR_WORK_OFF ) && (GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_13) == 0))  //在所有中断里面处理电机停止，防止中断导致电机关闭延时
	{
			GPIO_SetBits(GPIOB,GPIO_Pin_13);
	}	
	
//	if(EXTI_GetITStatus(RUNPAUSE_DETECT_EXTI) != RESET)//运行/停止控制中断
//	{
//		if( KeyScanStatus.keyRunPause == emKeyActionOff )
//		{
//			KeyScanStatus.keyRunPause = emKeyActionWait;
//		}
//		/* Clear EXTI Line Pending Bit */
//		EXTI_ClearFlag(RUNPAUSE_DETECT_EXTI);  
//		EXTI_ClearITPendingBit(RUNPAUSE_DETECT_EXTI);
//	}	
//	if(EXTI_GetITStatus(MINUS_DETECT_EXTI) != RESET)//减控制中断
//	{
//		if( KeyScanStatus.keyMinus == emKeyActionOff )
//		{
//			KeyScanStatus.keyMinus = emKeyActionWait;	
//		}
//		/* Clear EXTI Line Pending Bit */
//		EXTI_ClearFlag(MINUS_DETECT_EXTI);  
//		EXTI_ClearITPendingBit(MINUS_DETECT_EXTI);
//	}
//	if(EXTI_GetITStatus(PLUS_DETECT_EXTI) != RESET)//加控制中断
//	{
//		if( KeyScanStatus.keyPlus == emKeyActionOff )
//		{
//			KeyScanStatus.keyPlus = emKeyActionWait;	
//		}
//		/* Clear EXTI Line Pending Bit */
//		EXTI_ClearFlag(PLUS_DETECT_EXTI);  
//		EXTI_ClearITPendingBit(PLUS_DETECT_EXTI);
//	}
//	if(EXTI_GetITStatus(PS_DETECT_EXTI) != RESET)//返回键控制中断
//	{
//		if( KeyScanStatus.keyPS == emKeyActionOff )
//		{
//			KeyScanStatus.keyPS = emKeyActionWait;	
//		}
//		/* Clear EXTI Line Pending Bit */
//		EXTI_ClearFlag(PS_DETECT_EXTI);  
//		EXTI_ClearITPendingBit(PS_DETECT_EXTI);
//	}
//	if(EXTI_GetITStatus(ENTER_DETECT_EXTI) != RESET)//ENTER控制中断 
//	{		
//		if( KeyScanStatus.keyEnter == emKeyActionOff )
//		{
//			KeyScanStatus.keyEnter = emKeyActionWait;	
//		}	
//		/* Clear EXTI Line Pending Bit */
//		EXTI_ClearFlag(ENTER_DETECT_EXTI);  
//		EXTI_ClearITPendingBit(ENTER_DETECT_EXTI);
//	}
//	if(EXTI_GetITStatus(PWR_DETECT_EXTI) != RESET)//电源控制中断
//	{
//		if( KeyScanStatus.keyPower == emKeyActionOff )
//		{
//			KeyScanStatus.keyPower = emKeyActionWait;	
//		}
//		/* Clear EXTI Line Pending Bit */
//		EXTI_ClearFlag(PWR_DETECT_EXTI);  
//		EXTI_ClearITPendingBit(PWR_DETECT_EXTI);		
//	}
	if(EXTI_GetITStatus(AIROUT_DETECT_EXTI) != RESET)//Airout控制中断
	{
		if( KeyScanStatus.keyAirout == emKeyActionOff )
		{
			KeyScanStatus.keyAirout = emKeyActionWait;					
		}
		/* Clear EXTI Line Pending Bit */
		EXTI_ClearFlag(AIROUT_DETECT_EXTI);  
		EXTI_ClearITPendingBit(AIROUT_DETECT_EXTI);		
	}
	if(EXTI_GetITStatus(MINUS_DETECT_EXTI) != RESET)//减控制中断
	{
		if( KeyScanStatus.keyMinus == emKeyActionOff )
		{
			KeyScanStatus.keyMinus = emKeyActionWait;	
		}
		/* Clear EXTI Line Pending Bit */
		EXTI_ClearFlag(MINUS_DETECT_EXTI);  
		EXTI_ClearITPendingBit(MINUS_DETECT_EXTI);
	}
	if(EXTI_GetITStatus(PLUS_DETECT_EXTI) != RESET)//加控制中断
	{
		if( KeyScanStatus.keyPlus == emKeyActionOff )
		{
			KeyScanStatus.keyPlus = emKeyActionWait;	
		}
		/* Clear EXTI Line Pending Bit */
		EXTI_ClearFlag(PLUS_DETECT_EXTI);  
		EXTI_ClearITPendingBit(PLUS_DETECT_EXTI);
	}
	if(EXTI_GetITStatus(PS_DETECT_EXTI) != RESET)//密码/静音控制中断
	{
		if( KeyScanStatus.keyPS == emKeyActionOff )
		{
			KeyScanStatus.keyPS = emKeyActionWait;	
		}
		/* Clear EXTI Line Pending Bit */
		EXTI_ClearFlag(PS_DETECT_EXTI);  
		EXTI_ClearITPendingBit(PS_DETECT_EXTI);
	}
	if(EXTI_GetITStatus(ENTER_DETECT_EXTI) != RESET)//ENTER控制中断 
	{		
		if( KeyScanStatus.keyEnter == emKeyActionOff )
		{
			KeyScanStatus.keyEnter = emKeyActionWait;	
		}	
		/* Clear EXTI Line Pending Bit */
		EXTI_ClearFlag(ENTER_DETECT_EXTI);  
		EXTI_ClearITPendingBit(ENTER_DETECT_EXTI);
	}
	if(EXTI_GetITStatus(PWR_DETECT_EXTI) != RESET)//电源控制中断
	{
		if( KeyScanStatus.keyPower == emKeyActionOff )
		{
			KeyScanStatus.keyPower = emKeyActionWait;	
		}
		/* Clear EXTI Line Pending Bit */
		EXTI_ClearFlag(PWR_DETECT_EXTI);  
		EXTI_ClearITPendingBit(PWR_DETECT_EXTI);		
	}
}

/**
  * @brief  This function handles RTC_IRQHandler.
  * @param  None
  * @retval None
  */
// void RTC_IRQHandler(void)
// {
//  if(RTC_GetITStatus(RTC_IT_ALR)!= RESET)//闹钟中断
//  {
//   	RTC_ClearITPendingBit(RTC_IT_ALR);  //清闹钟中断
//  }                 
// }

/**
  * @brief  This function handles USART1_IRQHandler.
  * @param  None
  * @retval None
  */
void USART1_IRQHandler(void)
{
	if(USART_GetITStatus(USART1, USART_IT_RXNE) == SET)
	{	
		//接收中断
		usart1RecvBuffer[usart1RecvCnt] = (uint8_t)USART_ReceiveData(USART1); 	//保存一个字节
		
		if( usart1RecvBuffer[0] == USART_PDU_START )
		{
			if( usart1RecvCnt < ( usart1RecvBuffer[1] + 2 ) )
			{
				usart1RecvCnt++;
			}	
			else if( usart1RecvBuffer[usart1RecvCnt] == USART_PDU_END )
			{
				usart1RecvCnt = 0;
				TaskSchedulerFlag.historyRequestFlag = TASK_FLAG_SET;						
			}
			else
			{
				usart1RecvCnt = 0;
			}
		}	
	}
}

//==========================================================
//	函数名称：	USART2_IRQHandler replace USART2_IRQHandler by wulianwei
//
//	函数功能：	串口2收发中断
//
//	入口参数：	无
//
//	返回参数：	无
//
//	说明：		
void USART2_IRQHandler(void)
{

	if(USART_GetITStatus(USART2, USART_IT_RXNE) != RESET) //接收中断
	{
		if(uart_recv.len >= sizeof(uart_recv.buf)-1)	uart_recv.len = 0; //防止串口被刷爆
		
		uart_recv.buf[uart_recv.len++] = USART2->DR;
		
		USART_ClearFlag(USART2, USART_FLAG_RXNE);
	}

}

/**
  * @brief  This function handles USART2_IRQHandler.
  * @param  None
  * @retval None
  */
void USART2_IRQHandler1(void) // rename USART2_IRQHandler by wulianwei
{
	/*
	if(USART_GetITStatus(USART2, USART_IT_IDLE) != RESET)
	{
//     uint8_t Dmabuff_lenght=0;
		 uint8_t i=0,data=0;
		
		USART_ClearITPendingBit(USART2,USART_IT_IDLE );  //清除空闲中断
		data = USART1->SR;
		USART_ReceiveData(USART2);                       //读DR,只有读过一次，才能真正清除标志
		USART_ClearFlag(USART2,USART_IT_IDLE);           //清除空闲标志
		//DMA_Cmd(DMA1_Channel6,DISABLE);
		//USART1Send(usart2RecvBuffer,usart2RecvCnt);
		//DMA_Cmd(DMA1_Channel6,ENABLE); 
		//usart2RecvCnt=0;
	}
*/		
	if(( motorWorkState == MOTOR_WORK_OFF ) && (GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_13) == 0))  //在所有中断里面处理电机停止，防止中断导致电机关闭延时
	{
			GPIO_SetBits(GPIOB,GPIO_Pin_13);
	}	
	if(USART_GetITStatus(USART2, USART_IT_RXNE) == SET)
	{	
		//接收中断
		
		USART_ClearITPendingBit(USART1, USART_IT_RXNE);
		usart2RecvBuffer[usart2RecvCnt++] = (uint8_t)USART_ReceiveData(USART2); 	//保存一个字节
		
		if(usart2RecvBuffer[0] == 0xad)
		{
			if(usart2RecvCnt<2)
				return;
			if(usart2RecvBuffer[1] == 0xfc)
			{ 
			
				if( usart2RecvCnt >5)
				{
					USART1Send(usart2RecvBuffer,usart2RecvCnt);
					usart2RecvCnt=0;	
					TaskSchedulerFlag.zigbeeRecvFlag = TASK_FLAG_SET;
					//printf("OK \r\n");
				} 
			}
			else
			{
				usart2RecvCnt = 0;
				printf("%x\r\n",usart2RecvBuffer[1]);
			}
		}
		else
		{
			
			usart2RecvCnt = 0;
		}
		
		/*if( usart2RecvBuffer[1] == 0x7c )
			{
				
				//printf("a=%x\r\n",usart2RecvBuffer[1]);
				if( usart2RecvCnt >= 3 )
				{
					usart2RecvCnt = 0;
					//TaskSchedulerFlag.zigbeeRecvFlag = TASK_FLAG_SET;	
					//printf("%x\r\n",usart2RecvBuffer[0]);
					//printf("%x\r\n",usart2RecvBuffer[1]);
					//USART1Send(usart2RecvBuffer,3);
				}	*/
		
	}
}

/**
  * @brief  This function handles USART2_IRQHandler.
  * @param  None
  * @retval None
  */
void USART3_IRQHandler(void)
{
	if(USART_GetITStatus(USART3, USART_IT_RXNE) == SET)
	{	
		//接收中断
		usart3RecvBuffer[usart3RecvCnt++] = (uint8_t)USART_ReceiveData(USART3); 	//保存一个字节
		if( ( usart3RecvBuffer[0] == USART_PDU_START ) && ( usart3RecvBuffer[1] == USART_PDU_END ) )
		{
			if( usart3RecvCnt >= ( usart3RecvBuffer[2] + 3 ) )
			{
				usart3RecvCnt = 0;
			}	
		}
		else
		{
			if( usart3RecvCnt >= 2)
			{
				usart3RecvCnt = 0;
			}
		}
	}
}

/******************************************************************************/
/*                 STM32F10x Peripherals Interrupt Handlers                   */
/*  Add here the Interrupt Handler for the used peripheral(s) (PPP), for the  */
/*  available peripheral interrupt handler's name please refer to the startup */
/*  file (startup_stm32f10x_xx.s).                                            */
/******************************************************************************/




		   

/******************* (C) COPYRIGHT 2011 STMicroelectronics *****END OF FILE****/
