1 | //############################################################################
|
2 | // ______Luftdrucküberwachung_____
|
3 | //
|
4 | // __Messbereich
|
5 | // A/D 10bit Aufloesung 0,0V bis 5,0V 0 bis 1023
|
6 | // Pdiff-Sensor 0,25V bis 4,25V 0 bis 250Pa
|
7 | // __Ein- und Ausgaenge
|
8 | // Poti an PC0 (ADC0) Pin23 MESSWERT(0) Schwellwert
|
9 | // Sensor an PC2 (ADC2) Pin25 MESSWERT(2) Vout_Unidirektional
|
10 | // Tür_Sensor an PD7 Pin13
|
11 | // Ventil an PB0 Pin14
|
12 | // LED 1 (grün) an PB1 Pin15
|
13 | // LED 2 (rot) an PB2 Pin16
|
14 | //############################################################################
|
15 |
|
16 | #include <avr/io.h>
|
17 | #include <stdio.h>
|
18 | #include <util/delay.h>
|
19 | #include "messen.h"
|
20 | #include "lcd-routines.h"
|
21 | #define F_CPU 1000000 // Takt-Frequenz in Hz (=1Mhz)
|
22 |
|
23 | int main(void){
|
24 |
|
25 | //Variablen deklarieren
|
26 | //############################################################################
|
27 | int x=0, y=0, z=0, P_diff_ist=0, P_diff_soll=0;
|
28 | char messwertstring[20];
|
29 |
|
30 | //Ein- und Ausgaenge festlegen
|
31 | //############################################################################
|
32 | DDRB |= _BV(PB0); //Ausgang PB0 Ventil
|
33 | DDRB |= _BV(PB1); //Ausgang PB1 LED gruen
|
34 | DDRB |= _BV(PB2); //Ausgang PB2 LED rot
|
35 | DDRD &= _BV(PD7); //Eingang PD7 Tür_Sensor
|
36 |
|
37 | //Hauptprogramm
|
38 | //############################################################################
|
39 | lcd_init();
|
40 | lcd_clear();
|
41 | while (1) {
|
42 |
|
43 | //Berechnungen
|
44 | //####################################################################
|
45 | // -- Endpunktermittlung mit dem Einsetzungsverfahren
|
46 | // Pdiff = Vout = AD-Wandler-Wert
|
47 | // 0Pa = 0,25V = 51
|
48 | // 250Pa = 4,25V = 870
|
49 | //
|
50 | // 0Pa = 51 * z + y > y = 0Pa - 51 * z
|
51 | // 250Pa = 870 * z + y
|
52 | //
|
53 | // 250Pa = 870 * z - 51 * z + 0Pa
|
54 | // 250Pa = 819 * z
|
55 | // z = 250 / 819
|
56 | // z = 0.305250305250
|
57 | // y = 0Pa - (51 * 0.305250305250)
|
58 | // y =-15.56776556776
|
59 | // Der Druckwert ergibt sich also aus der gemessenen AD-Wert * z + y
|
60 | //####################################################################
|
61 | x = 99; //max. Wertebereich für P_diff_soll-Poti
|
62 | z = 0.305250305250;
|
63 | y = -15.56776556776;
|
64 | P_diff_soll = (x/1023) * MESSWERT(0); //A/D-Wert in 0-99Pa
|
65 | P_diff_ist = MESSWERT(2) * z + y; //Berechnung durch Einsetzverfahren
|
66 |
|
67 | //P_diff_soll
|
68 | //#########################################
|
69 | set_cursor (0, 1);
|
70 | lcd_string ("Pdiff_SOLL: ");
|
71 |
|
72 | set_cursor (12, 1);
|
73 | sprintf(messwertstring, "%d", P_diff_soll); //Umwandlung P_diff_soll in messwertstring
|
74 | if (P_diff_soll<10) lcd_string (" "); //10ner-stelle wird leer (Messwert<10)
|
75 | if (P_diff_soll<100) lcd_string (" "); //100ter-stelle wird leer (Messwert<100)
|
76 | lcd_string (messwertstring); //LCD-Display Ausgabe
|
77 |
|
78 | set_cursor (14, 1);
|
79 | lcd_string ("Pa");
|
80 |
|
81 | //P_diff_ist
|
82 | //#########################################
|
83 | set_cursor (0, 2);
|
84 | lcd_string ("Pdiff_IST: ");
|
85 |
|
86 | set_cursor (11, 2);
|
87 | sprintf(messwertstring, "%d", P_diff_ist); //Umwandlung P_diff_ist in messwertstring
|
88 | if (P_diff_ist<10) lcd_string (" "); //10ner-stelle wird leer (Messwert<10)
|
89 | if (P_diff_ist<100) lcd_string (" "); //100ter-stelle wird leer (Messwert<100)
|
90 | lcd_string (messwertstring); //LCD-Display Ausgabe
|
91 |
|
92 | set_cursor (14, 2);
|
93 | lcd_string ("Pa");
|
94 |
|
95 | // Ansteuerung der Ausgaenge
|
96 | //#########################################
|
97 | if (P_diff_ist<P_diff_soll)
|
98 | {PORTB &=~_BV(PB2); //LED 2 rot an
|
99 | PORTB |=_BV(PB1);} //LED 1 gruen aus
|
100 | else
|
101 | {PORTB |=_BV(PB2); //LED 2 rot aus
|
102 | PORTB &=~_BV(PB1);} //LED 1 gruen an
|
103 |
|
104 | if ((P_diff_ist<P_diff_soll)&&(bit_is_set(PIND,7)))
|
105 | {PORTB &=~_BV(PB0);} //Ventil angezogen (Tuer gesperrt)
|
106 | else
|
107 | {PORTB |=_BV(PB0);} //Ventil abgefallen (Tuer offen)
|
108 |
|
109 | _delay_ms(50);
|
110 | }
|
111 | return(0);
|
112 | }
|