2016-11-17 05:26 AM
I comunicate stm32f4dis with hc sr04 but always
distance_mm=0,
PB1-Trig PB0-Echo#include ''stm32f4xx.h''
GPIO_InitTypeDef GPIO_InitStructure;
void
GPIO_Configuration(
void
);
void
Delay(__IO uint32_t nCount);
volatile
float
distance_mm;
int
main(
void
)
{
GPIO_Configuration();
GPIO_ResetBits(GPIOB,GPIO_Pin_1);
while
(1)
{
float
time
;
int
i=0;
GPIO_SetBits(GPIOB,GPIO_Pin_1);
Delay(120);
GPIO_ResetBits(GPIOB,GPIO_Pin_1);
while
(GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_0)==0);
while
(GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_0)) i++;
time
= (
float
)i/9500000;
distance_mm = (
time
*344)*1000/2;
Delay(1440000);
}
}
void
GPIO_Configuration(
void
)
{
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE);
/* Configure PB1-Trigger in output pushpull mode */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_Init(GPIOB, &GPIO_InitStructure);
/* Configure PB0-Echo in input mode */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_Init(GPIOB, &GPIO_InitStructure);
}
void
Delay(__IO uint32_t nCount)
{
while
(nCount--)
{
}
}
#ifdef USE_FULL_ASSERT
void
assert_failed(uint8_t* file, uint32_t line)
{
/* User can add his own implementation to report the file name and line number,
ex: printf(''Wrong parameters value: file %s on line %d\r\n'', file, line) */
/* Infinite loop */
while
(1)
{
}
}
#endif
#clive1