#include "main.h"
#include "timer.h"
#include "led.h"
#include "usart.h"
#include "delay.h"
#include "stdlib.h"
#include "stdio.h"
#include "string.h"
#include "dac.h"
#include "motorstat.h"

extern char USART_RX_BUF[];
extern struct StructMotorStat MotorStat;

void TIM1_Int_Init(u16 arr,u16 psc)//TIM1为主定时器
{
  	TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;
	NVIC_InitTypeDef NVIC_InitStructure;

	RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1, ENABLE); //时钟使能

	TIM_TimeBaseStructure.TIM_Period = arr; //设置在下一个更新事件装入活动的自动重装载寄存器周期的值	 计数到5000为500ms
	TIM_TimeBaseStructure.TIM_Prescaler =psc; //设置用来作为TIMx时钟频率除数的预分频值  10Khz的计数频率  
	TIM_TimeBaseStructure.TIM_ClockDivision = 0; //设置时钟分割:TDTS = Tck_tim
	TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;  //TIM向上计数模式
	TIM_TimeBaseInit(TIM1, &TIM_TimeBaseStructure); //根据TIM_TimeBaseInitStruct中指定的参数初始化TIMx的时间基数单位
 
	TIM_ITConfig(TIM1,TIM_IT_Update,ENABLE ); //使能指定的TIM1中断,允许更新中断

	NVIC_InitStructure.NVIC_IRQChannel = TIM1_CC_IRQn;  //TIM1中断
	NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;  //先占优先级0级
	NVIC_InitStructure.NVIC_IRQChannelSubPriority = 2;  //从优先级3级
	NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQ通道被使能
	NVIC_Init(&NVIC_InitStructure);  //根据NVIC_InitStruct中指定的参数初始化外设NVIC寄存器
	
	TIM_Cmd(TIM1, ENABLE);  //使能TIMx外设
							 
}
//定时器1中断服务程序
void TIM1_IRQHandler(void)   //TIM1中断
{
	if (TIM_GetITStatus(TIM1, TIM_IT_Update) != RESET) //检查指定的TIM中断发生与否:TIM 中断源 
		{
			TIM_ClearITPendingBit(TIM1, TIM_IT_Update  );  //清除TIMx的中断待处理位:TIM 中断源 
		}
}



void TIM1_PWM_CH1_Init(void)
{
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1, ENABLE);   //使能TIM1时钟
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);  //使能GPIOA时钟
	
	//配置PA为复用输出模式，输出速度为50MHZ
	GPIO_InitTypeDef GPIO_InitStructure;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8;
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_Init(GPIOA, &GPIO_InitStructure);
	
	//开启TIM内部时钟
	TIM_InternalClockConfig(TIM1);
	//配置TIM2
	TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStructure;
	TIM_TimeBaseInitStructure.TIM_ClockDivision = TIM_CKD_DIV1;  //时钟分频
	TIM_TimeBaseInitStructure.TIM_CounterMode = TIM_CounterMode_Up; //计数方式向上计数
	TIM_TimeBaseInitStructure.TIM_Period = 999;		//ARR 自动重装载值
	TIM_TimeBaseInitStructure.TIM_Prescaler = 71;		//PSC 预分频器 
	TIM_TimeBaseInitStructure.TIM_RepetitionCounter = 0;    //
	TIM_TimeBaseInit(TIM1,&TIM_TimeBaseInitStructure);
	//输出通道配置
	TIM_OCInitTypeDef TIM_OCInitStructure;
	TIM_OCStructInit(&TIM_OCInitStructure);
	TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM2;//配置PWM模式1
	TIM_OCInitStructure.TIM_OCPolarity =  TIM_OCPolarity_High;//有效极性为高电平
	TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
	TIM_OCInitStructure.TIM_Pulse = 10000;		//CCR
	TIM_OC1Init(TIM1, &TIM_OCInitStructure);  //初始化通道1  PA8

	
	TIM_CtrlPWMOutputs(TIM1,ENABLE);
	
		TIM_ARRPreloadConfig(TIM1, ENABLE); //使能TIMx在ARR上的预装载寄存器
	// 输出触发配置（使得TIM1的更新事件触发TIM3）
    TIM_SelectOutputTrigger(TIM1, TIM_TRGOSource_Update);
    // 使能定时器1的更新事件触发输出
    TIM_ClearFlag(TIM1, TIM_FLAG_Update);
    TIM_ITConfig(TIM1, TIM_IT_Update, ENABLE);
	
	TIM_Cmd(TIM1, ENABLE);  //使能定时器2
}

 
//TIM1 PWM部分初始化 
//PWM输出初始化
//arr：自动重装值
//psc：时钟预分频数
void TIM1_PWM_Init(u16 arr,u16 psc)
{  
	//TIM1_PWM_Init1();
	//return;
	
	
	GPIO_InitTypeDef GPIO_InitStructure;
	TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;
	TIM_OCInitTypeDef  TIM_OCInitStructure;
	

	RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1, ENABLE);	//使能定时器1时钟
 	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA  | RCC_APB2Periph_AFIO, ENABLE);  //使能GPIO外设和AFIO复用功能模块时钟
	
	GPIO_PinRemapConfig(GPIO_PartialRemap_TIM1, ENABLE); //Timer3部分重映射  TIM1_CH2   
 
  	//设置该引脚为复用输出功能,输出TIM1 CH2的PWM脉冲波形	
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8; //TIM_CH2
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;  //复用推挽输出
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_Init(GPIOA, &GPIO_InitStructure);//初始化GPIO
 
    //初始化TIM1
	TIM_TimeBaseStructure.TIM_Period = arr; //设置在下一个更新事件装入活动的自动重装载寄存器周期的值
	TIM_TimeBaseStructure.TIM_Prescaler =psc; //设置用来作为TIMx时钟频率除数的预分频值 
	TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1; //设置时钟分割:TDTS = Tck_tim
	TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;  //TIM向上计数模式
	TIM_TimeBaseInit(TIM1, &TIM_TimeBaseStructure); //根据TIM_TimeBaseInitStruct中指定的参数初始化TIMx的时间基数单位
	
	//初始化TIM1 Channel2 PWM模式	 
	TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM2; //选择定时器模式:TIM脉冲宽度调制模式2
 	TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; //比较输出使能
	//TIM_OCInitStructure.TIM_Pulse = 1800;  //CCR
	//TIM_OCInitStructure.TIM_Pulse = 0; 			//设置待装入捕获比较寄存器的脉冲值
	TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; //输出极性:TIM输出比较极性高
	TIM_OC1Init(TIM1, &TIM_OCInitStructure);  //根据T指定的参数初始化外设TIM1 OC2

	TIM_OC1PreloadConfig(TIM1, TIM_OCPreload_Enable);  //使能TIM1在CCR2上的预装载寄存器,即channel-2
 
    TIM_CtrlPWMOutputs(TIM1,ENABLE);	//MOE 主输出使能	

	TIM_ARRPreloadConfig(TIM1, ENABLE); //使能TIMx在ARR上的预装载寄存器
	// 输出触发配置（使得TIM1的更新事件触发TIM3）
    TIM_SelectOutputTrigger(TIM1, TIM_TRGOSource_Update);
    // 使能定时器1的更新事件触发输出
    TIM_ClearFlag(TIM1, TIM_FLAG_Update);
    TIM_ITConfig(TIM1, TIM_IT_Update, ENABLE);
		
		
	TIM_Cmd(TIM1, ENABLE);  //使能TIM1
	

}




//TIM3 PWM部分初始化 
//PWM输出初始化
//arr：自动重装值
//psc：时钟预分频数
void TIM3_Int_Init(u16 arr,u16 psc)
{  
	GPIO_InitTypeDef GPIO_InitStructure;
	TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;
	NVIC_InitTypeDef NVIC_InitStructure;
	TIM_ICInitTypeDef TIM_ICInitStructure;
	RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE); //时钟使能

	// 定时器基本配置  
    TIM_TimeBaseStructure.TIM_Prescaler = 72 - 1; // 设置预分频器，得到1MHz的计数时钟
    TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
    TIM_TimeBaseStructure.TIM_Period = 0xFFFF; // 设置定时器最大计数值，用于防止溢出
    TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;
    TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure);

    // 捕获模式配置（用于捕获TIM1的输出触发）
    TIM_ICInitStructure.TIM_Channel = TIM_Channel_1;
    TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_BothEdge; // 仅捕获上升沿
    TIM_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI;
    TIM_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1;
    TIM_ICInitStructure.TIM_ICFilter = 0;
    TIM_ICInit(TIM3, &TIM_ICInitStructure);

	//初始化GPIO6
	//GPIO_PinRemapConfig(GPIO_PartialRemap_TIM3, ENABLE); //Timer3部分重映射  TIM1_CH2   
    //设置该引脚为复用输出功能,输出TIM1 CH2的PWM脉冲波形	
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6; //TIM_CH2
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPD;  
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_Init(GPIOA, &GPIO_InitStructure);//初始化GPIO

    // 配置定时器3的中断
    NVIC_InitStructure.NVIC_IRQChannel = TIM3_IRQn;
    NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
    NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3;
    NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
    NVIC_Init(&NVIC_InitStructure);

    // 开始定时器3计数
    TIM_Cmd(TIM3, ENABLE);

    // 使能捕获中断
    TIM_ITConfig(TIM3, TIM_IT_CC1, ENABLE);

}
//定时器3中断服务程序
void TIM3_IRQHandler(void)   //TIM3中断
{
	if (TIM_GetITStatus(TIM3, TIM_IT_CC1) != RESET) {

//		if (MotorStat.moveflag==1)//如果正在转动
//		{
//			
//		MotorStat.pulse_count++; // 更新脉冲计数器
//														
//    int target_pulse_count = abs(MotorStat.pulse_count) / 0.0046;	// 计算目标脉冲数
//		printf("mm:%d \r\n", target_pulse_count);
			
			
//			if (MotorStat.direction==0)//正转
//			{
//				MotorStat.NmberoNow++;
//			}else if (MotorStat.direction==1)//反转
//				{	
//					MotorStat.NmberoNow--;
//				}								
			//printf("NmberoNow = %lld\r\n",MotorStat.NmberoNow);	

				
//			if (MotorStat.MoveMode==1)		//by
//			{
//				if (MotorStat.NmberoTarget>0)
//				{
//					MotorStat.NmberoTarget--;
//					
//				}
//				if (MotorStat.NmberoTarget<0)
//				{
//					MotorStat.NmberoTarget++;
//					
//				}
//			 	if (MotorStat.NmberoTarget==0)
//				{
//					//停止转动
//					stopmotormove();
//				}      
				//printf("NmberoTarget = %lld\r\n",MotorStat.NmberoTarget);	
//			}
		
//			if(MotorStat.backzero ==1&&MotorStat.NmberoNow==0)
//			{
//				stopmotormove();
//				printf("already at zero\r\n");
//				MotorStat.backzero = 0;
//			}	
			
//		}
		
		
		TIM_ClearITPendingBit(TIM3, TIM_IT_CC1);
			 
    }
	
}


void TIM5_Int_Init(u16 arr,u16 psc)
{
  TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;
	NVIC_InitTypeDef NVIC_InitStructure;

	RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM5, ENABLE); //时钟使能
	
	//定时器TIM5初始化
	TIM_TimeBaseStructure.TIM_Period = arr; //设置在下一个更新事件装入活动的自动重装载寄存器周期的值	
	TIM_TimeBaseStructure.TIM_Prescaler =psc; //设置用来作为TIMx时钟频率除数的预分频值
	TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1; //设置时钟分割:TDTS = Tck_tim
	TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;  //TIM向上计数模式
	TIM_TimeBaseInit(TIM5, &TIM_TimeBaseStructure); //根据指定的参数初始化TIMx的时间基数单位
 
	TIM_ITConfig(TIM5,TIM_IT_Update,ENABLE ); //使能指定的TIM4中断,允许更新中断

	//中断优先级NVIC设置
	NVIC_InitStructure.NVIC_IRQChannel = TIM5_IRQn;  //TIM4中断
	NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;  //先占优先级0级
	NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;  //从优先级3级
	NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQ通道被使能
	NVIC_Init(&NVIC_InitStructure);  //初始化NVIC寄存器


	TIM_Cmd(TIM5, ENABLE);  //使能TIMx					 
}
//定时器4中断服务程序
void TIM5_IRQHandler(void)   //TIM4中断
{
	if (TIM_GetITStatus(TIM5, TIM_IT_Update) != RESET)  //检查TIM4更新中断发生与否
		{
			//printf("TIM4 IRQ triggered!\r\n"); // 检测中断是否被触发
			ledloop();
			motorloop();
			TIM_ClearITPendingBit(TIM5, TIM_IT_Update  );  //清除TIMx更新中断标志 
			
		}
}


//定时器2初始化

//void TIM2_Int_Init(u16 arr,u16 psc)
//{  
//	GPIO_InitTypeDef GPIO_InitStructure;
//	TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;
//	NVIC_InitTypeDef NVIC_InitStructure;
//	TIM_ICInitTypeDef TIM_ICInitStructure;
//	RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE); //时钟使能

//	// 定时器基本配置
//    
//    TIM_TimeBaseStructure.TIM_Prescaler = 72 - 1; // 设置预分频器，得到1MHz的计数时钟
//    TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
//    TIM_TimeBaseStructure.TIM_Period = 0xFFFF; // 设置定时器最大计数值，用于防止溢出
//    TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;
//    TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);

//    // 捕获模式配置（用于捕获TIM1的输出触发）
//    
//    TIM_ICInitStructure.TIM_Channel = TIM_Channel_1;
//    TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Rising; // 仅捕获上升沿
//    TIM_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI;
//    TIM_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1;
//    TIM_ICInitStructure.TIM_ICFilter = 0;
//    TIM_ICInit(TIM2, &TIM_ICInitStructure);


////		//初始化GPIO6
////	  GPIO_PinRemapConfig(GPIO_Remap_TIM4 , ENABLE);  	
//		GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0; //TIM_CH2
//		GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;  
//		GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
//		GPIO_Init(GPIOA, &GPIO_InitStructure);//初始化GPIO
//    // 配置定时器2的中断
//    
//    NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQn;
//    NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
//    NVIC_InitStructure.NVIC_IRQChannelSubPriority = 4;
//    NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
//    NVIC_Init(&NVIC_InitStructure);

//    // 开始定时器2计数
//    TIM_Cmd(TIM2, ENABLE);

//    // 使能捕获中断
//    TIM_ITConfig(TIM2, TIM_IT_CC1, ENABLE);

//}

//定时器2初始化  正限位PA0

void TIM2_Capture_Init(void) {
    GPIO_InitTypeDef GPIO_InitStructure;
    TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
    TIM_ICInitTypeDef TIM_ICInitStructure;
    NVIC_InitTypeDef NVIC_InitStructure;

    //  使能时钟
    RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);
//    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);

    //  配置PA0为TIM2输入捕获引脚
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;       // 模式
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_Init(GPIOA, &GPIO_InitStructure);

    //  配置TIM2时基
    TIM_TimeBaseStructure.TIM_Period = 0xFFFF;          // 自动重装载值
    TIM_TimeBaseStructure.TIM_Prescaler = 71;           // 预分频72分频（72MHz/72=1MHz计数频率）
    TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;
    TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
    TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);

    //  配置输入捕获通道（TIM2_CH1）
    TIM_ICInitStructure.TIM_Channel = TIM_Channel_1;
    TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Rising; // 上升沿触发
    TIM_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI; // 直接映射到TI1
    TIM_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1;      // 无分频
    TIM_ICInitStructure.TIM_ICFilter = 0x0;                     // 无滤波
    TIM_ICInit(TIM2, &TIM_ICInitStructure);

    //  配置TIM2中断
    NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQn;
    NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
    NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
    NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
    NVIC_Init(&NVIC_InitStructure);

    //  使能捕获中断和定时器
    TIM_ITConfig(TIM2, TIM_IT_CC1, ENABLE);
    TIM_Cmd(TIM2, ENABLE);
}

//定时器2中断服务程序
void TIM2_IRQHandler(void) {
    if (TIM_GetITStatus(TIM2, TIM_IT_CC1) != RESET) {
			
			//正极限，上升沿触发事件
			
			stopmotormove();
		//	MotorStat.NmberoNow = 0;			
			printf("already close & zheng\r\n");
			//moveto(-100);
        TIM_ClearITPendingBit(TIM2, TIM_IT_CC1); // 清除中断标志      
    }
}

//定时器4初始化  负限位PB6

void TIM4_Capture_Init(void) {
    GPIO_InitTypeDef GPIO_InitStructure;
    TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
    TIM_ICInitTypeDef TIM_ICInitStructure;
    NVIC_InitTypeDef NVIC_InitStructure;

    //  使能时钟
    RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4, ENABLE);
//    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);

    //  配置PB6为TIM2输入捕获引脚
		    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;  // 上拉输入
    GPIO_Init(GPIOB, &GPIO_InitStructure);


    //  配置TIM2时基
    TIM_TimeBaseStructure.TIM_Period = 0xFFFF;          // 自动重装载值
    TIM_TimeBaseStructure.TIM_Prescaler = 71;           // 预分频72分频（72MHz/72=1MHz计数频率）
    TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;
    TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
    TIM_TimeBaseInit(TIM4, &TIM_TimeBaseStructure);

    //  配置输入捕获通道（TIM4_CH1）
    TIM_ICInitStructure.TIM_Channel = TIM_Channel_1;
    TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Rising; // 上升沿触发
    TIM_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI; // 直接映射到TI1
    TIM_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1;      // 无分频
    TIM_ICInitStructure.TIM_ICFilter = 0x0;                     // 无滤波
    TIM_ICInit(TIM2, &TIM_ICInitStructure);

    //  配置TIM2中断
    NVIC_InitStructure.NVIC_IRQChannel = TIM4_IRQn;
    NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
    NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
    NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
    NVIC_Init(&NVIC_InitStructure);

    //  使能捕获中断和定时器
    TIM_ITConfig(TIM4, TIM_IT_CC1, ENABLE);
    TIM_Cmd(TIM4, ENABLE);
}

//定时器4中断服务程序
// timer.c
void TIM4_IRQHandler(void) {
    if (TIM_GetITStatus(TIM4, TIM_IT_CC1) != RESET) {
		
			//负极限，上升沿触发事件
			//stopmotormove();	 
			printf("already close & fu\r\n");
        TIM_ClearITPendingBit(TIM4, TIM_IT_CC1);
    }
}




//TIM4有误，改用EXTI
void EXTI_Config(void)
{
		    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
    GPIO_InitTypeDef GPIO_InitStructure;
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
    GPIO_Init(GPIOB, &GPIO_InitStructure);
	
	    RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE);
    GPIO_EXTILineConfig(GPIO_PortSourceGPIOB, GPIO_PinSource6);
 
    EXTI_InitTypeDef EXTI_InitStructure;
    EXTI_InitStructure.EXTI_Line = EXTI_Line6;
    EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
    EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising;  // 上升沿触发
    EXTI_InitStructure.EXTI_LineCmd = ENABLE;
    EXTI_Init(&EXTI_InitStructure);
 
    NVIC_InitTypeDef NVIC_InitStructure;
    NVIC_InitStructure.NVIC_IRQChannel = EXTI9_5_IRQn;
    NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x00;
    NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x00;
    NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
    NVIC_Init(&NVIC_InitStructure);
}

void EXTI9_5_IRQHandler(void) {
    if (EXTI_GetITStatus(EXTI_Line6) != RESET) {

			MotorStat.fu_LimitTrigger = 1;	
			
			stopmotormove();
			
			if(MotorStat.zero_flag == 1) {
							
				printf("Already at ZERO\r\n");
				
				MotorStat.move_phase = -1;
				MotorStat.NmberoNow = 0;
				//moveto(100);

			}else{
				printf("already close & fu\r\n");
			}
        // 清除中断标志
        EXTI_ClearITPendingBit(EXTI_Line6);
   
		} 
}

