STM32 MCU learning about ultrasound module (2)

Code explained

#include "BSP_TIM.h"
extern uint16_t msHcCount = 0; 
void TIM_NVIC_Config(void)
{
    NVIC_InitTypeDef NVIC_InitStructure; 
    NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);		
    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);
}

void HYSRF05_Init(void)
	{
	    TIM_TimeBaseInitTypeDef TIM_Init_structure;
	 
	GPIO_InitTypeDef GPIO_InitStructer;
	RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);
	RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOB, ENABLE);
	/*TRIG  */
GPIO_InitStructer.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_InitStructer.GPIO_Mode=GPIO_Mode_Out_PP;
GPIO_InitStructer.GPIO_Pin=GPIO_Pin_10;
GPIO_Init(GPIOB, &GPIO_InitStructer);

    /*ECOH   */
	GPIO_InitStructer.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_InitStructer.GPIO_Mode=GPIO_Mode_IN_FLOATING;
GPIO_InitStructer.GPIO_Pin=GPIO_Pin_11;
GPIO_Init(GPIOB, &GPIO_InitStructer);
	
	    
      TIM_Init_structure.TIM_Period=999;
      TIM_Init_structure.TIM_Prescaler=71;
	    TIM_Init_structure.TIM_CounterMode=TIM_CounterMode_Up;
	    TIM_Init_structure.TIM_ClockDivision=TIM_CKD_DIV1;
	    TIM_Init_structure.TIM_RepetitionCounter=0;
	    TIM_TimeBaseInit(TIM2,&TIM_Init_structure);
	
	
	TIM_ClearFlag(TIM2,TIM_FLAG_Update);
	TIM_ITConfig(TIM2, TIM_IT_Update, ENABLE);
	TIM_NVIC_Config();
	TIM_Cmd(TIM2, DISABLE);
	}

static void OpenTimerForHc(void)  
{
   TIM_SetCounter(TIM2,0);
   msHcCount = 0;
   TIM_Cmd(TIM2, ENABLE); 
}



static void CloseTimerForHc(void)    
{
   TIM_Cmd(TIM2, DISABLE); 
}                            


uint32_t GetEchoTimer(void)
{
   u32 t1 = 0;
   t1 = msHcCount*1000;
   t1 += TIM_GetCounter(TIM2);
  TIM2->CNT = 0;  
   Delay_Ms(50);
   return t1;
}

float Hysrf05GetLength(void)
{
   uint32_t t2 = 0;
	uint32_t t = 0;
   int i = 0;
   float lengthTemp = 0;
   float sum = 0;
	while(i!=5){
	GPIO_WriteBit(GPIOC,GPIO_Pin_6, Bit_SET);
	Delay_Us(20);
	GPIO_WriteBit(GPIOC,GPIO_Pin_6, Bit_RESET);
	while(GPIO_ReadInputDataBit(GPIOC, GPIO_Pin_7)==RESET);
	OpenTimerForHc() ;
	i++;
	while(GPIO_ReadInputDataBit(GPIOC, GPIO_Pin_7)==SET);
		t = TIM_GetCounter(TIM2);     
		CloseTimerForHc();
	t2=TIM_GetCounter(TIM2);
	 lengthTemp = ((float)t2/58);//cm
     sum = lengthTemp + sum ;}
   lengthTemp = sum/5.0;
		 return lengthTemp;
	}	

1, the first extern uint16_t msHcCount, extern keyword is a statement on behalf of msHcCount as a global variable, this is the number of timer overflow representatives. My last blog illustrates the calculation time of the timer, as with the number of times that the timer overflow interrupt service routine must ultimately configure the timer interrupt configuration and timer.
Interrupt service routine
extern uint16_t msHcCount; void TIM2_IRQHandler(void) //TIM2 { if (TIM_GetITStatus(TIM2, TIM_IT_Update) != RESET) //jian cha geng xin zhong duan fa sheng yu fou { TIM_ClearITPendingBit(TIM2, TIM_IT_Update ); // qing chu geng xin zhong duan biao zhi wei msHcCount++; } }
because this text blogger comments I changed the words, some programs can not be executed, I might keil5 some software problems.
2, on the original code has a place that bothers me a long time, do not know is how to understand. while(GPIO_ReadInputDataBit(GPIOC, GPIO_Pin_7)==RESET); OpenTimerForHc() ; i=i+1; while(GPIO_ReadInputDataBit(GPIOC, GPIO_Pin_7)==SET); CloseTimerForHc();
I was wondering why is equal to 0 when it began to turn the timer should not be equal to the SET and RESET of the timer starts to open when the timer is turned off
then I deliberately went to check usage while statement
when the while statement is while (condition); when such a case, after the first added while the semicolon (;) representative of the idle loop, if the actually detected is low, if so, has been in circulation. The same is true of the back

Title while (statement); that statement is true behind the program statement is not executed

This can be a reasonable explanation, while not the first SET statement did not execute down

Next I tell you about and explain extern configuration and function of the serial port, while the microcontroller usage summary

Released two original articles · won praise 0 · Views 264

Guess you like

Origin blog.csdn.net/weixin_45749794/article/details/104651974