• 23.07.2025, 12:21
  • Registrieren
  • Anmelden
  • Sie sind nicht angemeldet.

 

hurra

God

Falsche Zeiten bei µC-Messung...

Freitag, 21. Januar 2005, 18:12

Hallo zusammen.

Ich möchte per µC die Schallgeschwindigkeit durch Laufzeitmessung per Lautsprechr und Mikrophon bestimmen.
Hier erstmal der Code:

Atmega8, 16Mhz, LS an C5, Taster an B3, Mic an B0(ICP)

Quellcode

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#include <avr/io.h>
#include <avr/signal.h>
#include <avr/interrupt.h>
#include <stdlib.h>
#include <stdio.h>
#include "lcd.c"




//taster3 pressed=bit_is_clear(PINB, PINB1)
//taster2 pressed=bit_is_clear(PINB, PINB2)
//taster1 pressed=bit_is_clear(PINB, PINB3)

//led 2 an cbi(PORTC,PINC4);
//led 2 aus sbi(PORTC,PINC4);

//led 1 an cbi(PORTC,PINC5);
//led 1 aus sbi(PORTC,PINC5);

//ls an sbi(PORTC,PINC3);
//ls aus cbi(PORTC,PINC3);



SIGNAL (SIG_INPUT_CAPTURE1)//wenn mikrophon was bekommen
{
      cli();      //interrupts aus      
      cbi(PORTC,PINC3);//ls aus                                                      
}

int main( void )
{
   

      DDRC=0xff;//out
      DDRB=0x00;//in      
      
      
      sbi(PORTC,PINC4);//led aus
      sbi(PORTC,PINC5);//led aus
      cbi(PORTC,PINC3);//ls aus
      
      lcd_init(LCD_DISP_ON);
      lcd_clrscr();      
      
      
            
      
      unsigned volatile int i,j;      
      char act[20];
      unsigned char l,h;
      unsigned int wert;
      for(;;)
      {      
                  if(bit_is_clear(PINB, PINB3))//taster 1
                  {
                        lcd_clrscr();
                        
                  
                  
                        outb(TCCR1A, 0);
                        outb(TCCR1B, (1<<ICES1 &#124; 1<<CS10));// rising edge for ICP, full speed
                        outb(TIMSK, 1<<TICIE1);// ICP interrupt enable
                        ICR1H=0;
                        ICR1L=0;
                        
                        TCNT1H=0;
                        TCNT1L=0;
                        
                        sbi(PORTC,PINC3);//ls an
                        
                        sei();//interrupt an
                        for(i=0;i<65000;i++)for(j=0;j<10;j++)//pause, waerendessen interrupt
                        {
                              asm volatile("nop");
                        }                        
                        
                        
                        
                        
                        
                        
                        l=ICR1L;      
                        h=ICR1H;
                        wert=256*h+l;
                        sprintf(act,"%u %u %u\n",l, h,wert);                        
                        lcd_puts(act);                        
                        
                  
                  }
                  
      }
      return 0;
}


So, leider bekomme ich

1. komplett falsche ergebnisse (Trägheit von Mikrophon und LS würde ich später noch abziehen)
2. Die messung ist komsich:

LS und Mic nah zusammen: Zeit groß
LS und Mic weiter auseinander: Zeit wird kleiner
LS und Mic noch weiter auseinander:Zeit wird noch kleiner
LS und Mic noch weiter auseinander:Zeit wird noch kleiner
LS und Mic noch weiter auseinander:Zeit nimmt wieder zu
LS und Mic noch weiter auseinander:Zeit nimmt noch mehr zu

Sieht irgendjemand fehler im code? Wie kann ichs besser machen?

Vielen Dank

Cu Hurra

somnatic

Junior Member

Re: Falsche Zeiten bei µC-Messung...

Montag, 24. Januar 2005, 13:29

also:
for(i=0;i<65000;i++)for(j=0;j<10;j++)//pause, waerendessen interrupt
{
asm volatile("nop");
}
l=ICR1L;
h=ICR1H;
wert=256*h+l;
sprintf(act,"%u %u %u\n",l, h,wert);
lcd_puts(act);

was soll dieser Teil bringen ?

ich schätze mal du wartest auf einen IRQ .. warum nicht einfach while(1);
macht eine wunderbare endlosschleife ...

warum nimmst du einen unsigned int für wert ? warum nicht einen normalen (aber da dürfte der fehler nicht begraben sein).


wie kommt denn die information, wann der ton eingetroffen ist, daher ? Was hat dein Audio-Sensor denn für einen Ausgang bzw. wie schliesst du ihn an den Atmel an ?

hurra

God

Re: Falsche Zeiten bei µC-Messung...

Montag, 24. Januar 2005, 13:44

Danke für deine Hilfe.

Hab meinen Code jetzt etwas umgestellt.

Quellcode

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#include <avr/io.h>
#include <avr/signal.h>
#include <avr/interrupt.h>
#include <stdlib.h>
#include <stdio.h>
#include "lcd.c"




//taster3 pressed=bit_is_clear(PINB, PINB1)
//taster2 pressed=bit_is_clear(PINB, PINB2)
//taster1 pressed=bit_is_clear(PINB, PINB3)

//led 2 an cbi(PORTC,PINC4);
//led 2 aus sbi(PORTC,PINC4);

//led 1 an cbi(PORTC,PINC5);
//led 1 aus sbi(PORTC,PINC5);

//ls an sbi(PORTC,PINC3);
//ls aus cbi(PORTC,PINC3);


unsigned volatile char l,h;
unsigned volatile char first=0;
SIGNAL (SIG_INPUT_CAPTURE1)
{
      cli();      //interrupts aus
      if(first==0)//nur den ersten wert nehmen
      {
            first=1;
            l=ICR1L;      
            h=ICR1H;
      }
      cbi(PORTC,PINC3);//ls aus                                                      
}

int main( void )
{
   

      DDRC=0xff;//out
      DDRB=0x00;//in      
      
      
      sbi(PORTC,PINC4);//led aus
      sbi(PORTC,PINC5);//led aus
      cbi(PORTC,PINC3);//ls aus
      
      lcd_init(LCD_DISP_ON);
      lcd_clrscr();
      lcd_puts("...");
      
      
            
      
      unsigned volatile int i,j,mess;      
      char act[20];
      unsigned volatile int t=0;
      unsigned int wert=0;
      for(;;)
      {      
                  if(bit_is_clear(PINB, PINB3))//taster 1
                  {
                        lcd_clrscr();
                        lcd_puts("Messe");
                        t=0;
                        for(mess=0;mess<10;mess++)
                        {
                              lcd_puts(".");
                              first=0;
                              TCCR1A=0;
                              TCCR1B=(1<<ICES1 &#124; 1<<CS11);// rising edge for ICP,CPU/8
                              TIMSK=(1<<TICIE1);// ICP interrupt enable
                              ICR1H=0;
                              ICR1L=0;
                        
                              TCNT1H=0;
                              TCNT1L=0;
                        
                              sbi(PORTC,PINC3);//ls an
                        
                              sei();//interrupt an
                              for(i=0;i<65000;i++)for(j=0;j<10;j++)//pause, waerendessen interrupt
                              {
                                    asm volatile("nop");
                              }                        
                        
                              wert=256*h+l;
                              
                              //sprintf(act,"%u %u %u\n",l, h,wert-550);                        
                              t+=wert-364;
                              //lcd_puts(act);                                                
                        }            
                        
                        
                        sprintf(act,"\n%u",t);                                                
                        lcd_puts(act);                                                                  
                        
                  
                  }
                  
      }
      return 0;
}



Jetzt stimmen die Zeiten zumidnest im groben. Leider habe ich mit der Messung immernoch Probleme, wenngleich der Hund jetzt woanders begraben liegt.

Vielen Dank

Cu Hurra