Stm32f4 ile Hc-Sr04 Kullanımı

Başlatan buraksa, 01 Ağustos 2013, 00:19:40

buraksa

Merhaba;
Forumda yeniyim ve stm32f4 ile basit bir uzaklık ölçme işlemi yapmak istiyorum.Kodum rahatça derleniyor lakin uygulamada istediğim çıkışı vermiyor.Kodum biraz acemice olabilir,yardım ederseniz çok sevinirim.


------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
#include "stm32f4xx.h"
#include <math.h>
#include "delay.h"

//genel yapi tanimlari
GPIO_InitTypeDef GPIO_InitStructure;
TIM_TimeBaseInitTypeDef   TIM_TimeBaseStructure;

// degisken tanimlamalari yapilir
uint32_t result=0;
uint32_t distance=0;
uint32_t TIM=0;

//fonksiyon tanimlamalari yapilir.
void gpio_setup(void);
void timer(void);
float Sensor_using(void);

//ana fonksiyon
int main(void)
{
   gpio_setup();
   timer();
   Sensor_using();
   GPIO_ResetBits(GPIOD,GPIO_Pin_12|GPIO_Pin_15);
 
   
//sonsuz donguye girilir.
   while(1)
   {
   result=Sensor_using();
   distance=(result)*10/58;
   if(distance>=0 && distance<60)
   {
     GPIO_SetBits(GPIOD,GPIO_Pin_12);
     GPIO_ResetBits(GPIOD,GPIO_Pin_15);
   }
   else
   {
     GPIO_SetBits(GPIOD,GPIO_Pin_15);
     GPIO_ResetBits(GPIOD,GPIO_Pin_12);
   }
   }   
}

//uzaklik bulan fonksiyonumuzu yazalim
float Sensor_using(void)
{
  GPIO_SetBits(GPIOB,GPIO_Pin_6);
  delay_us(10);
  GPIO_ResetBits(GPIOB,GPIO_Pin_6);
 
   TIM_ClearFlag(TIM5, TIM_IT_Update);
   TIM_SetCounter(TIM5, 0);
   
   TIM_Cmd(TIM5, ENABLE);
  while(GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_7)==0 && TIM_GetFlagStatus(TIM5, TIM_FLAG_Update) == RESET);
   TIM_Cmd(TIM5, DISABLE);
   
   if(TIM_GetFlagStatus(TIM5, TIM_FLAG_Update) != RESET)
   {
      TIM_ClearFlag(TIM5, TIM_IT_Update);
      TIM_SetCounter(TIM5, 0);
   }
   
   TIM_Cmd(TIM5, ENABLE);
  while(GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_7)==1 && TIM_GetFlagStatus(TIM5, TIM_FLAG_Update) == RESET);
   TIM_Cmd(TIM5, DISABLE);
   
   if(TIM_GetFlagStatus(TIM5, TIM_FLAG_Update) != RESET)
   {
      TIM_ClearFlag(TIM5, TIM_IT_Update);
      TIM_SetCounter(TIM5, 0);
   }
   else
   {
   TIM=TIM_GetCounter(TIM5);
  }
  return(TIM);
}

//echo pininin ne kadar 1 oldugunu bulmak üzere bir timer kurulur.
void timer(void)
{
float PrescalerValue=0;
  /* TIM5 clock enable */
   RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM5, ENABLE);
   PrescalerValue = (uint16_t) ((SystemCoreClock / 2) / 8000000) - 1;            //each 10us a step
   TIM_TimeBaseStructure.TIM_Period = 8000;                     //1000 = 1ms
   TIM_TimeBaseStructure.TIM_Prescaler = PrescalerValue;         // PrescalerValue = 0
   TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;
   TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
   TIM_TimeBaseInit(TIM5, &TIM_TimeBaseStructure);
   TIM_PrescalerConfig(TIM5, PrescalerValue, TIM_PSCReloadMode_Immediate);     
   TIM_ARRPreloadConfig(TIM5, ENABLE);
   /* TIM5 TRGO selection */
   TIM_SelectOutputTrigger(TIM5, TIM_TRGOSource_Update);
   TIM5->SR = 0;               
   TIM_ITConfig(TIM5, TIM_IT_Update, ENABLE);
   /* TIM5 enable counter */
   TIM_Cmd(TIM5, DISABLE);
}





//port ayarlari yapilir.
void gpio_setup(void)
{
   RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD|RCC_AHB1Periph_GPIOB, ENABLE);

  //echo B7
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN;
  GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_25MHz;
  GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
   GPIO_Init(GPIOB, &GPIO_InitStructure);
   
   //trig B6
   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
  GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_25MHz;
  GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
   GPIO_Init(GPIOB, &GPIO_InitStructure);
   
   //cikis pinleri
   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12|GPIO_Pin_15;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
  GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_25MHz;
  GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
   GPIO_Init(GPIOD, &GPIO_InitStructure);
}

-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------