Mainframe_neu.c


1
//B2 Taster K?ltebad
2
//B3 Taster W?rmebad
3
4
5
6
#include <avr/io.h>
7
#include <avr/interrupt.h>
8
#define F_CPU 2000000
9
#include <util/delay.h>
10
#include <inttypes.h>
11
#include <stdint.h>
12
13
#define TRUE 1
14
#define FALSE 0
15
16
// Anchlussbelegung:
17
18
#define KTEMP_DDR DDRD        //Temperatur Input vom Kältebad
19
#define WTEMP_DDR DDRC        //Temperatur Input vom Wärmebad
20
#define PTEMP_DDR DDRA        //Temperatur Input von Probe
21
22
#define MOTOR_DDR DDRB        //Datenrichtung zum Motor-Steuer-Atmega festlegen
23
#define MOTOR_OUT PORTB        //Ausgänge zum Motor-Steuer-Atmega
24
#define MOTOR_IN  PINB
25
26
#define CW   PB0          //Motor cw richtung Kältebad
27
#define CCW  PB1          //Motor ccw richtung Wärmebad
28
#define KB   PB2          //Taster Kältebad abfragen 
29
#define WB   PB3          //Taster Wärmebad abfragen
30
31
void entprellung( volatile uint8_t *port, uint8_t maske ) {
32
  uint8_t   port_puffer;
33
  uint8_t   entprellungs_puffer;
34
 
35
  for( entprellungs_puffer=0 ; entprellungs_puffer!=0xff ; ) {
36
    entprellungs_puffer<<=1;
37
    port_puffer = *port;
38
    _delay_us(150);
39
    if( (*port & maske) == (port_puffer & maske) )
40
      entprellungs_puffer |= 0x01;
41
  }
42
}
43
44
45
int main(void)
46
{
47
    uint8_t w_temp = 0;          //Variable zum speichern der Zustände vom Wärmebad
48
    uint8_t k_temp = 0;          //Variable zum speichern der Zustände vom Kältebad
49
    uint8_t p_temp = 0;          //Variable zum speichern der Zustände von der Probe
50
         
51
        const uint16_t pause = 200;
52
    const uint16_t pause_2 =80;
53
    
54
    
55
    KTEMP_DDR = 0x00;    //Eing?nge D
56
        WTEMP_DDR = 0x00;    //Eing?nge C
57
    PTEMP_DDR = 0x00;    //Eing?nge A
58
59
    //B0 un B1 als Ausgänge, B2 und B3 als Eingänge:
60
    MOTOR_DDR = (1 << DDB0) | (1 << DDB1);
61
62
    //Pause für 20 Sekunden damit alles andere initialisieren kann
63
    for(int a=0; a<20; a++){_delay_ms( 1000 );}
64
65
        do
66
        {
67
      k_temp = 0;          
68
      p_temp = 0;          
69
      w_temp = 0;          
70
  
71
      //Temperaturen von den LED Anzeigen abholen.
72
      if ( PINA & (1<<PINA0) ) {p_temp = p_temp + 1;}
73
      if ( PINA & (1<<PINA1) ) {p_temp = p_temp + 2;}
74
      if ( PINA & (1<<PINA2) ) {p_temp = p_temp + 4;}
75
      if ( PINA & (1<<PINA3) ) {p_temp = p_temp + 8;}
76
      if ( PINA & (1<<PINA4) ) {p_temp = p_temp + 10;}
77
      if ( PINA & (1<<PINA5) ) {p_temp = p_temp + 20;}
78
      if ( PINA & (1<<PINA6) ) {p_temp = p_temp + 40;}
79
      if ( PINA & (1<<PINA7) ) {p_temp = p_temp + 80;}
80
81
      if ( PINC & (1<<PINC0) ) {w_temp = w_temp + 1;}
82
      if ( PINC & (1<<PINC1) ) {w_temp = w_temp + 2;}
83
      if ( PINC & (1<<PINC2) ) {w_temp = w_temp + 4;}
84
      if ( PINC & (1<<PINC3) ) {w_temp = w_temp + 8;}
85
      if ( PINC & (1<<PINC4) ) {w_temp = w_temp + 10;}
86
      if ( PINC & (1<<PINC5) ) {w_temp = w_temp + 20;}
87
      if ( PINC & (1<<PINC6) ) {w_temp = w_temp + 40;}
88
      if ( PINC & (1<<PINC7) ) {w_temp = w_temp + 80;}
89
90
      if ( PIND & (1<<PIND0) ) {k_temp = k_temp + 1;}
91
      if ( PIND & (1<<PIND1) ) {k_temp = k_temp + 2;}
92
      if ( PIND & (1<<PIND2) ) {k_temp = k_temp + 4;}
93
      if ( PIND & (1<<PIND3) ) {k_temp = k_temp + 8;}
94
      if ( PIND & (1<<PIND4) ) {k_temp = k_temp + 10;}
95
      if ( PIND & (1<<PIND5) ) {k_temp = k_temp + 20;}
96
      if ( PIND & (1<<PIND6) ) {k_temp = k_temp + 40;}
97
      if ( PIND & (1<<PIND7) ) {k_temp = k_temp + 80;}
98
99
      //+-2 damit das Testen schneller geht.
100
      k_temp = k_temp + 2;
101
      w_temp = w_temp - 2;
102
103
      //Wenn Taster vom K?ltebad an (d.h. Probe ist im Kältebad)
104
      entprellung( &PINB, (1<<PINB2) ); // ggf. Prellen abwarten
105
      if((PINB & (1<<KB)))
106
      {
107
        //Motor erst mal grundlegend ausschalten, um einen definierten Startwert zu haben
108
      PORTB &= ~ (1<<CCW);  //Motor aus
109
      PORTB &= ~ (1<<CW);    //Motor aus
110
111
112
        if((p_temp <= k_temp))   //fals Probentemp gleich Kältebadtemp ist
113
        {
114
115
         //ins Wärmebad fahren)
116
         //PORTB &= ~ (1<<CCW);
117
         PORTB |= (1<<CW); 
118
        }
119
      }
120
         
121
      //Wenn Taster vom Wärmebad an (d.h. Probe ist im Wärmebad)
122
      entprellung( &PINB, (1<<PINB3) ); // ggf. Prellen abwarten
123
      if((PINB & (1<<WB)))
124
      {
125
        //Motor erst mal grundlegend ausschalten, um einen definierten Startwert zu haben
126
      PORTB &= ~ (1<<CCW);  //Motor aus
127
      PORTB &= ~ (1<<CW);    //Motor aus
128
        
129
      
130
        if((p_temp >= w_temp))// fals Probentemp gleich Wärmebadtemp ist
131
        {
132
         //ins Kältebad fahren
133
         //PORTB &= ~ (1<<CW);
134
         PORTB |= (1<<CCW);
135
         
136
        }
137
      }
138
139
        } //ENDE do
140
    while(1);
141
142
} //ENDE main