Home > other >  'just to don't understand the new bosses to help take a look at this HC_SR04 code
'just to don't understand the new bosses to help take a look at this HC_SR04 code

Time:09-24

//the main c
# include "stm32f10x. H"
# include "sys. H"
# include "delay. H"
# include "hc_sr04. H"
# include "oled. H"

Extern under-16 TIM4_STA;
Extern u32 TIM4_VAL;


Int main (void)
{
The float length;
Hcsr04Init ();
Delay_init ();
OLED_Init ();
OLED_Clear ();
While (1)
{
PBout (11)=1;
Delay_us (20);
PBout (11)=0;
If (TIM4_STA & amp; 0 x80)
{
Length=TIM4_VAL/1000000 * 170;
OLED_Clear ();
OLED_ShowFloat (3, 3, length, 6);
}

Delay_ms (10000);

}


}

//hc_sr04 c
# include "hc_sr04. H"


# define HCSR04_PORT GPIOB

# define HCSR04_PORTC_CLK_FUN RCC_APB2PeriphClockCmd

# define HCSR04_CLK RCC_APB2Periph_GPIOB

# define HCSR04_TRIG GPIO_Pin_5

# define HCSR04_ECHO GPIO_Pin_6



Void gpioinit (void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd (HCSR04_CLK, ENABLE);

GPIO_InitStructure. GPIO_Pin=HCSR04_TRIG;

GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;

GPIO_InitStructure. GPIO_Mode=GPIO_Mode_Out_PP;

GPIO_Init (HCSR04_PORT, & amp; GPIO_InitStructure);

GPIO_ResetBits (HCSR04_PORT HCSR04_TRIG);



GPIO_InitStructure. GPIO_Pin=HCSR04_ECHO;

GPIO_InitStructure. GPIO_Mode=GPIO_Mode_IN_FLOATING;

GPIO_Init (HCSR04_PORT, & amp; GPIO_InitStructure);

GPIO_ResetBits (HCSR04_PORT HCSR04_ECHO);



}


Void Hcsr04Init (void)

{

TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;

NVIC_InitTypeDef NVIC_InitStructure;
TIM_ICInitTypeDef TIM_ICInitStruct;
Gpioinit ();

//TIM_TimeBaseStructure.

RCC_APB1PeriphClockCmd (RCC_APB1Periph_TIM4, ENABLE);



TIM_DeInit (TIM4);

TIM_TimeBaseStructure. TIM_Period=0 XFFFF;

TIM_TimeBaseStructure. TIM_Prescaler=71;

TIM_TimeBaseStructure.TIM_ClockDivision=TIM_CKD_DIV1;

TIM_TimeBaseStructure.TIM_CounterMode=TIM_CounterMode_Up;

TIM_TimeBaseInit (TIM4, & amp; TIM_TimeBaseStructure);

TIM_ARRPreloadConfig (TIM4, DISABLE);

TIM_ICInitStruct. TIM_Channel=TIM_Channel_1;
TIM_ICInitStruct. TIM_ICPolarity=TIM_ICPolarity_Rising;
TIM_ICInitStruct. TIM_ICSelection=TIM_ICSelection_DirectTI;
TIM_ICInitStruct. TIM_ICPrescaler=TIM_ICPSC_DIV1;
TIM_ICInitStruct. TIM_ICFilter=0 x00;
TIM_ICInit (TIM4, & amp; TIM_ICInitStruct);

NVIC_InitStructure. NVIC_IRQChannel=TIM4_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=0;
NVIC_InitStructure. NVIC_IRQChannelSubPriority=0;
NVIC_InitStructure.NVIC_IRQChannelCmd=ENABLE;
NVIC_Init (& amp; NVIC_InitStructure);


TIM_ClearFlag (TIM4, TIM_FLAG_Update | TIM_IT_CC1);
TIM_ITConfig (TIM4, TIM_IT_Update | TIM_IT_CC1, ENABLE);

TIM_Cmd (TIM4, ENABLE);

}




Under-16 TIM4_STA=0;
U32 TIM4_VAL=0;

Void TIM4_IRQHandler (void)
{
If (TIM_GetITStatus (TIM4, TIM_IT_Update)!=RESET)
{
}

If (TIM_GetITStatus (TIM4, TIM_IT_CC1)!=RESET)
{
If (TIM4_STA & amp; 0 x40)
{
TIM4_STA |=0 x80;
TIM4_VAL=TIM_GetCapture1 (TIM4);
TIM_OC1PolarityConfig (TIM4, TIM_ICPolarity_Rising);
} else {
TIM4_STA=0;
TIM4_VAL=0;
TIM_SetCounter (TIM4, 0);
TIM4_STA |=0 x40;
TIM_OC1PolarityConfig (TIM4, TIM_ICPolarity_Falling);

}
}
TIM_ClearITPendingBit (TIM4, TIM_IT_CC1 | TIM_IT_Update);
}





Hc_sr04. H
# # ifndef __HC_SR04_H
# define __HC_SR04_H
# include "sys. H"
# include "stm32f10x. H"
# include "stdlib. H"
# include "delay. H"



Void Hcsr04Init (void);
Void TIM4_IRQHandler (void);
# endif


Operation has been the result of the jump and not a right, where there is a problem oh,

CodePudding user response:

Timer period is set to 5000 prescaler is set to 0, TIM interrupt function is set to take a look at below,

Void TIM4_IRQHandler (void)//TIM3 interrupt
{
If (TIM_GetITStatus (TIM4, TIM_IT_Update)!=RESET)//check the TIM3 update interrupt occurs or not
{
TIM_ClearITPendingBit (TIM4, TIM_IT_Update);//remove TIMx update interrupt flag
MsHcCount++;
}
}

CodePudding user response:

@

reference 1st floor SeanOY response:
timer period is set to 5000 set to 0 prescaler, TIM interrupt function is set to take a look at below,

Void TIM4_IRQHandler (void)//TIM3 interrupt
{
If (TIM_GetITStatus (TIM4, TIM_IT_Update)!=RESET)//check the TIM3 update interrupt occurs or not
{
TIM_ClearITPendingBit (TIM4, TIM_IT_Update);//remove TIMx update interrupt flag
MsHcCount++;
}
}
precaler to 0 if he what frequency,,

CodePudding user response:

Frequency division 1, fixed duration=(PSC + 1) + 1) (PERIOD/f, f main frequency
  • Related