Hallo !

Ich habe hier ein Programm für einen Drehzahlmesser gefunden.
Benutze einen Mega 32 und 16 MHz Quartz.

Meine Frequenz liegt zwischen 20Hz und 200Hz.

Der Quellcode zeigt mir Frequenzen bis 117MHz (7000 U/min) richtig an.

Ab dann springt die Anzeige zurück auf 6000 und die Zählung wird fortgesetzt.

Woran kann denn das liegen ?

Wenn ich mir nur das Timerergebnis anzeigen lasse stimmt dieses über den ganzen Bereich. Der Fehler muss also in der Berechnung oder den Variablen liegen oder ?

Danke für eure Antwort.

Gruß

Hier der Code:

'-------------------------------------------------------------------
' Drehzahlmesser mit
' Messung der Periodendauer mit Timer1
'-------------------------------------------------------------------
$regfile = "m32def.dat" 'Mega32
$crystal = 16000000 '16 MHz Quarz

Config Pind.6 = Input
Portd.6 = 1

Dim Impulse As Byte
Dim D As Long
Dim W As Long
Dim W1 As Long
Dim W2 As Long
Dim W3 As Long
Config Timer1 = Timer , Prescale = 64 , Capture Edge = Rising

Config Lcd = 16 * 2 , Chipset = Dogm162v5
Config Lcdpin = Pin , Db4 = Portb.0 , Db5 = Portb.1 , Db6 = Portb.2 , Db7 = Portb.3 , E = Portb.4 , Rs = Portb.5

Waitms 50

Gosub Dog

On Ovf1 Overflow
On Icp1 Steigende_flanke
Enable Icp1
Enable Ovf1

Enable Interrupts




Main:

D = 0
W3 = 0

Impulse = 0

Cls
Cursor Off

Locate 1 , 1

Lcd "Drehzahl"

Locate 2 , 1

Lcd D

Lcd " UpM "


Tcnt1l = 0
Tcnt1h = 0


Do

nop

Loop

End


Steigende_flanke:

Disable Icp1
Disable Ovf1

W3 = Timer1

Timer1 = 0

W2 = W2 + W3

Incr Impulse

If Impulse = 6 Then

W1 = W2 * 400

W = W1 / 60


D = 600000000 / W

Locate 2 , 1

Lcd D

Lcd " UpM "

W = 0
W2 = 0
W3 = 0
D = 0
Impulse = 0

Else

End If

Enable Icp1
Enable Ovf1

Return


Overflow: ' Timer1 Ueberlauf, bei Stillstand.

Disable Icp1 ' Zahler auf Null setzen.
Disable Ovf1

D = 0

Locate 2 , 1

Lcd D

Lcd " UpM "

W = 0
W2 = 0
D = 0

Enable Icp1
Enable Ovf1
Timer1 = 0
Return




' ---------------------- LCD DOG162 Initialisieren -----------------------------

Dog:
Portb = &B00000011 : Gosub Enbl : Waitms 2 ' dummy Funktion Set
Portb = &B00000011 : Gosub Enbl : Waitms 2 ' dummy Funktion Set
Portb = &B00000011 : Gosub Enbl : Waitms 2 ' dummy Funktion Set
Portb = &B00000010 : Gosub Enbl : Waitms 1 ' dummy Funktion Set
Portb = &B00000010 : Gosub Enbl : Waitms 1 ' Funktion Set 1
Portb = &B00001001 : Gosub Enbl : Waitms 1 ' Funktion Set 2
Portb = &B00000001 : Gosub Enbl : Waitms 1 ' Bias 1
Portb = &B00001100 : Gosub Enbl : Waitms 1 ' Bias 2
Portb = &B00000101 : Gosub Enbl : Waitms 1 ' Power/Icon/Contrast 1
Portb = &B00000010 : Gosub Enbl : Waitms 1 ' Power/Icon/Contrast 2 Contrast =38
Portb = &B00000110 : Gosub Enbl : Waitms 1 ' Follower control 1
Portb = &B00001001 : Gosub Enbl : Waitms 1 ' Follower control 2
Portb = &B00000111 : Gosub Enbl : Waitms 1 ' Contrast Set 1
Portb = &B00000000 : Gosub Enbl : Waitms 1 ' Contrast Set 2 Contrast =38
Portb = &B00000000 : Gosub Enbl : Waitms 1 ' Display On 1
Portb = &B00001100 : Gosub Enbl : Waitms 1 ' Display On 2
Portb = &B00000000 : Gosub Enbl : Waitms 1 ' Clear Display 1
Portb = &B00000001 : Gosub Enbl : Waitms 2 ' Clear Display 2
Portb = &B00000010 : Gosub Enbl : Waitms 1 ' Funktion Set 1
Portb = &B00001000 : Gosub Enbl : Waitms 1 ' Funktion Set 2
Portb = &B00000000 : Gosub Enbl : Waitms 1 ' Entry Mode Set 1
Portb = &B00000110 : Gosub Enbl : Waitms 1 ' Entry Mode Set 2
Portb = &B00000000
Return
Enbl: ' Enable ein
Set Portb.4
Waitus 400 ' Enable aus
Reset Portb.4
Return
Rw:
Set Portb.5
Waitus 400
Reset Portb.5
Return

End