Forum: Mikrocontroller und Digitale Elektronik HC-SR04 mit Atmega8


von Lars L. (Firma: Privat) (lars86)


Lesenswert?

Hallo,

versuche leider vergebens den HC-SR04 mit dem Atmega zu verbinden bzw 
anzusteuern.

Möchte eigentlich erstmal nur eine LED Leuchten lassen, wenn eine 
ENtfernung unterschritten wird.
Kann sich den Code mal wer anschauen?
Wo liegt der Fehler?

Danke.


1
/*
2
 * Ultraschall.c
3
 *
4
 * Created: 16.08.2014 17:21:46
5
 *  Author: Pi
6
 */ 
7
/*Sensor | MCU
8
_____________
9
Trig   | PC0
10
Echo   | PC1
11
*/
12
#include <avr/io.h>
13
//#include <util/delay.h>
14
15
#define US_PORT PORTC
16
#define  US_PIN  PINC
17
#define US_DDR   DDRC
18
19
#define US_TRIG_POS  PC0
20
#define US_ECHO_POS  PC1
21
22
23
#define US_ERROR    -1
24
#define  US_NO_OBSTACLE  -2
25
26
void HCSR04Init();
27
void HCSR04Trigger();
28
29
void HCSR04Init()
30
{
31
  US_DDR|=(1<<US_TRIG_POS);
32
}
33
34
void HCSR04Trigger()
35
{
36
  //Send a 10uS pulse on trigger line
37
  
38
  US_PORT|=(1<<US_TRIG_POS);  //high
39
  
40
  //_delay_us(15);        //wait 15uS
41
  
42
  US_PORT&=~(1<<US_TRIG_POS);  //low
43
}
44
45
uint16_t GetPulseWidth()
46
{
47
  uint32_t i,result;
48
49
  //Wait for the rising edge
50
  for(i=0;i<600000;i++)
51
  {
52
    if(!(US_PIN & (1<<US_ECHO_POS)))
53
    continue;  //Line is still low, so wait
54
    else
55
    break;    //High edge detected, so break.
56
  }
57
58
  if(i==600000)
59
  return US_ERROR;  //Indicates time out
60
  
61
  //High Edge Found
62
63
  //Setup Timer1
64
  TCCR1A=0X00;
65
  TCCR1B=(1<<CS11);  //Prescaler = Fcpu/8
66
  TCNT1=0x00;      //Init counter
67
68
  //Now wait for the falling edge
69
  for(i=0;i<600000;i++)
70
  {
71
    if(US_PIN & (1<<US_ECHO_POS))
72
    {
73
      if(TCNT1 > 60000) break; else continue;
74
    }
75
    else
76
    break;
77
  }
78
79
  if(i==600000)
80
  return US_NO_OBSTACLE;  //Indicates time out
81
82
  //Falling edge found
83
84
  result=TCNT1;
85
86
  //Stop Timer
87
  TCCR1B=0x00;
88
89
  if(result > 60000)
90
  return US_NO_OBSTACLE;  //No obstacle
91
  else
92
  return (result>>1);
93
}
94
95
96
97
98
int main(void)
99
{
100
  uint16_t r;
101
  int d;
102
  //_delay_ms(100);
103
  
104
  HCSR04Init();
105
  
106
  DDRB |= (1 << PB0);
107
  
108
  
109
    while(1)
110
    {
111
        //Send a trigger pulse
112
    HCSR04Trigger(); 
113
    r=GetPulseWidth();  //Measure the width of pulse
114
    
115
    d=(r/58.0);
116
    
117
    if (d<5)
118
    {
119
      PORTB |= (1 << PB0);
120
    } 
121
    else
122
    {
123
      PORTB |= (1 << PB1);
124
    }
125
    }
126
}

von Karl (Gast)


Lesenswert?

Was ist ein HC-SR04 ?

von Lars L. (Firma: Privat) (lars86)


Lesenswert?

Ultraschallsensor

von Nox (Gast)


Lesenswert?

hey,
habe das gleiche vor darum würde mich interessieren ob du eine Lösung 
für dein Problem gefunden hast

von J. D. (jch)


Lesenswert?

Hat jemand den Fehler gefunden?
Ich habe den gleichen code, aber leider funktioniert dieser nicht.

von Wolfgang (Gast)


Lesenswert?

Mindestens der Triggerpuls ist zu kurz.

von horst (Gast)


Lesenswert?

Lars L. schrieb:
> //Send a 10uS pulse on trigger line
>
>   US_PORT|=(1<<US_TRIG_POS);  //high
>
>   //_delay_us(15);        //wait 15uS
>
>   US_PORT&=~(1<<US_TRIG_POS);  //low

Also das ist schonmal unklar

von horst (Gast)


Lesenswert?

Und wo wird die LED wieder abgeschaltet, wenn sie einmal aktiviert 
wurde?

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.