Esempio n. 1
0
/**********************************************************************
*	函 数 名: timer_init
*	功能说明: 定时器初始化,使用定时器3
*	形    参:  Period自动重装载值,Prescale预分频值
*	返 回 值:  无
**********************************************************************/
void timer_init(uint16_t Period,uint16_t Prescale)
{
	TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStructure;
	NVIC_InitTypeDef NVIC_InitStructure;
	
	RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3,ENABLE);  			//使能TIM3时钟
	
	TIM_TimeBaseInitStructure.TIM_Period = Period; 					//自动重装载值
	TIM_TimeBaseInitStructure.TIM_Prescaler=Prescale;  				//定时器分频
	TIM_TimeBaseInitStructure.TIM_CounterMode=TIM_CounterMode_Up; 	//向上计数模式
	TIM_TimeBaseInitStructure.TIM_ClockDivision=TIM_CKD_DIV1; 
	
	TIM_TimeBaseInit(TIM3,&TIM_TimeBaseInitStructure);				//初始化TIM3
	
	
	TIM_ClearFlag(TIM3,TIM_IT_Update);								//清除更新标志
	TIM_SetCounter(TIM3,0);																					//设置定时器值
	 
	TIM_ITConfig(TIM3,TIM_IT_Update,ENABLE); 						//允许定时器3更新中断
	TIM_Cmd(TIM3,ENABLE); 																					//使能定时器3
	
	NVIC_InitStructure.NVIC_IRQChannel=TIM3_IRQn; //定时器3中断
	NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=0x01; 		//抢占优先级1
	NVIC_InitStructure.NVIC_IRQChannelSubPriority=0x00; 			//子优先级3
	NVIC_InitStructure.NVIC_IRQChannelCmd=ENABLE;
	NVIC_Init(&NVIC_InitStructure); 
	
	beep_init();
} 
Esempio n. 2
0
/*******************************************************************************
* Function Name  : System_TimerConfiguration
* Description    :  
* Input          : None
* Output         : None
* Return         : None
*******************************************************************************/
static void System_TimerConfiguration(void)
{
	TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure = {0};
	//---------------------------------------------------------------------------
	// 初始化定时器TIM
	//---------------------------------------------------------------------------
	TIM_DeInit( BSP_TIM );//复位TIM定时器
	/* ---------------------------------------------------------------
	TIM Configuration: Output Compare Timing Mode:
	TIMCLK = 84 MHz, Prescaler = 8400, TIM counter clock = 10 KHz
	--------------------------------------------------------------- */
	/* TIM configuration */
	TIM_TimeBaseStructure.TIM_Period        = 9;
	TIM_TimeBaseStructure.TIM_Prescaler     = 0;      
	TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;    
	TIM_TimeBaseStructure.TIM_CounterMode   = TIM_CounterMode_Up;   
	TIM_TimeBaseInit(BSP_TIM, &TIM_TimeBaseStructure);

	/*--------------------------------------------------------------------- */
	/* Prescaler configuration TIM 的时钟设定为10KHz,计数器的计量单位:1ms */
	/*   注意:分频数不能超过65535 (0xFFFF)                               */
	/*--------------------------------------------------------------------- */
	TIM_PrescalerConfig(BSP_TIM, (8400 - 1), TIM_PSCReloadMode_Immediate);

	/*  */
	TIM_ARRPreloadConfig(BSP_TIM, ENABLE);
	/* Clear TIM update pending flag[清除TIM 溢出中断标志] */
	TIM_ClearFlag(BSP_TIM, TIM_FLAG_Update);

	/* Enable TIM Update interrupt [TIM 溢出中断允许]*/
	TIM_ITConfig(BSP_TIM, TIM_IT_Update, ENABLE);  

	/* TIM enable counter [允许tim计数]*/
	TIM_Cmd(BSP_TIM, ENABLE);
}
/*=====================================================================================================*/
void TIM_Config( void )
{
  TIM_TimeBaseInitTypeDef TIM_TimeBaseStruct;
  NVIC_InitTypeDef NVIC_InitStruct;
	
  RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);

  TIM_DeInit(TIM3);

  /************************** PWM Output **************************************/
  /* 設定 TIM3 Time Base */
  TIM_TimeBaseStruct.TIM_Period = (u16)(1000-1);              // 週期 = 10ms, 100Hz
  TIM_TimeBaseStruct.TIM_Prescaler = (u16)(720-1);             // 除頻720 = 100kHz
  TIM_TimeBaseStruct.TIM_ClockDivision = TIM_CKD_DIV1;
  TIM_TimeBaseStruct.TIM_CounterMode = TIM_CounterMode_Up;    // 上數
  TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStruct);

	NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);
	NVIC_InitStruct.NVIC_IRQChannel = TIM3_IRQn;
	NVIC_InitStruct.NVIC_IRQChannelPreemptionPriority = 0;
	NVIC_InitStruct.NVIC_IRQChannelSubPriority = 0;
	NVIC_InitStruct.NVIC_IRQChannelCmd = ENABLE;
	NVIC_Init(&NVIC_InitStruct);
	
  /* 啟動 */
  TIM_ARRPreloadConfig(TIM3, ENABLE);                         // 致能 TIM3 重載寄存器ARR
	TIM_ClearFlag(TIM3, TIM_FLAG_Update);
	TIM_ITConfig(TIM3, TIM_IT_Update, ENABLE);
  TIM_Cmd(TIM3, ENABLE);                                      // 致能 TIM3

}
Esempio n. 4
0
void InitTIM2(void)
{
    TIM_TimeBaseInitTypeDef TIM_2_TimeBaseInitStruct;
    NVIC_InitTypeDef MYNVIC;
    
    //AHB clock = 50, MHz
    RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);
    
    // Configure TIM2 IRQ
    MYNVIC.NVIC_IRQChannel = TIM2_IRQn;
    MYNVIC.NVIC_IRQChannelCmd = ENABLE;
    MYNVIC.NVIC_IRQChannelPreemptionPriority = 0;
    MYNVIC.NVIC_IRQChannelSubPriority = 1;
    NVIC_Init(&MYNVIC);
   
    TIM_DeInit(TIM2);
    
    //TIM_2 clock = 50, MHz / prescaler = 50 000 000 / 500 = 100 000, Hz
    //time = TIM_2 period * (1 / TIM_2 clock) = 100 * (1 / 100 000) = 0.001, s
       
    TIM_2_TimeBaseInitStruct.TIM_Prescaler = 500;
    TIM_2_TimeBaseInitStruct.TIM_Period = 100;
    TIM_2_TimeBaseInitStruct.TIM_ClockDivision = TIM_CKD_DIV1; // 0
    TIM_2_TimeBaseInitStruct.TIM_CounterMode = TIM_CounterMode_Up;
    TIM_2_TimeBaseInitStruct.TIM_RepetitionCounter = 0;
    
    TIM_TimeBaseInit(TIM2, &TIM_2_TimeBaseInitStruct);
    
    TIM_ClearFlag(TIM2, TIM_FLAG_Update);
    TIM_ClearITPendingBit(TIM2, TIM_IT_Update);
    
    TIM_ITConfig(TIM2, TIM_IT_Update, ENABLE);
}
Esempio n. 5
0
//TIM_3 is for ModBus communication
void InitTIM3(void)
{
    TIM_TimeBaseInitTypeDef TIM_3_TimeBaseInitStruct;
    NVIC_InitTypeDef MYNVIC;
    
    RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);
    
    // Configure TIM3 IRQ
    MYNVIC.NVIC_IRQChannel = TIM3_IRQn;
    MYNVIC.NVIC_IRQChannelCmd = ENABLE;
    MYNVIC.NVIC_IRQChannelPreemptionPriority = 0;
    MYNVIC.NVIC_IRQChannelSubPriority = 2;
    NVIC_Init(&MYNVIC);	
    
    TIM_DeInit(TIM3);
    
    //TIM_3 clock = 50, MHz / prescaler = 50 000 000 / 50 000 = 1000, Hz
    //time = TIM_3 period * (1 / TIM_3 clock) = 1 * (1 / 1000) = 0.001, s
       
    TIM_3_TimeBaseInitStruct.TIM_Prescaler = 50000;
    TIM_3_TimeBaseInitStruct.TIM_Period = 1;
    TIM_3_TimeBaseInitStruct.TIM_ClockDivision = TIM_CKD_DIV1; // 0
    TIM_3_TimeBaseInitStruct.TIM_CounterMode = TIM_CounterMode_Up;
    TIM_3_TimeBaseInitStruct.TIM_RepetitionCounter = 0;
    
    TIM_TimeBaseInit(TIM3, &TIM_3_TimeBaseInitStruct);
    
    TIM_ClearFlag(TIM3, TIM_FLAG_Update);
    TIM_ClearITPendingBit(TIM3, TIM_IT_Update);
    
    TIM_ITConfig(TIM3, TIM_IT_Update, ENABLE);
    
    //TIM3 is not enabled from init function
    //ENABLE command is set by ModBusTimerEnable()
}
/*
 * TIM_Period / Auto Reload Register(ARR) = 1000   TIM_Prescaler--71 
 * 中断周期为 = 1/(72MHZ /72) * 1000 = 1ms
 *
 * TIMxCLK/CK_PSC --> TIMxCNT --> TIM_Period(ARR) --> 中断 且TIMxCNT重置为0重新计数 
 */
void TIM2_Configuration(void)
{
    TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;
		
		/* 设置TIM2CLK 为 72MHZ */
    RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2 , ENABLE);
    //TIM_DeInit(TIM2);
	
	/* 自动重装载寄存器周期的值(计数值) */
    TIM_TimeBaseStructure.TIM_Period=1000;
	
    /* 累计 TIM_Period个频率后产生一个更新或者中断 */
	  /* 时钟预分频数为72 */
    TIM_TimeBaseStructure.TIM_Prescaler= 71;
	
		/* 对外部时钟进行采样的时钟分频,这里没有用到 */
    TIM_TimeBaseStructure.TIM_ClockDivision=TIM_CKD_DIV1;
	
    TIM_TimeBaseStructure.TIM_CounterMode=TIM_CounterMode_Up; 
    TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);
	
    TIM_ClearFlag(TIM2, TIM_FLAG_Update);
	
    TIM_ITConfig(TIM2,TIM_IT_Update,ENABLE);
		
    TIM_Cmd(TIM2, ENABLE);																		
    
    RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2 , DISABLE);		/*先关闭等待使用*/    
}
Esempio n. 7
0
static void s_hal_timer_cpufamily_start(hal_timer_t id)
{
#if     defined(HAL_USE_CPU_FAM_STM32F1) || defined(HAL_USE_CPU_FAM_STM32F4)
    
    hal_timer_internal_item_t *intitem = s_hal_timer_theinternals.items[HAL_timer_id2index(id)];
    TIM_TypeDef* TIMx               = s_hal_timer_cpufamily_regs[HAL_timer_id2index(id)].TIMx;
    uint32_t RCC_APBxPeriph_TIMx    = s_hal_timer_cpufamily_regs[HAL_timer_id2index(id)].RCC_APBxPeriph_TIMx;
    IRQn_Type TIMx_IRQn             = s_hal_timer_cpufamily_regs[HAL_timer_id2index(id)].TIMx_IRQn;
    hal_timer_stm32_periphclock_t periphclock = s_hal_timer_cpufamily_regs[HAL_timer_id2index(id)].periphclock;
    TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;
   // NVIC_InitTypeDef NVIC_InitStructure;
   


    // Enable TIMx clock 
    if(hal_timer_stm32_periphclock_apb1 == periphclock)
    {
        RCC_APB1PeriphClockCmd(RCC_APBxPeriph_TIMx, ENABLE);
    }
    else if(hal_timer_stm32_periphclock_apb2 == periphclock)
    {
        RCC_APB2PeriphClockCmd(RCC_APBxPeriph_TIMx, ENABLE);
    }

    // registers of TIMx

    TIM_TimeBaseStructInit(&TIM_TimeBaseStructure);
    TIM_TimeBaseStructure.TIM_Period = intitem->period - 1;          
    TIM_TimeBaseStructure.TIM_Prescaler = intitem->prescaler - 1; 
    TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;    
    TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Down;  
    TIM_TimeBaseStructure.TIM_RepetitionCounter = 0x0000;
    TIM_TimeBaseInit(TIMx, &TIM_TimeBaseStructure);


    //* enable counter 
    TIM_Cmd(TIMx, ENABLE);

    // Immediate load of  Prescaler value */
    TIM_PrescalerConfig(TIMx, intitem->prescaler - 1, TIM_PSCReloadMode_Immediate);
    //TIM_PrescalerConfig(TIMx, intitem->prescaler - 1, TIM_PSCReloadMode_Update);
    

    // Clear  update pending flag */
    TIM_ClearFlag(TIMx, TIM_FLAG_Update);

    if(hal_int_priorityNONE != intitem->config.priority)
    {
        // Enable TIM2 Update interrupt */
        TIM_ITConfig(TIMx, TIM_IT_Update, ENABLE);
    
        // enable irqs in nvic
        hal_sys_irqn_priority_set(TIMx_IRQn, intitem->config.priority);
        hal_sys_irqn_enable(TIMx_IRQn);
    }
    
#else //defined(HAL_USE_CPU_FAM_STM32F1) || defined(HAL_USE_CPU_FAM_STM32F4)
    #error ERR --> choose a HAL_USE_CPU_FAM_*
#endif    
}
Esempio n. 8
0
void Delay_t::Init(void) {
    // Interrupt config
    NVIC_InitTypeDef NVIC_InitStructure;
    NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQn;
    NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
    NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
    NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
    NVIC_Init(&NVIC_InitStructure);
    // TIM2 clock enable
    RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);
    // Time base configuration: 1 ms == 1000 Hz = FCLK / (100 * (FCLK/100 000))
    TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
    TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; // Up-counter needed, nothing special
    TIM_TimeBaseStructure.TIM_ClockDivision = 0; // Dead-time divisor, not needed here
    TIM_TimeBaseStructure.TIM_Period = 100; // Auto-reload value
    TIM_TimeBaseStructure.TIM_Prescaler = (uint16_t) (SystemCoreClock / 100000) - 1; // Input clock divisor
    TIM_TimeBaseStructure.TIM_RepetitionCounter = 0;
    TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);
    // Enable timer
    TIM_Cmd(TIM2, ENABLE);
    // Clear Timer update pending flag
    TIM_ClearFlag(TIM2, TIM_FLAG_Update);
    // Interrupts config
    TIM_ITConfig(TIM2, TIM_IT_Update, ENABLE);
}
Esempio n. 9
0
/**
  * @brief  Configure the TIM Init and IRQ Handler.
  * @param  None
  * @retval None
  */
void ENC_2_TIM_Init(void) {	
	/* Private typedef -----------------------------------------------------------*/
	NVIC_InitTypeDef  NVIC_InitStructure;
	
	/* Timer peripheral clock enable */
	RCC_APB1PeriphClockCmd 				(ENC_2_TIMER_CLK, ENABLE); 
	
	/** 
	* set them up as encoder inputs 
	* set both inputs to rising polarity to let it use both edges
	*/
	TIM_EncoderInterfaceConfig 		(ENC_2_TIMER, TIM_EncoderMode_TI12, TIM_ICPolarity_Falling, TIM_ICPolarity_Falling); 
	TIM_SetAutoreload 						(ENC_2_TIMER, 0xffffff); 
	 
	/* clear the interrupt */
	TIM_ClearFlag(ENC_2_TIMER, TIM_FLAG_Update);
	
	/* enable TIM2 Interrupt */
  TIM_ITConfig(ENC_2_TIMER, TIM_IT_Update, ENABLE);
	
	/* Enable the TIM2 gloabal Interrupt */
  NVIC_InitStructure.NVIC_IRQChannel 										= ENC_2_TIMER_IQRn;
  NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority 	= 0;
  NVIC_InitStructure.NVIC_IRQChannelSubPriority 				= 1;
  NVIC_InitStructure.NVIC_IRQChannelCmd 								= ENABLE;
  NVIC_Init(&NVIC_InitStructure);
}
Esempio n. 10
0
/*定时器配置*/
void TIM3_Config( void )
{
    NVIC_InitTypeDef		NVIC_InitStructure;
    TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;

    /* TIM3 clock enable */
    RCC_APB1PeriphClockCmd( RCC_APB1Periph_TIM3, ENABLE );

    /* Enable the TIM3 gloabal Interrupt */
    NVIC_InitStructure.NVIC_IRQChannel						= TIM3_IRQn;
    NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority	= 0;
    NVIC_InitStructure.NVIC_IRQChannelSubPriority			= 2;
    NVIC_InitStructure.NVIC_IRQChannelCmd					= ENABLE;
    NVIC_Init( &NVIC_InitStructure );

    /* Time base configuration */
    TIM_TimeBaseStructure.TIM_Period		= 1000;             /* 1ms */
    TIM_TimeBaseStructure.TIM_Prescaler		= ( 168 / 2 - 1 );  /* 1M*/
    TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;
    TIM_TimeBaseStructure.TIM_CounterMode	= TIM_CounterMode_Up;
    TIM_ARRPreloadConfig( TIM3, ENABLE );

    TIM_TimeBaseInit( TIM3, &TIM_TimeBaseStructure );

    TIM_ClearFlag( TIM3, TIM_FLAG_Update );
    /* TIM Interrupts enable */
    TIM_ITConfig( TIM3, TIM_IT_Update, ENABLE );

    /* TIM3 enable counter */
    TIM_Cmd( TIM3, ENABLE );
}
Esempio n. 11
0
/**
\brief Schedule the callback to be called in some specified time.

The delay is expressed relative to the last compare event. It doesn't matter
how long it took to call this function after the last compare, the timer will
expire precisely delayTicks after the last one.

The only possible problem is that it took so long to call this function that
the delay specified is shorter than the time already elapsed since the last
compare. In that case, this function triggers the interrupt to fire right away.

This means that the interrupt may fire a bit off, but this inaccuracy does not
propagate to subsequent timers.

\param delayTicks Number of ticks before the timer expired, relative to the
                  last compare event.
*/
void bsp_timer_scheduleIn(PORT_TIMER_WIDTH delayTicks) 
{
   PORT_TIMER_WIDTH newCompareValue;
   PORT_TIMER_WIDTH temp_last_compare_value;
   //enable it if not enabled.
   TIM_Cmd(TIM2, ENABLE); 
   
   temp_last_compare_value = bsp_timer_vars.last_compare_value;
   
   newCompareValue = bsp_timer_vars.last_compare_value+delayTicks;
   bsp_timer_vars.last_compare_value = newCompareValue;
   
   if (delayTicks < (TIM_GetCounter(TIM2)-temp_last_compare_value)) 
   {
      // setting the interrupt flag triggers an interrupt
        TIM2->SR |= (u16)TIM_FLAG_CC1;
   } 
   else
   {
      // this is the normal case, have timer expire at newCompareValue
      TIM_SetCompare1(TIM2,newCompareValue);
      TIM_ClearFlag(TIM2, TIM_FLAG_CC1);
      TIM_ITConfig(TIM2, TIM_IT_CC1, ENABLE);
   }
}
Esempio n. 12
0
/*******************************************************************************
* Function Name  : TIM2_Configuration
* Description    : 每1秒发生一次更新事件(进入中断服务程序).
* Input          : None
* Return         : None
*******************************************************************************/
void TIM2_Configuration(unsigned int MsTime)
{
  TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;

  //重新将Timer设置为缺省值
  TIM_DeInit(TIM2);
  
  //采用内部时钟给TIM2提供时钟源
  TIM_InternalClockConfig(TIM2);
  
  //预分频系数为36000-1,这样计数器时钟为64MHz/32000 = 2kHz
  TIM_TimeBaseStructure.TIM_Prescaler = 32000 - 1;
  
  //设置时钟分割
  TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;
  
  //设置计数器模式为向上计数模式
  TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
  
  //设置计数溢出大小,每计2000个数就产生一个更新事件
  TIM_TimeBaseStructure.TIM_Period = (2*MsTime);
  
  //将配置应用到TIM2中
  TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);

  //清除溢出中断标志
  TIM_ClearFlag(TIM2, TIM_FLAG_Update);
  
  //禁止ARR预装载缓冲器
  TIM_ARRPreloadConfig(TIM2, DISABLE);  //预装载寄存器的内容被立即传送到影子寄存器 
  
  //开启TIM2的中断
  TIM_ITConfig(TIM2, TIM_IT_Update, ENABLE);
}
Esempio n. 13
0
/**************************************************************************
函数功能:把TIM4初始化为编码器接口模式
入口参数:无
返回  值:无
**************************************************************************/
void Encoder_Init_TIM3(void)
{
	TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;  
  TIM_ICInitTypeDef TIM_ICInitStructure;  
  GPIO_InitTypeDef GPIO_InitStructure;
  RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);//使能定时器3的时钟
  RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);//使能PA端口时钟
	
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6|GPIO_Pin_7;	//端口配置
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; //浮空输入
  GPIO_Init(GPIOA, &GPIO_InitStructure);					      //根据设定参数初始化GPIOA
  
  TIM_TimeBaseStructInit(&TIM_TimeBaseStructure);
  TIM_TimeBaseStructure.TIM_Prescaler = 0x0; // 预分频器 
  TIM_TimeBaseStructure.TIM_Period = ENCODER_TIM_PERIOD; //设定计数器自动重装值
  TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;//选择时钟分频:不分频
  TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;////TIM向上计数  
  TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure);
  TIM_EncoderInterfaceConfig(TIM3, TIM_EncoderMode_TI12, TIM_ICPolarity_Rising, TIM_ICPolarity_Rising);//使用编码器模式3
  TIM_ICStructInit(&TIM_ICInitStructure);
  TIM_ICInitStructure.TIM_ICFilter = 10;
  TIM_ICInit(TIM3, &TIM_ICInitStructure);
  TIM_ClearFlag(TIM3, TIM_FLAG_Update);//清除TIM的更新标志位
  TIM_ITConfig(TIM3, TIM_IT_Update, ENABLE);
  //Reset counter
  TIM_SetCounter(TIM3,0);
  TIM_Cmd(TIM3, ENABLE); 
}
Esempio n. 14
0
void TIM2_Init(void)
{
	TIM_TimeBaseInitTypeDef  T;	//创建定时器变量

	RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);	  	//TIM2使用APB1时钟总线
	TIM_DeInit(TIM2);											//复位TIM2定时器到默认值

	T.TIM_Period=500-1;								//计数器溢出值,最大65535,此值不能更改,与输出比较的设定值一致,输出比较计数器溢出也为65535

	if(get_APB1_prevdiv() > 0){
		tim2_clock = Get_SyS_CLK(3)<<1;
	}else{
		tim2_clock = Get_SyS_CLK(3);
	}

	T.TIM_Prescaler = tim2_clock/TIM2_PREV_DIV - 1; 	//预分频系数,系统每秒72,000,000次,72000000/50000=1440分频,对系统频率72000000进行1440分频后,每秒计数频率为50000次,即每次计数时间为20us,每计满TIM_Period多次时变0重计
	//T.TIM_Prescaler =7200 - 1; 	//预分频系数,系统每秒72,000,000次,72000000/50000=1440分频,对系统频率72000000进行1440分频后,每秒计数频率为50000次,即每次计数时间为20us,每计满TIM_Period多次时变0重计

	T.TIM_ClockDivision = 0;						//时钟分割 在定时器预分频之前作一段延时,很少用到,设为0即可
  	T.TIM_CounterMode = TIM_CounterMode_Up;			//向上计数		UP:0到10000的计数	DOWN:FFFF到10000 
  	TIM_TimeBaseInit(TIM2, &T);						//根据上面设置初始化定时器TIM2

  	TIM_ClearFlag(TIM2, TIM_FLAG_Update);			//清空溢出中断
   	TIM_SetCounter(TIM2,0);							//设置计数器初值为0	//目前尚未启用该定时器
}
Esempio n. 15
0
/*********************************************************************************************************
  功能 :TIM4用做5ms中断
 **********************************************************************************************************/
void Tim4_Init(void)
{
    NVIC_InitTypeDef NVIC_InitStructure;
    TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;
    RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4, ENABLE);

    TIM_TimeBaseStructure.TIM_Period = 60000;
    TIM_TimeBaseStructure.TIM_Prescaler = 5;	//分频后 72/(5+1) = 12MHz 每计1个数为0.08333us
    TIM_TimeBaseStructure.TIM_ClockDivision = 0;
    TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
    TIM_TimeBaseInit(TIM4, &TIM_TimeBaseStructure);


    //Enable the TIM6 gloabal Interrupt
    NVIC_InitStructure.NVIC_IRQChannel = TIM4_IRQn;
    NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
    NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3;
    NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
    NVIC_Init(&NVIC_InitStructure);

    TIM_ClearFlag(TIM4, TIM_FLAG_Update);	//清除更新标志位
    TIM_ARRPreloadConfig(TIM4, DISABLE);	//预装载寄存器的内容被立即传送到影子寄存器

    TIM_ITConfig(TIM4, TIM_IT_Update, ENABLE);
    TIM_Cmd(TIM4, ENABLE);
}
Esempio n. 16
0
void TIM5_IRQHandler(void)
{
    //mRedON;
    //strum();
    TIM_ClearFlag(TIM5,TIM_FLAG_CC1);
    //TIM5->CNT=0;
}
void TIM2_Configuration(void)
{
	TIM_TimeBaseInitTypeDef  TIM2_TimeBaseStructure;
	NVIC_InitTypeDef         NVIC_Structure;
	
	uint16_t PrescalerValue = 0;
	RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2,ENABLE);
	/*To get TIM1 counter clock at 1 MHz, the prescaler is computed as follows:
				 Prescaler = (TIM9CLK / TIM1 counter clock) - 1
				 Prescaler = (SystemCoreClock /1 MHz) - 1	*/
	NVIC_PriorityGroupConfig(NVIC_PriorityGroup_4);
	NVIC_Structure.NVIC_IRQChannel = TIM2_IRQn;
	NVIC_Structure.NVIC_IRQChannelPreemptionPriority = 1;
	NVIC_Structure.NVIC_IRQChannelSubPriority = 0;
	NVIC_Structure.NVIC_IRQChannelCmd = ENABLE;
	NVIC_Init(&NVIC_Structure);	
		
	PrescalerValue = (uint16_t) (SystemCoreClock / 100000)-1;
	TIM2_TimeBaseStructure.TIM_Prescaler = PrescalerValue;
	TIM2_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
	TIM2_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;
	TIM2_TimeBaseStructure.TIM_Period = 1000;      //20ms
	//(采样时间设定为10ms会引起震荡,原因不明;设为20ms后,震荡消失)
	TIM2_TimeBaseStructure.TIM_RepetitionCounter = 0x00;
	TIM_TimeBaseInit(TIM2,&TIM2_TimeBaseStructure);
	
	TIM_ITConfig(TIM2,TIM_IT_Update,ENABLE);
	TIM_Cmd(TIM2,ENABLE);
	TIM_ClearFlag(TIM2,TIM_FLAG_Update);
}
Esempio n. 18
0
	void F4Timer::set_period(uint32_t period)
	{
		if(TIM1==TIMx)
			RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1,ENABLE);
		if(TIM2==TIMx)
			RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2,ENABLE);
		if(TIM3==TIMx)
			RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3,ENABLE);
		if(TIM4==TIMx)
			RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4,ENABLE);
		if(TIM5==TIMx)
			RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM5,ENABLE);
		if(TIM6==TIMx)
			RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM6,ENABLE);
		TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
		TIM_DeInit(TIMx);
		TIM_InternalClockConfig(TIMx);
		TIM_TimeBaseStructure.TIM_Prescaler=167;//1Mhz 1us 65536
		TIM_TimeBaseStructure.TIM_ClockDivision=TIM_CKD_DIV1;
		TIM_TimeBaseStructure.TIM_CounterMode=TIM_CounterMode_Up;
		TIM_TimeBaseStructure.TIM_Period=period;
		TIM_TimeBaseStructure.TIM_RepetitionCounter = 0x0;
		TIM_TimeBaseInit(TIMx,&TIM_TimeBaseStructure);
		TIM_ClearFlag(TIMx,TIM_FLAG_Update);
		TIM_ARRPreloadConfig(TIMx,DISABLE);
		TIM_ITConfig(TIMx,TIM_IT_Update,ENABLE);
		TIM_Cmd(TIMx,ENABLE);
		TimerInit(this->TIMx);
	}
Esempio n. 19
0
/*******************************************************************************
* Function Name  : TIM3_IRQHandler
* Description    : This function handles TIM3 global interrupt request.
* Input          : None
* Output         : None
* Return         : None
*******************************************************************************/
void TIM3_IRQHandler(void)
{
  if(TIM_GetFlagStatus(TIM3,TIM_FLAG_CC1) != RESET)
  {
    TIM_ClearFlag(TIM3,TIM_FLAG_CC1);
  }
}
Esempio n. 20
0
void Timer2Config()
{
	TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStructure;
	RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2,ENABLE);
	NVIC_InitTypeDef NVIC_InitStructure;
	
	RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2,ENABLE);
	TIM_TimeBaseInitStructure.TIM_ClockDivision = TIM_CKD_DIV1;
	TIM_TimeBaseInitStructure.TIM_CounterMode = TIM_CounterMode_Up;
	TIM_TimeBaseInitStructure.TIM_Period = (200 - 1);     	    //t is the time between each Timer irq.
	TIM_TimeBaseInitStructure.TIM_Prescaler = (16800 - 1);  		//t = (1+TIM_Prescaler/SystemCoreClock)*(1+TIM_Period)
	TIM_TimeBaseInitStructure.TIM_RepetitionCounter = 0x00;     //here configure TIM1 in 50Hz
	TIM_TimeBaseInit(TIM2,&TIM_TimeBaseInitStructure);
	
	 
	NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQn;  
	NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 2;   
	NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3;   
	NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;  
	NVIC_Init(&NVIC_InitStructure);   
	
	TIM_ClearFlag(TIM2,TIM_FLAG_Update);   
	TIM_ITConfig(TIM2,TIM_IT_Update,ENABLE);    
	TIM_Cmd(TIM2,DISABLE);    

}
void TIM2_IRQHandler( void )
{
  if(TIM_GetITStatus(TIMx, TIM_FLAG_Update) != RESET) {
    LED_B_Toggle;
  }
  TIM_ClearFlag(TIMx, TIM_FLAG_Update);
}
Esempio n. 22
0
void TIM4_Init(void)
{
    TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
    
    NVIC_InitTypeDef NVIC_InitStructure;
    
    RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4,ENABLE);  //打开时钟
    
    TIM_DeInit(TIM4);

    TIM_TimeBaseStructure.TIM_Period = 1000;
    TIM_TimeBaseStructure.TIM_Prescaler = 36-1;//定时1ms
    TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1; 
    TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
    
    TIM_TimeBaseInit(TIM4,&TIM_TimeBaseStructure);
    TIM_ClearFlag(TIM4,TIM_FLAG_Update);

    TIM_ITConfig(TIM4,TIM_IT_Update,ENABLE);
    

   
    /* NVIC_PriorityGroup 1 */
    NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);

    //TIM4
    NVIC_InitStructure.NVIC_IRQChannel = TIM4_IRQn;
    NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
    NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
    NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
    NVIC_Init(&NVIC_InitStructure);

    TIM_Cmd(TIM4,ENABLE);
    
}		
Esempio n. 23
0
void Timer_Config( void )
{
  NVIC_InitTypeDef NVIC_InitStruct;
  TIM_TimeBaseInitTypeDef TIM_TimeBaseStruct;

  RCC_APB1PeriphClockCmd(TIMx_CLK, ENABLE);

  NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0);
  NVIC_InitStruct.NVIC_IRQChannel = TIMx_NVIC_CHANNEL;
  NVIC_InitStruct.NVIC_IRQChannelPreemptionPriority = 0;
  NVIC_InitStruct.NVIC_IRQChannelSubPriority = 1;
  NVIC_InitStruct.NVIC_IRQChannelCmd = ENABLE;
  NVIC_Init(&NVIC_InitStruct);

  /* TIM Time Base */
  TIM_TimeBaseStruct.TIM_Period = 1000;     // 100us * 1000 = 100ms
  TIM_TimeBaseStruct.TIM_Prescaler = (SystemCoreClock / 10000) - 1;   // F_APB1 / 7200 = 10kHz ( 100us )
  TIM_TimeBaseStruct.TIM_ClockDivision = TIM_CKD_DIV1;
  TIM_TimeBaseStruct.TIM_CounterMode = TIM_CounterMode_Down;
  TIM_TimeBaseInit(TIMx, &TIM_TimeBaseStruct);

  /* TIM Start */
  TIM_ClearFlag(TIMx, TIM_FLAG_Update);
  TIM_ITConfig(TIMx, TIM_IT_Update, ENABLE);
  TIM_Cmd(TIMx, ENABLE);
}
Esempio n. 24
0
void Delay_us(uint32_t nus)
{		
  TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;
  
  /* TIM1 clock enable */
  RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1, ENABLE);
  
  // TIM1 configuration    10ms
  TIM_DeInit(TIM1);
  TIM_TimeBaseStructure.TIM_Period = nus;          
  TIM_TimeBaseStructure.TIM_Prescaler = (100-1);       
  TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;
  TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;  
  TIM_TimeBaseInit(TIM1, &TIM_TimeBaseStructure);
  
  // Clear TIM1 update pending flag
  TIM_ClearFlag(TIM1, TIM_FLAG_Update);
  // Disable TIM1 Update interrupt 
  TIM_ITConfig(TIM1, TIM_IT_Update, ENABLE);
  // TIM1 enable counter 
  TIM_Cmd(TIM1, ENABLE);
  
  while(TIM_GetITStatus(TIM1, TIM_IT_Update) == RESET);
  
  // stop TIM1
  TIM_ClearITPendingBit(TIM1, TIM_IT_Update);
  TIM_Cmd(TIM1, DISABLE);
}
Esempio n. 25
0
/*****************************************************************************
 * 函数功能:		定时器初始化(单位 500ms)
 * 形式参数:		无
 * 返回参数:		无
 * 更改日期;		2018-03-29				函数编写
 ****************************************************************************/
void Timer_Config( void )
{
	TIM_TimeBaseInitTypeDef		newTIM_Init;
	NVIC_InitTypeDef					newNVIC_Init;
	
	newTIM_Init.TIM_ClockDivision	=	TIM_CKD_DIV1;
	newTIM_Init.TIM_CounterMode		=	TIM_CounterMode_Up;
	newTIM_Init.TIM_Period				=	SystemCoreClock / TIM_FREQ_MS -1;	//1KHz  48000分频
	newTIM_Init.TIM_Prescaler			=	TIM_COUNT-1;										//100ms
	
	/*1> 定时器 配置*/	
	TIM_DeInit( TIM_COM );
	RCC_TIM_CMD( RCC_TIM_CLK , ENABLE );
	TIM_TimeBaseInit(TIM_COM , &newTIM_Init );
	TIM_ITConfig(TIM_COM , TIM_IT_Update , ENABLE );	//允许更新中断
	TIM_ClearFlag(TIM_COM , TIM_FLAG_Update );				//清除中断标志
	TIM_ARRPreloadConfig(TIM_COM , DISABLE );				  //禁止预装缓存器
	
	newNVIC_Init.NVIC_IRQChannelCmd      = ENABLE;
	/*5> 中断 配置*/	
	newNVIC_Init.NVIC_IRQChannel				 = TIM_IRQn;
	newNVIC_Init.NVIC_IRQChannelPriority = 2;
	NVIC_SetPriority( TIM_IRQn	, 1);
	NVIC_Init( &newNVIC_Init );
	
	TIM_Cmd(TIM_COM , ENABLE);											//定时器使能
}
Esempio n. 26
0
/*
TIM2时钟初始化:0.5s溢出(定位周期)
*/
void TIM2_init(void) {
	NVIC_InitTypeDef NVIC_InitStructure;
	TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;

	NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
	NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQn;
	NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 3;
	NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3;
	NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
	NVIC_Init(&NVIC_InitStructure);

	RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2 , ENABLE);

	TIM_DeInit(TIM2);

	// 0.0005 * 100 = 0.05s
	TIM_TimeBaseStructure.TIM_Period = (int) ((float)LOCATION_PERIOD / (float)TICK_IN_PERIOD / 0.0005); // 0.05s
	DEBUG2(("%f %f %f\r\n", (float)LOCATION_PERIOD ,(float)TICK_IN_PERIOD, (float)LOCATION_PERIOD / (float)TICK_IN_PERIOD));
	DEBUG2(("定位TICK周期: %f\r\n", ((float)LOCATION_PERIOD / (float)TICK_IN_PERIOD / 0.0005)));
	TIM_TimeBaseStructure.TIM_Prescaler = 36000; // 1 / 72MHz * 36k = 0.0005/period
	TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;
	TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
	TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);

	TIM_ClearFlag(TIM2, TIM_FLAG_Update);
	TIM_ITConfig(TIM2, TIM_IT_Update, ENABLE);
	TIM_Cmd(TIM2, ENABLE);

	DEBUG2(("定位TICK周期: %d\r\n", TIM_TimeBaseStructure.TIM_Period));
}
Esempio n. 27
0
static void PWM2_Mode_Config(void)
{
    TIM_TimeBaseInitTypeDef TIM_TimeBaseInitSturcture;
    TIM_OCInitTypeDef TIM_OCInitSturcture;

    u16 CCR1_Val = 1250;

    TIM_DeInit(PWM2_TIM);
    TIM_TimeBaseInitSturcture.TIM_Prescaler = (36-1);
    TIM_TimeBaseInitSturcture.TIM_CounterMode = TIM_CounterMode_Up;
    TIM_TimeBaseInitSturcture.TIM_Period = 2500;
    TIM_TimeBaseInitSturcture.TIM_ClockDivision = 0;

    TIM_TimeBaseInit(PWM2_TIM,&TIM_TimeBaseInitSturcture);

    TIM_ClearFlag(PWM2_TIM, TIM_FLAG_Update);							    		/* 清除溢出中断标志 */
    TIM_ITConfig(PWM2_TIM,TIM_IT_Update,ENABLE);

   TIM_OCInitSturcture.TIM_OCMode = TIM_OCMode_PWM1;	    //配置为PWM模式1
   TIM_OCInitSturcture.TIM_OutputState = TIM_OutputState_Enable;
   TIM_OCInitSturcture.TIM_Pulse = CCR1_Val;
//设置跳变值,当计数器计数到这个值时,电平发生跳变
  TIM_OCInitSturcture.TIM_OCPolarity = TIM_OCPolarity_High;
//当定时器计数值小于CCR1_Val时为高电平

  TIM_OC1Init(PWM2_TIM, &TIM_OCInitSturcture);	 //使能通道1

    TIM_OC1PreloadConfig(PWM2_TIM, TIM_OCPreload_Disable);
     TIM_ARRPreloadConfig(PWM2_TIM, ENABLE);			 // 使能TIM3重载寄存器ARR

  /* TIM3 enable counter */
  TIM_Cmd(PWM2_TIM, DISABLE);                   //使能定时器3

}
Esempio n. 28
0
void ENC_Init(void)
{
	TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
	TIM_ICInitTypeDef TIM_ICInitStructure;
	
	//Encoder uint connected to TIM2
	GPIO_InitTypeDef GPIO_InitStructure;
	NVIC_InitTypeDef NVIC_InitStructure;

	//RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2,ENABLE);
	RCC_APB1PeriphClockCmd(ENC_TIMER_CLK,ENABLE);
	RCC_APB2PeriphClockCmd(ENC_GPIO_CLK ,ENABLE);

	GPIO_StructInit(&GPIO_InitStructure);
	//GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1;
	GPIO_InitStructure.GPIO_Pin = ENC_GPIO_PIN_A  | ENC_GPIO_PIN_B ;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
	GPIO_Init(ENC_GPIO_PORT ,&GPIO_InitStructure);

	NVIC_InitStructure.NVIC_IRQChannel = ENC_TIMER_IRQn ;
	NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = TIMx_PRE_EMPTION_PRIORITY;
	NVIC_InitStructure.NVIC_IRQChannelSubPriority = TIMx_SUB_PRIORITY;
	NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
	NVIC_Init(&NVIC_InitStructure);

	TIM_DeInit(ENC_TIMER);
	//TIM_TimeBaseInit(&TIM_TimeBaseStructure);

	TIM_TimeBaseStructure.TIM_Prescaler = 0x0;
	TIM_TimeBaseStructure.TIM_Period = (4*ENCODER_PPR)-1;
	TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;
	TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
	TIM_TimeBaseInit(ENC_TIMER,&TIM_TimeBaseStructure);

	TIM_EncoderInterfaceConfig(ENC_TIMER,TIM_EncoderMode_TI12,
	TIM_ICPolarity_Rising,TIM_ICPolarity_Rising);
	TIM_ICStructInit(&TIM_ICInitStructure);
	TIM_ICInitStructure.TIM_ICFilter = ICx_FILTER;
	TIM_ICInit(ENC_TIMER,&TIM_ICInitStructure);
 
	TIM_ICInitStructure.TIM_Channel = TIM_Channel_2;//注意通道
	TIM_ICInit(ENC_TIMER, &TIM_ICInitStructure);

	TIM_ClearFlag(ENC_TIMER,TIM_FLAG_Update);
	TIM_ITConfig(ENC_TIMER,TIM_IT_Update,ENABLE);

	TIM2->CNT = COUNTER_RESET;//!!此处注意修改

	//局部初始化
	ENC_Clear_Speed_Buffer();
	hPrevious_angle=0;
  hRot_Speed=0;;
  bSpeed_Buffer_Index = 0;
  hEncoder_Timer_Overflow=0;
  bIs_First_Measurement = true;
  hEncoder_Revolutions_Num=0; 
   
  TIM_Cmd(ENC_TIMER,ENABLE);
}
Esempio n. 29
0
/**
  * @brief TIM5的溢出中断
  * @param None
  * @retval None
  * @details 执行清除中断位的操作
  */
void TIM5_IRQHandler(void)										
{
	  if (TIM_GetITStatus(TIM5,TIM_IT_Update)!= RESET) 
		{
			  TIM_ClearITPendingBit(TIM5,TIM_IT_Update);
        TIM_ClearFlag(TIM5, TIM_FLAG_Update);
		}
} 
Esempio n. 30
0
//复位runtime
//需要根据所移植到的MCU的定时器参数进行修改
void usmart_reset_runtime(void)
{

    TIM_ClearFlag(TIM4,TIM_FLAG_Update);//清除中断标志位
    TIM_SetAutoreload(TIM4,0XFFFF);//将重装载值设置到最大
    TIM_SetCounter(TIM4,0);		//清空定时器的CNT
    usmart_dev.runtime=0;
}