303 lines
9.6 KiB
QBasic
303 lines
9.6 KiB
QBasic
|
|
|||
|
$regfile = "m8def.dat" ' ATMega8
|
|||
|
$crystal = 1000000 ' 1 MHz ist schnell genug
|
|||
|
|
|||
|
Const Const5 = 0.0048828125 ' 5Volt / 1024 (10-Bit-ADC)
|
|||
|
|
|||
|
Dim Adc0 As Word ' Wert vom NTC
|
|||
|
Dim Volt0 As Single ' Volt vom NTC
|
|||
|
Dim Disp_volt0 As String * 10 ' f<>r die LCD-Anzeige
|
|||
|
|
|||
|
Dim Adc1 As Word ' Wert vom LM35
|
|||
|
Dim Volt1 As Single ' Volt vom LM35
|
|||
|
Dim Templm35 As Single ' Temperatur (celsius) von LM35
|
|||
|
Dim Isttemp As Single ' aktuelle Temperatur in Celsius
|
|||
|
Dim Letzter_isttemp As Single ' Messwert davor
|
|||
|
Dim Disp_volt1 As String * 10 ' zur LCD-Anzeige
|
|||
|
Dim Disp_templm35 As String * 10 ' zur LCD-Anzeige
|
|||
|
|
|||
|
Dim Adc2 As Word ' nicht ben<65>tigt
|
|||
|
|
|||
|
Dim Adc3 As Word ' Wert vom ADC vom IRF510
|
|||
|
Dim Volt3 As Single ' Volt vom IRF510
|
|||
|
Dim Strom As Single ' Strom vom IRF510
|
|||
|
Dim Disp_strom As String * 10 ' zur LCD-Anzeige
|
|||
|
|
|||
|
Dim Solltemp As Single ' Vorgabe der maximal zul<75>ssigen Temperatur
|
|||
|
Dim Disp_solltemp As String * 10 ' zur LCD-Anzeige
|
|||
|
|
|||
|
Dim I As Integer ' Z<>hler / Schleife
|
|||
|
|
|||
|
Declare Sub Adc_in
|
|||
|
Declare Sub Regeln
|
|||
|
Declare Sub Anzeigen
|
|||
|
Declare Sub Tasten
|
|||
|
|
|||
|
Config Lcdbus = 4
|
|||
|
Config Lcd = 16 * 2
|
|||
|
'Config Lcd = 20 * 4
|
|||
|
Config Lcdpin = Pin , Db4 = Portd.4 , Db5 = Portd.5 , Db6 = Portd.6 , Db7 = Portd.7 , E = Portd.3 , Rs = Portd.2
|
|||
|
|
|||
|
Config Portb = Output
|
|||
|
Config Timer1 = Pwm , Pwm = 10 , Compare A Pwm = Clear Down , Prescale = 1
|
|||
|
|
|||
|
|
|||
|
' Tasten zur Einstellung der Solltemperatur
|
|||
|
Taste_up Alias Pinc.4
|
|||
|
Taste_dn Alias Pinc.5
|
|||
|
Dim Tasteup As Integer
|
|||
|
Dim Tastedn As Integer
|
|||
|
|
|||
|
Config Pinc.0 = Input
|
|||
|
Config Pinc.1 = Input
|
|||
|
Config Pinc.2 = Input
|
|||
|
Config Pinc.3 = Input
|
|||
|
Config Pinc.4 = Input
|
|||
|
Set Portc.4
|
|||
|
Config Pinc.5 = Input
|
|||
|
Set Portc.5
|
|||
|
|
|||
|
|
|||
|
Config Adc = Single , Prescaler = Auto , Reference = Avcc
|
|||
|
|
|||
|
' Led
|
|||
|
Ledgruen Alias Portd.0
|
|||
|
Ledrot Alias Portd.1
|
|||
|
Config Portd.0 = Output
|
|||
|
Config Portd.1 = Output
|
|||
|
|
|||
|
|
|||
|
|
|||
|
Dim Dynamikgrenze_oben As Single
|
|||
|
Dim Dynamikgrenze_unten As Single
|
|||
|
|
|||
|
Dim Toleranzgrenze_oben As Single ' in den Toleranzgrenzem wired nicht geregelt
|
|||
|
Dim Toleranzgrenze_unten As Single
|
|||
|
|
|||
|
Dim Pausenzeit As Single ' Pause zwischen den Regelungen
|
|||
|
Dim Pausenzeit_int As Integer
|
|||
|
Dim Pausenzeit_max As Single ' max. zul<75>ssige Pausenzeit
|
|||
|
Dim Pausenzeit_faktor As Single
|
|||
|
|
|||
|
Dim Schrittweite As Single ' um diesen Betrag wird PWM korrigiert
|
|||
|
Dim Schrittweite_int As Integer
|
|||
|
Dim Schrittweite_faktor As Single
|
|||
|
|
|||
|
Dim Pwmwert As Integer ' 10-Bit PWM (0...1023)
|
|||
|
|
|||
|
Dim Differenz As Single
|
|||
|
|
|||
|
' ------------------------------------------------------------------------------
|
|||
|
' ----------------------- Initialisierung der Variablen ------------------------
|
|||
|
' ------------------------------------------------------------------------------
|
|||
|
|
|||
|
'Solltemperatur (max, zul<75>ssige Temperatur)
|
|||
|
Solltemp = 28
|
|||
|
|
|||
|
Toleranzgrenze_oben = Solltemp + 1
|
|||
|
Toleranzgrenze_unten = Solltemp - 1
|
|||
|
|
|||
|
Dynamikgrenze_oben = Toleranzgrenze_oben + 10
|
|||
|
Dynamikgrenze_unten = Toleranzgrenze_unten - 10
|
|||
|
|
|||
|
Pausenzeit = 25 ' zu Beginn mittlere Pausenzeit einstellen (2 Sek.)
|
|||
|
Pausenzeit_faktor = 1 ' Multiplikator f<>r die Bestimmung der Pausenzeit
|
|||
|
Pausenzeit_max = 50 ' z.B. 50 * 1/10 Sek. = 5 Sekunden
|
|||
|
|
|||
|
Schrittweite = 50 ' Anfangswert
|
|||
|
Schrittweite_int = 50 ' Anfangswert
|
|||
|
Schrittweite_faktor = 10 '
|
|||
|
|
|||
|
Disable Interrupts
|
|||
|
|
|||
|
' ------------------------------------------------------------------------------
|
|||
|
' Beginn Programm
|
|||
|
' ------------------------------------------------------------------------------
|
|||
|
|
|||
|
|
|||
|
Reset Ledgruen ' gr<67>ne LED einschalten
|
|||
|
|
|||
|
Call Adc_in
|
|||
|
|
|||
|
'Pwm1a = 0 ' Motor aus
|
|||
|
Pwmwert = 500
|
|||
|
Pwm1a = Pwmwert ' Motor max ein
|
|||
|
Cursor Off
|
|||
|
Cls
|
|||
|
Locate 1 , 4
|
|||
|
Lcd "Luefter 1.4"
|
|||
|
Wait 5 ' L<>fter anlaufen lassen
|
|||
|
Cls
|
|||
|
|
|||
|
Do
|
|||
|
Call Adc_in
|
|||
|
Call Tasten
|
|||
|
Call Anzeigen
|
|||
|
Call Regeln
|
|||
|
Loop
|
|||
|
|
|||
|
|
|||
|
|
|||
|
' ADC lesen
|
|||
|
Sub Adc_in
|
|||
|
Start Adc
|
|||
|
Letzter_isttemp = Isttemp ' letzten Messwert merken
|
|||
|
|
|||
|
Adc0 = Getadc(0) ' NTC
|
|||
|
Adc1 = Getadc(1) ' LM35
|
|||
|
Adc2 = Getadc(2) ' nix
|
|||
|
Adc3 = Getadc(3) ' f<>r Strom IRF510
|
|||
|
|
|||
|
Volt0 = Adc0 * Const5 ' NTC
|
|||
|
Volt1 = Adc1 * Const5 ' LM35
|
|||
|
Templm35 = Volt1 * 100 ' in Celsius umrechnen (10mV je Grad <20>ber Null)
|
|||
|
Isttemp = Templm35 ' aktuelle Temperatur am LM35
|
|||
|
|
|||
|
Volt3 = Adc3 * Const5 ' Strom durch den IRF510 berechnen
|
|||
|
'Strom = Volt3 / 2.7 ' I = U / R R=2,7 Ohm
|
|||
|
Strom = Volt3 ' I = U / R R=1 Ohm
|
|||
|
Strom = Strom * 1000 ' Ampere in mA
|
|||
|
Stop Adc
|
|||
|
End Sub
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
Sub Regeln
|
|||
|
|
|||
|
' Pausenzeit au<61>erhalb Dynamikbereichs einstellen
|
|||
|
If Isttemp > Dynamikgrenze_oben Then
|
|||
|
Pausenzeit = 0 ' k<>rzest m<>gliche Pause
|
|||
|
End If ' dann schnell regeln
|
|||
|
|
|||
|
' Temperatur unterhalb Solltemperatur
|
|||
|
If Isttemp <= Solltemp Then ' Temp ist k<>lter als erlaubt
|
|||
|
If Isttemp < Toleranzgrenze_unten Then ' unterhalb Toleranzgrenze ?
|
|||
|
Pwmwert = Pwmwert - Schrittweite_int ' versuchen, ob L<>fter leiser werden kann
|
|||
|
If Pwmwert < 0 Then
|
|||
|
Pwmwert = 0
|
|||
|
End If
|
|||
|
End If
|
|||
|
Pwm1a = Pwmwert
|
|||
|
End If ' endif isttemp < solltemp
|
|||
|
|
|||
|
' Bereich oberhalb Solltemperatur
|
|||
|
If Isttemp > Toleranzgrenze_oben Then ' Temp zu hoch, PWM-Wert steigern
|
|||
|
' Schrittweite berechnen, um die der PWM-Wert korrigiert wird
|
|||
|
Schrittweite = Isttemp - Solltemp
|
|||
|
Schrittweite = Schrittweite * Schrittweite_faktor
|
|||
|
Schrittweite_int = Round(schrittweite)
|
|||
|
If Schrittweite_int < 1 Then
|
|||
|
Schrittweite_int = 1
|
|||
|
End If
|
|||
|
|
|||
|
If Isttemp > Letzter_isttemp Then
|
|||
|
Pwmwert = Pwmwert + Schrittweite_int
|
|||
|
End If
|
|||
|
|
|||
|
If Isttemp < Letzter_isttemp Then
|
|||
|
Pwmwert = Pwmwert - Schrittweite_int
|
|||
|
End If
|
|||
|
|
|||
|
If Isttemp = Letzter_isttemp Then
|
|||
|
Pwmwert = Pwmwert + 10
|
|||
|
End If
|
|||
|
|
|||
|
If Pwmwert > 1023 Then
|
|||
|
Pwmwert = 1023
|
|||
|
End If
|
|||
|
|
|||
|
If Pwmwert < 0 Then
|
|||
|
Pwmwert = 0
|
|||
|
End If
|
|||
|
|
|||
|
Pwm1a = Pwmwert ' PWM des Motors steuern
|
|||
|
|
|||
|
If Isttemp < Dynamikgrenze_oben Then ' im Regelbereich, in dem die Pausenzeit angepasst wird ?
|
|||
|
Set Ledrot ' rote LED aus
|
|||
|
' Pausenzeit berechnen f<>r (Toleranzgrenze oben < x < Dynamikgrenze oben)
|
|||
|
Differenz = Dynamikgrenze_oben - Isttemp
|
|||
|
Pausenzeit = Pausenzeit_max / Differenz
|
|||
|
Pausenzeit = Pausenzeit * Pausenzeit_faktor
|
|||
|
If Pausenzeit > Pausenzeit_max Then
|
|||
|
Pausenzeit = Pausenzeit_max
|
|||
|
End If
|
|||
|
' nun steht in Pausenzeit die Pausenzeit 1/10 Sekunden
|
|||
|
End If
|
|||
|
End If ' if Toleranzgrenze_oben < Isttemp
|
|||
|
|
|||
|
|
|||
|
If Isttemp > Dynamikgrenze_oben Then ' Ist > als Dynamikgrenze oben, also SEHR hoch
|
|||
|
Reset Ledrot ' warnen
|
|||
|
Pausenzeit = 0
|
|||
|
End If
|
|||
|
|
|||
|
Pausenzeit_int = Round(pausenzeit) ' Pausenzeit (SINGLE) in INTEGER wandeln
|
|||
|
|
|||
|
' Pausenzeit vergehen lassen
|
|||
|
If Pausenzeit_int > 0 Then
|
|||
|
For I = 0 To Pausenzeit_int
|
|||
|
Waitms 100 ' Zeit verplempern in Einheiten von 1/10 Sekunde
|
|||
|
Call Tasten
|
|||
|
Next I
|
|||
|
End If
|
|||
|
|
|||
|
|
|||
|
End Sub
|
|||
|
|
|||
|
|
|||
|
|
|||
|
Sub Anzeigen
|
|||
|
' Disp_volt0 = Fusing(volt0 , "#.###")
|
|||
|
' Disp_volt1 = Fusing(volt1 , "#.###")
|
|||
|
Disp_templm35 = Fusing(templm35 , "##.#")
|
|||
|
Disp_strom = Fusing(strom , "###.#")
|
|||
|
Disp_solltemp = Fusing(solltemp , "###.#")
|
|||
|
|
|||
|
' LCD zweizeilig je 16 Zeichen
|
|||
|
Locate 1 , 1
|
|||
|
Lcd Disp_templm35 ; " C "
|
|||
|
Locate 1 , 7
|
|||
|
Lcd " Soll:" ; Disp_solltemp ; " "
|
|||
|
Locate 2 , 1
|
|||
|
Lcd Disp_strom ; "mA ";
|
|||
|
Locate 2 , 9
|
|||
|
Lcd "Pwm:" ; Pwmwert ; " "
|
|||
|
|
|||
|
' LCD vierzeilig je 20 Zeilen
|
|||
|
'Locate 2 , 2
|
|||
|
'Lcd "NTC " ; Disp_volt0 ; " V "
|
|||
|
'Locate 3 , 2
|
|||
|
'Lcd "LM35 " ; Disp_templm35 ; " C "
|
|||
|
'Locate 4 , 2
|
|||
|
'Lcd "IRF510 " ; Disp_strom ; " mA ";
|
|||
|
'Lcd "Pwm:" ; Pwmwert ; " "
|
|||
|
'Locate 4 , 11
|
|||
|
'Lcd " Sw:" ; Schrittweite_int ; " "
|
|||
|
|
|||
|
End Sub
|
|||
|
|
|||
|
|
|||
|
|
|||
|
Sub Tasten
|
|||
|
|
|||
|
|
|||
|
Tasteup = Taste_up
|
|||
|
Tastedn = Taste_dn
|
|||
|
|
|||
|
If Tasteup = 0 Then
|
|||
|
Solltemp = Solltemp + 1
|
|||
|
Waitms 300
|
|||
|
End If
|
|||
|
|
|||
|
If Tastedn = 0 Then
|
|||
|
Solltemp = Solltemp - 1
|
|||
|
Waitms 300
|
|||
|
End If
|
|||
|
If Solltemp < 1 Then
|
|||
|
Solltemp = 1
|
|||
|
End If
|
|||
|
|
|||
|
End Sub
|
|||
|
|