Ejemplo n.º 1
0
void hardwareBlinking(void)
{
		    TIM_Cmd(TIM2, DISABLE);
				TIM_ITConfig(TIM2, TIM_IT_CC1, DISABLE);
				/* change the period of the ISR */
				factor = (factor+1)%2;
				TIM_PrescalerConfig(TIM2, 12000, TIM_PSCReloadMode_Immediate);
				/* restart interrupt on TIM3 */
				TIM_ITConfig(TIM2, TIM_IT_CC1, ENABLE);
				TIM_Cmd(TIM2, ENABLE);
				
				for(;;){
						int num = TIM_GetCounter(TIM2);
						if(num == 6000){
							STM_EVAL_LEDOn(LED3);
						  STM_EVAL_LEDOff(LED4);
						}
						else if(num == 12000){
							STM_EVAL_LEDOff(LED3);
							STM_EVAL_LEDOn(LED4);
							break;
						}
				}
				hardwareBlinking();
}
Ejemplo n.º 2
0
/*******************************************************************************
* Function Name  : USART1_IRQHandler
* Description    : This function handles USART1 global interrupt request.
* Input          : None
* Output         : None
* Return         : None
*******************************************************************************/
void USART1_IRQHandler(void)
{	

	USART_ClearITPendingBit(USART1, USART_IT_RXNE);
	
   if(USART_GetITStatus(USART1, USART_IT_RXNE) != RESET)
   {
	   	Rx_Buffer[data]=USART_ReceiveData(USART1);
			
		Usart_Putnum(Rx_Buffer[data]);

		if(Rx_Buffer[data]==1){
			/* Enable ADC1 DMA */
		    ADC_DMACmd(ADC1, ENABLE);

		    /* Enable ADC1 */
		    ADC_Cmd(ADC1, ENABLE);

		    /* Enable ADC1 reset calibaration register */   
		    ADC_ResetCalibration(ADC1);
		    /* Check the end of ADC1 reset calibration register */
		    while(ADC_GetResetCalibrationStatus(ADC1));

		    /* Start ADC1 calibaration */
		    ADC_StartCalibration(ADC1);
		    /* Check the end of ADC1 calibration */
		    while(ADC_GetCalibrationStatus(ADC1));
		 
		    /* Start ADC1 Software Conversion */ 
		    ADC_SoftwareStartConvCmd(ADC1, ENABLE);
		}

		else if(Rx_Buffer[data]==2)
			 ADC_Cmd(ADC1, DISABLE);
		else if(Rx_Buffer[data]==3)
		Usart_Putnum(TIM_GetCounter(TIM4));
		
	
   }

   
	//{	
		//Rx_Buffer[data]=data;
		//data++;
		//USART_ClearITPendingBit(USART1, USART_IT_RXNE);
		/*Rx_Buffer[data]=USART_ReceiveData(USART1);
		
		Usart_Putnum(Rx_Buffer[data]);
		data++;*/
		/*
		if(Rx_Buffer[data]==1)
		Usart_Putnum(10);
		else if(Rx_Buffer[data]==2)
		Usart_Putnum(20);
		*/
	
    
	
	
}
Ejemplo n.º 3
0
/******************************************
程序功能:红外接收中断服务程序
参程序数:无
返回参数:无
*******************************************/
void IRstudydata(void)
{
    uint16_t datatimerbuf;      //红外接收数据缓存

//      if(hwred_bit_status != GPIO_ReadInputPin(IR_PORT, IRRED_PIN))
    //     {
    //        hwred_bit_status = (BitStatus)!hwred_bit_status;
    if( bit_is_clear(Irtxflag,IRRXFINSH) && IRstudyflag)
    {
        datatimerbuf = TIM_GetCounter(TIM2);//- TIMECOUNT;
        TIM_SetCounter(TIM2,0);
        if(bit_is_clear(IRrxflag,IRRXSTART))
        {
            sbi_(IRrxflag,IRRXSTART);
            memset((void *)Irtimebuf,'\0',300);
            irdatalen = 0;
            //	uart_print(&irdatalen,1);
        }
        else if(irdatalen < TEMPTIMEBUF)
        {
            Irtimebuf[irdatalen] = datatimerbuf;
            irdatalen++;
        }

    }
    //     }
}
Ejemplo n.º 4
0
/**********************************************************************
* 名    称:EXTI9_5_IRQHandler()
* 功    能:外部中断通道5中断
* 入口参数: 
* 出口参数:
***********************************************************************/
void EXTI9_5_IRQHandler (void)
{
  OSIntEnter(); 
if(EXTI_GetITStatus(EXTI_Line5) != RESET) 
{
  if(SONICDEVICE.step == 1)
  {
    TIM_SetCounter(TIM6,0);
    TIM_Cmd(TIM6, ENABLE);
	SONICDEVICE.step = 2;
  }
  else if(SONICDEVICE.step == 2)
  {
    SONICDEVICE.databuff[SONICDEVICE.nextindex] = TIM_GetCounter(TIM6);
	if(SONICDEVICE.nextindex == 9)
	{
	  SONICDEVICE.nextindex = 0;
	}
	else
	{
	  SONICDEVICE.nextindex ++;
	}
    TIM_Cmd(TIM6, DISABLE);
	SONICDEVICE.step = 0;
  }
  EXTI_ClearFlag(EXTI_Line5);			       //清除中断标志(必须)
  EXTI_ClearITPendingBit(EXTI_Line5);
 }
  OSIntExit();
}
Ejemplo n.º 5
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);
   }
}
Ejemplo n.º 6
0
uint16_t CMX7262_Transcode (CMX7262_TypeDef *pCmx7262, uint16_t uMode)
{

	uint16_t uData;

	// Set the codec mode.
	CBUS_Write16(VCTRL_REG,(uint16_t *)&uMode,1,pCmx7262->uInterface);
	// Wait until we have confirmation of the mode being set.
	TIM_SetCounter(TIM5,0);
	while (TIM_GetCounter(TIM5) < CMX7262_TRANSCODE_TIMEOUT)
	{
		CBUS_Read16 (IRQ_STATUS_REG,&uData,1,pCmx7262->uInterface);
		pCmx7262->uIRQ_STATUS_REG |= uData;
		if ((pCmx7262->uIRQ_STATUS_REG & REGDONE) == REGDONE)
		{
			// Clear the REGDONE bit in the shadow regsiter.
			pCmx7262->uIRQ_STATUS_REG &= (uint16_t)(~REGDONE);
			return 1;
		}
	}
	// If we get here we have timed out and the mode selection was not successful,
	// so return 0.
	return 0;

}
Ejemplo n.º 7
0
extern "C" void EXTI2_3_IRQHandler(void) {
	uint32_t count = TIM_GetCounter(TIM14);

	if (EXTI_GetITStatus(EXTI_Line2)) {
		EXTI_ClearITPendingBit(EXTI_Line2);
		if (GPIOA->IDR & GPIO_Pin_2) {
			s_dht11_outer.startTime = count;
		} else {
			if (count < s_dht11_inner.startTime)
				s_dht11_outer.buffer[s_dht11_outer.bufferCnt++] = count + (255 - s_dht11_outer.startTime);
			else
				s_dht11_outer.buffer[s_dht11_outer.bufferCnt++] = count - s_dht11_outer.startTime;
		}
		if (s_dht11_outer.bufferCnt == s_bitsCount) {
			dht11_process(&s_dht11_outer);
		}
	}

	if (EXTI_GetITStatus(EXTI_Line3)) {
		EXTI_ClearITPendingBit(EXTI_Line3);
		if (GPIOA->IDR & GPIO_Pin_3) {
			s_dht11_inner.startTime = count;
		} else {
			if (count < s_dht11_inner.startTime)
				s_dht11_inner.buffer[s_dht11_inner.bufferCnt++] = count + (255 - s_dht11_inner.startTime);
			else
				s_dht11_inner.buffer[s_dht11_inner.bufferCnt++] = count - s_dht11_inner.startTime;
		}
		if (s_dht11_inner.bufferCnt == s_bitsCount) {
			dht11_process(&s_dht11_inner);
			s_dht11_inner.bufferCnt = 0;
		}
	}
}
Ejemplo n.º 8
0
CPU_INT32U  OSProbe_TmrRd (void)
{
#if (OS_PROBE_TIMER_SEL == 2)

    return ((CPU_INT32U)TIM_GetCounter(TIM2));

#elif (OS_PROBE_TIMER_SEL == 3)

    return ((CPU_INT32U)TIM_GetCounter(TIM3));

#elif (OS_PROBE_TIMER_SEL == 4)

    return ((CPU_INT32U)TIM_GetCounter(TIM4));

#endif
}
Ejemplo n.º 9
0
/*******************************************************************************
* Function Name  : ENC_Get_Electrical_Angle
* Description    : Returns the absolute electrical Rotor angle 
* Input          : None
* Output         : None
* Return         : Rotor electrical angle: 0 -> 0 degrees, 
*                                          S16_MAX-> 180 degrees, 
*                                          S16_MIN-> -180 degrees                  
*******************************************************************************/
s16 ENC_Get_Electrical_Angle(void)
{
  s32 temp;
  
  temp = (s32)(TIM_GetCounter(ENC_TIMER)) * (s32)(U32_MAX / (4*ENCODER_PPR)); 
  return((s16)(temp/65536)); // s16 result
}
Ejemplo n.º 10
0
/******************角位移******/
s32 ENC_Get_AnglularPosition(void)
{
		s32 temp;
		temp = hEncoder_Revolutions_Num*4*ENCODER_PPR+TIM_GetCounter(ENC_TIMER);
		return (temp*3600/(REDUCTION_RATIO*4*ENCODER_PPR));//单位0.1度
// 	return  temp;
}
Ejemplo n.º 11
0
void dhtRead(u8 * rh, u8 * temp, u8 * checkSum ){
	u8 tmp,j,i,tab[5] = {0x00,0x00,0x00,0x00,0x00};
	dhtGpioOutInit();
	GPIO_ResetBits(GPIOD,GPIO_Pin_7);
	dhtDelay(18000);
	GPIO_SetBits(GPIOD,GPIO_Pin_7);
	dhtDelay(40);
	dhtGpioInInit();

	while(!GPIO_ReadInputDataBit(GPIOD,GPIO_Pin_7));
	while(GPIO_ReadInputDataBit(GPIOD,GPIO_Pin_7));

	for (i = 0; i < 5; ++i) {
		for (j = 0; j < 8; ++j) {
			while(!GPIO_ReadInputDataBit(GPIOD,GPIO_Pin_7));
			TIM_SetCounter(TIM3,0);
			while(GPIO_ReadInputDataBit(GPIOD,GPIO_Pin_7));
			tmp = TIM_GetCounter(TIM3);
			if(tmp<30){// trwanie sygna³u <30us-> 0; ok. 70us -> 1;
				tab[i]=tab[i]<<1;
			}
			else{
				tab[i] = tab[i]<<1;
				tab[i] += 1;
			}
		}
	}
	*rh = tab[0];
	*temp = tab[2];
	*checkSum = tab[4];

}
Ejemplo n.º 12
0
/*
 *berif: Timer interrupt request function
 *param: None
 *reval: None
*/
void TIM2_IRQHandler(void)			//5ms
{
    /* Clear the interrupt pending flag */
    if(TIM_GetITStatus(TIM2, TIM_IT_Update) != RESET)
    {
        TIM_Cmd(TIM4, DISABLE);
        TIM_Cmd(TIM3, DISABLE);
        SpeedL = 30000 - TIM_GetCounter(TIM4);
        SpeedR = 30000 - TIM_GetCounter(TIM3);
        TIM_SetCounter(TIM4, 30000);
        TIM_SetCounter(TIM3, 30000);
        TIM_Cmd(TIM4, ENABLE);
        TIM_Cmd(TIM3, ENABLE);
        TIM_ClearFlag(TIM2, TIM_FLAG_Update);
    }
}
Ejemplo n.º 13
0
uint32_t Timer_Time::current_time(){
	//uint32_t current_time1;
	//uint32_t atual_time = TIM_GetCounter(TIM6);
	//TIM_SetCounter(TIM6, (uint32_t) 0);
	//time_elapsed1 = atual_time - this->last_time;
	//this->last_time = atual_time;
	return TIM_GetCounter(TIM6);
};
Ejemplo n.º 14
0
/**
 * Update our last capture time
 *
 */
void comm_tim_update_capture(void)
{
	comm_tim_data.last_capture_time = TIM_GetCounter(TIM2);
	TIM_SetCompare1(TIM2,
			comm_tim_data.last_capture_time + comm_tim_data.freq);

	OFF(DP_EXT_SCL);
}
Ejemplo n.º 15
0
void delay_core(){
	TIM_SetCounter(TIM2,1);
	TIM_Cmd(TIM2,ENABLE);

	while(TIM_GetCounter(TIM2));

	TIM_Cmd(TIM2,DISABLE);
}
Ejemplo n.º 16
0
/*
*********************************************************************************************************
*	函 数 名: bsp_StartHardTimer
*	功能说明: 使用TIM2-5做单次定时器使用, 定时时间到后执行回调函数。可以同时启动4个定时器,互不干扰。
*             定时精度正负10us (主要耗费在调用本函数的执行时间,函数内部进行了补偿减小误差)
*			 TIM2和TIM5 是32位定时器。定时范围很大
*			 TIM3和TIM4 是16位定时器。
*	形    参: _CC : 捕获通道几,1,2,3, 4
*             _uiTimeOut : 超时时间, 单位 1us.       对于16位定时器,最大 65.5ms; 对于32位定时器,最大 4294秒
*             _pCallBack : 定时时间到后,被执行的函数
*	返 回 值: 无
*********************************************************************************************************
*/
void bsp_StartHardTimer(uint8_t _CC, uint32_t _uiTimeOut, void * _pCallBack)
{
    uint32_t cnt_now;
    uint32_t cnt_tar;

    /*
        执行下面这个语句,时长 = 18us (通过逻辑分析仪测量IO翻转)
        bsp_StartTimer2(3, 500, (void *)test1);
    */
    if (_uiTimeOut < 5)
    {
        ;
    }
    else
    {
        _uiTimeOut -= 5;
    }

    cnt_now = TIM_GetCounter(TIM_HARD);    	/* 读取当前的计数器值 */
    cnt_tar = cnt_now + _uiTimeOut;			/* 计算捕获的计数器值 */
    if (_CC == 1)
    {
        s_TIM_CallBack1 = (void (*)(void))_pCallBack;

        TIM_SetCompare1(TIM_HARD, cnt_tar);      	/* 设置捕获比较计数器CC1 */
        TIM_ClearITPendingBit(TIM_HARD, TIM_IT_CC1);
		TIM_ITConfig(TIM_HARD, TIM_IT_CC1, ENABLE);	/* 使能CC1中断 */

    }
    else if (_CC == 2)
    {
		s_TIM_CallBack2 = (void (*)(void))_pCallBack;

        TIM_SetCompare2(TIM_HARD, cnt_tar);      	/* 设置捕获比较计数器CC2 */
		TIM_ClearITPendingBit(TIM_HARD, TIM_IT_CC2);
		TIM_ITConfig(TIM_HARD, TIM_IT_CC2, ENABLE);	/* 使能CC2中断 */
    }
    else if (_CC == 3)
    {
        s_TIM_CallBack3 = (void (*)(void))_pCallBack;

        TIM_SetCompare3(TIM_HARD, cnt_tar);      	/* 设置捕获比较计数器CC3 */
        TIM_ClearITPendingBit(TIM_HARD, TIM_IT_CC3);
		TIM_ITConfig(TIM_HARD, TIM_IT_CC3, ENABLE);	/* 使能CC3中断 */
    }
    else if (_CC == 4)
    {
        s_TIM_CallBack4 = (void (*)(void))_pCallBack;

        TIM_SetCompare4(TIM_HARD, cnt_tar);      	/* 设置捕获比较计数器CC4 */
		TIM_ClearITPendingBit(TIM_HARD, TIM_IT_CC4);
		TIM_ITConfig(TIM_HARD, TIM_IT_CC4, ENABLE);	/* 使能CC4中断 */
    }
	else
    {
        return;
    }
}
Ejemplo n.º 17
0
/**
 * Record the time of the last commutation
 */
void comm_tim_capture_time(void)
{
	u16 new_time = TIM_GetCounter(TIM2);
	comm_tim_data.prev_time = comm_tim_state.next_prev_time;
	comm_tim_data.curr_time = new_time;
	comm_tim_state.next_prev_time = new_time;
	comm_tim_data.update_count = comm_tim_state.update_count;
	comm_tim_state.update_count = 0;
}
Ejemplo n.º 18
0
//获得runtime时间
//返回值:执行时间,单位:0.1ms,最大延时时间为定时器CNT值的2倍*0.1ms
//需要根据所移植到的MCU的定时器参数进行修改
u32 usmart_get_runtime(void)
{
    if(TIM_GetFlagStatus(TIM4,TIM_FLAG_Update)==SET)//在运行期间,产生了定时器溢出
    {
        usmart_dev.runtime+=0XFFFF;
    }
    usmart_dev.runtime+=TIM_GetCounter(TIM4);
    return usmart_dev.runtime;		//返回计数值
}
Ejemplo n.º 19
0
Archivo: main.c Proyecto: sandves/Sonar
/**
 * Return the pulse width of the signal generated by the HC-SR04.
 * This is easily converted to centimeters on the client side
 * by multiplying the value by 0.01715 and add 0.8 (which is the offset
 * from the drive unit to the surrounding hardware of the ultrasonic sensor).
 */
static uint16_t HC_SR04_read(void) {
	toggleInternalLEDs();

	TIM_SetCounter(TIM2, 0);

	// Send a 15us pulse to the HC-SR04
	GPIO_SetBits(GPIOC, GPIO_Pin_6);
	while(TIM_GetCounter(TIM2) < 15);
	GPIO_ResetBits(GPIOC, GPIO_Pin_6);

	// Read the pulse width returned by the HC-SR04
	while(!GPIO_ReadInputDataBit(GPIOC, GPIO_Pin_7));
	TIM_SetCounter(TIM2, 0);
	while(GPIO_ReadInputDataBit(GPIOC, GPIO_Pin_7));

	// Pulse width of signal
	return TIM_GetCounter(TIM2);
}
Ejemplo n.º 20
0
/**********线位移******/
s32 ENC_Calc_Position(void)
{
		s32 temp;
		float ftemp;
		ftemp=(float)(TIM_GetCounter(ENC_TIMER))/(4*ENCODER_PPR );
		//temp = (s32)(TIM_GetCounter(ENCODER_TIMER ))+ 
		temp= hEncoder_Revolutions_Num*LENGTH/REDUCTION_RATIO  + ftemp*LENGTH/REDUCTION_RATIO ;
	//	temp=(hEncoder_Revolutions_Num*(4*ENCODER_PPR )+(s32)(TIM_GetCounter(ENCODER_TIMER)))/(REDUCTION_RATIO *4*ENCODER_PPR);
		return  temp;
}
Ejemplo n.º 21
0
static void delay_nus(vu32 nCount)
{
  u16 TIMCounter = nCount;
  TIM_Cmd(TIM4, ENABLE);
  TIM_SetCounter(TIM4, TIMCounter);
  while (TIMCounter)
  {
    TIMCounter = TIM_GetCounter(TIM4);
  }
  TIM_Cmd(TIM4, DISABLE);
}
Ejemplo n.º 22
0
/*---------------------------------------------------------------------------*/
void Delay1ms(uint16_t period)
{
  uint16_t start_counter;
  SdkEvalTimersTimConfig(TIM2,32000-1, 0xFFFF); // 1ms @ 32MHz clk
  SdkEvalTimersResetCounter(TIM2);  
  TIM_Cmd(TIM2, ENABLE);
  do { 
    start_counter = TIM_GetCounter(TIM2);
  } while(start_counter < period);
  TIM_Cmd(TIM2, DISABLE);
}
Ejemplo n.º 23
0
static void delay_us(uint16_t count)
{
    uint16_t TIMCounter = count;
    TIM_Cmd(TIM2, ENABLE);
    TIM_SetCounter(TIM2, TIMCounter);
    while (TIMCounter)
    {
        TIMCounter = TIM_GetCounter(TIM2);
    }
    TIM_Cmd(TIM2, DISABLE);
}
Ejemplo n.º 24
0
static void encoder_update(void)
{
	leftPID.PrevEncoder = leftPID.Encoder;
	rightPID.PrevEncoder = rightPID.Encoder;
	
	leftPID.Dir = TIM1->CR1 | 0xFFEF;
	rightPID.Dir = TIM3->CR1 | 0xFFEF;

	leftPID.Encoder = TIM_GetCounter(TIM1);
	rightPID.Encoder = TIM_GetCounter(TIM3);

	if(leftPID.Dir == 0xFFFF)	//向前转
	{
		if(leftPID.Encoder < 1000)
			leftPID.Encoder = 0;
		else
			leftPID.Encoder -= 0x10000;
	}
	else
	{
		if(leftPID.Encoder > 1000)
			leftPID.Encoder = 0;
	}
	
	if(rightPID.Dir == 0xFFEF)	//向前转
	{
		if(rightPID.Encoder > 1000)
			rightPID.Encoder = 0;
	}
	else
	{
		if(rightPID.Encoder < 1000)
			rightPID.Encoder = 0;
		else
			rightPID.Encoder -= 0x10000;
	}
	rightPID.Encoder = -rightPID.Encoder;

	TIM_SetCounter(TIM1, 0);	//清零计数器CNT
	TIM_SetCounter(TIM3, 0);	//清零计数器CNT
}
Ejemplo n.º 25
0
Archivo: adc.c Proyecto: pothub/vim_f4
void int_pre_process()
{
	// 割り込みフラグのクリア
	DMA_ClearITPendingBit(DMAx_StreamY_endADC_autoADC, DMAx_IT_TCIFy_endADC_autoADC);

	// 時間の更新
	static uint16_t value_old = 0;
	subTIM_value = TIM_GetCounter(TIMx_sub_autoADC);
	time_count += (uint16_t)(subTIM_value - value_old);
	value_old = subTIM_value;
	abs_time = time_count / subTIM_period * dt_int;
}
Ejemplo n.º 26
0
/**
 * Update our last capture time and curr time
 *
 */
void comm_tim_update_capture_and_time(void)
{
	comm_tim_data.last_capture_time = TIM_GetCounter(TIM2);
	TIM_SetCompare1(TIM2,
			comm_tim_data.last_capture_time + comm_tim_data.freq);

	comm_tim_data.prev_time = comm_tim_state.next_prev_time;
	comm_tim_data.curr_time = comm_tim_data.last_capture_time;
	comm_tim_state.next_prev_time = comm_tim_data.last_capture_time;
	comm_tim_data.update_count = comm_tim_state.update_count;
	comm_tim_state.update_count = 0;
}
Ejemplo n.º 27
0
void calcSpeed()
{
	
	int temp[3]={0,0,0};     
	temp[0]=TIM_GetCounter(TIM3);
	temp[1]=TIM_GetCounter(TIM4);
	temp[2]=TIM_GetCounter(TIM5);
	// printf("counter0=%d\n",temp[0]);
	// printf("counter1=%d\n",temp[1]);
	// printf("counter2=%d\n",temp[2]);
	// //
	for(int i=0;i<3;i++)
	{
       TIM_counter[i][1]=temp[i];
	   if((abs(temp[i]-TIM_counter[i][0])<50000)){
	   		RealSpeed[i]=TIM_counter[i][1]-TIM_counter[i][0];	
	   }
	   TIM_counter[i][0]=TIM_counter[i][1];
    }

}
Ejemplo n.º 28
0
//延时的方式读取距离值
void GetDistanceDelay(void)
{
		//发送触发信号
		Trig_SET;
		status = 1;
	TIM_SetCounter(TIM4, 0);
	TIM_Cmd(TIM4, ENABLE);//打开定时器
		while(TIM_GetCounter(TIM4) < 11);//延时超过10us
		status = 2;
		Trig_RESET;
		TIM_SetCounter(TIM4, 0);
	
		while(Echo == 0)//等待回向信号起始位置
		{
			if(TIM_GetCounter(TIM4) > 60000)//回应时间超出正常范围 
			{
				status = 0;
				distance_cm = 0;//失败后就后退
				TIM_Cmd(TIM4, DISABLE);//关闭定时器
				return;//本次失败
			}
		}
		TIM_SetCounter(TIM4, 0);
		while(Echo == 1)//开始计算长度
		{
			int count = TIM_GetCounter(TIM4);
			if(count > 60000)//回应时间超出正常范围 
			{
				status = 0;
				distance_cm = 0;//失败后就后退
				TIM_Cmd(TIM4, DISABLE);//关闭定时器
				return;//本次失败
			}
		}
		dis_count = TIM_GetCounter(TIM4);
		TIM_Cmd(TIM4, DISABLE);//关闭定时器
		distance_cm = (unsigned int)(((long)(dis_count) * 34)/2000);//声速340m/s
		status = 0;//准备下次发送		
}
Ejemplo n.º 29
0
/*
	函数功能:定时器6延时函数(x)us级别
*/
void Delay_us(u32 timer)
{
	TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
	/*配置相关寄存器*/
	TIM_TimeBaseStructure.TIM_Period = timer;
	TIM_TimeBaseStructure.TIM_Prescaler = 72-1;
	TIM_TimeBaseStructure.TIM_ClockDivision = 0x0;
	TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
	TIM_TimeBaseInit(TIM6, & TIM_TimeBaseStructure);
	/*使能定时器*/
	TIM_Cmd(TIM6, ENABLE);
	while(TIM_GetCounter(TIM6)!= timer);/*等待*/
	TIM_Cmd(TIM6, DISABLE);		//关闭定时器6
}
Ejemplo n.º 30
0
void TIM3_IRQHandler(void)
{  
  uint16_t tmp;
  rt_interrupt_enter();
  if (TIM_GetITStatus(TIM3,TIM_IT_CC3)==SET)
  {
    TIM_ClearITPendingBit(TIM3,TIM_IT_CC3);
    tmp=TIM_GetCounter(TIM3);
    IRInt=tmp>LastTS?tmp-LastTS:tmp+59999-LastTS;
    LastTS=tmp;
    rt_mq_send(ir_mq,&IRInt,sizeof(uint16_t));
  }
  rt_interrupt_leave();
}