Forum: Mikrocontroller und Digitale Elektronik HC-SR04 Atmega644PA


von Nox B. (Firma: Privat) (biscuit)


Lesenswert?

hey
ich versuche momentan mithilfe eines ATmega644Pa und einem HC-SR04 eine 
LED zum leuchten zu bringen wenn eine gewisse Distanz unterschritten 
wird, bei durchgehender messung

probiert hab ich es mit diesem Code

im ergebnis findet er immer sofort ein Hindernis unabhängig davon ob nun 
irgendwo etwas zu finden ist oder auch nicht

für hilfe wäre ich wirklich dankbar
grüße biscuit


#define F_CPU 16000000
#include <avr/io.h>
#include <avr/interrupt.h>
#include <util/delay.h>

#define ERROR    -1
#define  NO_OBSTACLE  -2

void HCSR04Init()
{

}

void HCSR04Trigger()
{
  PORTC |= (1 << PC0);  //high
  _delay_us(15);      //wait 15uS
  PORTC &= ~(1<<PC0);    //low
}

int GetPulseWidth()
{
  int i, result;

  //Wait for rising edge
  for(i = 0; i < 60; i++)
  {
    if(!(PINC & (1 << PC1)))
    {
      continue;  //Line is still low, so wait
    }
    else
    {
      break;    //High edge detected, so break.
    }
  }
  if(i == 60)
  {
      return ERROR;  //Indicates time out
  }

  //High Edge Found

  //Setup Timer1
  TCCR1A = 0x00;
  TCCR1B = (1 << CS11);  //Prescaler = Fcpu/8
  TCNT1 = 0x00;      //Init counter

  //Now wait for the falling edge
  for(i=0;i<60;i++)
  {
    if(PINC & (1<<PC1))
    {
      if(TCNT1 > 60000)
      {
        break;
      }
      else
      {
        continue;
      }
    }
    else
    {
      break;
    }
  }

  if(i==60)
  {
    return NO_OBSTACLE;  //Indicates time out
  }

  //Falling edge found

Bitte melde dich an um einen Beitrag zu schreiben. Anmeldung ist kostenlos und dauert nur eine Minute.
Bestehender Account
Schon ein Account bei Google/GoogleMail? Keine Anmeldung erforderlich!
Mit Google-Account einloggen
Noch kein Account? Hier anmelden.