Stm32F4 ile HC-SR04 mesafe ölçer sorunu

Başlatan erkinkent, 20 Ocak 2015, 21:47:52

erkinkent

Mesafe bilgisi Lcd de görüntülenmiyor

#include "stm32f4xx.h"
#include "defines.h"
#include "tm_stm32f4_hcsr04.h"
static __IO uint32_t TimingDelay;
void Delay(__IO uint32_t nTime);
static __IO uint32_t TimingDelayus=0;
void Delayus(__IO uint32_t nTime);

TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;

  #define RS GPIO_Pin_13
  #define RW GPIO_Pin_14
  #define EN GPIO_Pin_15

  #define HCSR04_TRIGGER_RCC        RCC_AHB1Periph_GPIOB
  #define HCSR04_TRIGGER_PORT       GPIOB
  #define HCSR04_TRIGGER_PIN        GPIO_Pin_2

  #define HCSR04_ECHO_RCC           RCC_AHB1Periph_GPIOB
  #define HCSR04_ECHO_PORT          GPIOB
  #define HCSR04_ECHO_PIN           GPIO_Pin_3

  #define HCSR04_NUMBER         ((float)0.0171821)

extern uint8_t TM_HCSR04_Init(void);
extern float TM_HCSR04_Read(void);

void Init_GPIO(void)

  GPIO_InitTypeDef GPIO_InitStructure;
  RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB | RCC_AHB1Periph_GPIOC | RCC_AHB1Periph_GPIOE, ENABLE);

  GPIO_InitStructure.GPIO_Pin =GPIO_Pin_13 | GPIO_Pin_14 | GPIO_Pin_15;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
  GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
  GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
  GPIO_Init(GPIOC, &GPIO_InitStructure);

  GPIO_InitStructure.GPIO_Pin =GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_2 | GPIO_Pin_3 | GPIO_Pin_4 | GPIO_Pin_5 | GPIO_Pin_6 | GPIO_Pin_7;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
  GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
  GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
  GPIO_Init(GPIOE, &GPIO_InitStructure);
}
uint8_t TM_HCSR04_Init(void)
{
        RCC_AHB1PeriphClockCmd(HCSR04_TRIGGER_RCC | HCSR04_ECHO_RCC , ENABLE);
   GPIO_InitTypeDef GPIO_InitStructure;
   
   GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
   GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_DOWN;
   GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
   
   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
   GPIO_InitStructure.GPIO_Pin = HCSR04_TRIGGER_PIN;
   GPIO_Init(HCSR04_TRIGGER_PORT, &GPIO_InitStructure);
   
   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN;
   GPIO_InitStructure.GPIO_Pin = HCSR04_ECHO_PIN;
   GPIO_Init(HCSR04_ECHO_PORT, &GPIO_InitStructure);
   
   HCSR04_TRIGGER_LOW;
   
//   if (TM_HCSR04_Read() >= 0)
//        {
//      return 1;
//   }
   return 0;
}
float TM_HCSR04_Read(void)
{
   uint32_t time, timeout;
   float dist;
   HCSR04_TRIGGER_LOW;
   Delayus(2);
   HCSR04_TRIGGER_HIGH;
   Delayus(10);
   HCSR04_TRIGGER_LOW;
   
   timeout = HCSR04_TIMEOUT;
   while (HCSR04_ECHO_CHECK == Bit_RESET)
        {
      if (timeout-- == 0x00)
                {
         return -1;
        }
     }   
   time = 0;
   while (HCSR04_ECHO_CHECK == Bit_SET)
        {
      time++;
      Delayus(1);
   }
   
   dist = (float)time * HCSR04_NUMBER * 10;
   return dist;
}
void INTTIM_Config(void)
{
  NVIC_InitTypeDef NVIC_InitStructure;
 
  NVIC_InitStructure.NVIC_IRQChannel = TIM3_IRQn;
  NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
  NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
  NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  NVIC_Init(&NVIC_InitStructure);
   
  RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);
   
  TIM_TimeBaseStructure.TIM_Period = ((SystemCoreClock / 4) / 1000000) - 1;  // 1 MHz (1us)
  TIM_TimeBaseStructure.TIM_Prescaler = 0;
  TIM_TimeBaseStructure.TIM_ClockDivision = 0;
  TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
  TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure);
 
  TIM_ITConfig(TIM3, TIM_IT_Update, ENABLE);
  TIM_Cmd(TIM3, ENABLE);
}
void s_init()
{
  GPIOC->BSRRL=0;
  GPIOC->BSRRH=RS;
  GPIOC->BSRRH=RW;
}
void s_data()
{
  GPIOC->BSRRL=RS;
  GPIOC->BSRRH=RW;
}
void s_latch()
{
  GPIOC->BSRRL=EN;
  Delay(10);
  GPIOC->BSRRL=0x0000;
  GPIOC->BSRRH=EN;
  Delay(10);
}
void LCD_Init()
{
  GPIOC->BSRRL=0;
  GPIOC->BSRRH=RS;
  GPIOC->BSRRH=RW;
  GPIOC->BSRRL=EN;
  Delay(10);

  s_init();
  GPIOE->ODR=0x0001;
  s_latch();
 
  GPIOE->ODR=0x0038;
  s_latch();
  s_latch();
  s_latch();
  s_latch();
 
  GPIOE->ODR=0x000E;
  s_latch();
 
  GPIOE->ODR=0x0010;
  s_latch();
 
  GPIOE->ODR=0x0006;
  s_latch();
}
void LCD_Birinci_Satira_Yaz(char *a)
{
  s_init();
  GPIOE->ODR=0x0002;
  s_latch();
     
  s_data();
  for(int k=0;a[k];k++)
  {
    GPIOE->ODR=a[k];
    s_latch();
  }
}
void LCD_Ikinci_Satira_Yaz(char *b)
{
  s_init();
  GPIOE->ODR=0x00C0;
  s_latch();
 
  s_data();
  for(int k=0;b[k];k++)
  {
    GPIOE->ODR=b[k];
    s_latch();
  }
}
int main(void)
{     
    char a[]="HC-SR04 UYGULAMA";
    char b[]="*ALANA ** GIRIN*";
    float distance, old_distance;
    Init_GPIO();
    INTTIM_Config();
    TM_HCSR04_Init();   
   
   
   
   
   
    old_distance=0;
   
     if (SysTick_Config(SystemCoreClock / 1000))
     {
      while (1);
     }   
     
    LCD_Init();
    LCD_Birinci_Satira_Yaz(a);
    LCD_Ikinci_Satira_Yaz(b);   
    TM_HCSR04_Read();
    while (1)
    {
        distance = TM_HCSR04_Read();
        if (old_distance!=distance)
        {
          if(0 < distance < 4000)
          {
            a[0]='M';a[1]='E';a[2]='S';a[3]='A';a[4]='F';a[5]='E';a[6]=':';
            a[7]= (int)(distance/1000)+'0';
            a[8]= (((int)distance)%1000)/100+'0';
            a[9]= (((int)distance)%100)/10+'0';
            a[10]=(((int)distance)%10)+'0';
            a[11]=' ';
            a[12]='m';a[13]='m';
            a[14]=' ';a[15]=' ';
            LCD_Birinci_Satira_Yaz(a);
          }         
          else
          {
            b[0]='U';b[1]='Y';b[2]='A';b[3]='R';b[4]='I';
            b[5]=' ';b[6]='A';b[7]='L';b[8]='A';b[9]='N';
            b[10]=' ';b[11]='D';b[12]='I';b[13]='S'; b[14]='I';b[15]=' ';           
            LCD_Ikinci_Satira_Yaz(b);
          }
          old_distance=distance;
        }
    }
}
void Delay(__IO uint32_t nTime)
{
  TimingDelay = nTime;
  while(TimingDelay != 0);
}
void TimingDelay_Decrement(void)
{
  if (TimingDelay != 0x00)
  {
    TimingDelay--;
  }
}
void Delayus(__IO uint32_t nTime)
{
  TimingDelayus = nTime;
  while(TimingDelayus != 0);
}
void TimingDelayus_Decrement(void)
{
  if (TimingDelayus != 0x00)
  {
    TimingDelayus--;
  }
}
#ifdef  USE_FULL_ASSERT
void assert_failed(uint8_t* file, uint32_t line)
{
  while (1)
  {
  }
}
#endif

balance