Ultraschallmessung atmega328 & atmega2560

Gast #6376151
Lesenswert?
• ▲
▼
Hallo Leute,

ich habe einen Aufbau mit einem atmega328 und HC-SR04 als 
Ultraschallsensor. Den source habe ich von
https://www.electronicwings.com/avr-atmega/ultrasonic-module-hc-sr04-interfacing-with-atmega1632

mit dem 328 passt die Entfernung.

nun nimm ich einen atmega2560 und stelle alles auf den timer4 um.
Hier passt meine Entfernung nicht mehr. Statt 15cm erhalte ich jetzt nur 
noch knapp 13.
Wo ist denn der Unterschied zwischen den beiden? Es sind doch beide 
16bit Timer somit kann ich alles doch übernehmen oder liege ich da 
falsch?

PS: Alles ist mit Arduinos aufgebaut. Umschwenken vom 328 auf den 2560 
will ich daher dass ich später 2 Sensoren ankommen will.
1
/*
2
 * Ultrasonic sensor HC-05 interfacing with AVR ATmega16
3
 * http://www.electronicwings.com
4
 */
5

6
#define F_CPU 16000000UL
7
#include <Arduino.h>
8
#include <avr/io.h>
9
#include <avr/interrupt.h>
10
#include <util/delay.h>
11
#include <stdlib.h>
12

13
#define Trigger_pin PA0 /* Trigger pin */
14

15
int TimerOverflow = 0;
16
double count = 0;
17
double speed = 0;
18
double distance = 0;
19

20
double mittelwert = 0;
21

22
uint8_t intterupt = 0;
23
uint8_t logCounter = 0;
24

25
ISR(INT0_vect)
26
{
27
  intterupt = 1;
28
}
29

30
ISR(TIMER4_OVF_vect)
31
{
32
  TimerOverflow++; /* Increment Timer Overflow count */
33
}
34

35
void interruptconfig()
36
{
37
  EICRA |= (1 << ISC01) | (1 << ISC00);
38
  EIMSK |= (1 << INT0);
39
  // sei();
40
}
41

42
int main(void)
43
{
44
  // char string[10];
45
  // long count;
46
  // double distance;
47
  interruptconfig();
48
  Serial.begin(9600);
49

50
  DDRA = (1 << Trigger_pin);  /* Make trigger pin as output */
51
  PORTA = (1 << Trigger_pin); /* Turn on Pull-up */
52

53
  sei();                 /* Enable global interrupt */
54
  TIMSK4 = (1 << TOIE4); /* Enable Timer1 overflow interrupts */
55
  TCCR4A = 0;            /* Set all bit to zero Normal operation */
56

57
  while (1)
58
  {
59
    /* Give 10us trigger pulse on trig. pin to HC-SR04 */
60
    PORTA |= (1 << Trigger_pin);
61
    _delay_us(10);
62
    PORTA &= (~(1 << Trigger_pin));
63

64
    TCNT4 = 0;         /* Clear Timer counter */
65
    TCCR4B = 0x41;     /* Capture on rising edge, No prescaler*/
66
    TIFR4 = 1 << ICF4; /* Clear ICP flag (Input Capture flag) */
67
    TIFR4 = 1 << TOV4; /* Clear Timer Overflow flag */
68
    TimerOverflow = 0; /* Clear Timer overflow count */
69
    /*Calculate width of Echo by Input Capture (ICP) */
70

71
    while ((TIFR4 & (1 << ICF4)) == 0)
72
      ;                /* Wait for rising edge */
73
    TCNT4 = 0;         /* Clear Timer counter */
74
    TCCR4B = 0x01;     /* Capture on falling edge, No prescaler */
75
    TIFR4 = 1 << ICF4; /* Clear ICP flag (Input Capture flag) */
76
    TIFR4 = 1 << TOV4; /* Clear Timer Overflow flag */
77
                       //  TimerOverflow = 0; /* Clear Timer overflow count */
78

79
    while ((TIFR4 & (1 << ICF4)) == 0)
80
      ;                                     /* Wait for falling edge */
81
    count = ICR4 + (65535 * TimerOverflow); /* Take count */
82
    /* 8MHz Timer freq, sound speed =343 m/s */
83
    //54.88 cm *100 / (count * taktDauer / 100) =
84
    speed = distance / (((double)count * 62.5) / 100);
85

86
    if (intterupt == 1)
87
    {
88
      Serial.print("Distanz: ");
89
      intterupt = 0;
90
      distance = (double)count / 465.6;
91
      Serial.println(distance);
92
    }
93

94
    logCounter++;
95

96
    mittelwert = (double)(mittelwert + (((speed * 100000) - 343) * 1000));
97

98
    if (logCounter == 10)
99
    {
100
      logCounter = 0;
101
      Serial.println((double)(mittelwert / 1000) / 10);
102
      mittelwert = 0;
103
    }
104

105
    //  distance = (double)count / 466.47;
106
    //  Serial.println((speed * 100000) - 343);
107
    // Serial.println((speed * 100000));
108
    // Serial.println(distance);
109
    _delay_ms(200);
110
  }
111
}

Antwort schreiben

Hinweis: der ursprüngliche Beitrag ist mehr als 6 Monate alt. Bitte hier nur auf die ursprüngliche Frage antworten, für neue Fragen einen neuen Beitrag erstellen.

Formatierung
  • [c]C-Code[/c]
  • [code]Code in anderen Sprachen, ASCII-Zeichnungen[/code]
  • [math]Formel in LaTeX-Syntax[/math]
  • [[Titel]] – Link zu einem Artikel
  • Verweis auf einen anderen Beitrag: Rechtsklick auf dessen Beitrags-ID (#8078628), „Adresse kopieren“, in den Text einfügen

Mehr zur Formatierung

Längeren Sourcecode als Anhang, nicht in den Text. JPG nur für Fotos und Scans; Zeichnungen und Screenshots als PNG oder GIF. Siehe Bildformate.

Schon registriert? Anmelden

Wird nicht angezeigt. Wir schicken dir einen Link; sobald du damit bestätigst, erscheint dein Beitrag.

Mit dem Abschicken legst du einen kostenlosen Account an und erkennst die Nutzungsbedingungen und die Datenschutzerklärung an.